mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
The first camera-family slice. The camera port's events move into a shared module (render/camera/events.js) that the stub and the Cesium port both use: listener lists, delivery with errors reported, and the contract's per-listener `changed` measure. The Cesium port gains: - camera.on: `changed` measured per listener on every tick, where Cesium measures its single threshold (the counts match Cesium's own at each threshold); moveStart and moveEnd relayed from Cesium's camera; - camera.viewRectangle, in degrees (now in CESIUM_CHECKS); - lon and lat in pose, and NaN where Cesium has no cartographic position. Code that still holds the viewer reaches the renderer through rendererOf(viewer); the application renderer binds it on create and unbinds on destroy. The HUD reads its telemetry from the camera port and no longer imports Cesium (off the allow-list). The share link writes the hash from the pose. Its restore path stays on the viewer's camera for a later slice. Behaviour changes: - The share link listens to `changed` at its own 0.5 threshold, not at whatever global threshold layers had set, and also to moveEnd, so the hash always catches up with where the camera comes to rest. - The HUD's and share link's camera callbacks run from the port's single relay, ahead of other moveStart/moveEnd listeners, and a throw in one is reported to the renderer's error listeners instead of escaping from Cesium's render.
102 lines
2.9 KiB
JavaScript
102 lines
2.9 KiB
JavaScript
/**
|
|
* A camera port for tests of code that reads the camera: a settable pose,
|
|
* a settable view rectangle, and events the test raises itself.
|
|
*/
|
|
import { createCameraEvents } from '../render/camera/events.js';
|
|
import { toDegrees } from '../geo/index.js';
|
|
import { bindRenderer, rendererOf } from '../render/rendererOf.js';
|
|
|
|
const DEFAULT_POSE = Object.freeze({
|
|
lon: 0,
|
|
lat: 0,
|
|
height: 1000,
|
|
heading: 0,
|
|
pitch: -Math.PI / 2,
|
|
roll: 0,
|
|
position: { x: 0, y: 0, z: 0 },
|
|
direction: { x: 0, y: 0, z: -1 },
|
|
up: { x: 0, y: 1, z: 0 },
|
|
right: { x: 1, y: 0, z: 0 },
|
|
fovy: Math.PI / 3,
|
|
aspectRatio: 1,
|
|
near: 1,
|
|
far: 5e8,
|
|
frameIsIdentity: true,
|
|
});
|
|
|
|
/**
|
|
* @param {{pose?: object, viewRectangle?: object|null}} [initial]
|
|
*/
|
|
export function fakeCameraPort({ pose = {}, viewRectangle = null } = {}) {
|
|
let current = { ...DEFAULT_POSE, ...pose };
|
|
let rect = viewRectangle;
|
|
const events = createCameraEvents({
|
|
pose: () => ({ ...current }),
|
|
report: (error) => {
|
|
throw error;
|
|
},
|
|
});
|
|
const counts = {};
|
|
const port = {
|
|
pose: () => ({ ...current }),
|
|
viewRectangle: () => (rect ? { ...rect } : null),
|
|
on(event, cb, options) {
|
|
const off = events.on(event, cb, options);
|
|
counts[event] = (counts[event] ?? 0) + 1;
|
|
let live = true;
|
|
return () => {
|
|
if (live) counts[event] -= 1;
|
|
live = false;
|
|
off();
|
|
};
|
|
},
|
|
};
|
|
return {
|
|
port,
|
|
/** Change the pose the port reports. */
|
|
setPose: (fields) => (current = { ...current, ...fields }),
|
|
setViewRectangle: (next) => (rect = next),
|
|
emit: (event) => events.emit(event),
|
|
/** Run the contract's `changed` measure, as a frame would. */
|
|
stepChanged: () => events.stepChanged(),
|
|
listenerCount: (event) => counts[event] ?? 0,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Bind to a fake viewer a camera port that reads its Cesium-shaped fake
|
|
* camera live (`positionCartographic`, `heading`, `pitch`, `roll`, and the
|
|
* `changed`/`moveStart`/`moveEnd` events it has), for tests whose code
|
|
* still moves that camera directly. Returns the viewer.
|
|
* @template {{camera: object}} V
|
|
* @param {V} viewer
|
|
* @returns {V}
|
|
*/
|
|
export function bindFakeCesiumCamera(viewer) {
|
|
if (rendererOf(viewer)) return viewer;
|
|
const camera = viewer.camera;
|
|
const port = {
|
|
pose() {
|
|
const c = camera.positionCartographic;
|
|
return {
|
|
...DEFAULT_POSE,
|
|
lon: c ? toDegrees(c.longitude) : NaN,
|
|
lat: c ? toDegrees(c.latitude) : NaN,
|
|
height: c ? c.height : NaN,
|
|
heading: camera.heading ?? 0,
|
|
pitch: camera.pitch ?? -Math.PI / 2,
|
|
roll: camera.roll ?? 0,
|
|
};
|
|
},
|
|
viewRectangle: () => null,
|
|
on(event, cb) {
|
|
const source = camera[event];
|
|
const off = source?.addEventListener?.(cb);
|
|
if (typeof off === 'function') return off;
|
|
return () => source?.removeEventListener?.(cb);
|
|
},
|
|
};
|
|
bindRenderer(viewer, { camera: port });
|
|
return viewer;
|
|
}
|