mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
refactor(camera): use the globe engine
The startup fly-in, orbit, world-target focus, the arrival ground guard, arrival ownership, camera sensitivity claims, landmark/viewport/globe flights and place search (with its view bias and near-view recovery) take the globe engine. Callers that still hold a viewer (voice, layers until phase 1c) reach the same engine through engineFromViewer, and arrival ownership is keyed by the engine whichever one a caller names. The ground guard yields on camera.commanded instead of patching the viewer camera itself; on Cesium that event hooks the same command methods and tracking changes, so direct Cesium callers still end it. Landmark framing reads the frustum fov from the pose's fovy and aspectRatio. Tests that built viewer doubles now run on the fake engine (or on the Cesium engine over the same double), with the same expectations.
This commit is contained in:
1 parent
a3e929870d
commit
16e63f4c50
19 files changed
+601
-215
No files matched your search
@@ -1,16 +1,26 @@
|
||||
/** Arrival corrections belong to one viewer and yield to its next camera owner. */
|
||||
import { engineFromViewer } from '../globe/cesium/engineFromViewer.js';
|
||||
|
||||
/**
|
||||
* Arrival corrections belong to one globe engine and yield to its next camera
|
||||
* owner. Callers may name the engine or (layers until phase 1c) the viewer it
|
||||
* is registered for; both reach the same correction.
|
||||
*/
|
||||
const arrivals = new WeakMap();
|
||||
|
||||
const ownerOf = (engineOrViewer) =>
|
||||
engineFromViewer(engineOrViewer) ?? engineOrViewer;
|
||||
|
||||
/** Cancel an outstanding arrival correction before navigation or teardown. */
|
||||
export function cancelCameraArrival(viewer) {
|
||||
if (viewer) arrivals.get(viewer)?.();
|
||||
export function cancelCameraArrival(engineOrViewer) {
|
||||
if (engineOrViewer) arrivals.get(ownerOf(engineOrViewer))?.();
|
||||
}
|
||||
|
||||
/** Register a correction and return an idempotent ownership release. */
|
||||
export function ownCameraArrival(viewer, cancel) {
|
||||
cancelCameraArrival(viewer);
|
||||
arrivals.set(viewer, cancel);
|
||||
export function ownCameraArrival(engineOrViewer, cancel) {
|
||||
const owner = ownerOf(engineOrViewer);
|
||||
cancelCameraArrival(owner);
|
||||
arrivals.set(owner, cancel);
|
||||
return () => {
|
||||
if (arrivals.get(viewer) === cancel) arrivals.delete(viewer);
|
||||
if (arrivals.get(owner) === cancel) arrivals.delete(owner);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createFakeEngine } from '../globe/testing/fakeEngine.js';
|
||||
import { registerViewerEngine } from '../globe/cesium/engineFromViewer.js';
|
||||
import { cancelCameraArrival, ownCameraArrival } from './cameraArrival.js';
|
||||
|
||||
test('an arrival owned on the engine yields to a cancel through its viewer', () => {
|
||||
const engine = createFakeEngine();
|
||||
const viewer = {};
|
||||
const unregister = registerViewerEngine(viewer, engine);
|
||||
const cancelled = [];
|
||||
// A correction releases its ownership when it stops, as the ground guard does.
|
||||
const own = (key, name) => {
|
||||
let release = () => {};
|
||||
release = ownCameraArrival(key, () => {
|
||||
cancelled.push(name);
|
||||
release();
|
||||
});
|
||||
};
|
||||
own(engine, 'engine-owned');
|
||||
cancelCameraArrival(viewer);
|
||||
own(viewer, 'viewer-owned');
|
||||
cancelCameraArrival(engine);
|
||||
assert.deepEqual(cancelled, ['engine-owned', 'viewer-owned']);
|
||||
unregister();
|
||||
});
|
||||
|
||||
test('a newer arrival replaces the older one and release is idempotent', () => {
|
||||
const engine = createFakeEngine();
|
||||
const seen = [];
|
||||
const releaseOld = ownCameraArrival(engine, () => seen.push('old'));
|
||||
ownCameraArrival(engine, () => seen.push('new'));
|
||||
assert.deepEqual(seen, ['old']);
|
||||
releaseOld();
|
||||
cancelCameraArrival(engine);
|
||||
assert.deepEqual(seen, ['old', 'new']);
|
||||
cancelCameraArrival(null);
|
||||
assert.deepEqual(seen, ['old', 'new']);
|
||||
});
|
||||
@@ -30,6 +30,12 @@ const _cameras = new WeakMap();
|
||||
/** The value Cesium starts a camera at, used when the camera reports nothing. */
|
||||
const DEFAULT_PERCENTAGE_CHANGED = 0.5;
|
||||
|
||||
/**
|
||||
* A globe engine (or a viewer) claims through its camera; anything else is
|
||||
* the camera itself (a camera has no `camera` of its own).
|
||||
*/
|
||||
const cameraOf = (target) => target?.camera ?? target;
|
||||
|
||||
function ledgerFor(camera) {
|
||||
let ledger = _cameras.get(camera);
|
||||
if (!ledger) {
|
||||
@@ -53,12 +59,16 @@ function applyLedger(camera, ledger) {
|
||||
|
||||
/**
|
||||
* Ask for a camera-change sensitivity at least as fine as `value`.
|
||||
* @param {object|null|undefined} camera A Cesium camera (or anything with `percentageChanged`).
|
||||
* @param {object|null|undefined} target A globe engine, its camera, or a
|
||||
* Cesium camera (anything with `percentageChanged`). Layers still pass the
|
||||
* viewer's Cesium camera until phase 1c; that is a separate claimant from
|
||||
* the engine's camera, so one camera must not be claimed both ways.
|
||||
* @param {string} owner Stable claim name, usually the layer id.
|
||||
* @param {number} value Requested `percentageChanged`.
|
||||
* @returns {number|null} The value now in force, or null when there is no camera.
|
||||
*/
|
||||
export function claimCameraSensitivity(camera, owner, value) {
|
||||
export function claimCameraSensitivity(target, owner, value) {
|
||||
const camera = cameraOf(target);
|
||||
if (!camera || !owner || !Number.isFinite(value)) return null;
|
||||
const ledger = ledgerFor(camera);
|
||||
ledger.claims.set(owner, value);
|
||||
@@ -68,11 +78,12 @@ export function claimCameraSensitivity(camera, owner, value) {
|
||||
/**
|
||||
* Give up one claim. The camera keeps the finest value still claimed by
|
||||
* somebody else, and returns to its original only when none are left.
|
||||
* @param {object|null|undefined} camera
|
||||
* @param {object|null|undefined} target A globe engine or a camera.
|
||||
* @param {string} owner
|
||||
* @returns {number|null} The value now in force, or null when there is no camera.
|
||||
*/
|
||||
export function releaseCameraSensitivity(camera, owner) {
|
||||
export function releaseCameraSensitivity(target, owner) {
|
||||
const camera = cameraOf(target);
|
||||
if (!camera || !owner) return null;
|
||||
const ledger = _cameras.get(camera);
|
||||
if (!ledger) return null;
|
||||
@@ -84,10 +95,11 @@ export function releaseCameraSensitivity(camera, owner) {
|
||||
|
||||
/**
|
||||
* Who currently holds a claim. For tests and diagnostics only.
|
||||
* @param {object|null|undefined} camera
|
||||
* @param {object|null|undefined} target A globe engine or a camera.
|
||||
* @returns {Array<string>}
|
||||
*/
|
||||
export function cameraSensitivityClaims(camera) {
|
||||
export function cameraSensitivityClaims(target) {
|
||||
const camera = cameraOf(target);
|
||||
const ledger = camera ? _cameras.get(camera) : null;
|
||||
return ledger ? [...ledger.claims.keys()] : [];
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createFakeEngine } from '../globe/testing/fakeEngine.js';
|
||||
import {
|
||||
cameraSensitivityClaims,
|
||||
claimCameraSensitivity,
|
||||
releaseCameraSensitivity,
|
||||
} from './cameraSensitivity.js';
|
||||
|
||||
test('claims on a globe engine set its camera sensitivity and share one ledger', () => {
|
||||
const engine = createFakeEngine();
|
||||
assert.equal(claimCameraSensitivity(engine, 'traffic', 0.05), 0.05);
|
||||
assert.equal(engine.camera.percentageChanged, 0.05);
|
||||
// The engine and its camera are the same claimant.
|
||||
assert.equal(claimCameraSensitivity(engine.camera, 'installations', 0.01), 0.01);
|
||||
assert.deepEqual(cameraSensitivityClaims(engine), ['traffic', 'installations']);
|
||||
assert.equal(releaseCameraSensitivity(engine, 'installations'), 0.05);
|
||||
assert.equal(releaseCameraSensitivity(engine.camera, 'traffic'), 0.5);
|
||||
assert.equal(engine.camera.percentageChanged, 0.5);
|
||||
assert.deepEqual(cameraSensitivityClaims(engine), []);
|
||||
});
|
||||
Reference in new issue
Block a user