Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 432 async draco loader #433

Open
wants to merge 2 commits into
base: v4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions src/core/loaders/useGLTF/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,37 @@ export interface GLTFResult {
scene: THREE.Scene
}

let dracoLoader: DRACOLoader | null = null
/**
* Create the loader for Draco.
*
* @param {string} decoderPath
* @return {*}
*/

async function createDRACOLoader(decoderPath: string): Promise<DRACOLoader> {
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)
}
}
Expand All @@ -63,13 +75,20 @@ function setExtensions(options: GLTFLoaderOptions, extendLoader?: (loader: GLTFL
*/
export async function useGLTF<T extends string | string[]>(
path: T,
options: GLTFLoaderOptions = {
draco: false,
},
options: GLTFLoaderOptions = { draco: false },
extendLoader?: (loader: GLTFLoader) => void,
): Promise<T extends string[] ? GLTFResult[] : GLTFResult> {
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
}