mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 16:52:20 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d37e8cf17d | ||
|
|
3e1070a2cb |
No files matched your search
@@ -1,5 +0,0 @@
|
||||
- Fix rendering stopping for good after a wheel zoom with the pointer on the
|
||||
centre of a whole-globe view. Cesium's zoom could ask the camera to rotate
|
||||
about a zero axis; that rotation is now ignored, and a render loop stopped by
|
||||
any error is restarted from the last drawn camera pose, up to three times in
|
||||
ten seconds (mazy1998, #1704).
|
||||
@@ -0,0 +1,3 @@
|
||||
- A still-image CCTV camera no longer keeps the scene rendering 60 times a
|
||||
second. Only a video feed holds continuous rendering; a still redraws when a
|
||||
new image arrives (mazy1998, #1710).
|
||||
@@ -207,7 +207,6 @@
|
||||
"modules": [
|
||||
"src/app/viewer.js",
|
||||
"src/app/atmosphereCompat.js",
|
||||
"src/app/renderRecovery.js",
|
||||
"src/globeInView.js",
|
||||
"src/sky/starrySky.js",
|
||||
"src/sky/stars.js",
|
||||
@@ -2611,7 +2610,6 @@
|
||||
"src/sky/starrySky.js",
|
||||
"src/sky/stars.js",
|
||||
"src/app/atmosphereCompat.js",
|
||||
"src/app/renderRecovery.js",
|
||||
"src/bloom.js",
|
||||
"src/camera.js",
|
||||
"src/cameraGroundGuard.js",
|
||||
|
||||
@@ -1,155 +0,0 @@
|
||||
/**
|
||||
* Keeps one bad frame from stopping the globe for good.
|
||||
*
|
||||
* Cesium's render loop stops itself on the first error a frame throws and
|
||||
* leaves an error panel over the canvas; nothing restarts it short of a
|
||||
* reload. Two things here:
|
||||
*
|
||||
* 1. `guardDegenerateRotation` removes one such error at its cause. Zooming
|
||||
* with the wheel above 1,000 km, Cesium turns the globe from the point under
|
||||
* the screen centre towards the point under the pointer
|
||||
* (`ScreenSpaceCameraController` `handleZoom`): it normalizes both points,
|
||||
* takes their dot product, and rotates about their cross product when the
|
||||
* dot product is below 1. With the pointer on the centre pixel the two
|
||||
* points are the same, but a unit vector's dot product with itself often
|
||||
* rounds to 0.9999999999999999, so the check passes and the cross product is
|
||||
* the zero vector. `Camera.rotate` then normalizes it and throws
|
||||
* "normalized result is not a number" from `Scene.initializeFrame`, which
|
||||
* runs outside the scene's own error handling. Builds without Cesium's
|
||||
* debug checks would write NaN into the camera instead.
|
||||
*
|
||||
* 2. `installRenderRecovery` restarts the loop after any error that stops it,
|
||||
* from the last pose a frame was drawn with, a bounded number of times.
|
||||
*/
|
||||
import * as Cesium from 'cesium';
|
||||
import { governorRequestRender } from '../renderGovernor.js';
|
||||
|
||||
/** Restarts allowed within RECOVERY_WINDOW_MS before the error is left to stand. */
|
||||
export const RECOVERY_LIMIT = 3;
|
||||
/** An error that keeps coming back this fast is not one a restart gets past. */
|
||||
export const RECOVERY_WINDOW_MS = 10_000;
|
||||
|
||||
/**
|
||||
* Make `camera.rotate` ignore an axis it cannot normalize (zero or not finite)
|
||||
* and an angle that is not finite. There is nothing to rotate about, so the
|
||||
* camera stays where it is and the caller carries on; Cesium's wheel zoom
|
||||
* still moves the camera in after the skipped turn.
|
||||
* @param {Cesium.Camera} camera
|
||||
* @returns {() => void} Restore the camera's own rotate.
|
||||
*/
|
||||
export function guardDegenerateRotation(camera) {
|
||||
const own = Object.hasOwn(camera, 'rotate');
|
||||
const rotate = camera.rotate;
|
||||
const guarded = function (axis, angle) {
|
||||
if (axis) {
|
||||
const magnitudeSquared = Cesium.Cartesian3.magnitudeSquared(axis);
|
||||
if (
|
||||
!(magnitudeSquared > 0) ||
|
||||
!Number.isFinite(magnitudeSquared) ||
|
||||
(angle !== undefined && !Number.isFinite(angle))
|
||||
)
|
||||
return;
|
||||
}
|
||||
return rotate.call(this, axis, angle);
|
||||
};
|
||||
camera.rotate = guarded;
|
||||
return () => {
|
||||
if (camera.rotate !== guarded) return;
|
||||
if (own) camera.rotate = rotate;
|
||||
else delete camera.rotate;
|
||||
};
|
||||
}
|
||||
|
||||
function finite(cartesian) {
|
||||
return (
|
||||
Number.isFinite(cartesian?.x) &&
|
||||
Number.isFinite(cartesian?.y) &&
|
||||
Number.isFinite(cartesian?.z)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restart Cesium's render loop when an error stops it.
|
||||
*
|
||||
* Cesium reports a stopped loop only through `showErrorPanel`: errors thrown
|
||||
* while a frame is prepared (camera input, in `Scene.initializeFrame`) never
|
||||
* reach `scene.renderError`, so that event alone would miss them. The panel
|
||||
* call is the one place both kinds of stop pass through, after the loop is
|
||||
* already off.
|
||||
*
|
||||
* On a stop this puts the camera back on the last pose a frame was drawn
|
||||
* with, discards the input that was being applied (Cesium skips its own reset
|
||||
* when the frame throws, so the same gesture would otherwise run again), and
|
||||
* turns the loop back on. After RECOVERY_LIMIT restarts inside
|
||||
* RECOVERY_WINDOW_MS the error is shown as before and rendering stays stopped.
|
||||
* @param {Cesium.Viewer} viewer
|
||||
* @param {object} [options]
|
||||
* @param {() => number} [options.now]
|
||||
* @param {(reason: string) => void} [options.requestRender]
|
||||
* @returns {() => void} Idempotent disposer.
|
||||
*/
|
||||
export function installRenderRecovery(
|
||||
viewer,
|
||||
{ now = () => performance.now(), requestRender = governorRequestRender } = {},
|
||||
) {
|
||||
const widget = viewer?.cesiumWidget;
|
||||
const scene = viewer?.scene;
|
||||
const camera = viewer?.camera;
|
||||
if (!widget?.showErrorPanel || !scene?.postRender || !camera)
|
||||
throw new TypeError('A complete Cesium viewer is required');
|
||||
|
||||
// The pose of the last frame that drew: three vector copies a frame.
|
||||
const position = new Cesium.Cartesian3();
|
||||
const direction = new Cesium.Cartesian3();
|
||||
const up = new Cesium.Cartesian3();
|
||||
let hasPose = false;
|
||||
const removePostRender = scene.postRender.addEventListener(() => {
|
||||
if (!finite(camera.position) || !finite(camera.direction)) return;
|
||||
Cesium.Cartesian3.clone(camera.position, position);
|
||||
Cesium.Cartesian3.clone(camera.direction, direction);
|
||||
Cesium.Cartesian3.clone(camera.up, up);
|
||||
hasPose = true;
|
||||
});
|
||||
|
||||
const recoveredAt = [];
|
||||
const own = Object.hasOwn(widget, 'showErrorPanel');
|
||||
const showErrorPanel = widget.showErrorPanel;
|
||||
const recovering = function (title, message, error) {
|
||||
const at = now();
|
||||
while (recoveredAt.length && at - recoveredAt[0] > RECOVERY_WINDOW_MS)
|
||||
recoveredAt.shift();
|
||||
// Only a stopped loop is restarted; other panels (a failed start-up, a
|
||||
// caller's own message) are shown as they are.
|
||||
if (
|
||||
viewer.useDefaultRenderLoop !== false ||
|
||||
recoveredAt.length >= RECOVERY_LIMIT
|
||||
)
|
||||
return showErrorPanel.call(this, title, message, error);
|
||||
recoveredAt.push(at);
|
||||
console.error(
|
||||
`[viewer] render loop stopped by an error; restarting (${recoveredAt.length} of ${RECOVERY_LIMIT})`,
|
||||
error,
|
||||
);
|
||||
if (hasPose) {
|
||||
Cesium.Cartesian3.clone(position, camera.position);
|
||||
Cesium.Cartesian3.clone(direction, camera.direction);
|
||||
Cesium.Cartesian3.clone(up, camera.up);
|
||||
Cesium.Cartesian3.cross(camera.direction, camera.up, camera.right);
|
||||
}
|
||||
// Cesium has no public way to drop the input a failed frame left queued.
|
||||
scene.screenSpaceCameraController?._aggregator?.reset?.();
|
||||
viewer.useDefaultRenderLoop = true;
|
||||
requestRender('render-recovery');
|
||||
};
|
||||
widget.showErrorPanel = recovering;
|
||||
|
||||
let active = true;
|
||||
return () => {
|
||||
if (!active) return;
|
||||
active = false;
|
||||
removePostRender?.();
|
||||
if (widget.showErrorPanel !== recovering) return;
|
||||
if (own) widget.showErrorPanel = showErrorPanel;
|
||||
else delete widget.showErrorPanel;
|
||||
};
|
||||
}
|
||||
@@ -1,220 +0,0 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
guardDegenerateRotation,
|
||||
installRenderRecovery,
|
||||
RECOVERY_LIMIT,
|
||||
RECOVERY_WINDOW_MS,
|
||||
} from './renderRecovery.js';
|
||||
|
||||
/** Cesium's own Camera.rotate on a camera-shaped object, with no scene behind it. */
|
||||
function cameraFixture() {
|
||||
const camera = Object.create(Cesium.Camera.prototype);
|
||||
camera.position = new Cesium.Cartesian3(28_378_137, 0, 0);
|
||||
camera.direction = new Cesium.Cartesian3(-1, 0, 0);
|
||||
camera.up = new Cesium.Cartesian3(0, 0, 1);
|
||||
camera.right = new Cesium.Cartesian3(0, 1, 0);
|
||||
camera._adjustOrthographicFrustum = () => {};
|
||||
return camera;
|
||||
}
|
||||
|
||||
const pose = (camera) =>
|
||||
[camera.position, camera.direction, camera.up, camera.right].map((v) =>
|
||||
Cesium.Cartesian3.clone(v),
|
||||
);
|
||||
|
||||
test("the wheel zoom's own arithmetic hands Camera.rotate a zero axis", () => {
|
||||
// What handleZoom computes with the pointer on the centre pixel: the same
|
||||
// picked point twice. The dot product rounds below 1, the cross product is 0.
|
||||
const picked = Cesium.Cartesian3.fromDegrees(-41.11, -13.37);
|
||||
const pickedNormal = Cesium.Cartesian3.normalize(
|
||||
picked,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const centreNormal = Cesium.Cartesian3.normalize(
|
||||
picked,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const axis = Cesium.Cartesian3.cross(
|
||||
pickedNormal,
|
||||
centreNormal,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
assert.deepEqual([axis.x, axis.y, axis.z], [0, 0, 0]);
|
||||
|
||||
const unguarded = cameraFixture();
|
||||
assert.throws(() => unguarded.rotate(axis, 0.01), /not a number/);
|
||||
|
||||
const camera = cameraFixture();
|
||||
const before = pose(camera);
|
||||
guardDegenerateRotation(camera);
|
||||
assert.doesNotThrow(() => camera.rotate(axis, 0.01));
|
||||
assert.deepEqual(pose(camera), before);
|
||||
});
|
||||
|
||||
test('an axis or angle that is not finite leaves the camera where it is', () => {
|
||||
const camera = cameraFixture();
|
||||
const before = pose(camera);
|
||||
guardDegenerateRotation(camera);
|
||||
camera.rotate(new Cesium.Cartesian3(NaN, 0, 1), 0.01);
|
||||
camera.rotate(new Cesium.Cartesian3(Infinity, 0, 1), 0.01);
|
||||
camera.rotate(Cesium.Cartesian3.UNIT_Z, NaN);
|
||||
camera.rotate(Cesium.Cartesian3.UNIT_Z, Infinity);
|
||||
assert.deepEqual(pose(camera), before);
|
||||
});
|
||||
|
||||
test('an ordinary rotation is exactly what Cesium does unguarded', () => {
|
||||
const axis = new Cesium.Cartesian3(0.2, -0.5, 0.84);
|
||||
const plain = cameraFixture();
|
||||
plain.rotate(axis, 0.37);
|
||||
plain.rotate(Cesium.Cartesian3.UNIT_Z);
|
||||
|
||||
const camera = cameraFixture();
|
||||
const restore = guardDegenerateRotation(camera);
|
||||
camera.rotate(axis, 0.37);
|
||||
// No angle: Cesium's default amount still applies.
|
||||
camera.defaultRotateAmount = plain.defaultRotateAmount;
|
||||
camera.rotate(Cesium.Cartesian3.UNIT_Z);
|
||||
assert.deepEqual(pose(camera), pose(plain));
|
||||
|
||||
restore();
|
||||
assert.equal(Object.hasOwn(camera, 'rotate'), false);
|
||||
assert.equal(camera.rotate, Cesium.Camera.prototype.rotate);
|
||||
});
|
||||
|
||||
/** Vector equality as Cesium has it: a recomputed right vector may hold -0. */
|
||||
function assertPose(camera, expected) {
|
||||
pose(camera).forEach((vector, i) =>
|
||||
assert.ok(Cesium.Cartesian3.equals(vector, expected[i]), `vector ${i}`),
|
||||
);
|
||||
}
|
||||
|
||||
function viewerFixture() {
|
||||
const postRender = new Cesium.Event();
|
||||
const panels = [];
|
||||
const resets = [];
|
||||
const camera = cameraFixture();
|
||||
const viewer = {
|
||||
camera,
|
||||
useDefaultRenderLoop: true,
|
||||
cesiumWidget: {
|
||||
showErrorPanel(title, message, error) {
|
||||
panels.push({ title, message, error });
|
||||
},
|
||||
},
|
||||
scene: {
|
||||
postRender,
|
||||
screenSpaceCameraController: {
|
||||
_aggregator: { reset: () => resets.push(true) },
|
||||
},
|
||||
},
|
||||
};
|
||||
// What Cesium's loop does when a frame throws.
|
||||
const fail = (error = new Error('frame failed')) => {
|
||||
viewer.useDefaultRenderLoop = false;
|
||||
viewer.cesiumWidget.showErrorPanel('Rendering has stopped.', undefined, error);
|
||||
};
|
||||
return { viewer, camera, postRender, panels, resets, fail };
|
||||
}
|
||||
|
||||
function quietly(run) {
|
||||
const original = console.error;
|
||||
const logged = [];
|
||||
console.error = (...args) => logged.push(args);
|
||||
try {
|
||||
run();
|
||||
} finally {
|
||||
console.error = original;
|
||||
}
|
||||
return logged;
|
||||
}
|
||||
|
||||
test('a stopped render loop restarts from the last drawn pose', () => {
|
||||
const { viewer, camera, postRender, panels, resets, fail } = viewerFixture();
|
||||
const requests = [];
|
||||
installRenderRecovery(viewer, {
|
||||
now: () => 0,
|
||||
requestRender: (reason) => requests.push(reason),
|
||||
});
|
||||
postRender.raiseEvent();
|
||||
const drawn = pose(camera);
|
||||
|
||||
// The failed frame left the camera unusable.
|
||||
camera.position.x = NaN;
|
||||
camera.direction.y = NaN;
|
||||
const error = new Error('normalized result is not a number');
|
||||
const logged = quietly(() => fail(error));
|
||||
|
||||
assert.equal(viewer.useDefaultRenderLoop, true);
|
||||
assert.deepEqual(panels, []);
|
||||
assertPose(camera, drawn);
|
||||
assert.deepEqual(resets, [true]);
|
||||
assert.deepEqual(requests, ['render-recovery']);
|
||||
assert.equal(logged.length, 1);
|
||||
assert.equal(logged[0][1], error);
|
||||
});
|
||||
|
||||
test('a frame drawn with a broken camera is not kept as the pose to return to', () => {
|
||||
const { viewer, camera, postRender, fail } = viewerFixture();
|
||||
installRenderRecovery(viewer, { now: () => 0, requestRender: () => {} });
|
||||
postRender.raiseEvent();
|
||||
const drawn = pose(camera);
|
||||
camera.position.x = NaN;
|
||||
postRender.raiseEvent();
|
||||
quietly(() => fail());
|
||||
assertPose(camera, drawn);
|
||||
});
|
||||
|
||||
test('an error that keeps returning is shown and rendering stays stopped', () => {
|
||||
const { viewer, panels, fail } = viewerFixture();
|
||||
let at = 0;
|
||||
installRenderRecovery(viewer, { now: () => at, requestRender: () => {} });
|
||||
quietly(() => {
|
||||
for (let i = 0; i < RECOVERY_LIMIT; i++) {
|
||||
at += 100;
|
||||
fail();
|
||||
assert.equal(viewer.useDefaultRenderLoop, true, `restart ${i + 1}`);
|
||||
}
|
||||
at += 100;
|
||||
fail();
|
||||
});
|
||||
assert.equal(viewer.useDefaultRenderLoop, false);
|
||||
assert.equal(panels.length, 1);
|
||||
assert.equal(panels[0].title, 'Rendering has stopped.');
|
||||
|
||||
// Once the window has passed, a later, unrelated error is recovered again.
|
||||
at += RECOVERY_WINDOW_MS + 1;
|
||||
quietly(() => fail());
|
||||
assert.equal(viewer.useDefaultRenderLoop, true);
|
||||
assert.equal(panels.length, 1);
|
||||
});
|
||||
|
||||
test('a panel shown while the loop is running is left alone', () => {
|
||||
const { viewer, panels, resets } = viewerFixture();
|
||||
installRenderRecovery(viewer, { now: () => 0, requestRender: () => {} });
|
||||
viewer.cesiumWidget.showErrorPanel('Error constructing CesiumWidget.', 'x');
|
||||
assert.equal(panels.length, 1);
|
||||
assert.deepEqual(resets, []);
|
||||
});
|
||||
|
||||
test('disposing restores the widget and stops recording poses', () => {
|
||||
const { viewer, postRender, panels, fail } = viewerFixture();
|
||||
const original = viewer.cesiumWidget.showErrorPanel;
|
||||
const dispose = installRenderRecovery(viewer, {
|
||||
now: () => 0,
|
||||
requestRender: () => {},
|
||||
});
|
||||
assert.equal(postRender.numberOfListeners, 1);
|
||||
dispose();
|
||||
dispose();
|
||||
assert.equal(postRender.numberOfListeners, 0);
|
||||
assert.equal(viewer.cesiumWidget.showErrorPanel, original);
|
||||
fail();
|
||||
assert.equal(viewer.useDefaultRenderLoop, false);
|
||||
assert.equal(panels.length, 1);
|
||||
});
|
||||
|
||||
test('an incomplete viewer is refused', () => {
|
||||
assert.throws(() => installRenderRecovery({}), TypeError);
|
||||
});
|
||||
@@ -1,9 +1,5 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { applyModelAtmosphereWorkaround } from './atmosphereCompat.js';
|
||||
import {
|
||||
guardDegenerateRotation,
|
||||
installRenderRecovery,
|
||||
} from './renderRecovery.js';
|
||||
import { StarrySky } from '../sky/starrySky.js';
|
||||
import {
|
||||
FREE_LOOK_MAX_HEIGHT_METERS,
|
||||
@@ -419,11 +415,6 @@ export function createApplicationViewer({ container, creditContainer }) {
|
||||
if (globalThis.matchMedia?.('(pointer: coarse)').matches)
|
||||
viewer.scene.screenSpaceCameraController.zoomFactor *= 3;
|
||||
limitZoomOut(viewer);
|
||||
// Cesium's wheel zoom can hand the camera a zero rotation axis and stop
|
||||
// the render loop for good; and whatever else stops it, restart it rather
|
||||
// than leave the globe dead until a reload. See app/renderRecovery.js.
|
||||
guardDegenerateRotation(viewer.camera);
|
||||
installRenderRecovery(viewer);
|
||||
// Before any tile builds a draw command: Cesium's per-vertex model
|
||||
// atmosphere fails to LINK on Apple's Metal backend and kills the
|
||||
// render loop. See app/atmosphereCompat.js.
|
||||
|
||||
@@ -144,33 +144,35 @@ export function createFrames({ state: layerState, services, parts, source }) {
|
||||
* the double-buffer canvas reference, throttled to PROJECTION_TEXTURE_SWAP_MS.
|
||||
*
|
||||
* @param {Object} record - Camera record with an initialized projection runtime.
|
||||
* @returns {boolean} Whether a new image was put on the plane.
|
||||
*/
|
||||
|
||||
function refreshProjectionTextures(record) {
|
||||
const runtime = record?.projection;
|
||||
if (!runtime || runtime.mode === 'video') return;
|
||||
if (!runtime || runtime.mode === 'video') return false;
|
||||
const now = Date.now();
|
||||
if (
|
||||
now - parts.model.safeNumber(runtime.lastTextureSwapAt, 0) <
|
||||
PROJECTION_TEXTURE_SWAP_MS
|
||||
)
|
||||
return;
|
||||
return false;
|
||||
|
||||
const planeShowing = !!(runtime.planeEntity?.show && runtime.planeMaterial);
|
||||
if (!planeShowing) return;
|
||||
if (!planeShowing) return false;
|
||||
|
||||
// Only swap when the canvas content actually changed since the last swap.
|
||||
// Frames land every ~10 s but this runs at 1 Hz — swapping an UNCHANGED
|
||||
// canvas re-uploads the texture for nothing, and each material image
|
||||
// reassignment is a flash opportunity on the live plane (owner field test
|
||||
// 2026-07-04: intermittent white flashes on the monitor plane).
|
||||
if (runtime.canvasStamp === runtime.lastSwappedCanvasStamp) return;
|
||||
if (runtime.canvasStamp === runtime.lastSwappedCanvasStamp) return false;
|
||||
|
||||
const buffer = paintNextProjectionBuffer(runtime);
|
||||
if (!buffer) return;
|
||||
if (!buffer) return false;
|
||||
runtime.lastTextureSwapAt = now;
|
||||
runtime.lastSwappedCanvasStamp = runtime.canvasStamp;
|
||||
runtime.planeMaterial.image = buffer;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -123,6 +123,11 @@ export const CCTV_CALIBRATION_STORAGE_KEY_V2 =
|
||||
|
||||
export const PROJECTION_TEXTURE_SWAP_MS = 1000;
|
||||
|
||||
// A still-image projection changes only when a new still is swapped onto the
|
||||
// plane, so the scene renders for this long after a swap (Cesium binds the new
|
||||
// image over the next frames) and is otherwise left idle.
|
||||
export const PROJECTION_SWAP_RENDER_MS = 500;
|
||||
|
||||
// V2 frustum geometry (design §2a/§6): the far-cap center + corners never sink
|
||||
// below groundAlt + this clearance, so a fabricated pitch (-24°) cannot bury
|
||||
// the monitor plane in the 3D tiles. Exported for the unit suite.
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
PLANE_OUTLINE_COLOR,
|
||||
PROJECTION_CANVAS_WIDTH,
|
||||
PROJECTION_CANVAS_HEIGHT,
|
||||
PROJECTION_SWAP_RENDER_MS,
|
||||
} from './policy.js';
|
||||
|
||||
export function createProjection({
|
||||
@@ -14,7 +15,15 @@ export function createProjection({
|
||||
parts,
|
||||
source,
|
||||
}) {
|
||||
const { holdContinuousRender, releaseContinuousRender } = services.render;
|
||||
const {
|
||||
holdContinuousRender,
|
||||
releaseContinuousRender,
|
||||
governorRequestRender = () => {},
|
||||
} = services.render;
|
||||
// Until this time (performance.now) the projection loop asks for a frame on
|
||||
// every tick: after a still is swapped onto the plane, and after an edge
|
||||
// that re-arms the loop (activation, projection shown).
|
||||
let renderUntil = 0;
|
||||
|
||||
/**
|
||||
* Build the protected label associated with one active monitor plane.
|
||||
@@ -352,12 +361,9 @@ export function createProjection({
|
||||
}
|
||||
|
||||
function startProjectionLoop() {
|
||||
renderUntil = performance.now() + PROJECTION_SWAP_RENDER_MS;
|
||||
if (layerState._projectionRaf) return;
|
||||
if (!parts.model.projectionLoopIsNeeded()) return;
|
||||
// The armed projection loop uploads video textures / runs focus fades per
|
||||
// frame — the scene must render continuously while it runs. Released when
|
||||
// the tick self-stops. (perf wave 2)
|
||||
holdContinuousRender('cctv-projection');
|
||||
|
||||
const tick = () => {
|
||||
if (!layerState._viewer || !parts.model.projectionLoopIsNeeded()) {
|
||||
@@ -366,15 +372,30 @@ export function createProjection({
|
||||
return;
|
||||
}
|
||||
|
||||
parts.rendering.refreshCctvFocusStyles(performance.now());
|
||||
const now = performance.now();
|
||||
const restyled = parts.rendering.refreshCctvFocusStyles(now);
|
||||
|
||||
const active = parts.selection.getActiveRecord();
|
||||
let video = false;
|
||||
if (layerState._enabled && layerState._showProjection && active) {
|
||||
ensureProjectionRuntime(active);
|
||||
if (active.projection && !active.projection.video) {
|
||||
parts.frames.drawProjectionFrame(active);
|
||||
parts.frames.refreshProjectionTextures(active);
|
||||
}
|
||||
if (parts.frames.refreshProjectionTextures(active))
|
||||
renderUntil = now + PROJECTION_SWAP_RENDER_MS;
|
||||
} else video = Boolean(active.projection?.video);
|
||||
}
|
||||
|
||||
// Only a video feed changes the scene on every frame, so only video
|
||||
// holds the continuous loop. A still lands about every ten seconds and
|
||||
// a focus fade restyles icons every 80 ms; each asks for the frames it
|
||||
// needs. Holding for stills rendered the scene 60 times a second for as
|
||||
// long as a camera was active. (perf wave 2 held for both.)
|
||||
if (video) holdContinuousRender('cctv-projection');
|
||||
else {
|
||||
releaseContinuousRender('cctv-projection');
|
||||
if (restyled || now < renderUntil)
|
||||
governorRequestRender('cctv-projection');
|
||||
}
|
||||
|
||||
layerState._projectionRaf = requestAnimationFrame(tick);
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createProjection } from './projection.js';
|
||||
import { createFrames } from './frames.js';
|
||||
import {
|
||||
PROJECTION_SWAP_RENDER_MS,
|
||||
PROJECTION_TEXTURE_SWAP_MS,
|
||||
} from './policy.js';
|
||||
|
||||
/** The projection loop on a hand-cranked clock, with the render governor's calls recorded. */
|
||||
function harness(t, { video = false, showProjection = true } = {}) {
|
||||
let clock = 1000;
|
||||
const frames = [];
|
||||
const original = {
|
||||
now: performance.now,
|
||||
request: globalThis.requestAnimationFrame,
|
||||
cancel: globalThis.cancelAnimationFrame,
|
||||
};
|
||||
performance.now = () => clock;
|
||||
globalThis.requestAnimationFrame = (step) => frames.push(step);
|
||||
globalThis.cancelAnimationFrame = () => frames.splice(0);
|
||||
t.after(() => {
|
||||
performance.now = original.now;
|
||||
globalThis.requestAnimationFrame = original.request;
|
||||
globalThis.cancelAnimationFrame = original.cancel;
|
||||
});
|
||||
|
||||
const holds = new Set();
|
||||
const calls = { hold: 0, requests: 0, draws: 0 };
|
||||
const record = {
|
||||
camera: { id: 'cam-1' },
|
||||
projection: { video: video ? {} : null, mode: video ? 'video' : 'image' },
|
||||
};
|
||||
const scene = { needed: true, swap: false, restyled: false };
|
||||
const state = {
|
||||
_viewer: {},
|
||||
_enabled: true,
|
||||
_showProjection: showProjection,
|
||||
_projectionRaf: 0,
|
||||
_projectionEntities: [],
|
||||
};
|
||||
const projection = createProjection({
|
||||
state,
|
||||
services: {
|
||||
render: {
|
||||
holdContinuousRender(owner) {
|
||||
calls.hold++;
|
||||
holds.add(owner);
|
||||
},
|
||||
releaseContinuousRender: (owner) => holds.delete(owner),
|
||||
governorRequestRender: () => calls.requests++,
|
||||
},
|
||||
},
|
||||
parts: {
|
||||
model: { projectionLoopIsNeeded: () => scene.needed },
|
||||
rendering: { refreshCctvFocusStyles: () => scene.restyled },
|
||||
selection: { getActiveRecord: () => record },
|
||||
frames: {
|
||||
drawProjectionFrame: () => calls.draws++,
|
||||
refreshProjectionTextures() {
|
||||
const swapped = scene.swap;
|
||||
scene.swap = false;
|
||||
return swapped;
|
||||
},
|
||||
},
|
||||
},
|
||||
source: {},
|
||||
});
|
||||
return {
|
||||
projection,
|
||||
state,
|
||||
record,
|
||||
scene,
|
||||
holds,
|
||||
calls,
|
||||
/** Run `count` animation frames 16 ms apart; returns the frames requested in them. */
|
||||
run(count = 1) {
|
||||
const before = calls.requests;
|
||||
for (let i = 0; i < count; i++) {
|
||||
clock += 16;
|
||||
frames.shift()?.();
|
||||
}
|
||||
return calls.requests - before;
|
||||
},
|
||||
advance(ms) {
|
||||
clock += ms;
|
||||
},
|
||||
get queued() {
|
||||
return frames.length;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test('a still-image camera never holds the continuous render loop', (t) => {
|
||||
const h = harness(t);
|
||||
h.projection.startProjectionLoop();
|
||||
h.run(600);
|
||||
assert.equal(h.calls.hold, 0);
|
||||
assert.equal(h.holds.size, 0);
|
||||
assert.equal(h.calls.draws, 600, 'the loop still polls for a new still');
|
||||
assert.equal(h.queued, 1, 'and stays armed');
|
||||
});
|
||||
|
||||
test('a new still on the plane is rendered, then the scene goes idle again', (t) => {
|
||||
const h = harness(t);
|
||||
h.projection.startProjectionLoop();
|
||||
// Arming the loop (activation, projection shown) renders its first frames.
|
||||
assert.ok(h.run(5) === 5);
|
||||
h.advance(PROJECTION_SWAP_RENDER_MS);
|
||||
assert.equal(h.run(120), 0, 'an unchanged still asks for no frame');
|
||||
|
||||
h.scene.swap = true;
|
||||
assert.equal(h.run(1), 1, 'the tick that swaps the image asks for a frame');
|
||||
const settling = Math.floor(PROJECTION_SWAP_RENDER_MS / 16) - 2;
|
||||
assert.equal(h.run(settling), settling, 'and so do the ticks while it binds');
|
||||
h.advance(PROJECTION_SWAP_RENDER_MS);
|
||||
assert.equal(h.run(120), 0);
|
||||
assert.equal(h.holds.size, 0);
|
||||
});
|
||||
|
||||
test('activating another camera while the loop runs renders its first frames', (t) => {
|
||||
const h = harness(t);
|
||||
h.projection.startProjectionLoop();
|
||||
h.advance(PROJECTION_SWAP_RENDER_MS * 2);
|
||||
assert.equal(h.run(10), 0);
|
||||
h.projection.startProjectionLoop();
|
||||
assert.equal(h.run(10), 10);
|
||||
assert.equal(h.queued, 1, 'still one loop');
|
||||
});
|
||||
|
||||
test('a video camera holds continuous render until it stops being video', (t) => {
|
||||
const h = harness(t, { video: true });
|
||||
h.projection.startProjectionLoop();
|
||||
h.run(3);
|
||||
assert.deepEqual([...h.holds], ['cctv-projection']);
|
||||
assert.equal(h.calls.draws, 0, 'Cesium updates the video texture itself');
|
||||
// The stream failed over to stills.
|
||||
h.record.projection.video = null;
|
||||
h.run(1);
|
||||
assert.equal(h.holds.size, 0);
|
||||
h.record.projection.video = {};
|
||||
h.run(1);
|
||||
assert.deepEqual([...h.holds], ['cctv-projection']);
|
||||
h.projection.stopProjectionLoop();
|
||||
assert.equal(h.holds.size, 0);
|
||||
});
|
||||
|
||||
test('a focus fade asks for a frame only on the ticks that restyle an icon', (t) => {
|
||||
const h = harness(t, { showProjection: false });
|
||||
h.projection.startProjectionLoop();
|
||||
h.advance(PROJECTION_SWAP_RENDER_MS * 2);
|
||||
assert.equal(h.run(10), 0);
|
||||
h.scene.restyled = true;
|
||||
assert.equal(h.run(4), 4);
|
||||
h.scene.restyled = false;
|
||||
assert.equal(h.run(10), 0);
|
||||
assert.equal(h.holds.size, 0);
|
||||
});
|
||||
|
||||
test('the loop releases and stops when it has no work', (t) => {
|
||||
const h = harness(t, { video: true });
|
||||
h.projection.startProjectionLoop();
|
||||
h.run(2);
|
||||
h.scene.needed = false;
|
||||
h.run(1);
|
||||
assert.equal(h.holds.size, 0);
|
||||
assert.equal(h.state._projectionRaf, 0);
|
||||
assert.equal(h.queued, 0);
|
||||
});
|
||||
|
||||
test('refreshProjectionTextures reports whether it put a new image on the plane', (t) => {
|
||||
const previousDocument = globalThis.document;
|
||||
globalThis.document = {
|
||||
createElement: () => ({
|
||||
getContext: () => ({ clearRect() {}, drawImage() {} }),
|
||||
}),
|
||||
};
|
||||
t.after(() => {
|
||||
if (previousDocument === undefined) delete globalThis.document;
|
||||
else globalThis.document = previousDocument;
|
||||
});
|
||||
const frames = createFrames({
|
||||
state: {},
|
||||
services: {},
|
||||
parts: { model: { safeNumber: (value, fallback) => value ?? fallback } },
|
||||
source: {},
|
||||
});
|
||||
const runtime = {
|
||||
mode: 'image',
|
||||
canvas: {},
|
||||
canvasStamp: 1,
|
||||
planeEntity: { show: true },
|
||||
planeMaterial: { image: null },
|
||||
};
|
||||
const record = { projection: runtime };
|
||||
assert.equal(frames.refreshProjectionTextures(record), true);
|
||||
const first = runtime.planeMaterial.image;
|
||||
assert.ok(first);
|
||||
// Unchanged canvas: nothing to render.
|
||||
runtime.lastTextureSwapAt -= PROJECTION_TEXTURE_SWAP_MS;
|
||||
assert.equal(frames.refreshProjectionTextures(record), false);
|
||||
assert.equal(runtime.planeMaterial.image, first);
|
||||
runtime.canvasStamp = 2;
|
||||
assert.equal(frames.refreshProjectionTextures(record), true);
|
||||
assert.notEqual(runtime.planeMaterial.image, first);
|
||||
runtime.planeEntity.show = false;
|
||||
runtime.canvasStamp = 3;
|
||||
runtime.lastTextureSwapAt -= PROJECTION_TEXTURE_SWAP_MS;
|
||||
assert.equal(frames.refreshProjectionTextures(record), false);
|
||||
assert.equal(frames.refreshProjectionTextures({ projection: { mode: 'video' } }), false);
|
||||
});
|
||||
@@ -61,8 +61,8 @@ export function createRendering({
|
||||
!layerState._viewer ||
|
||||
!focusPassIsNeeded(target, layerState._activeFocusStyleCount)
|
||||
)
|
||||
return;
|
||||
if (nowMs - layerState._lastFocusStyleAt < 80) return;
|
||||
return false;
|
||||
if (nowMs - layerState._lastFocusStyleAt < 80) return false;
|
||||
layerState._lastFocusStyleAt = nowMs;
|
||||
const scene = layerState._viewer.scene;
|
||||
const camera = layerState._viewer.camera;
|
||||
@@ -82,6 +82,8 @@ export function createRendering({
|
||||
baseColorFor: cameraColorFor,
|
||||
});
|
||||
layerState._activeFocusStyleCount = result.activeCount;
|
||||
// Whether an icon was restyled, so the caller can ask for a frame.
|
||||
return result.writes > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user