forked from pmndrs/drei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useGLTF.tsx
48 lines (42 loc) · 1.64 KB
/
useGLTF.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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) => {
if (extendLoader) {
extendLoader(loader as GLTFLoader)
}
if (useDraco) {
if (!dracoLoader) {
dracoLoader = new DRACOLoader()
}
dracoLoader.setDecoderPath(typeof useDraco === 'string' ? useDraco : decoderPath)
;(loader as GLTFLoader).setDRACOLoader(dracoLoader)
}
if (useMeshopt) {
;(loader as GLTFLoader).setMeshoptDecoder(
typeof MeshoptDecoder === 'function' ? MeshoptDecoder() : MeshoptDecoder
)
}
}
}
export function useGLTF<T extends string | string[]>(
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))
}
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.clear = (input: string | string[]) => useLoader.clear(GLTFLoader, input)
useGLTF.setDecoderPath = (path: string) => {
decoderPath = path
}