-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.js
332 lines (293 loc) · 11.9 KB
/
index.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
let modelInfo = ModelIndex.getCurrentModel();
if (!modelInfo) {
modelInfo = TutorialModelIndex.getCurrentModel();
}
if (!modelInfo) {
modelInfo = TutorialPbrModelIndex.getCurrentModel();
}
if (!modelInfo) {
modelInfo = TutorialFurtherPbrModelIndex.getCurrentModel();
}
if (!modelInfo) {
modelInfo = TutorialFeatureTestModelIndex.getCurrentModel();
}
if (!modelInfo) {
modelInfo = TutorialExtensionTestModelIndex.getCurrentModel();
}
if (!modelInfo) {
document.getElementById('container').innerHTML = 'Please specify a model to load';
throw new Error('Model not specified or not found in list.');
}
import * as THREE from '../../libs/three.js/r123dev/build/three.module.js';
import { GUI } from '../../libs/three.js/r123dev/examples/jsm/libs/dat.gui.module.js';
import { OrbitControls } from '../../libs/three.js/r123dev/examples/jsm/controls/OrbitControls.js';
import { GLTFLoader } from '../../libs/three.js/r123dev/examples/jsm/loaders/GLTFLoader.js';
import { DRACOLoader } from '../../libs/three.js/r123dev/examples/jsm/loaders/DRACOLoader.js';
import { RGBELoader } from '../../libs/three.js/r123dev/examples/jsm/loaders/RGBELoader.js';
import { HDRCubeTextureLoader } from '../../libs/three.js/r123dev/examples/jsm/loaders/HDRCubeTextureLoader.js';
let gltf = null;
let mixer = null;
let clock = new THREE.Clock();
let axis;
let hemispheric;
let gui;
let state = {
ROTATE: true,
AXIS: true,
CUBEMAP: true,
IBL: true,
LIGHTS: false, // The default is to use IBL instead of lights
CAMERA: "",
VARIANT: ""
}
let scene;
let camera;
let renderer;
let controls;
let hdrCubeMap;
let hdrCubeRenderTarget;
let renderTarget;
let cubeMap;
let width;
let height;
init();
animate();
function resize() {
let container = document.getElementById('container');
width = container.offsetWidth;
height = container.offsetHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
}
function init() {
scene = new THREE.Scene();
hemispheric = new THREE.HemisphereLight( 0xffffff, 0x222222, 3.0 );
hemispheric.visible = state.LIGHTS; // The default is to use IBL instead of lights
scene.add(hemispheric);
camera = new THREE.PerspectiveCamera( 75, 1, 1, 10000 );
camera.position.set(0, 2, 3);
scene.add( camera );
let manager = new THREE.LoadingManager();
manager.onProgress = function ( item, loaded, total ) {
console.log( item, loaded, total );
};
// monkeypatch
// https://github.com/mrdoob/three.js/pull/11498#issuecomment-308136310
THREE.PropertyBinding.sanitizeNodeName = (n) => n;
let loader = new GLTFLoader();
loader.setCrossOrigin( 'anonymous' );
let dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath( '../../libs/three.js/r123dev/examples/js/libs/draco/gltf/' );
loader.setDRACOLoader( dracoLoader );
let scale = modelInfo.scale;
let url = "../../" + modelInfo.category + "/" + modelInfo.path;
if(modelInfo.url) {
url = modelInfo.url;
}
loader.load(url, function (data) {
gltf = data;
let object;
if ( gltf.scene !== undefined ) {
object = gltf.scene; // default scene
} else if ( gltf.scenes.length > 0 ) {
object = gltf.scenes[0]; // other scene
}
object.scale.set(scale, scale, scale);
if (modelInfo.name == "GearboxAssy" ) {
object.position.set(-159.20*scale, -17.02*scale, -3.21*scale);
}
if ( gltf.cameras.length > 0 ) {
for (let i = 0; i < gltf.cameras.length; i++ ) {
let camera_ = gltf.cameras[i];
camera_.name = camera_.name == "" ? "camera" + i : camera_.name;
camera_.aspect = width / height;
}
let cameraNames = gltf.cameras.map(camera => camera.name);
let guiCameras = gui.add(state, 'CAMERA', cameraNames).name("Camera");
guiCameras.onChange(function (value) {
var camera_ = gltf.cameras.find(function(camera_) {
return camera_.name === value;
});
camera = camera_;
camera.updateProjectionMatrix();
});
}
let animations = gltf.animations;
if ( animations && animations.length ) {
mixer = new THREE.AnimationMixer( object );
if (modelInfo.name == "Fox" ) {
let animation = animations[2]; // 0:Survey, 1:Walk, 2:Run
mixer.clipAction( animation ).play();
} else {
for ( let i = 0; i < animations.length; i ++ ) {
let animation = animations[ i ];
mixer.clipAction( animation ).play();
}
}
}
const hdrUrls = [
'specular_right_0.hdr',
'specular_left_0.hdr',
'specular_top_0.hdr',
'specular_bottom_0.hdr',
'specular_front_0.hdr',
'specular_back_0.hdr'
];
hdrCubeMap = new HDRCubeTextureLoader()
.setPath( 'https://rawcdn.githack.com/ux3d/glTF-Sample-Environments/4eace30f795fa77f6e059e3b31aa640c08a82133/papermill/specular/' )
.setDataType( THREE.UnsignedByteType )
.load( hdrUrls, function () {
let pmremGenerator = new THREE.PMREMGenerator( renderer );
pmremGenerator.compileCubemapShader();
hdrCubeRenderTarget = pmremGenerator.fromCubemap( hdrCubeMap );
hdrCubeMap.magFilter = THREE.LinearFilter;
hdrCubeMap.needsUpdate = true;
renderTarget = hdrCubeRenderTarget;
cubeMap = hdrCubeMap;
let newEnvMap = renderTarget ? renderTarget.texture : null;
applyEnvMap(object, newEnvMap);
scene.background = cubeMap;
} );
scene.add(object);
//window.scene = scene; // for Three.js Inspector
// KHR_materials_variants support
// See: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_variants
if (gltf.userData.gltfExtensions !== undefined) {
const extension = gltf.userData.gltfExtensions['KHR_materials_variants'];
if (extension !== undefined) {
let variants = extension.variants.map(variant => variant.name);
state.VARIANT = modelInfo.variant == undefined ? variants[0] : modelInfo.variant;
let guiVariants = gui.add(state, 'VARIANT', variants).name("Variant");
changeVariant(scene, extension, state.VARIANT);
guiVariants.onChange(function(value) {
changeVariant(scene, extension, value);
});
}
}
});
axis = new THREE.AxesHelper(1000);
scene.add(axis);
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.outputEncoding = THREE.sRGBEncoding; // if >r112, specify outputEncoding instead of gammaOutput
renderer.setClearColor( 0xaaaaaa );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.physicallyCorrectLights = true; // This will be required for matching the glTF spec.
controls = new OrbitControls( camera, renderer.domElement );
controls.userPan = false;
controls.userPanSpeed = 0.0;
controls.maxDistance = 5000.0;
controls.maxPolarAngle = Math.PI * 0.495;
controls.autoRotate = true;
controls.autoRotateSpeed = -3.0;
// GUI
gui = new GUI();
let guiRotate = gui.add(state, 'ROTATE').name('Rotate');
let guiAxis = gui.add(state, 'AXIS').name('Axis');
let guiCubeMap = gui.add(state, 'CUBEMAP').name('CubeMap');
let guiIbl = gui.add(state, 'IBL').name('IBL');
let guiLights = gui.add(state, 'LIGHTS').name('Lights');
guiRotate.onChange(function (value) {
controls.autoRotate = value;
});
guiAxis.onChange(function (value) {
axis.visible = value;
});
guiCubeMap.onChange(function (value) {
scene.background = value ? cubeMap : null;
});
guiIbl.onChange(function (value) {
let object = scene;
let newEnvMap = (renderTarget && value) ? renderTarget.texture : null;
applyEnvMap(object, newEnvMap);
});
guiLights.onChange(function (value) {
// TODO: Improvement is needed when displaying KHR_lights_punctual samples.
hemispheric.visible = value;
});
document.body.appendChild( renderer.domElement );
resize();
window.addEventListener( 'resize', resize, false );
}
function changeVariant(scene, extension, value) {
scene.traverse(async (object) => {
const variantIndex = extension.variants.findIndex((v) => v.name.includes(value));
if (!object.isMesh) return;
const meshVariantData = object.userData.gltfExtensions['KHR_materials_variants'];
if (meshVariantData !== undefined) {
let materialIndex = -1;
for (let i = 0; i < meshVariantData.mappings.length; i++) {
const mapping = meshVariantData.mappings[i];
if (mapping.variants.indexOf(variantIndex) != -1) {
materialIndex = mapping.material;
break;
}
}
if (materialIndex != -1) {
object.material = await gltf.parser.getDependency('material', materialIndex);
let newEnvMap = renderTarget ? renderTarget.texture : null;
applyEnvMap(object, newEnvMap);
}
}
});
}
function applyEnvMap(object, envMap) {
object.traverse( function( node ) {
if ( node.isMesh ) {
let materials = Array.isArray( node.material ) ? node.material : [ node.material ];
materials.forEach( function( material ) {
// MeshBasicMaterial means that KHR_materials_unlit is set, so reflections are not needed.
if ( 'envMap' in material && !material.isMeshBasicMaterial ) {
material.envMap = envMap;
material.needsUpdate = true;
}
} );
}
} );
}
/*
// https://github.com/mrdoob/three.js/tree/dev/examples/textures/cube/skybox
function getEnvMap() {
let path = '../../textures/cube/skybox/';
let format = '.jpg';
let urls = [
path + 'px' + format, path + 'nx' + format,
path + 'py' + format, path + 'ny' + format,
path + 'pz' + format, path + 'nz' + format
];
let loader = new THREE.CubeTextureLoader();
loader.setCrossOrigin( 'anonymous' );
let envMap = loader.load( urls );
envMap.format = THREE.RGBFormat;
// The color of the environment map is displayed brighter than r98
// https://github.com/mrdoob/three.js/issues/15285
envMap.encoding = THREE.sRGBEncoding;
return envMap;
}
*/
function getEnvMap() {
let path = '../../textures/papermill/specular/specular_';
let format = '.jpg';
let urls = [
path + 'right_0' + format, path + 'left_0' + format,
path + 'top_0' + format, path + 'bottom_0' + format,
path + 'front_0' + format, path + 'back_0' + format
];
let loader = new THREE.CubeTextureLoader();
loader.setCrossOrigin( 'anonymous' );
let envMap = loader.load( urls );
envMap.format = THREE.RGBFormat;
// The color of the environment map is displayed brighter than r98
// https://github.com/mrdoob/three.js/issues/15285
envMap.encoding = THREE.sRGBEncoding;
return envMap;
}
function animate() {
requestAnimationFrame( animate );
if ( mixer ) mixer.update( clock.getDelta() );
controls.update();
render();
}
function render() {
renderer.render( scene, camera );
}