Drive the camera orbit and the FPS readout through the frame port

The orbit controller and the frame-rate monitor had left the Cesium
allow-list but still hooked the scene's preRender and postRender events.
They now listen through the frame port; the monitor looks the port up
each time it is shown, so a renderer bound after it was created is the
one it counts.
This commit is contained in:
Sameh Khamis committed 2026-11-07 01:04:47 -08:00
1 parent d0aa8d8f7d
commit 30b6be1da0
4 files changed
+20 -7

No files matched your search

+3 -3
View File
@@ -1,5 +1,5 @@
import { toRadians } from './geo/index.js'; import { toRadians } from './geo/index.js';
import { cameraOf, findCamera } from './render/rendererOf.js'; import { cameraOf, findCamera, frameOf } from './render/rendererOf.js';
import { import {
holdContinuousRender, holdContinuousRender,
releaseContinuousRender, releaseContinuousRender,
@@ -7,7 +7,7 @@ import {
/** /**
* OrbitController — smooth orbit around a target point. * OrbitController — smooth orbit around a target point.
* Uses scene.preRender for frame-rate-independent 60fps updates. * Uses the frame port's preRender for frame-rate-independent 60fps updates.
* Toggle with O key; auto-stops on POI/city change. * Toggle with O key; auto-stops on POI/city change.
*/ */
export class OrbitController { export class OrbitController {
@@ -50,7 +50,7 @@ export class OrbitController {
this.angle = pose.heading; this.angle = pose.heading;
let lastTime = Date.now(); let lastTime = Date.now();
this._removeListener = this.viewer.scene.preRender.addEventListener(() => { this._removeListener = frameOf(this.viewer).on('preRender', () => {
if (!this.active) return; if (!this.active) return;
const now = Date.now(); const now = Date.now();
+6 -3
View File
@@ -1,5 +1,6 @@
import { useStatusStore } from './stores/status.js'; import { useStatusStore } from './stores/status.js';
import { applicationPinia } from './vueRuntime.js'; import { applicationPinia } from './vueRuntime.js';
import { findFrame } from '../render/rendererOf.js';
/** /**
* Count rendered globe frames while the optional readout is visible. The * Count rendered globe frames while the optional readout is visible. The
@@ -10,9 +11,9 @@ export function createFrameRateMonitor({
documentRef = document, documentRef = document,
store = useStatusStore(applicationPinia()), store = useStatusStore(applicationPinia()),
}) { }) {
const frameEvent = viewer?.scene?.postRender; const frameOfViewer = () => findFrame(viewer);
// A page without the scene chrome has no title bar to show it in. // A page without the scene chrome has no title bar to show it in.
if (!documentRef.getElementById('title-bar') || !frameEvent) if (!documentRef.getElementById('title-bar') || !frameOfViewer())
return { destroy() {} }; return { destroy() {} };
// i18n-ignore: FPS is a unit code, the same in every language // i18n-ignore: FPS is a unit code, the same in every language
@@ -43,7 +44,9 @@ export function createFrameRateMonitor({
startedAt = performance.now(); startedAt = performance.now();
// i18n-ignore: FPS is a unit code, the same in every language // i18n-ignore: FPS is a unit code, the same in every language
present({ visible: true, text: 'FPS —' }); present({ visible: true, text: 'FPS —' });
removeFrameListener = frameEvent.addEventListener(() => { // Looked up when shown, so a renderer bound since creation is the one
// counted.
removeFrameListener = frameOfViewer()?.on('postRender', () => {
frames++; frames++;
}); });
timer = setInterval(() => { timer = setInterval(() => {
+4
View File
@@ -1,6 +1,8 @@
import test from 'node:test'; import test from 'node:test';
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import { createFrameRateMonitor } from './frameRateMonitor.js'; import { createFrameRateMonitor } from './frameRateMonitor.js';
import { bindRenderer } from '../render/rendererOf.js';
import { fakeSceneFrame } from '../testSupport/fakeCesiumScene.mjs';
function fixture(t) { function fixture(t) {
const keys = new Set(); const keys = new Set();
@@ -27,6 +29,7 @@ function fixture(t) {
}, },
}, },
}; };
bindRenderer(viewer, { frame: fakeSceneFrame(viewer) });
const monitor = createFrameRateMonitor({ viewer, documentRef, store }); const monitor = createFrameRateMonitor({ viewer, documentRef, store });
t.after(() => monitor.destroy()); t.after(() => monitor.destroy());
return { return {
@@ -133,6 +136,7 @@ test("destroying an earlier monitor leaves a newer one's readout", () => {
const viewer = { const viewer = {
scene: { postRender: { addEventListener: () => () => {} } }, scene: { postRender: { addEventListener: () => () => {} } },
}; };
bindRenderer(viewer, { frame: fakeSceneFrame(viewer) });
const first = createFrameRateMonitor({ viewer, documentRef, store }); const first = createFrameRateMonitor({ viewer, documentRef, store });
const second = createFrameRateMonitor({ viewer, documentRef, store }); const second = createFrameRateMonitor({ viewer, documentRef, store });
const shown = store.fps; const shown = store.fps;
+7 -1
View File
@@ -8,6 +8,8 @@ import { createPinia } from 'pinia';
import { useStatusStore } from './stores/status.js'; import { useStatusStore } from './stores/status.js';
import { createFrameRateMonitor } from './frameRateMonitor.js'; import { createFrameRateMonitor } from './frameRateMonitor.js';
import SceneChromeView from './views/SceneChromeView.vue'; import SceneChromeView from './views/SceneChromeView.vue';
import { bindRenderer } from '../render/rendererOf.js';
import { fakeSceneFrame } from '../testSupport/fakeCesiumScene.mjs';
test('the FPS readout sits in the title bar, hidden until the backquote key', async (t) => { test('the FPS readout sits in the title bar, hidden until the backquote key', async (t) => {
document.body.replaceChildren(); document.body.replaceChildren();
@@ -18,8 +20,12 @@ test('the FPS readout sits in the title bar, hidden until the backquote key', as
const readout = () => const readout = () =>
document.querySelector('#title-bar .frame-rate-readout'); document.querySelector('#title-bar .frame-rate-readout');
assert.equal(readout(), null, 'no readout without a monitor'); assert.equal(readout(), null, 'no readout without a monitor');
const viewer = {
scene: { postRender: { addEventListener: () => () => {} } },
};
bindRenderer(viewer, { frame: fakeSceneFrame(viewer) });
const monitor = createFrameRateMonitor({ const monitor = createFrameRateMonitor({
viewer: { scene: { postRender: { addEventListener: () => () => {} } } }, viewer,
store: useStatusStore(pinia), store: useStatusStore(pinia),
}); });
t.after(() => monitor.destroy()); t.after(() => monitor.destroy());