Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timer: Added FixedTimer #27423

Merged
merged 4 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 5 additions & 27 deletions docs/examples/en/misc/Timer.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ <h1>[name]</h1>
<ul>
<li>[name] has an [page:.update]() method that updates its internal state. That makes it possible to call [page:.getDelta]() and [page:.getElapsed]() multiple times per simulation step without getting different values.</li>
<li>The class uses the Page Visibility API to avoid large time delta values when the app is inactive (e.g. tab switched or browser hidden).</li>
<li>It's possible to configure a fixed time delta and a time scale value (similar to Unity's Time interface).</li>
</ul>
</p>

Expand Down Expand Up @@ -65,21 +64,6 @@ <h3>Timer()</h3>

<h2>Methods</h2>

<h3>[method:this disableFixedDelta]()</h3>
<p>
Disables the usage of a fixed delta time.
</p>

<h3>[method:this dispose]()</h3>
<p>
Can be used to free all internal resources. Usually called when the timer instance isn't required anymore.
</p>

<h3>[method:this enableFixedDelta]()</h3>
<p>
Enables the usage of a fixed delta time.
</p>

<h3>[method:Number getDelta]()</h3>
<p>
Returns the time delta in seconds.
Expand All @@ -90,33 +74,27 @@ <h3>[method:Number getElapsed]()</h3>
Returns the elapsed time in seconds.
</p>

<h3>[method:Number getFixedDelta]()</h3>
<h3>[method:this setTimescale]( [param:Number timescale] )</h3>
<p>
Returns the fixed time delta that has been previously configured via [page:.setFixedDelta]().
Sets a time scale that scales the time delta in [page:.update]().
</p>

<h3>[method:this reset]()</h3>
<p>
Resets the time computation for the current simulation step.
</p>

<h3>[method:this setFixedDelta]( [param:Number delta] )</h3>
<p>
Defines a fixed time delta value which is used to update the timer per simulation step.
</p>

<h3>[method:this setTimescale]( [param:Number timescale] )</h3>
<h3>[method:this dispose]()</h3>
<p>
Sets a time scale that scales the time delta in [page:.update]().
Can be used to free all internal resources. Usually called when the timer instance isn't required anymore.
</p>

<h3>[method:this update]( [param:Number timestamp] )</h3>
<p>
timestamp -- (optional) The current time in milliseconds. Can be obtained from the
[link:https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame requestAnimationFrame]
callback argument. If not provided, the current time will be determined with
[link:https://developer.mozilla.org/en-US/docs/Web/API/Performance/now performance.now]. Please note that this
parameter has no effect when using a fixed time delta.<br /><br />
[link:https://developer.mozilla.org/en-US/docs/Web/API/Performance/now performance.now].<br /><br />

Updates the internal state of the timer. This method should be called once per simulation step
and before you perform queries against the timer (e.g. via [page:.getDelta]()).
Expand Down
79 changes: 27 additions & 52 deletions examples/jsm/misc/Timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class Timer {

this._timescale = 1;

this._useFixedDelta = false;
this._fixedDelta = 16.67; // ms, corresponds to approx. 60 FPS

// use Page Visibility API to avoid large time delta values

this._usePageVisibilityAPI = ( typeof document !== 'undefined' && document.hidden !== undefined );
Expand All @@ -28,34 +25,6 @@ class Timer {

}

disableFixedDelta() {

this._useFixedDelta = false;

return this;

}

dispose() {

if ( this._usePageVisibilityAPI === true ) {

document.removeEventListener( 'visibilitychange', this._pageVisibilityHandler );

}

return this;

}

enableFixedDelta() {

this._useFixedDelta = true;

return this;

}

getDelta() {

return this._delta / 1000;
Expand All @@ -68,15 +37,17 @@ class Timer {

}

getFixedDelta() {
getTimescale() {

return this._fixedDelta / 1000;
return this._timescale;

}

getTimescale() {
setTimescale( timescale ) {

return this._timescale;
this._timescale = timescale;

return this;

}

Expand All @@ -88,40 +59,44 @@ class Timer {

}

setFixedDelta( fixedDelta ) {
dispose() {

if ( this._usePageVisibilityAPI === true ) {

this._fixedDelta = fixedDelta * 1000;
document.removeEventListener( 'visibilitychange', this._pageVisibilityHandler );

}

return this;

}

setTimescale( timescale ) {
update( timestamp ) {

this._timescale = timescale;
this._previousTime = this._currentTime;
this._currentTime = ( timestamp !== undefined ? timestamp : now() ) - this._startTime;

this._delta = ( this._currentTime - this._previousTime ) * this._timescale;
this._elapsed += this._delta; // _elapsed is the accumulation of all previous deltas

return this;

}

update( timestamp ) {

if ( this._useFixedDelta === true ) {

this._delta = this._fixedDelta;
}

} else {
class FixedTimer extends Timer {

this._previousTime = this._currentTime;
this._currentTime = ( timestamp !== undefined ? timestamp : now() ) - this._startTime;
constructor( fps = 60 ) {

this._delta = this._currentTime - this._previousTime;
super();
this._delta = ( 1 / fps ) * 1000;

}
}

this._delta *= this._timescale;
update() {

this._elapsed += this._delta; // _elapsed is the accumulation of all previous deltas
this._elapsed += ( this._delta * this._timescale ); // _elapsed is the accumulation of all previous deltas

return this;

Expand All @@ -141,4 +116,4 @@ function handleVisibilityChange() {

}

export { Timer };
export { Timer, FixedTimer };