mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Application extras come from the renderer plugin; framing, occlusion and panel drawing are contract ports
A renderer plugin may supply applicationHooks(renderer) for what only its renderer has (map engine, own credits, stale commands, QA handles). What every renderer should do moves onto the contract: controls.keepEarthFramed (behind the keepEarthFramed capability), query.occlusionTest (behind occlusionTest), and panel drawing, which keepPanelRendering now does through resize and frame.renderNow through the contract. renderNow throws when the frame fails to draw. The Cesium port installs its zoom-out guard through keepEarthFramed (a setView no longer cancels its turn) and answers occlusionTest with the scene's probe; the stub answers it with a horizon test.
This commit is contained in:
1 parent
a755f63638
commit
619c19ea67
40 files changed
+737
-323
No files matched your search
@@ -9,16 +9,16 @@
|
||||
* and it has every member of the contract. The application's base (the
|
||||
* photoreal tiles, the map stack's imagery and terrain) is still loaded
|
||||
* through the Cesium map engine (`mapEngine.js`), which the plugin hands
|
||||
* the application with the renderer (`../applicationHooks.js`); the globe
|
||||
* the application with the renderer (its `applicationHooks`); the globe
|
||||
* port drapes its imagery on that base as drawn (see `globe.js`).
|
||||
* `whenSettled` waits for the globe, the scene's 3D tiles and what the draw
|
||||
* sets hold.
|
||||
*
|
||||
* What only this renderer needs at startup is set up here: the free-look
|
||||
* height limit, keyboard access to the credits' expand link (in the page's
|
||||
* language), and the hooks the application uses where a renderer has
|
||||
* them (stale command trimming, inline panel rendering, whether an entity
|
||||
* is the viewer's own, the zoom-out guard, the viewer QA scripts read).
|
||||
* language). The plugin's `applicationHooks` are what the application
|
||||
* uses where a renderer has them: the map engine, stale command trimming,
|
||||
* whether an entity is the viewer's own, and the viewer QA scripts read.
|
||||
* Adapter code reaches the viewer behind a renderer through
|
||||
* `cesiumViewerOf` (`viewers.js`), never through a renderer member, so no
|
||||
* engine type crosses the contract.
|
||||
@@ -26,16 +26,12 @@
|
||||
import { createApplicationViewer } from './applicationViewer.js';
|
||||
import { CONTRACT_VERSION } from '../contract/index.js';
|
||||
import { bindRenderer } from '../rendererOf.js';
|
||||
import { provideApplicationHooks } from '../applicationHooks.js';
|
||||
import { FREE_LOOK_MAX_HEIGHT_METERS } from '../../globeInView.js';
|
||||
import { createCesiumInput } from './input.js';
|
||||
import { cesiumCapabilities, createCesiumPorts } from './ports.js';
|
||||
import { cesiumMapEngine } from './mapEngine.js';
|
||||
import { installFreeLookHeightLimit } from './freeLookLimit.js';
|
||||
import { keepPanelRendering } from './panelRendering.js';
|
||||
import { trimStaleSceneCommands } from './sceneCommandRelease.js';
|
||||
import { installZoomOutGuard } from './zoomOutGuard.js';
|
||||
import { createViewerOcclusionProbe } from './sceneOcclusionProbe.js';
|
||||
import { configureCreditKeyboardAccess } from './creditKeyboard.js';
|
||||
import { onLocaleChange } from '../../i18n/index.js';
|
||||
import { cesiumViewerOf, recordViewer } from './viewers.js';
|
||||
@@ -168,29 +164,27 @@ export function createCesiumRendererPlugin({
|
||||
});
|
||||
recordViewer(renderer, viewer);
|
||||
unbind = bindRenderer(viewer, renderer);
|
||||
provideApplicationHooks(renderer, {
|
||||
return renderer;
|
||||
},
|
||||
/**
|
||||
* @param {TransitionalRenderer} renderer
|
||||
* @returns {import('../contract/renderer').ApplicationHooks}
|
||||
*/
|
||||
applicationHooks(renderer) {
|
||||
const viewer = cesiumViewerOf(renderer);
|
||||
return {
|
||||
mapEngine: cesiumMapEngine,
|
||||
// Cesium's credit display (#cesium-credits) shows the credits.
|
||||
ownCreditDisplay: true,
|
||||
// Drops the draw commands the scene keeps after it stops drawing
|
||||
// them; run after a drawn frame (see sceneCommandRelease.js).
|
||||
trimStaleCommands: () => trimStaleSceneCommands(viewer.scene),
|
||||
// Keeps the globe drawing in an inline panel whose host stops
|
||||
// animation frames (embed mode, src/app/embed.js).
|
||||
keepPanelRendering: (options) => keepPanelRendering(viewer, options),
|
||||
// Whether an entity is one of the viewer's own (Cockpit asks whether
|
||||
// the aircraft it flies still draws).
|
||||
viewerDrawsEntity: (entity) => viewer.entities.contains(entity),
|
||||
// Keeps outward user zooms bounded and the Earth framed; returns its
|
||||
// disposer (see zoomOutGuard.js).
|
||||
installZoomOutGuard: (options) => installZoomOutGuard(viewer, options),
|
||||
// Tests depth-tested contacts (street vehicles) against the frame's
|
||||
// own depth, for the detection overlay (see sceneOcclusionProbe.js).
|
||||
createOcclusionProbe: () => createViewerOcclusionProbe(viewer),
|
||||
// QA scripts drive and read the viewer itself.
|
||||
qaHandles: { viewer },
|
||||
});
|
||||
return renderer;
|
||||
};
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ import test from 'node:test';
|
||||
import * as Cesium from 'cesium';
|
||||
import { CONTRACT_VERSION, checkRendererShape } from '../contract/index.js';
|
||||
import { rendererOf } from '../rendererOf.js';
|
||||
import { applicationHooksOf } from '../applicationHooks.js';
|
||||
import { cesiumMapEngine } from './mapEngine.js';
|
||||
import {
|
||||
ReadonlySet,
|
||||
@@ -142,17 +141,13 @@ test('the plugin wraps the viewer it is given, behind the accessor only', async
|
||||
false,
|
||||
'free look is limited to low heights from the start',
|
||||
);
|
||||
const hooks = applicationHooksOf(renderer);
|
||||
const hooks = plugin.applicationHooks(renderer);
|
||||
assert.equal(hooks.mapEngine, cesiumMapEngine, 'the map stacks use Cesium');
|
||||
assert.equal(hooks.ownCreditDisplay, true, 'Cesium shows its own credits');
|
||||
assert.equal(hooks.qaHandles.viewer, viewer, 'QA scripts read the viewer');
|
||||
assert.equal(hooks.viewerDrawsEntity({ own: true }), true);
|
||||
assert.equal(hooks.viewerDrawsEntity({}), false);
|
||||
for (const name of [
|
||||
'trimStaleCommands',
|
||||
'keepPanelRendering',
|
||||
'installZoomOutGuard',
|
||||
])
|
||||
assert.equal(typeof hooks[name], 'function', name);
|
||||
assert.equal(typeof hooks.trimStaleCommands, 'function');
|
||||
|
||||
assert.ok(viewer.listeners.size > 0, 'the ports follow the frame phases');
|
||||
assert.equal(typeof renderer.draw.icons, 'function');
|
||||
|
||||
@@ -21,7 +21,10 @@ test("zooming out stops with the globe 30% of the screen's height across", () =>
|
||||
|
||||
test('a wide screen, whose view is narrower, may zoom out farther', () => {
|
||||
assert.ok(zoomOutLimit(0.4) > zoomOutLimit(Math.PI / 3));
|
||||
assert.ok(zoomOutLimit(Math.PI / 3) > 20_000_000, 'past the views a link opens');
|
||||
assert.ok(
|
||||
zoomOutLimit(Math.PI / 3) > 20_000_000,
|
||||
'past the views a link opens',
|
||||
);
|
||||
});
|
||||
|
||||
function fakeViewer({ fovy = Math.PI / 3, height = 10_000_000 } = {}) {
|
||||
|
||||
@@ -117,12 +117,16 @@ export function createCesiumCamera({
|
||||
})
|
||||
: null;
|
||||
|
||||
const controlsPart = createCesiumControls({ scene, widget });
|
||||
|
||||
// Camera commands: hooks run before each one, so an owner can yield.
|
||||
const commandHooks = listenerList(report);
|
||||
const command = (kind) => commandHooks.fire({ kind });
|
||||
|
||||
const controlsPart = createCesiumControls({
|
||||
scene,
|
||||
widget,
|
||||
onCommand: (cb) => commandHooks.on(cb),
|
||||
});
|
||||
|
||||
// The flight this port started, while it is in progress. Cesium cancels
|
||||
// a flight in progress when another starts, including one started on
|
||||
// its camera directly, and runs the callbacks inside the call or frame
|
||||
|
||||
@@ -26,11 +26,16 @@
|
||||
* contract has no name for is kept as it is when they are set. While
|
||||
* they hold `ctrlWheel`, trackpad pinches (small pixel-mode Ctrl+wheel
|
||||
* events) are relayed at a usable speed (`ctrlWheelZoom.js`).
|
||||
* - `keepEarthFramed` installs the zoom-out guard (`zoomOutGuard.js`) on
|
||||
* the widget, which reads the controller's own input; a camera command
|
||||
* (`onCommand`) ends its easing. Without a widget whose scene has a
|
||||
* controller, it only reads back.
|
||||
*/
|
||||
import * as Cesium from 'cesium';
|
||||
import { acquireCameraInput } from './cameraInput.js';
|
||||
import { installTrackpadPinchRelay } from './ctrlWheelZoom.js';
|
||||
import { followZoomBase, setFollowZoomBase } from './follow.js';
|
||||
import { installZoomOutGuard } from './zoomOutGuard.js';
|
||||
|
||||
const HALF_PI = Math.PI / 2;
|
||||
const { WHEEL, PINCH, RIGHT_DRAG } = Cesium.CameraEventType;
|
||||
@@ -88,9 +93,12 @@ export const controllerDefaults = () => ({
|
||||
|
||||
/**
|
||||
* @param {{scene: {screenSpaceCameraController?: object}, widget?: object,
|
||||
* pinchRelay?: typeof installTrackpadPinchRelay}} options `widget`, the
|
||||
* Viewer or CesiumWidget, takes the trackpad pinch relay; without one
|
||||
* `ctrlWheel` binds Ctrl+wheel alone.
|
||||
* pinchRelay?: typeof installTrackpadPinchRelay,
|
||||
* onCommand?: (cb: () => void) => () => void,
|
||||
* zoomOutGuard?: typeof installZoomOutGuard}} options `widget`, the
|
||||
* Viewer or CesiumWidget, takes the trackpad pinch relay and the zoom-out
|
||||
* guard; without one `ctrlWheel` binds Ctrl+wheel alone. `onCommand`
|
||||
* subscribes to the camera port's commands.
|
||||
* @returns {{controls: import('../contract/camera').CameraControls,
|
||||
* dispose: () => void}}
|
||||
*/
|
||||
@@ -98,6 +106,8 @@ export function createCesiumControls({
|
||||
scene,
|
||||
widget,
|
||||
pinchRelay = installTrackpadPinchRelay,
|
||||
onCommand,
|
||||
zoomOutGuard = installZoomOutGuard,
|
||||
}) {
|
||||
const ownController = scene.screenSpaceCameraController;
|
||||
const controller = ownController ?? controllerDefaults();
|
||||
@@ -125,6 +135,13 @@ export function createCesiumControls({
|
||||
// The pitch range as last set, read back while the controller still has
|
||||
// the tilt limit it was given.
|
||||
let pitchSet = null;
|
||||
// `keepEarthFramed`, and the guard's disposer while it is installed.
|
||||
let framed = false;
|
||||
let releaseGuard = null;
|
||||
const guardable = () =>
|
||||
Boolean(
|
||||
ownController && widget?.camera && widget?.canvas && scene.preRender,
|
||||
);
|
||||
|
||||
const controls = {
|
||||
// Read without taking over `enableInputs`: once any user holds input,
|
||||
@@ -249,6 +266,27 @@ export function createCesiumControls({
|
||||
];
|
||||
relayPinches(value.includes('ctrlWheel'));
|
||||
},
|
||||
get keepEarthFramed() {
|
||||
return framed;
|
||||
},
|
||||
set keepEarthFramed(value) {
|
||||
framed = Boolean(value);
|
||||
if (framed && !releaseGuard && !disposed && guardable())
|
||||
releaseGuard = zoomOutGuard(widget, {
|
||||
requestRender: () => scene.requestRender?.(),
|
||||
// A command that places the eye ends the guard's easing; the
|
||||
// step views a held key sends every frame do not (the guard
|
||||
// lets go of a pose it did not make on its own).
|
||||
subscribeCameraHandoff: (listener) =>
|
||||
onCommand?.((command) => {
|
||||
if (command?.kind !== 'setView') listener(command);
|
||||
}),
|
||||
});
|
||||
else if (!framed && releaseGuard) {
|
||||
releaseGuard();
|
||||
releaseGuard = null;
|
||||
}
|
||||
},
|
||||
};
|
||||
// A controller that already binds Ctrl+wheel relays pinches from the start.
|
||||
relayPinches(controls.zoomInputs.includes('ctrlWheel'));
|
||||
@@ -257,6 +295,8 @@ export function createCesiumControls({
|
||||
controls,
|
||||
dispose() {
|
||||
relayPinches(false);
|
||||
releaseGuard?.();
|
||||
releaseGuard = null;
|
||||
disposed = true;
|
||||
cameraInput?.release();
|
||||
cameraInput = null;
|
||||
|
||||
@@ -225,3 +225,46 @@ test('a scene without a controller still has settings', () => {
|
||||
assert.equal(controls.zoomFactor, 9);
|
||||
assert.deepEqual(controls.zoomInputs, ['rightDrag', 'wheel', 'pinch']);
|
||||
});
|
||||
|
||||
test('keepEarthFramed installs the zoom-out guard, which placing commands cancel', () => {
|
||||
const scene = { ...sceneWithController(), preRender: {}, requestRender() {} };
|
||||
const widget = { scene, camera: {}, canvas: {} };
|
||||
const commands = new Set();
|
||||
const installed = [];
|
||||
const { controls, dispose } = createCesiumControls({
|
||||
scene,
|
||||
widget,
|
||||
onCommand: (cb) => (commands.add(cb), () => commands.delete(cb)),
|
||||
zoomOutGuard(viewer, { subscribeCameraHandoff }) {
|
||||
const guard = { viewer, cancels: 0, released: false };
|
||||
const off = subscribeCameraHandoff(() => (guard.cancels += 1));
|
||||
installed.push(guard);
|
||||
return () => {
|
||||
guard.released = true;
|
||||
off();
|
||||
};
|
||||
},
|
||||
});
|
||||
assert.equal(controls.keepEarthFramed, false);
|
||||
controls.keepEarthFramed = true;
|
||||
controls.keepEarthFramed = true;
|
||||
assert.equal(installed.length, 1, 'installed once');
|
||||
assert.equal(installed[0].viewer, widget);
|
||||
// A held key's step views do not cancel it; a flight does.
|
||||
for (const cb of commands) cb({ kind: 'setView' });
|
||||
assert.equal(installed[0].cancels, 0);
|
||||
for (const cb of commands) cb({ kind: 'flyTo' });
|
||||
assert.equal(installed[0].cancels, 1);
|
||||
controls.keepEarthFramed = false;
|
||||
assert.equal(installed[0].released, true);
|
||||
assert.equal(commands.size, 0);
|
||||
controls.keepEarthFramed = true;
|
||||
dispose();
|
||||
assert.equal(installed[1].released, true, 'released on dispose');
|
||||
});
|
||||
|
||||
test('keepEarthFramed only reads back without a controller to guard', () => {
|
||||
const { controls } = createCesiumControls({ scene: {} });
|
||||
controls.keepEarthFramed = true;
|
||||
assert.equal(controls.keepEarthFramed, true);
|
||||
});
|
||||
@@ -10,8 +10,12 @@ function fakeElement() {
|
||||
dataset: {},
|
||||
focused: false,
|
||||
id: '',
|
||||
setAttribute(name, value) { attributes.set(name, String(value)); },
|
||||
getAttribute(name) { return attributes.get(name) ?? null; },
|
||||
setAttribute(name, value) {
|
||||
attributes.set(name, String(value));
|
||||
},
|
||||
getAttribute(name) {
|
||||
return attributes.get(name) ?? null;
|
||||
},
|
||||
addEventListener(type, listener) {
|
||||
if (!listeners.has(type)) listeners.set(type, []);
|
||||
listeners.get(type).push(listener);
|
||||
@@ -22,8 +26,12 @@ function fakeElement() {
|
||||
target: this,
|
||||
defaultPrevented: false,
|
||||
propagationStopped: false,
|
||||
preventDefault() { this.defaultPrevented = true; },
|
||||
stopPropagation() { this.propagationStopped = true; },
|
||||
preventDefault() {
|
||||
this.defaultPrevented = true;
|
||||
},
|
||||
stopPropagation() {
|
||||
this.propagationStopped = true;
|
||||
},
|
||||
...event,
|
||||
};
|
||||
for (const listener of listeners.get(type) || []) listener(dispatched);
|
||||
@@ -33,8 +41,12 @@ function fakeElement() {
|
||||
this.onclick?.();
|
||||
this.dispatch('click');
|
||||
},
|
||||
focus() { this.focused = true; },
|
||||
listenerCount(type) { return listeners.get(type)?.length || 0; },
|
||||
focus() {
|
||||
this.focused = true;
|
||||
},
|
||||
listenerCount(type) {
|
||||
return listeners.get(type)?.length || 0;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -48,7 +60,8 @@ function fixture() {
|
||||
lightbox.parentElement = overlay;
|
||||
const root = {
|
||||
querySelector(selector) {
|
||||
if (selector === '#cesium-credits .cesium-credit-expand-link') return expand;
|
||||
if (selector === '#cesium-credits .cesium-credit-expand-link')
|
||||
return expand;
|
||||
if (selector === '.cesium-credit-lightbox') return lightbox;
|
||||
return null;
|
||||
},
|
||||
@@ -72,8 +85,12 @@ test('Enter opens and closes attribution on initial keydown', () => {
|
||||
const f = fixture();
|
||||
let opened = 0;
|
||||
let closed = 0;
|
||||
f.expand.onclick = () => { opened++; };
|
||||
f.close.onclick = () => { closed++; };
|
||||
f.expand.onclick = () => {
|
||||
opened++;
|
||||
};
|
||||
f.close.onclick = () => {
|
||||
closed++;
|
||||
};
|
||||
configureCreditKeyboardAccess(f.root);
|
||||
const openEvent = f.expand.dispatch('keydown', { key: 'Enter' });
|
||||
assert.equal(openEvent.defaultPrevented, true);
|
||||
@@ -91,8 +108,12 @@ test('Space opens and closes attribution only on key release', () => {
|
||||
const f = fixture();
|
||||
let opened = 0;
|
||||
let closed = 0;
|
||||
f.expand.onclick = () => { opened++; };
|
||||
f.close.onclick = () => { closed++; };
|
||||
f.expand.onclick = () => {
|
||||
opened++;
|
||||
};
|
||||
f.close.onclick = () => {
|
||||
closed++;
|
||||
};
|
||||
configureCreditKeyboardAccess(f.root);
|
||||
|
||||
const openDown = f.expand.dispatch('keydown', { key: ' ' });
|
||||
@@ -118,7 +139,9 @@ test('Space opens and closes attribution only on key release', () => {
|
||||
test('held activation keys do not repeat open or close actions', () => {
|
||||
const f = fixture();
|
||||
let opened = 0;
|
||||
f.expand.onclick = () => { opened++; };
|
||||
f.expand.onclick = () => {
|
||||
opened++;
|
||||
};
|
||||
configureCreditKeyboardAccess(f.root);
|
||||
|
||||
f.expand.dispatch('keydown', { key: ' ' });
|
||||
@@ -132,7 +155,9 @@ test('held activation keys do not repeat open or close actions', () => {
|
||||
|
||||
const g = fixture();
|
||||
let enterOpened = 0;
|
||||
g.expand.onclick = () => { enterOpened++; };
|
||||
g.expand.onclick = () => {
|
||||
enterOpened++;
|
||||
};
|
||||
configureCreditKeyboardAccess(g.root);
|
||||
g.expand.dispatch('keydown', { key: 'Enter' });
|
||||
g.expand.dispatch('keydown', { key: 'Enter', repeat: true });
|
||||
@@ -142,7 +167,9 @@ test('held activation keys do not repeat open or close actions', () => {
|
||||
test('losing focus during a Space hold cancels release activation', () => {
|
||||
const f = fixture();
|
||||
let opened = 0;
|
||||
f.expand.onclick = () => { opened++; };
|
||||
f.expand.onclick = () => {
|
||||
opened++;
|
||||
};
|
||||
configureCreditKeyboardAccess(f.root);
|
||||
|
||||
f.expand.dispatch('keydown', { key: ' ' });
|
||||
@@ -156,10 +183,15 @@ test('losing focus during a Space hold cancels release activation', () => {
|
||||
test('Escape and backdrop dismissal close attribution and restore its disclosure', () => {
|
||||
const f = fixture();
|
||||
let closed = 0;
|
||||
f.close.onclick = () => { closed++; };
|
||||
f.close.onclick = () => {
|
||||
closed++;
|
||||
};
|
||||
configureCreditKeyboardAccess(f.root);
|
||||
f.expand.click();
|
||||
const escape = f.lightbox.dispatch('keydown', { key: 'Escape', target: f.close });
|
||||
const escape = f.lightbox.dispatch('keydown', {
|
||||
key: 'Escape',
|
||||
target: f.close,
|
||||
});
|
||||
assert.equal(escape.defaultPrevented, true);
|
||||
assert.equal(escape.propagationStopped, true);
|
||||
assert.equal(closed, 1);
|
||||
@@ -185,7 +217,10 @@ test('credit keyboard setup is idempotent and safely declines incomplete markup'
|
||||
assert.equal(f.close.listenerCount('blur'), 1);
|
||||
assert.equal(f.lightbox.listenerCount('keydown'), 1);
|
||||
assert.equal(f.overlay.listenerCount('click'), 1);
|
||||
assert.equal(configureCreditKeyboardAccess({ querySelector: () => null }), false);
|
||||
assert.equal(
|
||||
configureCreditKeyboardAccess({ querySelector: () => null }),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
function textFixture() {
|
||||
|
||||
@@ -102,6 +102,26 @@ export function willDraw(scene, time) {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The contract's `renderNow` on `scene`: draw a frame at `time` even when
|
||||
* nothing asked for one, and throw the error Cesium caught and raised on
|
||||
* `renderError` when the frame failed.
|
||||
* @param {object} scene
|
||||
* @param {object} [time] A Cesium JulianDate.
|
||||
*/
|
||||
export function renderNow(scene, time) {
|
||||
let failure = null;
|
||||
const off = scene.renderError?.addEventListener((_scene, error) => {
|
||||
failure ??= { error };
|
||||
});
|
||||
try {
|
||||
scene.forceRender(time);
|
||||
} finally {
|
||||
off?.();
|
||||
}
|
||||
if (failure) throw failure.error;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} viewer A Cesium `Viewer` or `CesiumWidget`.
|
||||
* @returns {{port: import('../contract/frame').FramePort,
|
||||
@@ -239,7 +259,7 @@ export function createSceneFrame(viewer) {
|
||||
viewer.useDefaultRenderLoop = suspended ? false : ownLoop;
|
||||
if (!suspended && !gone()) scene.requestRender();
|
||||
},
|
||||
renderNow: () => scene.forceRender(viewer.clock?.currentTime),
|
||||
renderNow: () => renderNow(scene, viewer.clock?.currentTime),
|
||||
renderOnce({ timeoutMs } = {}) {
|
||||
if (gone()) return Promise.resolve();
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import test from 'node:test';
|
||||
import { bindRenderer, findFrame, frameOf } from '../rendererOf.js';
|
||||
import './frame.js';
|
||||
import { renderNow } from './frame.js';
|
||||
|
||||
/** A viewer whose scene raises its phases when `tick` is called. */
|
||||
function fakeViewer() {
|
||||
@@ -189,3 +189,21 @@ test('a built frame port counts holds per reason, times out a wait, and settles
|
||||
await waiting;
|
||||
unbind();
|
||||
});
|
||||
|
||||
test('renderNow throws the error Cesium raises on renderError', () => {
|
||||
const listeners = new Set();
|
||||
const failure = new Error('worker failed');
|
||||
let fail = false;
|
||||
const scene = {
|
||||
renderError: {
|
||||
addEventListener: (cb) => (listeners.add(cb), () => listeners.delete(cb)),
|
||||
},
|
||||
forceRender() {
|
||||
if (fail) for (const cb of listeners) cb(scene, failure);
|
||||
},
|
||||
};
|
||||
renderNow(scene);
|
||||
fail = true;
|
||||
assert.throws(() => renderNow(scene), failure);
|
||||
assert.equal(listeners.size, 0, 'it stops listening either way');
|
||||
});
|
||||
@@ -1,69 +0,0 @@
|
||||
/**
|
||||
* @module render/cesium/panelRendering
|
||||
* @description Keeping the Cesium viewer drawing in an inline panel whose
|
||||
* host stops animation frames (`src/app/embed.js`, embed mode). The
|
||||
* application scene hands it to the embed views (`src/app/scene.js`).
|
||||
*/
|
||||
|
||||
// A panel keeps drawing at about this rate when its host stops animation
|
||||
// frames; see keepPanelRendering.
|
||||
const PANEL_FRAME_MS = 33;
|
||||
const MISSED_FRAMES_MS = 250;
|
||||
|
||||
/** A render error's details, including the plain objects workers report. */
|
||||
function describeError(error) {
|
||||
if (error instanceof Error)
|
||||
return `${error.name}: ${error.message}\n${error.stack}`;
|
||||
try {
|
||||
return JSON.stringify(error, Object.getOwnPropertyNames(error ?? {}));
|
||||
} catch {
|
||||
return String(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep the globe drawing in an inline panel. Some hosts report a panel on
|
||||
* screen as hidden, which stops the browser's animation frames and with them
|
||||
* Cesium's render loop; while frames stop arriving, draw from a timer.
|
||||
* Returns a function that stops it.
|
||||
*/
|
||||
export function keepPanelRendering(
|
||||
viewer,
|
||||
{ windowRef = globalThis.window, now = () => performance.now() } = {},
|
||||
) {
|
||||
let lastFrame = now();
|
||||
let frameRequest = null;
|
||||
const onFrame = () => {
|
||||
lastFrame = now();
|
||||
frameRequest = windowRef.requestAnimationFrame(onFrame);
|
||||
};
|
||||
frameRequest = windowRef.requestAnimationFrame(onFrame);
|
||||
// Cesium stops drawing after a render error, and so does the timer.
|
||||
// Report the error in full: workers report plain objects, which Cesium
|
||||
// prints as [object Object].
|
||||
let failed = false;
|
||||
const removeErrorListener = viewer.scene?.renderError?.addEventListener(
|
||||
(_scene, error) => {
|
||||
failed = true;
|
||||
console.error(
|
||||
"[God's Eye View panel] render error:",
|
||||
describeError(error),
|
||||
);
|
||||
},
|
||||
);
|
||||
const timer = windowRef.setInterval(() => {
|
||||
if (
|
||||
failed ||
|
||||
now() - lastFrame < MISSED_FRAMES_MS ||
|
||||
viewer.isDestroyed?.()
|
||||
)
|
||||
return;
|
||||
viewer.resize();
|
||||
viewer.render();
|
||||
}, PANEL_FRAME_MS);
|
||||
return () => {
|
||||
windowRef.clearInterval(timer);
|
||||
windowRef.cancelAnimationFrame(frameRequest);
|
||||
removeErrorListener?.();
|
||||
};
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { keepPanelRendering } from './panelRendering.js';
|
||||
|
||||
test('a panel keeps drawing from a timer while animation frames stop', () => {
|
||||
let clock = 0;
|
||||
let tick = null;
|
||||
let frame = null;
|
||||
const windowRef = {
|
||||
requestAnimationFrame: (fn) => ((frame = fn), 1),
|
||||
cancelAnimationFrame: () => (frame = null),
|
||||
setInterval: (fn) => ((tick = fn), 1),
|
||||
clearInterval: () => (tick = null),
|
||||
};
|
||||
const drawn = [];
|
||||
const viewer = {
|
||||
resize: () => drawn.push('resize'),
|
||||
render: () => drawn.push('render'),
|
||||
};
|
||||
const stop = keepPanelRendering(viewer, { windowRef, now: () => clock });
|
||||
// Frames arrive: the render loop draws, so the timer does nothing.
|
||||
clock = 100;
|
||||
frame();
|
||||
tick();
|
||||
assert.deepEqual(drawn, []);
|
||||
// Frames stop, as in a panel its host reports hidden: the timer draws.
|
||||
clock = 400;
|
||||
tick();
|
||||
assert.deepEqual(drawn, ['resize', 'render']);
|
||||
stop();
|
||||
assert.equal(tick, null);
|
||||
assert.equal(frame, null);
|
||||
});
|
||||
|
||||
test('a panel reports a render error in full and stops drawing', () => {
|
||||
let listener = null;
|
||||
let tick = null;
|
||||
const windowRef = {
|
||||
requestAnimationFrame: () => 1,
|
||||
cancelAnimationFrame() {},
|
||||
setInterval: (fn) => ((tick = fn), 1),
|
||||
clearInterval() {},
|
||||
};
|
||||
const drawn = [];
|
||||
const viewer = {
|
||||
resize() {},
|
||||
render: () => drawn.push('render'),
|
||||
scene: {
|
||||
renderError: {
|
||||
addEventListener: (fn) => ((listener = fn), () => (listener = null)),
|
||||
},
|
||||
},
|
||||
};
|
||||
const logged = [];
|
||||
const original = console.error;
|
||||
console.error = (...args) => logged.push(args.join(' '));
|
||||
try {
|
||||
let clock = 0;
|
||||
const stop = keepPanelRendering(viewer, { windowRef, now: () => clock });
|
||||
listener(viewer.scene, { name: 'RuntimeError', message: 'worker failed' });
|
||||
assert.match(logged[0], /render error: .*"message":"worker failed"/);
|
||||
clock = 1000;
|
||||
tick();
|
||||
assert.deepEqual(drawn, []);
|
||||
stop();
|
||||
assert.equal(listener, null);
|
||||
} finally {
|
||||
console.error = original;
|
||||
}
|
||||
});
|
||||
@@ -19,7 +19,7 @@ import { createCesiumCredits } from './credits.js';
|
||||
import { createCesiumDraw } from './draw.js';
|
||||
import { GPU_ICON_CAPABILITIES } from './gpuIcons.js';
|
||||
import { createCesiumEffects } from './effects.js';
|
||||
import { willDraw } from './frame.js';
|
||||
import { renderNow, willDraw } from './frame.js';
|
||||
import { createCesiumGlobe } from './globe.js';
|
||||
import { createCesiumQuery, queryCapabilities } from './query.js';
|
||||
import { createCesiumQuality } from './quality.js';
|
||||
@@ -70,6 +70,10 @@ export function cesiumCapabilities(scene, only) {
|
||||
'fields',
|
||||
'followFrame',
|
||||
'cameraChangeThreshold',
|
||||
// The zoom-out guard reads the scene's own controller and frame events.
|
||||
...(scene?.screenSpaceCameraController && scene?.preRender
|
||||
? ['keepEarthFramed']
|
||||
: []),
|
||||
...GPU_ICON_CAPABILITIES,
|
||||
'styles',
|
||||
'bloom',
|
||||
@@ -294,8 +298,8 @@ export function createCesiumPorts(widget, { capabilities, credentials = {} }) {
|
||||
widget.useDefaultRenderLoop = !suspended;
|
||||
if (!suspended) scene.requestRender();
|
||||
},
|
||||
// Draws even when nothing asked for a frame.
|
||||
renderNow: () => scene.forceRender(widget.clock.currentTime),
|
||||
// Draws even when nothing asked for a frame; throws when it fails.
|
||||
renderNow: () => renderNow(scene, widget.clock.currentTime),
|
||||
renderOnce: (options) => nextPostRender(options),
|
||||
setTargetFps(fps) {
|
||||
targetFps = fps > 0 ? fps : 0;
|
||||
|
||||
@@ -41,6 +41,7 @@ test('capabilities follow what the scene supports', () => {
|
||||
...draw,
|
||||
...effects,
|
||||
'pickPosition',
|
||||
'occlusionTest',
|
||||
]);
|
||||
assert.deepEqual(
|
||||
cesiumCapabilities({
|
||||
@@ -53,6 +54,7 @@ test('capabilities follow what the scene supports', () => {
|
||||
...effects,
|
||||
'globeVisibility',
|
||||
'pickPosition',
|
||||
'occlusionTest',
|
||||
'sampleHeight',
|
||||
'finestHeights',
|
||||
'rayCast',
|
||||
@@ -67,6 +69,14 @@ test('a caller can ask for fewer capabilities, never more', () => {
|
||||
'rayCast',
|
||||
]);
|
||||
assert.deepEqual(cesiumCapabilities({}, new Set(['sampleHeight'])), []);
|
||||
// The zoom-out guard needs the scene's own controller and frame events.
|
||||
assert.ok(
|
||||
cesiumCapabilities({
|
||||
screenSpaceCameraController: {},
|
||||
preRender: {},
|
||||
}).includes('keepEarthFramed'),
|
||||
);
|
||||
assert.ok(!cesiumCapabilities({ preRender: {} }).includes('keepEarthFramed'));
|
||||
});
|
||||
|
||||
test('a listener list keeps the listener rules', () => {
|
||||
|
||||
@@ -18,6 +18,7 @@ import { toCartesian } from './common.js';
|
||||
import { unboundDrawPart } from './draw.js';
|
||||
import { drawHitOf } from './drawHit.js';
|
||||
import { rememberPick } from './pickMemo.js';
|
||||
import { createSceneOcclusionTest } from './sceneOcclusionProbe.js';
|
||||
|
||||
/** The fewest things a bounded drill looks through (`QueryPort.drillPick`). */
|
||||
const DRILL_DEPTH = 16;
|
||||
@@ -31,7 +32,8 @@ const DRILL_DEPTH = 16;
|
||||
*/
|
||||
export function queryCapabilities(scene) {
|
||||
const found = [];
|
||||
if (scene.pickPositionSupported) found.push('pickPosition');
|
||||
// The occlusion test reads the same depth a depth pick does.
|
||||
if (scene.pickPositionSupported) found.push('pickPosition', 'occlusionTest');
|
||||
if (scene.sampleHeightSupported)
|
||||
found.push('sampleHeight', 'finestHeights', 'rayCast');
|
||||
return found;
|
||||
@@ -460,6 +462,9 @@ export function createCesiumQuery({
|
||||
: null;
|
||||
},
|
||||
};
|
||||
// Only where the scene keeps the depth it tests against.
|
||||
if (can('occlusionTest'))
|
||||
query.occlusionTest = () => createSceneOcclusionTest(scene);
|
||||
return query;
|
||||
}
|
||||
|
||||
|
||||
@@ -805,13 +805,13 @@ export class SceneOcclusionProbe {
|
||||
}
|
||||
|
||||
/**
|
||||
* A probe bound to `viewer`'s scene: what the application's detection
|
||||
* overlay is handed (the `createOcclusionProbe` application hook). Its
|
||||
* `beginFrame(now)` reads the scene itself; the rest is the probe's.
|
||||
* @param {Cesium.Viewer} viewer
|
||||
* @returns {object}
|
||||
* The query port's `occlusionTest()` on `scene`: a probe whose
|
||||
* `beginFrame(now)` reads the scene itself (none once it is destroyed); the
|
||||
* rest is the probe's.
|
||||
* @param {object} scene A Cesium scene.
|
||||
* @returns {import('../contract/query').OcclusionTest}
|
||||
*/
|
||||
export function createViewerOcclusionProbe(viewer) {
|
||||
export function createSceneOcclusionTest(scene) {
|
||||
const probe = new SceneOcclusionProbe();
|
||||
return {
|
||||
get mode() {
|
||||
@@ -827,9 +827,9 @@ export function createViewerOcclusionProbe(viewer) {
|
||||
return probe.lastFrameTests;
|
||||
},
|
||||
beginFrame(now) {
|
||||
probe.beginFrame(viewer.isDestroyed?.() ? null : viewer.scene, now);
|
||||
probe.beginFrame(scene.isDestroyed?.() ? null : scene, now);
|
||||
},
|
||||
add: (contact, position) => probe.add(contact, position),
|
||||
add: (point, position) => probe.add(point, position),
|
||||
endFrame: () => probe.endFrame(),
|
||||
reset: () => probe.reset(),
|
||||
destroy: () => probe.destroy(),
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
/**
|
||||
* @module render/cesium/zoomOutGuard
|
||||
* @description Keeps the user's outward zooms bounded and the Earth framed
|
||||
* on the Cesium camera controller. It reads that controller's own input, so
|
||||
* it lives in the adapter; the application installs it through the scene's
|
||||
* viewer hooks (`application.js`).
|
||||
* on the Cesium camera controller: the camera port's
|
||||
* `controls.keepEarthFramed` (`cameraControls.js`). It reads that
|
||||
* controller's own input, so it lives in the adapter.
|
||||
*/
|
||||
import * as Cesium from 'cesium';
|
||||
import { governorRequestRender } from '../../renderGovernor.js';
|
||||
import { cameraFlightActive } from './internals.js';
|
||||
|
||||
/** Whole-Earth input limit; explicit camera destinations can go farther. */
|
||||
@@ -87,7 +86,7 @@ function readInput(aggregator, bindings) {
|
||||
export function installZoomOutGuard(
|
||||
viewer,
|
||||
{
|
||||
requestRender = governorRequestRender,
|
||||
requestRender = () => viewer?.scene?.requestRender?.(),
|
||||
subscribeCameraHandoff,
|
||||
createAggregator = (canvas) => new Cesium.CameraEventAggregator(canvas),
|
||||
} = {},
|
||||
|
||||
Reference in new issue
Block a user