From fbd7b2ae0be6854ad6ea168ee91abe6b792c65cb 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 1e889353..3ed9575c 100644 --- a/src/core/loaders/useGLTF/index.ts +++ b/src/core/loaders/useGLTF/index.ts @@ -83,7 +83,11 @@ export interface GLTFResult { scene: Scene } -let dracoLoader: DRACOLoader | null = null +function createDRACOLoader(decoderPath: string): DRACOLoader { + const dracoLoader = new DRACOLoader() + dracoLoader.setDecoderPath(decoderPath) + return dracoLoader +} export interface TresGLTFLoaderType extends TresLoader { setDRACOLoader?: (dracoLoader: DRACOLoader) => void @@ -93,21 +97,20 @@ export interface TresGLTFLoaderType extends TresLoader { * Sets the extensions for the GLTFLoader. * * @param {GLTFLoaderOptions} options - Options for the loader + * @param {DRACOLoader} [dracoLoader] * @param {(loader: TresGLTFLoaderType) => void} [extendLoader] - Function to extend the loader */ -function setExtensions(options: GLTFLoaderOptions, extendLoader?: (loader: TresGLTFLoaderType) => void) { +function setExtensions( + options: GLTFLoaderOptions, + dracoLoader: DRACOLoader | null, + extendLoader?: (loader: TresGLTFLoaderType, +) => void) { return (loader: TresGLTFLoaderType) => { if (extendLoader) { extendLoader(loader) } - if (options.draco) { - if (!dracoLoader) { - dracoLoader = new DRACOLoader() - } - dracoLoader.setDecoderPath(options.decoderPath || 'https://www.gstatic.com/draco/versioned/decoders/1.4.3/') - if (loader.setDRACOLoader) { - loader.setDRACOLoader(dracoLoader) - } + if (options.draco && dracoLoader && loader.setDRACOLoader) { + loader.setDRACOLoader(dracoLoader) } } } @@ -129,8 +132,17 @@ export async function useGLTF( }, extendLoader?: (loader: TresGLTFLoaderType) => void, ): Promise { - const gltfModel = (await useLoader(TresGLTFLoader, path, setExtensions(options, extendLoader))) as unknown as GLTFResult + const dracoLoader = options.draco + ? createDRACOLoader( + options.decoderPath + || 'https://www.gstatic.com/draco/versioned/decoders/1.4.3/', + ) + : null + const gltfModel = (await useLoader( + TresGLTFLoader, + path, + setExtensions(options, dracoLoader, extendLoader), + )) as unknown as GLTFResult dracoLoader?.dispose() - dracoLoader = null return gltfModel as T extends string[] ? GLTFResult[] : GLTFResult } From aa7429c45e4105ac25fe33ea623cc71705585194 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 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/loaders/useGLTF/index.ts b/src/core/loaders/useGLTF/index.ts index 3ed9575c..113696e9 100644 --- a/src/core/loaders/useGLTF/index.ts +++ b/src/core/loaders/useGLTF/index.ts @@ -83,6 +83,12 @@ export interface GLTFResult { scene: Scene } +/** + * Create the loader for Draco. + * + * @param {string} decoderPath + * @return {*} + */ function createDRACOLoader(decoderPath: string): DRACOLoader { const dracoLoader = new DRACOLoader() dracoLoader.setDecoderPath(decoderPath)