Cesium系列(8)--自定义帧率显示
Cesium自定义帧率显示
Cesium 提供了不少用于调试的工具,特别方便在开发的时候使用。其中帧率显示特别方便我们对性能调试。因为帧率到了30以下就基
本会开始卡顿了。如果想一直显示帧率,直接用 Cesium 默认的话,有时候会跟我们的界面的整体风格不搭配。那么就得自定义来实现
了。
结果对比
源代码 Source\Scene\PerformanceDisplay.js
代码修改
class CesiumFPSUtil {
constructor(containerId) {
this.id = containerId || "fpsdiv";
this._lastFpsSampleTime = getTimestamp();
this._lastMsSampleTime = getTimestamp();
this._fpsFrameCount = 0;
this._msFrameCount = 0;
this.createDiv();
}
createDiv() {
const parent = document.getElementById(this.id);
const fpsDiv = document.createElement("div");
fpsDiv.className = "info-content";
this._fpsText = document.createElement("span");
this._msText = document.createElement("span");
fpsDiv.append(this._fpsText);
fpsDiv.append(this._msText);
parent.append(fpsDiv);
}
update() {
let time = getTimestamp();
this._fpsFrameCount++;
let updateDisplay = true;
let fpsElapsedTime = time - this._lastFpsSampleTime;
if (fpsElapsedTime > 1000) {
var fps = "N/A";
if (updateDisplay) {
fps = ((this._fpsFrameCount * 1000) / fpsElapsedTime) | 0;
}
this._fpsText.innerText = fps + " FPS";
this._lastFpsSampleTime = time;
this._fpsFrameCount = 0;
}
this._msFrameCount++;
let msElapsedTime = time - this._lastMsSampleTime;
if (msElapsedTime > 200) {
let ms = "N/A";
if (updateDisplay) {
ms = (msElapsedTime / this._msFrameCount).toFixed(2);
}
this._msText.innerText = ms + " MS";
this._lastMsSampleTime = time;
this._msFrameCount = 0;
}
}
}相关函数getTimestamp
function getTimestamp() {
var getTime;
if (
typeof performance !== "undefined" &&
typeof performance.now === "function" &&
isFinite(performance.now())
) {
getTime = function () {
return performance.now();
};
} else {
getTime = function () {
return Date.now();
};
}
return getTime();
}使用
//帧率显示
const fpsInfo = new CesiumFPSUtil('fpsdiv')
var fpsPostEvent = function (){
fpsInfo.update()
}
viewer.scene.postRender.addEventListener(fpsPostEvent)
代码
完整代码都放到 github 上,需要的移步Cesium-demo-view
音乐小憩
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Jercky!
评论