Skip to content

Commit c71a2d8

Browse files
authored
Added THREE.Timer. (#17912)
* Added THREE.Timer. * Timer: Make now() private. * Timer: Fix typo. * Timer: Shorten method and variable names. * Timer: Remove js and add d.ts file. * Timer: Transform to class. * Update webgl_morphtargets_sphere.html * Delete Timer.d.ts * Update webgl_morphtargets_sphere.html * Timer: Add optional timestamp parameter to update(). * Timer: Fix multiple instance usage. * Timer: Clean up.
1 parent 6b19d22 commit c71a2d8

File tree

2 files changed

+149
-19
lines changed

2 files changed

+149
-19
lines changed

examples/jsm/misc/Timer.js

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
class Timer {
2+
3+
constructor() {
4+
5+
this._previousTime = 0;
6+
this._currentTime = 0;
7+
this._startTime = now();
8+
9+
this._delta = 0;
10+
this._elapsed = 0;
11+
12+
this._timescale = 1;
13+
14+
this._useFixedDelta = false;
15+
this._fixedDelta = 16.67; // ms, corresponds to approx. 60 FPS
16+
17+
// use Page Visibility API to avoid large time delta values
18+
19+
this._usePageVisibilityAPI = ( typeof document !== 'undefined' && document.hidden !== undefined );
20+
21+
if ( this._usePageVisibilityAPI === true ) {
22+
23+
this._pageVisibilityHandler = handleVisibilityChange.bind( this );
24+
25+
document.addEventListener( 'visibilitychange', this._pageVisibilityHandler, false );
26+
27+
}
28+
29+
}
30+
31+
disableFixedDelta() {
32+
33+
this._useFixedDelta = false;
34+
35+
return this;
36+
37+
}
38+
39+
dispose() {
40+
41+
if ( this._usePageVisibilityAPI === true ) {
42+
43+
document.removeEventListener( 'visibilitychange', this._pageVisibilityHandler );
44+
45+
}
46+
47+
return this;
48+
49+
}
50+
51+
enableFixedDelta() {
52+
53+
this._useFixedDelta = true;
54+
55+
return this;
56+
57+
}
58+
59+
getDelta() {
60+
61+
return this._delta / 1000;
62+
63+
}
64+
65+
getElapsed() {
66+
67+
return this._elapsed / 1000;
68+
69+
}
70+
71+
getFixedDelta() {
72+
73+
return this._fixedDelta / 1000;
74+
75+
}
76+
77+
getTimescale() {
78+
79+
return this._timescale;
80+
81+
}
82+
83+
reset() {
84+
85+
this._currentTime = now() - this._startTime;
86+
87+
return this;
88+
89+
}
90+
91+
setFixedDelta( fixedDelta ) {
92+
93+
this._fixedDelta = fixedDelta * 1000;
94+
95+
return this;
96+
97+
}
98+
99+
setTimescale( timescale ) {
100+
101+
this._timescale = timescale;
102+
103+
return this;
104+
105+
}
106+
107+
update() {
108+
109+
if ( this._useFixedDelta === true ) {
110+
111+
this._delta = this._fixedDelta;
112+
113+
} else {
114+
115+
this._previousTime = this._currentTime;
116+
this._currentTime = now() - this._startTime;
117+
118+
this._delta = this._currentTime - this._previousTime;
119+
120+
}
121+
122+
this._delta *= this._timescale;
123+
124+
this._elapsed += this._delta; // _elapsed is the accumulation of all previous deltas
125+
126+
return this;
127+
128+
}
129+
130+
}
131+
132+
function now() {
133+
134+
return ( typeof performance === 'undefined' ? Date : performance ).now();
135+
136+
}
137+
138+
function handleVisibilityChange() {
139+
140+
if ( document.hidden === false ) this.reset();
141+
142+
}
143+
144+
export { Timer };

examples/webgl_morphtargets_sphere.html

+5-19
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828

2929
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
3030
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
31+
import { Timer } from 'three/addons/misc/Timer.js';
3132

32-
let camera, scene, renderer, clock;
33+
let camera, scene, renderer, timer;
3334

3435
let mesh;
3536

@@ -48,7 +49,7 @@
4849

4950
scene = new THREE.Scene();
5051

51-
clock = new THREE.Clock();
52+
timer = new Timer();
5253

5354
const light1 = new THREE.PointLight( 0xff2200, 50000 );
5455
light1.position.set( 100, 100, 100 );
@@ -102,8 +103,6 @@
102103

103104
window.addEventListener( 'resize', onWindowResize );
104105

105-
document.addEventListener( 'visibilitychange', onVisibilityChange );
106-
107106
}
108107

109108
function onWindowResize() {
@@ -115,30 +114,17 @@
115114

116115
}
117116

118-
function onVisibilityChange() {
119-
120-
if ( document.hidden === true ) {
121-
122-
clock.stop();
123-
124-
} else {
125-
126-
clock.start();
127-
128-
}
129-
130-
}
131-
132117
function animate() {
133118

134119
requestAnimationFrame( animate );
120+
timer.update();
135121
render();
136122

137123
}
138124

139125
function render() {
140126

141-
const delta = clock.getDelta();
127+
const delta = timer.getDelta();
142128

143129
if ( mesh !== undefined ) {
144130

0 commit comments

Comments
 (0)