浏览代码

Timer: Added FixedTimer (#27423)

* Timer: Added FixedTimer.

* Updated docs.

* Updated docs.
mrdoob 1 年之前
父节点
当前提交
08b24a17a4
共有 2 个文件被更改,包括 32 次插入79 次删除
  1. 5 27
      docs/examples/en/misc/Timer.html
  2. 27 52
      examples/jsm/misc/Timer.js

+ 5 - 27
docs/examples/en/misc/Timer.html

@@ -17,7 +17,6 @@
 			<ul>
 			<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>[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>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>
 			</ul>
 		</p>
 		</p>
 
 
@@ -65,21 +64,6 @@
 
 
 		<h2>Methods</h2>
 		<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>
 		<h3>[method:Number getDelta]()</h3>
 		<p>
 		<p>
 			Returns the time delta in seconds.
 			Returns the time delta in seconds.
@@ -90,9 +74,9 @@
 			Returns the elapsed time in seconds.
 			Returns the elapsed time in seconds.
 		</p>
 		</p>
 
 
-		<h3>[method:Number getFixedDelta]()</h3>
+		<h3>[method:this setTimescale]( [param:Number timescale] )</h3>
 		<p>
 		<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>
 		</p>
 
 
 		<h3>[method:this reset]()</h3>
 		<h3>[method:this reset]()</h3>
@@ -100,14 +84,9 @@
 			Resets the time computation for the current simulation step.
 			Resets the time computation for the current simulation step.
 		</p>
 		</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>
 		<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>
 		</p>
 
 
 		<h3>[method:this update]( [param:Number timestamp] )</h3>
 		<h3>[method:this update]( [param:Number timestamp] )</h3>
@@ -115,8 +94,7 @@
 			timestamp -- (optional) The current time in milliseconds. Can be obtained from the
 			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]
 			[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
 			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
 			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]()).
 			and before you perform queries against the timer (e.g. via [page:.getDelta]()).

+ 27 - 52
examples/jsm/misc/Timer.js

@@ -11,9 +11,6 @@ class Timer {
 
 
 		this._timescale = 1;
 		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
 		// use Page Visibility API to avoid large time delta values
 
 
 		this._usePageVisibilityAPI = ( typeof document !== 'undefined' && document.hidden !== undefined );
 		this._usePageVisibilityAPI = ( typeof document !== 'undefined' && document.hidden !== undefined );
@@ -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() {
 	getDelta() {
 
 
 		return this._delta / 1000;
 		return this._delta / 1000;
@@ -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;
 
 
 	}
 	}
 
 
@@ -88,40 +59,44 @@ class Timer {
 
 
 	}
 	}
 
 
-	setFixedDelta( fixedDelta ) {
+	dispose() {
+
+		if ( this._usePageVisibilityAPI === true ) {
 
 
-		this._fixedDelta = fixedDelta * 1000;
+			document.removeEventListener( 'visibilitychange', this._pageVisibilityHandler );
+
+		}
 
 
 		return this;
 		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;
 		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;
 		return this;
 
 
@@ -141,4 +116,4 @@ function handleVisibilityChange() {
 
 
 }
 }
 
 
-export { Timer };
+export { Timer, FixedTimer };