Skip to content

Commit 5d233d9

Browse files
authored
Examples: Improve GLTFLoader examples. (#32406)
1 parent f6d4703 commit 5d233d9

File tree

2 files changed

+78
-18
lines changed

2 files changed

+78
-18
lines changed

examples/webgl_loader_gltf.html

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
3333

3434
let camera, scene, renderer, controls;
35-
let currentModel;
35+
let currentModel, mixer;
36+
let currentLoadId = 0;
37+
38+
const timer = new THREE.Timer();
3639

3740
init();
3841

@@ -80,6 +83,8 @@
8083

8184
} );
8285

86+
gui.add( scene, 'backgroundBlurriness', 0, 1 );
87+
8388
const initialModel = models.find( m => m.name === params.model );
8489
if ( initialModel ) loadModel( initialModel );
8590

@@ -90,12 +95,12 @@
9095
renderer = new THREE.WebGLRenderer( { antialias: true } );
9196
renderer.setPixelRatio( window.devicePixelRatio );
9297
renderer.setSize( window.innerWidth, window.innerHeight );
98+
renderer.setAnimationLoop( render );
9399
renderer.toneMapping = THREE.ACESFilmicToneMapping;
94-
renderer.toneMappingExposure = 1;
95100
container.appendChild( renderer.domElement );
96101

97102
controls = new OrbitControls( camera, renderer.domElement );
98-
controls.addEventListener( 'change', render ); // use if there is no animation loop
103+
controls.enableDamping = true;
99104
controls.minDistance = 2;
100105
controls.maxDistance = 10;
101106
controls.target.set( 0, 0, - 0.2 );
@@ -115,31 +120,48 @@
115120

116121
scene.remove( currentModel );
117122
currentModel = null;
118-
render();
119123

120124
}
121125

126+
if ( mixer ) {
127+
128+
mixer.stopAllAction();
129+
mixer = null;
130+
131+
}
132+
133+
const loadId = ++ currentLoadId;
134+
122135
const loader = new GLTFLoader();
123136
loader.load( url, async function ( gltf ) {
124137

138+
if ( loadId !== currentLoadId ) return;
139+
125140
currentModel = gltf.scene;
126141

127142
// wait until the model can be added to the scene without blocking due to shader compilation
128143

129144
await renderer.compileAsync( currentModel, camera, scene );
130145

146+
if ( loadId !== currentLoadId ) return;
147+
131148
scene.add( currentModel );
132149

133-
// scale to 1.0
150+
fitCameraToSelection( camera, controls, currentModel );
134151

135-
const box = new THREE.Box3().setFromObject( currentModel );
136-
const size = box.getSize( new THREE.Vector3() );
137-
const maxSize = Math.max( size.x, size.y, size.z );
138-
currentModel.scale.multiplyScalar( 1.0 / maxSize );
152+
// animations
139153

140-
fitCameraToSelection( camera, controls, currentModel );
154+
if ( gltf.animations.length > 0 ) {
155+
156+
mixer = new THREE.AnimationMixer( currentModel );
157+
158+
for ( const animation of gltf.animations ) {
159+
160+
mixer.clipAction( animation ).play();
161+
162+
}
141163

142-
render();
164+
}
143165

144166
} );
145167

@@ -190,6 +212,12 @@
190212

191213
function render() {
192214

215+
timer.update();
216+
217+
controls.update();
218+
219+
if ( mixer ) mixer.update( timer.getDelta() );
220+
193221
renderer.render( scene, camera );
194222

195223
}

examples/webgpu_loader_gltf.html

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@
4343
import { Inspector } from 'three/addons/inspector/Inspector.js';
4444

4545
let camera, scene, renderer, controls;
46-
let currentModel;
46+
let currentModel, mixer;
47+
let currentLoadId = 0;
48+
49+
const timer = new THREE.Timer();
4750

4851
init().then( render );
4952

@@ -91,6 +94,8 @@
9194

9295
} );
9396

97+
gui.add( scene, 'backgroundBlurriness', 0, 1 );
98+
9499
const initialModel = models.find( m => m.name === params.model );
95100
if ( initialModel ) loadModel( initialModel );
96101

@@ -109,6 +114,7 @@
109114
await renderer.init();
110115

111116
controls = new OrbitControls( camera, renderer.domElement );
117+
controls.enableDamping = true;
112118
controls.minDistance = 2;
113119
controls.maxDistance = 10;
114120
controls.target.set( 0, 0, - 0.2 );
@@ -131,25 +137,45 @@
131137

132138
}
133139

140+
if ( mixer ) {
141+
142+
mixer.stopAllAction();
143+
mixer = null;
144+
145+
}
146+
147+
const loadId = ++ currentLoadId;
148+
134149
const loader = new GLTFLoader();
135150
loader.load( url, async function ( gltf ) {
136151

152+
if ( loadId !== currentLoadId ) return;
153+
137154
currentModel = gltf.scene;
138155

139156
// wait until the model can be added to the scene without blocking due to shader compilation
140157

141158
await renderer.compileAsync( currentModel, camera, scene );
142159

160+
if ( loadId !== currentLoadId ) return;
161+
143162
scene.add( currentModel );
144163

145-
// scale to 1.0
164+
fitCameraToSelection( camera, controls, currentModel );
165+
166+
// animations
146167

147-
const box = new THREE.Box3().setFromObject( currentModel );
148-
const size = box.getSize( new THREE.Vector3() );
149-
const maxSize = Math.max( size.x, size.y, size.z );
150-
currentModel.scale.multiplyScalar( 1.0 / maxSize );
168+
if ( gltf.animations.length > 0 ) {
151169

152-
fitCameraToSelection( camera, controls, currentModel );
170+
mixer = new THREE.AnimationMixer( currentModel );
171+
172+
for ( const animation of gltf.animations ) {
173+
174+
mixer.clipAction( animation ).play();
175+
176+
}
177+
178+
}
153179

154180
} );
155181

@@ -198,6 +224,12 @@
198224

199225
function render() {
200226

227+
timer.update();
228+
229+
controls.update();
230+
231+
if ( mixer ) mixer.update( timer.getDelta() );
232+
201233
renderer.render( scene, camera );
202234

203235
}

0 commit comments

Comments
 (0)