render: hold continuous render while the world clock plays away from live

Playback moves something every frame, so it draws at the display's rate
rather than the parked scene's 60 FPS cap. followWorldTime reports when the
clock starts and stops playing (not live, not stepped, not waiting for data).
This commit is contained in:
Sameh Khamis committed 2026-11-15 13:35:11 -08:00
1 parent cb3be34283
commit 8af8c086a6
3 files changed
+90 -4

No files matched your search

+8
View File
@@ -13,6 +13,8 @@ import { initLogoGaze } from '../logoGaze.js';
import {
uninstallRenderGovernor,
governorRequestRender,
holdContinuousRender,
releaseContinuousRender,
} from '../renderGovernor.js';
import { describeError } from './errors.js';
import { isEmbeddedInline } from './embed.js';
@@ -73,6 +75,12 @@ export async function createApplicationScene({
followWorldTime(viewer, worldTimeFor('sky'), {
subscribe: worldClock.subscribe,
requestRender: () => governorRequestRender('world-time'),
// A playing world time moves something every frame: draw at the
// display's rate meanwhile, not the parked scene's cap.
onPlaying: (playing) =>
playing
? holdContinuousRender('world-time')
: releaseContinuousRender('world-time'),
}),
);
defer(installFreeLookHeightLimit(viewer));
+23 -4
View File
@@ -11,16 +11,29 @@ import * as Cesium from 'cesium';
* The clock's `currentTime` follows too, for anything that samples it.
* @param {Cesium.Viewer} viewer
* @param {() => number} readTime Epoch ms the sky is drawn at.
* @param {{subscribe?: Function, requestRender?: () => void}} [options]
* @param {{subscribe?: Function, requestRender?: () => void,
* onPlaying?: (playing: boolean) => void}} [options]
* A world clock to hear moves from, so a resting scene repaints when the
* world time jumps.
* world time jumps. `onPlaying` hears when the world clock starts and stops
* playing away from live (not stepped by a driver and not waiting for a
* layer's data), so the scene can draw every frame meanwhile.
* @returns {() => void} Stop following.
*/
export function followWorldTime(
viewer,
readTime,
{ subscribe, requestRender = () => viewer.scene.requestRender() } = {},
{
subscribe,
requestRender = () => viewer.scene.requestRender(),
onPlaying = () => {},
} = {},
) {
let playing = false;
const setPlaying = (next) => {
if (next === playing) return;
playing = next;
onPlaying(next);
};
const date = new Date();
const removePreUpdate = viewer.scene.preUpdate.addEventListener(
(_scene, time) => {
@@ -33,7 +46,12 @@ export function followWorldTime(
);
let last = readTime();
const unsubscribe =
subscribe?.(() => {
subscribe?.((state) => {
setPlaying(
Boolean(
state?.playing && !state.live && !state.stepping && !state.buffering,
),
);
const next = readTime();
if (next === last) return;
last = next;
@@ -42,5 +60,6 @@ export function followWorldTime(
return () => {
removePreUpdate();
unsubscribe();
setPlaying(false);
};
}
+59
View File
@@ -0,0 +1,59 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { followWorldTime } from './viewerClock.js';
function fakeViewer() {
const listeners = new Set();
return {
clock: { currentTime: {} },
scene: {
requestRender() {},
preUpdate: {
addEventListener(fn) {
listeners.add(fn);
return () => listeners.delete(fn);
},
},
},
};
}
test('the scene hears when the world time plays on its own, and when it stops', () => {
let listener = null;
const subscribe = (fn) => {
listener = fn;
return () => (listener = null);
};
const heard = [];
let at = 0;
const stop = followWorldTime(fakeViewer(), () => at, {
subscribe,
requestRender: () => {},
onPlaying: (playing) => heard.push(playing),
});
const state = (fields) => ({
live: false,
playing: false,
stepping: false,
buffering: false,
...fields,
});
listener(state({ live: true, playing: true }));
assert.deepEqual(heard, [], 'live time is not playback');
listener(state({ playing: true }));
listener(state({ playing: true }));
assert.deepEqual(heard, [true], 'heard once');
listener(state({ playing: true, buffering: true }));
assert.deepEqual(heard, [true, false], 'waiting for a layer: still');
listener(state({ playing: true }));
listener(state({ playing: true, stepping: true }));
assert.deepEqual(heard, [true, false, true, false], 'a driver steps it');
listener(state({ playing: true }));
stop();
assert.deepEqual(
heard,
[true, false, true, false, true, false],
'stopping releases',
);
assert.equal(listener, null);
});