-
Notifications
You must be signed in to change notification settings - Fork 63
/
three.js
279 lines (218 loc) · 8.72 KB
/
three.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// This is a modified/simplified version of the published example:
// https://github.com/mrdoob/three.js/blob/dev/examples/webgl_loader_gltf2.html
var ThreePreview = function() {
// Tracks if this engine is currently the active engine.
var enabled = false;
var orbitControls = null;
var container = null;
var camera = null;
var scene = null;
var renderer = null;
var loader = null;
var defaultCamera = null;
var gltf = null;
var mixer = null;
var clock = new THREE.Clock();
var sceneList = null;
window.onerror = null;
function onload() {
switchScene(0);
animate();
window.addEventListener('resize', onWindowResize, false);
}
function initScene(index) {
container = document.getElementById('container');
scene = new THREE.Scene();
// Note: The near and far planes can be set this way due to the use of "logarithmicDepthBuffer" in the renderer below.
defaultCamera = new THREE.PerspectiveCamera(45, container.offsetWidth / container.offsetHeight, 1e-5, 1e10);
scene.add(defaultCamera);
camera = defaultCamera;
var sceneInfo = sceneList[0];
var spot1 = null;
if (sceneInfo.addLights) {
var ambient = new THREE.AmbientLight(0x222222);
scene.add(ambient);
var directionalLight = new THREE.DirectionalLight(0xcccccc);
directionalLight.position.set(-1, 0, -2).normalize();
scene.add(directionalLight);
if (sceneInfo.shadows) {
spot1 = new THREE.SpotLight(0xffffff, 1);
spot1.position.set(10, 20, 10);
spot1.angle = 0.25;
spot1.distance = 1024;
spot1.penumbra = 0.75;
spot1.castShadow = true;
spot1.shadow.bias = 0.0001;
spot1.shadow.mapSize.width = 2048;
spot1.shadow.mapSize.height = 2048;
scene.add(spot1);
} else {
var directionalLight2 = new THREE.DirectionalLight(0xdddddd);
directionalLight2.position.set(1, 2, 2).normalize();
scene.add(directionalLight2);
}
}
// RENDERER
renderer = new THREE.WebGLRenderer({ antialias: true, logarithmicDepthBuffer: true });
renderer.setClearColor(0x222222);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
if (sceneInfo.shadows) {
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
}
container.appendChild(renderer.domElement);
var ground = null;
if (sceneInfo.addGround) {
var groundMaterial = new THREE.MeshPhongMaterial({
color: 0xFFFFFF,
shading: THREE.SmoothShading
});
ground = new THREE.Mesh(new THREE.PlaneBufferGeometry(512, 512), groundMaterial);
if (sceneInfo.shadows) {
ground.receiveShadow = true;
}
if (sceneInfo.groundPos) {
ground.position.copy(sceneInfo.groundPos);
} else {
ground.position.z = -70;
}
ground.rotation.x = -Math.PI / 2;
scene.add(ground);
}
loader = new THREE.GLTF2Loader();
var url = sceneInfo.url;
var loadStartTime = performance.now();
var status = document.getElementById("status");
status.innerHTML = "Loading...";
loader.load(url, function(data) {
gltf = data;
var object = gltf.scene;
status.innerHTML = "Load time: " + (performance.now() - loadStartTime).toFixed(2) + " ms.";
var defaultThreeReflection = document.getElementById('defaultThreeReflection').textContent.split('{face}');
var envPath = defaultThreeReflection[0];
var envFormat = defaultThreeReflection[1];
var envMap = new THREE.CubeTextureLoader().load([
envPath + 'posx' + envFormat, envPath + 'negx' + envFormat,
envPath + 'posy' + envFormat, envPath + 'negy' + envFormat,
envPath + 'posz' + envFormat, envPath + 'negz' + envFormat
]);
envMap.format = THREE.RGBFormat;
object.traverse(function(node) {
if (node.material && 'envMap' in node.material) {
node.material.envMap = envMap;
node.material.normalScale.x = -1; // This fixes normal maps for ThreeJS.
node.material.needsUpdate = true;
}
});
backgroundGuiElement.style.display = 'block';
function applyBackground(showBackground) {
scene.background = showBackground ? envMap : null;
}
applyBackground(options.showBackground);
options.backgroundGuiCallback = applyBackground;
if (sceneInfo.cameraPos)
defaultCamera.position.copy(sceneInfo.cameraPos);
if (sceneInfo.center) {
orbitControls.target.copy(sceneInfo.center);
}
if (sceneInfo.objectPosition) {
object.position.copy(sceneInfo.objectPosition);
if (spot1) {
spot1.position.set(sceneInfo.objectPosition.x - 100, sceneInfo.objectPosition.y + 200, sceneInfo.objectPosition.z - 100);
spot1.target.position.copy(sceneInfo.objectPosition);
}
}
if (sceneInfo.objectRotation) {
object.rotation.copy(sceneInfo.objectRotation);
}
if (sceneInfo.objectScale) {
object.scale.copy(sceneInfo.objectScale);
}
var animations = gltf.animations;
if (animations && animations.length) {
mixer = new THREE.AnimationMixer(object);
for (var i = 0; i < animations.length; i++) {
var animation = animations[i];
// There's .3333 seconds junk at the tail of the Monster animation that
// keeps it from looping cleanly. Clip it at 3 seconds
if (sceneInfo.animationTime) {
animation.duration = sceneInfo.animationTime;
}
mixer.clipAction(animation).play();
}
}
scene.add(object);
onWindowResize();
});
orbitControls = new THREE.OrbitControls(defaultCamera, renderer.domElement);
}
function onWindowResize() {
if (!enabled) {
return;
}
defaultCamera.aspect = container.offsetWidth / container.offsetHeight;
defaultCamera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
if (!enabled) {
return;
}
requestAnimationFrame(animate);
if (mixer) {
mixer.update(clock.getDelta());
}
orbitControls.update();
render();
}
function render() {
renderer.render(scene, camera);
}
function switchScene(index) {
cleanup();
enabled = true;
initScene(index);
}
/**
* @function cleanup
* Perform any cleanup that needs to happen to stop rendering the current model.
* This is called right before the active engine for the preview window is switched.
*/
this.cleanup = function() {
enabled = false;
if (container && renderer) {
container.removeChild(renderer.domElement);
}
defaultCamera = null;
if (!loader || !mixer) {
return;
}
mixer.stopAllAction();
window.removeEventListener('resize', onWindowResize, false);
};
this.startPreview = function() {
var rootPath = "file:///" + document.getElementById("gltfRootPath").textContent;
var fileName = document.getElementById("gltfFileName").textContent;
sceneList = [
{
name: "glTF Preview", url: rootPath + fileName,
cameraPos: new THREE.Vector3(2, 1, 3),
objectRotation: new THREE.Euler(0, Math.PI, 0),
addLights: true
}
];
onload();
};
};
/**
* @function cleanup
* Perform any cleanup that needs to happen to stop rendering the current model.
* This is called right before the active engine for the preview window is switched.
*/
function cleanup() {
options.backgroundGuiCallback = function() {};
threePreview.cleanup();
}
var threePreview = new ThreePreview();
threePreview.startPreview();