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

Base zoom speed on browser refresh rate #12211

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
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;
3 changes: 3 additions & 0 deletions packages/engine/Source/Scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import View from "./View.js";
import DebugInspector from "./DebugInspector.js";
import VoxelCell from "./VoxelCell.js";
import VoxelPrimitive from "./VoxelPrimitive.js";
import FpsTracker from "../Core/FpsTracker.js";

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are accessing this from another class, can we expose this as fpsTracker with a getter? It can be marked as @private so it doesn't show up in the developer API documentation.


this._canvas = canvas;
this._context = context;
Expand Down Expand Up @@ -4096,6 +4098,7 @@ Scene.prototype.render = function (time) {
1.0
);
updateFrameNumber(this, frameNumber, time);
this._fpsTracker.updateFrameTimeStamp();
frameState.newFrame = true;
}

Expand Down
10 changes: 9 additions & 1 deletion packages/engine/Source/Scene/ScreenSpaceCameraController.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,15 @@ function handleZoom(
const maxHeight = object.maximumZoomDistance;

const minDistance = distanceMeasure - minHeight;
let zoomRate = zoomFactor * minDistance;

let fpsMultiplier = 1;
const fps = object._scene._fpsTracker.getAverageFps();
if (fps) {
fpsMultiplier = 30 / fps;
}

let zoomRate = zoomFactor * minDistance * fpsMultiplier;

zoomRate = CesiumMath.clamp(
zoomRate,
object._minimumZoomRate,
Expand Down
Loading