Skip to content

Commit

Permalink
refactor to fps tracker class
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemckinstry committed Sep 24, 2024
1 parent 6712b1d commit 1c3528c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
40 changes: 40 additions & 0 deletions packages/engine/Source/Core/FpsTracker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import destroyObject from "../Core/destroyObject.js";
import getTimestamp from "../Core/getTimestamp.js";

/**
* @private
*/
function FpsTracker(options) {
this._timeStamps = [];
}

/**
* Calculates average fps based on timeStamps of recently rendered frames
*/
FpsTracker.prototype.getAverageFps = function () {
if (this._timeStamps.length !== 10) {
return;
}
const elapsedTime = (this._timeStamps[0] - this._timeStamps[9]) / 9;
const fps = 1 / (elapsedTime / 1000);
return fps;
};

/**
* Adds the timestamp for the current frame to the queue
*/
FpsTracker.prototype.updateFrameTimeStamp = function () {
const time = getTimestamp();
this._timeStamps.unshift(time);
if (this._timeStamps.length > 10) {
this._timeStamps.pop();
}
};

/**
* Destroys the WebGL resources held by this object.
*/
FpsTracker.prototype.destroy = function () {
return destroyObject(this);
};
export default FpsTracker;
18 changes: 3 additions & 15 deletions packages/engine/Source/Scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ import View from "./View.js";
import DebugInspector from "./DebugInspector.js";
import VoxelCell from "./VoxelCell.js";
import VoxelPrimitive from "./VoxelPrimitive.js";
import getTimestamp from "../Core/getTimestamp.js";
import FpsTracker from "../Core/FpsTracker.js";

const requestRenderAfterFrame = function (scene) {
return function () {
Expand Down Expand Up @@ -163,6 +163,7 @@ function Scene(options) {
this._frameState.scene3DOnly = defaultValue(options.scene3DOnly, false);
this._removeCreditContainer = !hasCreditContainer;
this._creditContainer = creditContainer;
this._fpsTracker = new FpsTracker();

this._canvas = canvas;
this._context = context;
Expand Down Expand Up @@ -749,7 +750,6 @@ function Scene(options) {

// Give frameState, camera, and screen space camera controller initial state before rendering
updateFrameNumber(this, 0.0, JulianDate.now());
this.updateFrameTimeStamp();
this.updateFrameState();
this.initializeFrame();
}
Expand Down Expand Up @@ -1919,18 +1919,6 @@ function updateFrameNumber(scene, frameNumber, time) {
frameState.time = JulianDate.clone(time, frameState.time);
}

/**
* @private
*/
Scene.prototype.updateFrameTimeStamp = function () {
const frameState = this._frameState;
const time = getTimestamp();
frameState.timeStamps.unshift(time);
if (frameState.timeStamps.length > 10) {
frameState.timeStamps.pop();
}
};

/**
* @private
*/
Expand Down Expand Up @@ -4110,7 +4098,7 @@ Scene.prototype.render = function (time) {
1.0
);
updateFrameNumber(this, frameNumber, time);
this.updateFrameTimeStamp();
this._fpsTracker.updateFrameTimeStamp();
frameState.newFrame = true;
}

Expand Down
7 changes: 2 additions & 5 deletions packages/engine/Source/Scene/ScreenSpaceCameraController.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,9 @@ function handleZoom(

const minDistance = distanceMeasure - minHeight;

const timeStamps = object._scene.frameState?.timeStamps;
let fpsMultiplier = 1;
if (timeStamps?.length === 10) {
const elapsedTime = (timeStamps[0] - timeStamps[9]) / 9;
const fps = 1 / (elapsedTime / 1000);
// target refresh rate of 30hz
const fps = object._scene._fpsTracker.getAverageFps();
if (fps) {
fpsMultiplier = 30 / fps;
}

Expand Down

0 comments on commit 1c3528c

Please sign in to comment.