From 3a61506d5e8b43fcc396e77f6e0923cdf168f660 Mon Sep 17 00:00:00 2001 From: vitta Date: Wed, 29 May 2024 18:50:02 +0200 Subject: [PATCH 1/2] fix: 432 wrap DRACOLoader inside async function --- src/core/loaders/useGLTF/index.ts | 36 ++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/core/loaders/useGLTF/index.ts b/src/core/loaders/useGLTF/index.ts index 11f8ac02..b4660351 100644 --- a/src/core/loaders/useGLTF/index.ts +++ b/src/core/loaders/useGLTF/index.ts @@ -26,25 +26,30 @@ export interface GLTFResult { scene: THREE.Scene } -let dracoLoader: DRACOLoader | null = null +async function createDRACOLoader(decoderPath: string): Promise { + const dracoLoader = new DRACOLoader() + dracoLoader.setDecoderPath(decoderPath) + return dracoLoader +} /** * Sets the extensions for the GLTFLoader. * * @param {GLTFLoaderOptions} options + * @param {DRACOLoader} [dracoLoader] * @param {(loader: GLTFLoader) => void} [extendLoader] * @return {*} */ -function setExtensions(options: GLTFLoaderOptions, extendLoader?: (loader: GLTFLoader) => void) { +function setExtensions( + options: GLTFLoaderOptions, + dracoLoader: DRACOLoader | null, + extendLoader?: (loader: GLTFLoader) => void, +) { return (loader: GLTFLoader) => { if (extendLoader) { extendLoader(loader as GLTFLoader) } - if (options.draco) { - if (!dracoLoader) { - dracoLoader = new DRACOLoader() - } - dracoLoader.setDecoderPath(options.decoderPath || 'https://www.gstatic.com/draco/versioned/decoders/1.4.3/') + if (options.draco && dracoLoader) { loader.setDRACOLoader(dracoLoader) } } @@ -63,13 +68,20 @@ function setExtensions(options: GLTFLoaderOptions, extendLoader?: (loader: GLTFL */ export async function useGLTF( path: T, - options: GLTFLoaderOptions = { - draco: false, - }, + options: GLTFLoaderOptions = { draco: false }, extendLoader?: (loader: GLTFLoader) => void, ): Promise { - const gltfModel = (await useLoader(GLTFLoader, path, setExtensions(options, extendLoader))) as unknown as GLTFResult + const dracoLoader = options.draco + ? await createDRACOLoader( + options.decoderPath + || 'https://www.gstatic.com/draco/versioned/decoders/1.4.3/', + ) + : null + const gltfModel = (await useLoader( + GLTFLoader, + path, + setExtensions(options, dracoLoader, extendLoader), + )) as unknown as GLTFResult dracoLoader?.dispose() - dracoLoader = null return gltfModel } From 6c52a1b3bba3f82b25a296e786a8c7c4ce5453e2 Mon Sep 17 00:00:00 2001 From: vitta Date: Wed, 29 May 2024 18:54:24 +0200 Subject: [PATCH 2/2] fix: add missing jsdoc --- src/core/loaders/useGLTF/index.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/core/loaders/useGLTF/index.ts b/src/core/loaders/useGLTF/index.ts index b4660351..6bbc07c9 100644 --- a/src/core/loaders/useGLTF/index.ts +++ b/src/core/loaders/useGLTF/index.ts @@ -26,6 +26,13 @@ export interface GLTFResult { scene: THREE.Scene } +/** + * Create the loader for Draco. + * + * @param {string} decoderPath + * @return {*} + */ + async function createDRACOLoader(decoderPath: string): Promise { const dracoLoader = new DRACOLoader() dracoLoader.setDecoderPath(decoderPath)