diff --git a/src/core/Gltf.tsx b/src/core/Gltf.tsx index a7b5d981e..e9749d456 100644 --- a/src/core/Gltf.tsx +++ b/src/core/Gltf.tsx @@ -1,67 +1,60 @@ import * as React from 'react' - +import { GLTFLoader, DRACOLoader, MeshoptDecoder, GLTF } from 'three-stdlib' +import { ObjectMap, useLoader } from '@react-three/fiber' import { Clone } from './Clone' -import { type Loader } from 'three' -import { type GLTF, GLTFLoader, DRACOLoader, MeshoptDecoder } from 'three-stdlib' -import { type ObjectMap, useLoader } from '@react-three/fiber' let dracoLoader: DRACOLoader | null = null - let decoderPath: string = 'https://www.gstatic.com/draco/versioned/decoders/1.5.5/' -function extensions(useDraco: boolean | string, useMeshopt: boolean, extendLoader?: (loader: GLTFLoader) => void) { - return (loader: Loader) => { +type Path = string | string[] +type UseDraco = boolean | string +type UseMeshopt = boolean +type ExtendLoader = (loader: GLTFLoader) => void + +function extensions(useDraco: UseDraco = true, useMeshopt: UseMeshopt = true, extendLoader?: ExtendLoader) { + return (loader: GLTFLoader) => { if (extendLoader) { - extendLoader(loader as GLTFLoader) + extendLoader(loader) } if (useDraco) { if (!dracoLoader) { dracoLoader = new DRACOLoader() } dracoLoader.setDecoderPath(typeof useDraco === 'string' ? useDraco : decoderPath) - ;(loader as GLTFLoader).setDRACOLoader(dracoLoader) + loader.setDRACOLoader(dracoLoader) } if (useMeshopt) { - ;(loader as GLTFLoader).setMeshoptDecoder( - typeof MeshoptDecoder === 'function' ? MeshoptDecoder() : MeshoptDecoder - ) + loader.setMeshoptDecoder(typeof MeshoptDecoder === 'function' ? MeshoptDecoder() : MeshoptDecoder) } } } -export function useGLTF( +export const useGLTF = ( path: T, - useDraco: boolean | string = true, - useMeshOpt: boolean = true, - extendLoader?: (loader: GLTFLoader) => void -): T extends any[] ? (GLTF & ObjectMap)[] : GLTF & ObjectMap { - return useLoader(GLTFLoader, path, extensions(useDraco, useMeshOpt, extendLoader)) -} + useDraco?: UseDraco, + useMeshopt?: UseMeshopt, + extendLoader?: ExtendLoader +): T extends any[] ? (GLTF & ObjectMap)[] : GLTF & ObjectMap => + useLoader(GLTFLoader, path, extensions(useDraco, useMeshopt, extendLoader)) -useGLTF.preload = ( - path: string | string[], - useDraco: boolean | string = true, - useMeshOpt: boolean = true, - extendLoader?: (loader: GLTFLoader) => void -) => useLoader.preload(GLTFLoader, path, extensions(useDraco, useMeshOpt, extendLoader)) +useGLTF.preload = (path: Path, useDraco: UseDraco, useMeshopt: UseMeshopt, extendLoader?: ExtendLoader) => + useLoader.preload(GLTFLoader, path, extensions(useDraco, useMeshopt, extendLoader)) -useGLTF.clear = (input: string | string[]) => useLoader.clear(GLTFLoader, input) +useGLTF.clear = (path: Path) => useLoader.clear(GLTFLoader, path) useGLTF.setDecoderPath = (path: string) => { decoderPath = path } // -type UseGLTF = Parameters type CloneProps = React.ComponentProps - type GltfRef = React.ElementRef type GltfProps = Omit & { src: string // simple string, not a string[] as useGLTF supports (otherwise we should render multiple s?) - useDraco?: UseGLTF[1] - useMeshOpt?: UseGLTF[2] - extendLoader?: UseGLTF[3] + useDraco?: UseDraco + useMeshOpt?: UseMeshopt + extendLoader?: ExtendLoader } export const Gltf = /* @__PURE__ */ React.forwardRef( ({ src, useDraco, useMeshOpt, extendLoader, ...props }, ref) => {