-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
44 lines (40 loc) · 1.11 KB
/
model.js
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
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader"
import { DRACOLoader } from "three/addons/loaders/DRACOLoader.js"
const draco = new DRACOLoader()
draco.setDecoderConfig({ type: "js" })
draco.setDecoderPath("https://www.gstatic.com/draco/v1/decoders/")
export function loadGLTFModel(
scene,
glbPath,
options = { receiveShadow: true, castShadow: true },
) {
const { receiveShadow, castShadow } = options
return new Promise((resolve, reject) => {
const loader = new GLTFLoader()
loader.setDRACOLoader(draco)
loader.load(
glbPath,
gltf => {
const obj = gltf.scene
obj.name = "dog"
obj.position.y = 0
obj.position.x = 0
obj.rotation.y = Math.PI / 2
obj.receiveShadow = receiveShadow
obj.castShadow = castShadow
scene.add(obj)
obj.traverse(function (child) {
if (child.isMesh) {
child.castShadow = castShadow
child.receiveShadow = receiveShadow
}
})
resolve(obj)
},
undefined,
function (error) {
reject(error)
},
)
})
}