Skip to content

Commit dc91443

Browse files
mkeblxmrdoob
authored andcommitted
Framerate independent animating for WebVR samples (mrdoob#9292)
1 parent b4bc767 commit dc91443

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

examples/webvr_cubes.html

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
//
3737

38+
var clock = new THREE.Clock();
39+
3840
var container;
3941
var camera, scene, raycaster, renderer;
4042
var effect, controls;
@@ -177,16 +179,18 @@
177179

178180
function render() {
179181

182+
var delta = clock.getDelta() * 60;
183+
180184
if ( isMouseDown === true ) {
181185

182186
var cube = room.children[ 0 ];
183187
room.remove( cube );
184188

185189
cube.position.set( 0, 0, - 0.75 );
186190
cube.position.applyQuaternion( camera.quaternion );
187-
cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02;
188-
cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02;
189-
cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 );
191+
cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta;
192+
cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta;
193+
cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta;
190194
cube.userData.velocity.applyQuaternion( camera.quaternion );
191195
room.add( cube );
192196

@@ -224,7 +228,7 @@
224228

225229
var cube = room.children[ i ];
226230

227-
cube.userData.velocity.multiplyScalar( 0.999 );
231+
cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
228232

229233
cube.position.add( cube.userData.velocity );
230234

@@ -249,9 +253,9 @@
249253

250254
}
251255

252-
cube.rotation.x += cube.userData.velocity.x * 2;
253-
cube.rotation.y += cube.userData.velocity.y * 2;
254-
cube.rotation.z += cube.userData.velocity.z * 2;
256+
cube.rotation.x += cube.userData.velocity.x * 2 * delta;
257+
cube.rotation.y += cube.userData.velocity.y * 2 * delta;
258+
cube.rotation.z += cube.userData.velocity.z * 2 * delta;
255259

256260
}
257261

examples/webvr_rollercoaster.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,14 @@
204204
var velocity = 0;
205205
var progress = 0;
206206

207+
var clock = new THREE.Clock();
208+
207209
function animate( time ) {
208210

209211
effect.requestAnimationFrame( animate );
210212

213+
var delta = clock.getDelta() * 60;
214+
211215
for ( var i = 0; i < funfairs.length; i ++ ) {
212216

213217
funfairs[ i ].rotation.y = time * 0.0002;
@@ -216,7 +220,7 @@
216220

217221
//
218222

219-
progress += velocity;
223+
progress += velocity * delta;
220224
progress = progress % 1;
221225

222226
position.copy( curve.getPointAt( progress ) );
@@ -226,7 +230,7 @@
226230

227231
tangent.copy( curve.getTangentAt( progress ) );
228232

229-
velocity -= tangent.y * 0.0000015;
233+
velocity -= tangent.y * 0.0000015 * delta;
230234
velocity = Math.max( velocity, 0.00004 );
231235

232236
train.lookAt( lookAt.copy( position ).sub( tangent ) );

examples/webvr_vive.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
//
4040

41+
var clock = new THREE.Clock();
42+
4143
var container;
4244
var camera, scene, renderer;
4345
var effect, controls;
@@ -226,6 +228,8 @@
226228

227229
function render() {
228230

231+
var delta = clock.getDelta() * 60;
232+
229233
controls.update();
230234

231235
for ( var i = 0; i < room.children.length; i ++ ) {
@@ -257,7 +261,7 @@
257261

258262
}
259263

260-
cube.rotation.x += 0.01;
264+
cube.rotation.x += 0.01 * delta;
261265

262266
}
263267

0 commit comments

Comments
 (0)