forked from google/model-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.js
151 lines (133 loc) · 3.88 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Box3, Object3D, Vector3} from 'three';
import {CachingGLTFLoader} from './CachingGLTFLoader.js';
/**
* An Object3D that can swap out its underlying
* model.
*
* @extends THREE.Object3D
*/
export default class Model extends Object3D {
/**
* Creates a model.
*/
constructor() {
super();
this.name = 'Model';
this.loader = new CachingGLTFLoader();
this.modelContainer = new Object3D();
this.modelContainer.name = 'ModelContainer';
this.boundingBox = new Box3();
this.size = new Vector3();
this.add(this.modelContainer);
}
/**
* Returns a boolean indicating whether or not there is a
* loaded model attached.
*
* @return {Boolean}
*/
hasModel() {
return !!this.modelContainer.children.length;
}
applyEnvironmentMap(map) {
// Note that unlit models (using MeshBasicMaterial) should not apply
// an environment map, even though `map` is the currently configured
// environment map.
this.modelContainer.traverse(obj => {
// There are some cases where `obj.material` is
// an array of materials.
if (Array.isArray(obj.material)) {
for (let material of obj.material) {
if (material.isMeshBasicMaterial) {
continue;
}
material.envMap = map;
material.needsUpdate = true;
}
}
else if (obj.material && !obj.material.isMeshBasicMaterial) {
obj.material.envMap = map;
obj.material.needsUpdate = true;
}
});
this.dispatchEvent({type: 'envmap-change', value: map});
}
setEnvironmentMapIntensity(intensity) {
const intensityIsNumber =
typeof intensity === 'number' && !self.isNaN(intensity);
if (!intensityIsNumber) {
intensity = 1.0;
}
this.modelContainer.traverse(object => {
if (object && object.isMesh && object.material) {
const {material} = object;
if (Array.isArray(object.material)) {
object.material.forEach(
material => material.envMapIntensity = intensity);
} else {
object.material.envMapIntensity = intensity;
}
}
});
}
/**
* Pass in a THREE.Object3D to be controlled
* by this model.
*
* @param {THREE.Object3D}
*/
setObject(model) {
this.clear();
this.modelContainer.add(model);
this.updateBoundingBox();
this.dispatchEvent({type: 'model-load'});
}
/**
* @param {String} url
*/
async setSource(url) {
if (!url || url === this.url) {
return;
}
const scene = await this.loader.load(url);
this.clear();
this.url = url;
while (scene && scene.children.length) {
this.modelContainer.add(scene.children.shift());
}
this.modelContainer.traverse(obj => {
if (obj && obj.type === 'Mesh') {
obj.castShadow = true;
}
});
this.updateBoundingBox();
this.dispatchEvent({type: 'model-load'});
}
clear() {
this.url = null;
// Remove all current children
while (this.modelContainer.children.length) {
this.modelContainer.remove(this.modelContainer.children[0]);
}
}
updateBoundingBox() {
this.remove(this.modelContainer);
this.boundingBox.setFromObject(this.modelContainer);
this.boundingBox.getSize(this.size);
this.add(this.modelContainer);
}
}