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 #598

Open
wants to merge 2 commits into
base: main
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
42 changes: 30 additions & 12 deletions src/core/loaders/useGLTF/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ export interface GLTFResult {
scene: Scene
}

let dracoLoader: DRACOLoader | null = null
/**
* Create the loader for Draco.
*
* @param {string} decoderPath
* @return {*}
*/
function createDRACOLoader(decoderPath: string): DRACOLoader {
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath(decoderPath)
return dracoLoader
}

export interface TresGLTFLoaderType extends TresLoader<GLTF> {
setDRACOLoader?: (dracoLoader: DRACOLoader) => void
Expand All @@ -93,21 +103,20 @@ export interface TresGLTFLoaderType extends TresLoader<GLTF> {
* 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)
}
}
}
Expand All @@ -129,8 +138,17 @@ export async function useGLTF<T extends string | string[]>(
},
extendLoader?: (loader: TresGLTFLoaderType) => void,
): Promise<T extends string[] ? GLTFResult[] : GLTFResult> {
const gltfModel = (await useLoader<GLTF>(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<GLTF>(
TresGLTFLoader,
path,
setExtensions(options, dracoLoader, extendLoader),
)) as unknown as GLTFResult
dracoLoader?.dispose()
dracoLoader = null
return gltfModel as T extends string[] ? GLTFResult[] : GLTFResult
}