浏览代码

Merge pull request #7142 from sacharobarts/lockstep

Updating deterministic lockstep to work with 144Hz monitors
David Catuhe 5 年之前
父节点
当前提交
83dead20fb
共有 3 个文件被更改,包括 24 次插入7 次删除
  1. 11 0
      src/Engines/engine.ts
  2. 7 0
      src/Engines/thinEngine.ts
  3. 6 7
      src/scene.ts

+ 11 - 0
src/Engines/engine.ts

@@ -427,6 +427,7 @@ export class Engine extends ThinEngine {
     // Deterministic lockstepMaxSteps
     // Deterministic lockstepMaxSteps
     private _deterministicLockstep: boolean = false;
     private _deterministicLockstep: boolean = false;
     private _lockstepMaxSteps: number = 4;
     private _lockstepMaxSteps: number = 4;
+    private _timeStep: number = 1 / 60;
 
 
     protected get _supportsHardwareTextureRescaling() {
     protected get _supportsHardwareTextureRescaling() {
         return !!Engine._RescalePostProcessFactory;
         return !!Engine._RescalePostProcessFactory;
@@ -586,6 +587,8 @@ export class Engine extends ThinEngine {
 
 
             this._deterministicLockstep = !!options.deterministicLockstep;
             this._deterministicLockstep = !!options.deterministicLockstep;
             this._lockstepMaxSteps = options.lockstepMaxSteps || 0;
             this._lockstepMaxSteps = options.lockstepMaxSteps || 0;
+            this._timeStep = options.timeStep || 1 / 60;
+
         }
         }
 
 
         // Load WebVR Devices
         // Load WebVR Devices
@@ -667,6 +670,14 @@ export class Engine extends ThinEngine {
     }
     }
 
 
     /**
     /**
+     * Returns the time in ms between steps when using deterministic lock step.
+     * @returns time step in (ms)
+     */
+    public getTimeStep(): number {
+        return this._timeStep * 1000;
+    }
+
+    /**
      * Force the mipmap generation for the given render target texture
      * Force the mipmap generation for the given render target texture
      * @param texture defines the render target texture to use
      * @param texture defines the render target texture to use
      */
      */

+ 7 - 0
src/Engines/thinEngine.ts

@@ -88,10 +88,13 @@ export interface EngineOptions extends WebGLContextAttributes {
     deterministicLockstep?: boolean;
     deterministicLockstep?: boolean;
     /** Defines the maximum steps to use with deterministic lock step mode */
     /** Defines the maximum steps to use with deterministic lock step mode */
     lockstepMaxSteps?: number;
     lockstepMaxSteps?: number;
+    /** Defines the seconds between each deterministic lock step */
+    timeStep?: number;
     /**
     /**
      * Defines that engine should ignore context lost events
      * Defines that engine should ignore context lost events
      * If this event happens when this parameter is true, you will have to reload the page to restore rendering
      * If this event happens when this parameter is true, you will have to reload the page to restore rendering
      */
      */
+
     doNotHandleContextLost?: boolean;
     doNotHandleContextLost?: boolean;
     /**
     /**
      * Defines that engine should ignore modifying touch action attribute and style
      * Defines that engine should ignore modifying touch action attribute and style
@@ -488,6 +491,10 @@ export class ThinEngine {
                 options.lockstepMaxSteps = 4;
                 options.lockstepMaxSteps = 4;
             }
             }
 
 
+            if (options.timeStep === undefined) {
+                options.timeStep = 1 / 60;
+            }
+
             if (options.preserveDrawingBuffer === undefined) {
             if (options.preserveDrawingBuffer === undefined) {
                 options.preserveDrawingBuffer = false;
                 options.preserveDrawingBuffer = false;
             }
             }

+ 6 - 7
src/scene.ts

@@ -3739,7 +3739,7 @@ export class Scene extends AbstractScene implements IAnimatable {
      * User updatable function that will return a deterministic frame time when engine is in deterministic lock step mode
      * User updatable function that will return a deterministic frame time when engine is in deterministic lock step mode
      */
      */
     public getDeterministicFrameTime: () => number = () => {
     public getDeterministicFrameTime: () => number = () => {
-        return 1000.0 / 60.0; // frame time in ms
+        return this._engine.getTimeStep();
     }
     }
 
 
     /** @hidden */
     /** @hidden */
@@ -3752,18 +3752,17 @@ export class Scene extends AbstractScene implements IAnimatable {
         if (this._engine.isDeterministicLockStep()) {
         if (this._engine.isDeterministicLockStep()) {
             var deltaTime = Math.max(Scene.MinDeltaTime, Math.min(this._engine.getDeltaTime(), Scene.MaxDeltaTime)) + this._timeAccumulator;
             var deltaTime = Math.max(Scene.MinDeltaTime, Math.min(this._engine.getDeltaTime(), Scene.MaxDeltaTime)) + this._timeAccumulator;
 
 
-            var defaultFPS = (60.0 / 1000.0);
-
-            let defaultFrameTime = this.getDeterministicFrameTime();
+            let defaultFrameTime = this._engine.getTimeStep();
+            var defaultFPS = (1000.0 / defaultFrameTime) / 1000.0;
 
 
             let stepsTaken = 0;
             let stepsTaken = 0;
 
 
             var maxSubSteps = this._engine.getLockstepMaxSteps();
             var maxSubSteps = this._engine.getLockstepMaxSteps();
 
 
-            var internalSteps = Math.floor(deltaTime / (1000 * defaultFPS));
+            var internalSteps = Math.floor(deltaTime / defaultFrameTime);
             internalSteps = Math.min(internalSteps, maxSubSteps);
             internalSteps = Math.min(internalSteps, maxSubSteps);
 
 
-            do {
+            while (deltaTime > 0 && stepsTaken < internalSteps) {
                 this.onBeforeStepObservable.notifyObservers(this);
                 this.onBeforeStepObservable.notifyObservers(this);
 
 
                 // Animations
                 // Animations
@@ -3780,7 +3779,7 @@ export class Scene extends AbstractScene implements IAnimatable {
                 stepsTaken++;
                 stepsTaken++;
                 deltaTime -= defaultFrameTime;
                 deltaTime -= defaultFrameTime;
 
 
-            } while (deltaTime > 0 && stepsTaken < internalSteps);
+            }
 
 
             this._timeAccumulator = deltaTime < 0 ? 0 : deltaTime;
             this._timeAccumulator = deltaTime < 0 ? 0 : deltaTime;