|
24 | 24 | ////////////////////////////// |
25 | 25 | // Global objects |
26 | 26 | ////////////////////////////// |
27 | | - var worldScene = null; // THREE.Scene where it all will be rendered |
28 | | - var renderer = null; |
29 | | - var camera = null; |
30 | | - var clock = null; |
31 | | - var mixers = []; // All the THREE.AnimationMixer objects for all the animations in the scene |
| 27 | + let worldScene = null; // THREE.Scene where it all will be rendered |
| 28 | + let renderer = null; |
| 29 | + let camera = null; |
| 30 | + let clock = null; |
| 31 | + const mixers = []; // All the THREE.AnimationMixer objects for all the animations in the scene |
32 | 32 | ////////////////////////////// |
33 | 33 |
|
34 | 34 |
|
|
40 | 40 | // A model may have multiple SkinnedMesh objects as well as several rigs (armatures). Units will define which |
41 | 41 | // meshes, armatures and animations to use. We will load the whole scene for each object and clone it for each unit. |
42 | 42 | // Models are from https://www.mixamo.com/ |
43 | | - var MODELS = [ |
| 43 | + const MODELS = [ |
44 | 44 | { name: "Soldier" }, |
45 | 45 | { name: "Parrot" }, |
46 | 46 | // { name: "RiflePunch" }, |
47 | 47 | ]; |
48 | 48 |
|
49 | 49 | // Here we define instances of the models that we want to place in the scene, their position, scale and the animations |
50 | 50 | // that must be played. |
51 | | - var UNITS = [ |
| 51 | + const UNITS = [ |
52 | 52 | { |
53 | 53 | modelName: "Soldier", // Will use the 3D model from file models/gltf/Soldier.glb |
54 | 54 | meshName: "vanguard_Mesh", // Name of the main mesh to animate |
|
91 | 91 | ////////////////////////////// |
92 | 92 | // The main setup happens here |
93 | 93 | ////////////////////////////// |
94 | | - var numLoadedModels = 0; |
| 94 | + let numLoadedModels = 0; |
95 | 95 | initScene(); |
96 | 96 | initRenderer(); |
97 | 97 | loadModels(); |
|
109 | 109 | */ |
110 | 110 | function loadModels() { |
111 | 111 |
|
112 | | - for ( var i = 0; i < MODELS.length; ++ i ) { |
| 112 | + for ( let i = 0; i < MODELS.length; ++ i ) { |
113 | 113 |
|
114 | | - var m = MODELS[ i ]; |
| 114 | + const m = MODELS[ i ]; |
115 | 115 |
|
116 | 116 | loadGltfModel( m, function () { |
117 | 117 |
|
|
136 | 136 | */ |
137 | 137 | function instantiateUnits() { |
138 | 138 |
|
139 | | - var numSuccess = 0; |
| 139 | + let numSuccess = 0; |
140 | 140 |
|
141 | | - for ( var i = 0; i < UNITS.length; ++ i ) { |
| 141 | + for ( let i = 0; i < UNITS.length; ++ i ) { |
142 | 142 |
|
143 | | - var u = UNITS[ i ]; |
144 | | - var model = getModelByName( u.modelName ); |
| 143 | + const u = UNITS[ i ]; |
| 144 | + const model = getModelByName( u.modelName ); |
145 | 145 |
|
146 | 146 | if ( model ) { |
147 | 147 |
|
148 | | - var clonedScene = SkeletonUtils.clone( model.scene ); |
| 148 | + const clonedScene = SkeletonUtils.clone( model.scene ); |
149 | 149 |
|
150 | 150 | if ( clonedScene ) { |
151 | 151 |
|
152 | 152 | // THREE.Scene is cloned properly, let's find one mesh and launch animation for it |
153 | | - var clonedMesh = clonedScene.getObjectByName( u.meshName ); |
| 153 | + const clonedMesh = clonedScene.getObjectByName( u.meshName ); |
154 | 154 |
|
155 | 155 | if ( clonedMesh ) { |
156 | 156 |
|
157 | | - var mixer = startAnimation( clonedMesh, model.animations, u.animationName ); |
| 157 | + const mixer = startAnimation( clonedMesh, model.animations, u.animationName ); |
158 | 158 |
|
159 | 159 | // Save the animation mixer in the list, will need it in the animation loop |
160 | 160 | mixers.push( mixer ); |
|
211 | 211 | */ |
212 | 212 | function startAnimation( skinnedMesh, animations, animationName ) { |
213 | 213 |
|
214 | | - var mixer = new THREE.AnimationMixer( skinnedMesh ); |
215 | | - var clip = THREE.AnimationClip.findByName( animations, animationName ); |
| 214 | + const mixer = new THREE.AnimationMixer( skinnedMesh ); |
| 215 | + const clip = THREE.AnimationClip.findByName( animations, animationName ); |
216 | 216 |
|
217 | 217 | if ( clip ) { |
218 | 218 |
|
219 | | - var action = mixer.clipAction( clip ); |
| 219 | + const action = mixer.clipAction( clip ); |
220 | 220 | action.play(); |
221 | 221 |
|
222 | 222 | } |
|
232 | 232 | */ |
233 | 233 | function getModelByName( name ) { |
234 | 234 |
|
235 | | - for ( var i = 0; i < MODELS.length; ++ i ) { |
| 235 | + for ( let i = 0; i < MODELS.length; ++ i ) { |
236 | 236 |
|
237 | 237 | if ( MODELS[ i ].name === name ) { |
238 | 238 |
|
|
253 | 253 | */ |
254 | 254 | function loadGltfModel( model, onLoaded ) { |
255 | 255 |
|
256 | | - var loader = new GLTFLoader(); |
257 | | - var modelName = "models/gltf/" + model.name + ".glb"; |
| 256 | + const loader = new GLTFLoader(); |
| 257 | + const modelName = "models/gltf/" + model.name + ".glb"; |
258 | 258 |
|
259 | 259 | loader.load( modelName, function ( gltf ) { |
260 | 260 |
|
261 | | - var scene = gltf.scene; |
| 261 | + const scene = gltf.scene; |
262 | 262 |
|
263 | 263 | model.animations = gltf.animations; |
264 | 264 | model.scene = scene; |
|
292 | 292 |
|
293 | 293 | // Get the time elapsed since the last frame |
294 | 294 |
|
295 | | - var mixerUpdateDelta = clock.getDelta(); |
| 295 | + const mixerUpdateDelta = clock.getDelta(); |
296 | 296 |
|
297 | 297 | // Update all the animation frames |
298 | 298 |
|
299 | | - for ( var i = 0; i < mixers.length; ++ i ) { |
| 299 | + for ( let i = 0; i < mixers.length; ++ i ) { |
300 | 300 |
|
301 | 301 | mixers[ i ].update( mixerUpdateDelta ); |
302 | 302 |
|
|
316 | 316 | */ |
317 | 317 | function initRenderer() { |
318 | 318 |
|
319 | | - var container = document.getElementById( 'container' ); |
| 319 | + const container = document.getElementById( 'container' ); |
320 | 320 | renderer = new THREE.WebGLRenderer( { antialias: true } ); |
321 | 321 | renderer.setPixelRatio( window.devicePixelRatio ); |
322 | 322 | renderer.setSize( window.innerWidth, window.innerHeight ); |
|
342 | 342 | worldScene.background = new THREE.Color( 0xa0a0a0 ); |
343 | 343 | worldScene.fog = new THREE.Fog( 0xa0a0a0, 10, 22 ); |
344 | 344 |
|
345 | | - var hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 ); |
| 345 | + const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 ); |
346 | 346 | hemiLight.position.set( 0, 20, 0 ); |
347 | 347 | worldScene.add( hemiLight ); |
348 | 348 |
|
349 | | - var dirLight = new THREE.DirectionalLight( 0xffffff ); |
| 349 | + const dirLight = new THREE.DirectionalLight( 0xffffff ); |
350 | 350 | dirLight.position.set( - 3, 10, - 10 ); |
351 | 351 | dirLight.castShadow = true; |
352 | 352 | dirLight.shadow.camera.top = 10; |
|
358 | 358 | worldScene.add( dirLight ); |
359 | 359 |
|
360 | 360 | // ground |
361 | | - var groundMesh = new THREE.Mesh( |
| 361 | + const groundMesh = new THREE.Mesh( |
362 | 362 | new THREE.PlaneBufferGeometry( 40, 40 ), |
363 | 363 | new THREE.MeshPhongMaterial( { |
364 | 364 | color: 0x999999, |
|
0 commit comments