mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 08:42:21 +00:00
feat(time): the app clock on the engine
src/time/clock.js is the app's own time in ms since the epoch: by default
Date.now exactly (read at each call), with freeze/offset/rate/reset for QA
and tests, and one frame time latched per engine frame (beginFrame). The
contract gains engine.clock { now, frameTime, ticked }, in the fake engine
(a controllable wall clock, fake.advanceTime, fake.appClock), the contract
suite (one frame time for every listener of a frame, latched before
preUpdate, never decreasing), Cesium and Navara.
Cesium latches on the viewer clock's onTick, which CesiumWidget runs right
before scene.render, so scene listeners the app added before the engine
existed read the same frame time; the subscription appears on first use
and destroy removes it. The viewer clock itself is untouched: in the app
it never animates (shouldAnimate false, multiplier 1, its time frozen at
boot), and its time still drives Cesium's lighting as before. Navara
latches in the engine's handler of the view's preUpdate, and the look
reads the engine clock's frame time for the sun, so the app clock is the
one time source on Navara too.
src/time is a portable root in check-import-directions: no engine
packages, no browser globals.
This commit is contained in:
1 parent
27e28373d0
commit
f1a2df5cfe
20 files changed
+771
-13
No files matched your search
@@ -25,9 +25,12 @@ import {
|
||||
createEngineEvent,
|
||||
mergeSharpen,
|
||||
} from '../contract.js';
|
||||
import { createAppClock } from '../../time/clock.js';
|
||||
|
||||
const { cartesian2, cartesian3, cartographic, ellipsoid, math, matrix4 } = geo;
|
||||
const WGS84 = ellipsoid.WGS84;
|
||||
// Where the fake wall clock starts: 2026-01-01T00:00:00Z.
|
||||
const FAKE_EPOCH_MS = Date.UTC(2026, 0, 1);
|
||||
const RADII = WGS84.radii;
|
||||
const MAXIMUM_RADIUS = Math.max(RADII.x, RADII.y, RADII.z);
|
||||
// The terrain kinds the engines build themselves (src/maps/registry.js).
|
||||
@@ -220,10 +223,14 @@ function rectangleFromCartographics(points) {
|
||||
/**
|
||||
* @param {{ kind?: 'cesium'|'navara', width?: number, height?: number, fovy?: number,
|
||||
* longitude?: number, latitude?: number, altitude?: number,
|
||||
* container?: object, canvas?: object }} [options]
|
||||
* container?: object, canvas?: object, nowMs?: number,
|
||||
* appClock?: import('../../time/clock.js').AppClock }} [options]
|
||||
* Canvas size in CSS px, angles in radians; `container`/`canvas` stand in
|
||||
* for the host elements (DOM mocks for shell tests). The initial pose looks straight
|
||||
* down from `altitude` metres (10 000 km).
|
||||
* down from `altitude` metres (10 000 km). The engine clock reads
|
||||
* `appClock`, by default an app clock on a fake wall clock that starts at
|
||||
* `nowMs` (2026-01-01T00:00Z) and moves only with `fake.frame(dt)` and
|
||||
* `fake.advanceTime(ms)` (a given appClock keeps its own wall clock).
|
||||
* @returns {import('../contract.js').GlobeEngine & { fake: object }}
|
||||
*/
|
||||
export function createFakeEngine(options = {}) {
|
||||
@@ -248,7 +255,11 @@ export function createFakeEngine(options = {}) {
|
||||
'frames.preUpdate': createEngineEvent(),
|
||||
'frames.preRender': createEngineEvent(),
|
||||
'frames.postRender': createEngineEvent(),
|
||||
'clock.ticked': createEngineEvent(),
|
||||
};
|
||||
let wallMs = options.nowMs ?? FAKE_EPOCH_MS;
|
||||
const appClock = options.appClock ?? createAppClock({ now: () => wallMs });
|
||||
const ownsWall = !options.appClock;
|
||||
const inputs = new Map(INPUT_TYPES.map((type) => [type, new Set()]));
|
||||
const livePosition = cartesian3.create();
|
||||
let lastBaseImagery = null;
|
||||
@@ -782,6 +793,11 @@ export function createFakeEngine(options = {}) {
|
||||
viewProjection,
|
||||
horizonVisible,
|
||||
},
|
||||
clock: {
|
||||
now: () => appClock.now(),
|
||||
frameTime: () => appClock.frameTime(),
|
||||
ticked: events['clock.ticked'],
|
||||
},
|
||||
frames: {
|
||||
preUpdate: events['frames.preUpdate'],
|
||||
preRender: events['frames.preRender'],
|
||||
@@ -951,9 +967,17 @@ export function createFakeEngine(options = {}) {
|
||||
scripts.delete(name);
|
||||
},
|
||||
lookAtTarget: () => (lookAtTarget ? copy3(lookAtTarget) : undefined),
|
||||
/** Runs one frame: preUpdate, flight progress, preRender, camera events, postRender. */
|
||||
/** The app clock the engine clock reads (freeze/offset/rate for tests). */
|
||||
appClock,
|
||||
/** Moves the fake wall clock without a frame (own app clock only). */
|
||||
advanceTime(ms) {
|
||||
if (ownsWall) wallMs += ms;
|
||||
},
|
||||
/** Runs one frame: clock.ticked, preUpdate, flight progress, preRender, camera events, postRender. */
|
||||
frame(dt = 16) {
|
||||
clock += dt;
|
||||
if (ownsWall) wallMs += dt;
|
||||
events['clock.ticked'].raiseValue(appClock.beginFrame());
|
||||
events['frames.preUpdate'].raiseEvent(clock);
|
||||
advanceFlight(dt);
|
||||
events['frames.preRender'].raiseEvent(clock);
|
||||
|
||||
Reference in new issue
Block a user