mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Define the renderer contract, with a stub renderer and a conformance suite
The renderer contract is what the application asks of a 3D renderer. It is nine ports (frame, globe, draw, camera, query, input, effects, credits, quality) plus the renderer itself, one strict .d.ts per port in src/render/contract/. PORTS lists every member at run time, and a TypeScript-compiler test fails when the list and the types disagree on one member. An optional member exists only with its capability; a required one answers null or empty without it. Nothing in the application uses the contract yet. The stub renderer (src/render/stub/) implements every port in memory and draws nothing. Its camera is a port of the default camera's maths: views, lookAt, flights with their height arc and easing, rectangle framing on the geodesic midpoint, the view rectangle with its horizon and pole cases, changed thresholds, follows, and flights that finish inside the call. The conformance suite (src/render/conformance/) runs 59 checks against a renderer through the contract alone, in Node or in a browser page. The camera checks compare traces recorded from Cesium's own Camera, headless on a virtual clock (scripts/record-camera-goldens.mjs), to 1 mm, 1e-9 and 1e-3 px. The other checks cover listener rules on every subscription, the frame loop, draw sets, picks, motion clocks, heights, leases, imagery, styles, credits and quality knobs. The stub passes all of them but the model check, which needs a glTF asset. Reference scenes draw one scene per primitive family, a city and a calibration path from orbit to the street, from contract calls only and with no network. /dev/conformance/ draws them on the Cesium scene renderer (src/render/cesium/sceneRenderer.js), records references through a dev-server-only, loopback-only endpoint, and compares captures with pixelmatch's antialias detection plus a solid-pixel count. Measured on Cesium: every MSAA setting passes, and a dropped 4 px point, a dropped 2 px line and a 1 px shift of the view fail. visual.js states the limits. The package exports ./render/contract, ./render/stub and ./render/conformance. Documented in docs/RENDERER-CONTRACT.md, docs/CODE-BOUNDARIES.md and docs/CURRENT-STATE.md.
This commit is contained in:
1 parent
464c29a9ed
commit
8db995acfc
65 files changed
+12759
-9
No files matched your search
@@ -0,0 +1,316 @@
|
||||
/**
|
||||
* @module render/cesium/cameraGoldens
|
||||
* @description Records the reference camera traces: every conformance camera
|
||||
* script run on Cesium's own `Camera`, headless, on a virtual clock that
|
||||
* advances exactly 1/60 s per frame and restarts per script. No globe and no
|
||||
* tiles, so the traces depend on the camera alone and are bit-identical run
|
||||
* to run. `scripts/record-camera-goldens.mjs` writes them to
|
||||
* `src/render/conformance/goldens/camera.json`.
|
||||
*
|
||||
* Cesium has one `changed` threshold per camera, where the contract has one
|
||||
* per listener, so a script that listens at several thresholds is run once
|
||||
* per threshold and each count is taken from its own run.
|
||||
*/
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
CAMERA_SCRIPTS,
|
||||
SCRIPT_VIEWPORT,
|
||||
} from '../conformance/cameraScripts.js';
|
||||
import { runCameraScript } from '../conformance/cameraRunner.js';
|
||||
import { updateCameraChanged } from './internals.js';
|
||||
|
||||
const FRAME_MS = 1000 / 60;
|
||||
|
||||
/** The contract's easings as Cesium's. */
|
||||
const EASINGS = {
|
||||
linear: Cesium.EasingFunction.LINEAR_NONE,
|
||||
quadraticInOut: Cesium.EasingFunction.QUADRATIC_IN_OUT,
|
||||
cubicInOut: Cesium.EasingFunction.CUBIC_IN_OUT,
|
||||
cubicOut: Cesium.EasingFunction.CUBIC_OUT,
|
||||
quinticInOut: Cesium.EasingFunction.QUINTIC_IN_OUT,
|
||||
};
|
||||
|
||||
/** Run `fn` with `performance.now()` reading the virtual clock. */
|
||||
async function withVirtualClock(fn) {
|
||||
const real = Object.getOwnPropertyDescriptor(globalThis, 'performance');
|
||||
const clock = { now: 0 };
|
||||
Object.defineProperty(globalThis, 'performance', {
|
||||
configurable: true,
|
||||
value: { now: () => clock.now, mark() {}, measure() {} },
|
||||
});
|
||||
try {
|
||||
return await fn(clock);
|
||||
} finally {
|
||||
if (real) Object.defineProperty(globalThis, 'performance', real);
|
||||
}
|
||||
}
|
||||
|
||||
function headlessCamera() {
|
||||
const scene = {
|
||||
canvas: {
|
||||
clientWidth: SCRIPT_VIEWPORT.width,
|
||||
clientHeight: SCRIPT_VIEWPORT.height,
|
||||
},
|
||||
drawingBufferWidth: SCRIPT_VIEWPORT.width,
|
||||
drawingBufferHeight: SCRIPT_VIEWPORT.height,
|
||||
pixelRatio: 1,
|
||||
mapProjection: new Cesium.GeographicProjection(),
|
||||
mode: Cesium.SceneMode.SCENE3D,
|
||||
tweens: new Cesium.TweenCollection(),
|
||||
globe: undefined,
|
||||
ellipsoid: Cesium.Ellipsoid.WGS84,
|
||||
screenSpaceCameraController: {
|
||||
minimumZoomDistance: 1,
|
||||
maximumZoomDistance: Number.POSITIVE_INFINITY,
|
||||
enableInputs: true,
|
||||
},
|
||||
frameState: { mode: Cesium.SceneMode.SCENE3D },
|
||||
};
|
||||
const camera = new Cesium.Camera(scene);
|
||||
scene.camera = camera;
|
||||
camera.frustum.aspectRatio = SCRIPT_VIEWPORT.width / SCRIPT_VIEWPORT.height;
|
||||
return { scene, camera };
|
||||
}
|
||||
|
||||
const cart = (p) => new Cesium.Cartesian3(p.x, p.y, p.z);
|
||||
const toEcef = (p) =>
|
||||
'lon' in p
|
||||
? Cesium.Cartesian3.fromDegrees(p.lon, p.lat, p.height ?? 0)
|
||||
: cart(p);
|
||||
|
||||
function destinationOf(destination) {
|
||||
if (!destination) return undefined;
|
||||
if ('rect' in destination) {
|
||||
const r = destination.rect;
|
||||
return Cesium.Rectangle.fromDegrees(r.west, r.south, r.east, r.north);
|
||||
}
|
||||
return toEcef(destination);
|
||||
}
|
||||
|
||||
function orientationOf(orientation) {
|
||||
if (!orientation) return undefined;
|
||||
if ('direction' in orientation)
|
||||
return { direction: cart(orientation.direction), up: cart(orientation.up) };
|
||||
return { ...orientation };
|
||||
}
|
||||
|
||||
/** A driver for {@link runCameraScript} over a headless Cesium camera. */
|
||||
function cesiumDriver(clock, threshold) {
|
||||
const { scene, camera } = headlessCamera();
|
||||
let flying = false;
|
||||
let changedCount = 0;
|
||||
camera.percentageChanged = threshold ?? 0.5;
|
||||
if (threshold !== undefined)
|
||||
camera.changed.addEventListener(() => (changedCount += 1));
|
||||
|
||||
function flight(start) {
|
||||
let resolve;
|
||||
const done = new Promise((r) => (resolve = r));
|
||||
flying = true;
|
||||
start(
|
||||
() => {
|
||||
flying = false;
|
||||
resolve('complete');
|
||||
},
|
||||
() => {
|
||||
flying = false;
|
||||
resolve('cancelled');
|
||||
},
|
||||
);
|
||||
return { done, cancel: () => camera.cancelFlight() };
|
||||
}
|
||||
|
||||
const commands = {
|
||||
setView({ destination, orientation } = {}) {
|
||||
camera.setView({
|
||||
destination: destinationOf(destination),
|
||||
orientation: orientationOf(orientation),
|
||||
});
|
||||
},
|
||||
lookAt(target, offset) {
|
||||
camera.lookAt(
|
||||
cart(target),
|
||||
new Cesium.HeadingPitchRange(
|
||||
offset.heading,
|
||||
offset.pitch,
|
||||
offset.range,
|
||||
),
|
||||
);
|
||||
},
|
||||
release() {
|
||||
camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
|
||||
},
|
||||
move(direction, metres) {
|
||||
const name = {
|
||||
forward: 'moveForward',
|
||||
back: 'moveBackward',
|
||||
left: 'moveLeft',
|
||||
right: 'moveRight',
|
||||
up: 'moveUp',
|
||||
down: 'moveDown',
|
||||
}[direction];
|
||||
camera[name](metres);
|
||||
},
|
||||
look(direction, radians) {
|
||||
const name = {
|
||||
left: 'lookLeft',
|
||||
right: 'lookRight',
|
||||
up: 'lookUp',
|
||||
down: 'lookDown',
|
||||
}[direction];
|
||||
camera[name](radians);
|
||||
},
|
||||
rotateAround(axis, radians) {
|
||||
camera.rotate(cart(axis), radians);
|
||||
},
|
||||
zoom(metres) {
|
||||
camera.zoomIn(metres);
|
||||
},
|
||||
flyTo(destination, options = {}) {
|
||||
return flight((complete, cancel) =>
|
||||
camera.flyTo({
|
||||
destination: destinationOf(destination),
|
||||
orientation: orientationOf(options.orientation),
|
||||
duration: options.durationS,
|
||||
maximumHeight: options.maxHeight,
|
||||
pitchAdjustHeight: options.pitchAdjustHeight,
|
||||
easingFunction: EASINGS[options.easing],
|
||||
complete,
|
||||
cancel,
|
||||
}),
|
||||
);
|
||||
},
|
||||
flyToBounds(sphere, options = {}) {
|
||||
return flight((complete, cancel) =>
|
||||
camera.flyToBoundingSphere(
|
||||
new Cesium.BoundingSphere(cart(sphere.center), sphere.radius),
|
||||
{
|
||||
offset: options.offset
|
||||
? new Cesium.HeadingPitchRange(
|
||||
options.offset.heading,
|
||||
options.offset.pitch,
|
||||
options.offset.range,
|
||||
)
|
||||
: undefined,
|
||||
duration: options.durationS,
|
||||
complete,
|
||||
cancel,
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
cancelFlight() {
|
||||
camera.cancelFlight();
|
||||
},
|
||||
};
|
||||
|
||||
const viewProjection = () =>
|
||||
Cesium.Matrix4.multiply(
|
||||
camera.frustum.projectionMatrix,
|
||||
camera.viewMatrix,
|
||||
new Cesium.Matrix4(),
|
||||
);
|
||||
|
||||
return {
|
||||
command: (name, args) => commands[name](...args),
|
||||
frame() {
|
||||
clock.now += FRAME_MS;
|
||||
scene.tweens.update();
|
||||
updateCameraChanged(camera);
|
||||
},
|
||||
listen: () => () => changedCount,
|
||||
pose() {
|
||||
const c = camera.positionCartographic;
|
||||
return {
|
||||
position: camera.positionWC,
|
||||
direction: camera.directionWC,
|
||||
up: camera.upWC,
|
||||
right: camera.rightWC,
|
||||
heading: camera.heading,
|
||||
pitch: camera.pitch,
|
||||
roll: camera.roll,
|
||||
height: c.height,
|
||||
near: camera.frustum.near,
|
||||
far: camera.frustum.far,
|
||||
frameIsIdentity: Cesium.Matrix4.equals(
|
||||
camera.transform,
|
||||
Cesium.Matrix4.IDENTITY,
|
||||
),
|
||||
};
|
||||
},
|
||||
viewRectangle() {
|
||||
const rect = camera.computeViewRectangle(Cesium.Ellipsoid.WGS84);
|
||||
if (!rect) return null;
|
||||
const deg = Cesium.Math.toDegrees;
|
||||
return {
|
||||
west: deg(rect.west),
|
||||
south: deg(rect.south),
|
||||
east: deg(rect.east),
|
||||
north: deg(rect.north),
|
||||
};
|
||||
},
|
||||
matrices: () => ({
|
||||
view: Float64Array.from(Cesium.Matrix4.toArray(camera.viewMatrix)),
|
||||
projection: Float64Array.from(
|
||||
Cesium.Matrix4.toArray(camera.frustum.projectionMatrix),
|
||||
),
|
||||
}),
|
||||
project(position) {
|
||||
const clip = Cesium.Matrix4.multiplyByVector(
|
||||
viewProjection(),
|
||||
new Cesium.Cartesian4(position.x, position.y, position.z, 1),
|
||||
new Cesium.Cartesian4(),
|
||||
);
|
||||
if (clip.w <= 0) return null;
|
||||
return {
|
||||
x: ((clip.x / clip.w + 1) / 2) * SCRIPT_VIEWPORT.width,
|
||||
y: ((1 - clip.y / clip.w) / 2) * SCRIPT_VIEWPORT.height,
|
||||
};
|
||||
},
|
||||
pickEllipsoid: (s) =>
|
||||
camera.pickEllipsoid(new Cesium.Cartesian2(s.x, s.y)) ?? null,
|
||||
pickRay: (s) => camera.getPickRay(new Cesium.Cartesian2(s.x, s.y)),
|
||||
occluded: (p) =>
|
||||
!new Cesium.EllipsoidalOccluder(
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
camera.positionWC,
|
||||
).isPointVisible(p),
|
||||
flying: () => flying,
|
||||
toEcef: (p) => {
|
||||
const e = toEcef(p);
|
||||
return { x: e.x, y: e.y, z: e.z };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** Run one script on Cesium, once per `changed` threshold it listens at. */
|
||||
async function recordScript(script) {
|
||||
const thresholds = script.steps
|
||||
.filter((step) => step.do === 'listen')
|
||||
.map((step) => step.threshold);
|
||||
const base = await withVirtualClock((clock) =>
|
||||
runCameraScript(cesiumDriver(clock, undefined), script),
|
||||
);
|
||||
if (!thresholds.length) return base;
|
||||
const counts = [];
|
||||
for (const threshold of thresholds) {
|
||||
const run = await withVirtualClock((clock) =>
|
||||
runCameraScript(cesiumDriver(clock, threshold), script),
|
||||
);
|
||||
const changed = run.records.find((r) => r.kind === 'changed');
|
||||
counts.push({ threshold, count: changed.value[0].count });
|
||||
}
|
||||
for (const r of base.records) if (r.kind === 'changed') r.value = counts;
|
||||
return base;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every script's trace on the installed Cesium.
|
||||
* @returns {Promise<{cesium: string, viewport: object, scripts: Record<string, object>}>}
|
||||
*/
|
||||
export async function recordCameraGoldens() {
|
||||
const scripts = {};
|
||||
for (const script of CAMERA_SCRIPTS)
|
||||
scripts[script.name] = await recordScript(script);
|
||||
return { cesium: Cesium.VERSION, viewport: SCRIPT_VIEWPORT, scripts };
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { recordCameraGoldens } from './cameraGoldens.js';
|
||||
|
||||
test('the committed camera traces are what the installed Cesium records', async () => {
|
||||
const committed = readFileSync(
|
||||
new URL('../conformance/goldens/camera.json', import.meta.url),
|
||||
'utf8',
|
||||
);
|
||||
const recorded = `${JSON.stringify(await recordCameraGoldens(), null, 2)}\n`;
|
||||
assert.ok(
|
||||
recorded === committed,
|
||||
'goldens/camera.json is out of date: run node scripts/record-camera-goldens.mjs and review the diff',
|
||||
);
|
||||
});
|
||||
@@ -84,3 +84,12 @@ export function webglContext(scene) {
|
||||
export function cameraFlightActive(camera) {
|
||||
return camera?._currentFlight != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the camera's `changed` check, as a scene does once a frame. For a
|
||||
* camera driven without a scene (the reference camera traces).
|
||||
* @param {object} camera Cesium Camera.
|
||||
*/
|
||||
export function updateCameraChanged(camera) {
|
||||
camera._updateCameraChanged();
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
commandExtents,
|
||||
defaultView,
|
||||
pickOffscreenView,
|
||||
updateCameraChanged,
|
||||
webglContext,
|
||||
} from './internals.js';
|
||||
|
||||
@@ -47,6 +48,8 @@ test('every private field the app reaches is still declared by the installed Ces
|
||||
),
|
||||
);
|
||||
assert.deepEqual(missing, []);
|
||||
const camera = fs.readFileSync(path.join(ENGINE, 'Scene/Camera.js'), 'utf8');
|
||||
assert.match(camera, /Camera\.prototype\._updateCameraChanged = function/);
|
||||
});
|
||||
|
||||
test('the helpers read and write exactly those fields', () => {
|
||||
@@ -76,6 +79,9 @@ test('the helpers read and write exactly those fields', () => {
|
||||
assert.equal(webglContext({ context: { _gl: gl } }), gl);
|
||||
assert.equal(cameraFlightActive({ _currentFlight: {} }), true);
|
||||
assert.equal(cameraFlightActive({ _currentFlight: undefined }), false);
|
||||
let checked = 0;
|
||||
updateCameraChanged({ _updateCameraChanged: () => (checked += 1) });
|
||||
assert.equal(checked, 1);
|
||||
});
|
||||
|
||||
test('private Cesium fields are reached only through internals.js', () => {
|
||||
|
||||
@@ -0,0 +1,656 @@
|
||||
/**
|
||||
* @module render/cesium/sceneRenderer
|
||||
* @description The part of the Cesium renderer the reference scenes need,
|
||||
* through the contract's own members: a bare globe and sky, icons, points,
|
||||
* lines, shapes and models, `setView`, picks and projection, the frame
|
||||
* phases, settling and capture. The `/dev/conformance/` page draws the
|
||||
* scenes with it to make and check the visual references.
|
||||
*
|
||||
* It is not yet the whole Cesium renderer: each later migration adds the
|
||||
* members its layer family needs, until it passes the whole suite.
|
||||
* Members it does not have are absent, never stubbed.
|
||||
*/
|
||||
import * as Cesium from 'cesium';
|
||||
|
||||
const SKY_DEFAULTS = Object.freeze({
|
||||
atmosphere: false,
|
||||
stars: false,
|
||||
milkyWay: 0,
|
||||
sun: false,
|
||||
moon: false,
|
||||
fog: false,
|
||||
lighting: false,
|
||||
});
|
||||
|
||||
/** The contract's colour as Cesium's. */
|
||||
export function toColor(color, alpha = 1) {
|
||||
if (color === undefined || color === null)
|
||||
return Cesium.Color.WHITE.withAlpha(alpha);
|
||||
const c =
|
||||
typeof color === 'string'
|
||||
? Cesium.Color.fromCssColorString(color)
|
||||
: new Cesium.Color(color[0], color[1], color[2], color[3] ?? 1);
|
||||
return c.withAlpha(c.alpha * alpha);
|
||||
}
|
||||
|
||||
/** An image reference as something a billboard takes. */
|
||||
export function toImage(image) {
|
||||
if (typeof image === 'string') return image;
|
||||
if (image?.svg)
|
||||
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(image.svg)}`;
|
||||
if (image?.canvas) return image.canvas;
|
||||
throw new Error('the scene renderer takes URL, SVG and canvas images only');
|
||||
}
|
||||
|
||||
/** A contract position as ECEF. */
|
||||
export function toCartesian(pos) {
|
||||
if ('x' in pos) return new Cesium.Cartesian3(pos.x, pos.y, pos.z);
|
||||
return Cesium.Cartesian3.fromDegrees(pos.lon, pos.lat, pos.height ?? 0);
|
||||
}
|
||||
|
||||
const ANCHORS = {
|
||||
center: Cesium.VerticalOrigin.CENTER,
|
||||
bottom: Cesium.VerticalOrigin.BOTTOM,
|
||||
top: Cesium.VerticalOrigin.TOP,
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {{container: HTMLElement, msaa?: number}} options
|
||||
* @returns {Promise<object>} A renderer with the members listed above.
|
||||
*/
|
||||
export async function createCesiumSceneRenderer({ container, msaa = 4 }) {
|
||||
const credits = document.createElement('div');
|
||||
const widget = new Cesium.CesiumWidget(container, {
|
||||
baseLayer: false,
|
||||
terrainProvider: new Cesium.EllipsoidTerrainProvider(),
|
||||
skyBox: false,
|
||||
skyAtmosphere: false,
|
||||
requestRenderMode: true,
|
||||
maximumRenderTimeChange: Infinity,
|
||||
msaaSamples: msaa,
|
||||
creditContainer: credits,
|
||||
contextOptions: { webgl: { preserveDrawingBuffer: true } },
|
||||
});
|
||||
const scene = widget.scene;
|
||||
const camera = scene.camera;
|
||||
// A fixed time, so lighting is the same in every capture.
|
||||
widget.clock.shouldAnimate = false;
|
||||
widget.clock.currentTime = Cesium.JulianDate.fromIso8601(
|
||||
'2026-06-21T18:00:00Z',
|
||||
);
|
||||
scene.globe.baseColor = Cesium.Color.fromCssColorString('#1c2a36');
|
||||
scene.backgroundColor = Cesium.Color.BLACK;
|
||||
scene.globe.showGroundAtmosphere = false;
|
||||
scene.fog.enabled = false;
|
||||
if (scene.sun) scene.sun.show = false;
|
||||
if (scene.moon) scene.moon.show = false;
|
||||
|
||||
const sets = new Map();
|
||||
const sky = { ...SKY_DEFAULTS };
|
||||
const capabilities = new Set(['models']);
|
||||
|
||||
function applySky() {
|
||||
scene.globe.enableLighting = sky.lighting;
|
||||
scene.fog.enabled = sky.fog;
|
||||
if (scene.sun) scene.sun.show = sky.sun;
|
||||
if (scene.moon) scene.moon.show = sky.moon;
|
||||
scene.globe.showGroundAtmosphere = sky.atmosphere;
|
||||
scene.requestRender();
|
||||
}
|
||||
|
||||
const frameListeners = { preUpdate: [], preRender: [], postRender: [] };
|
||||
let frames = 0;
|
||||
const onPhase = (phase) => (_scene, time) => {
|
||||
const t = {
|
||||
frame: frames,
|
||||
time: Cesium.JulianDate.toDate(
|
||||
time ?? widget.clock.currentTime,
|
||||
).getTime(),
|
||||
now: performance.now(),
|
||||
dt: 1 / 60,
|
||||
rendering: phase !== 'preUpdate',
|
||||
};
|
||||
for (const cb of frameListeners[phase].slice()) cb(t);
|
||||
};
|
||||
const removers = [
|
||||
scene.preUpdate.addEventListener(() => (frames += 1)),
|
||||
scene.preUpdate.addEventListener(onPhase('preUpdate')),
|
||||
scene.preRender.addEventListener(onPhase('preRender')),
|
||||
scene.postRender.addEventListener(onPhase('postRender')),
|
||||
];
|
||||
|
||||
function nextPostRender() {
|
||||
return new Promise((resolve) => {
|
||||
const off = scene.postRender.addEventListener(() => {
|
||||
off();
|
||||
resolve();
|
||||
});
|
||||
scene.requestRender();
|
||||
});
|
||||
}
|
||||
|
||||
const frame = {
|
||||
on(phase, cb) {
|
||||
frameListeners[phase].push(cb);
|
||||
return () => {
|
||||
const i = frameListeners[phase].indexOf(cb);
|
||||
if (i >= 0) frameListeners[phase].splice(i, 1);
|
||||
};
|
||||
},
|
||||
requestRender: () => scene.requestRender(),
|
||||
renderNow: () => scene.render(widget.clock.currentTime),
|
||||
renderOnce: () => nextPostRender(),
|
||||
time: () => Cesium.JulianDate.toDate(widget.clock.currentTime).getTime(),
|
||||
setTime(ms) {
|
||||
widget.clock.currentTime = Cesium.JulianDate.fromDate(new Date(ms));
|
||||
widget.clock.shouldAnimate = false;
|
||||
scene.requestRender();
|
||||
},
|
||||
};
|
||||
|
||||
// --- Draw ------------------------------------------------------------------
|
||||
|
||||
function makeSet(kind, spec, build) {
|
||||
if (sets.has(spec.id)) throw new Error(`a set with id ${spec.id} is live`);
|
||||
const items = new Map();
|
||||
const listeners = { ready: [] };
|
||||
const record = { kind, spec, items, listeners, host: null, rebuild: null };
|
||||
const handle = {
|
||||
id: spec.id,
|
||||
set(list) {
|
||||
items.clear();
|
||||
for (const item of list) items.set(item.id, item);
|
||||
build(record);
|
||||
scene.requestRender();
|
||||
},
|
||||
upsert(list) {
|
||||
for (const item of list) items.set(item.id, item);
|
||||
build(record);
|
||||
scene.requestRender();
|
||||
},
|
||||
remove(ids) {
|
||||
for (const id of ids) items.delete(id);
|
||||
build(record);
|
||||
scene.requestRender();
|
||||
},
|
||||
clear() {
|
||||
items.clear();
|
||||
build(record);
|
||||
scene.requestRender();
|
||||
},
|
||||
setOptions(options) {
|
||||
Object.assign(spec, options);
|
||||
if (record.host) record.host.show = spec.show !== false;
|
||||
scene.requestRender();
|
||||
},
|
||||
size: () => items.size,
|
||||
get show() {
|
||||
return spec.show !== false;
|
||||
},
|
||||
set show(value) {
|
||||
spec.show = value;
|
||||
if (record.host) record.host.show = value;
|
||||
scene.requestRender();
|
||||
},
|
||||
on(event, cb) {
|
||||
listeners[event]?.push(cb);
|
||||
return () => {
|
||||
const list = listeners[event];
|
||||
const i = list?.indexOf(cb) ?? -1;
|
||||
if (i >= 0) list.splice(i, 1);
|
||||
};
|
||||
},
|
||||
dispose() {
|
||||
if (record.host && !record.host.isDestroyed?.())
|
||||
scene.primitives.remove(record.host);
|
||||
record.host = null;
|
||||
// A newer set under the same id is not this handle's to remove.
|
||||
if (sets.get(spec.id) === record) sets.delete(spec.id);
|
||||
scene.requestRender();
|
||||
},
|
||||
};
|
||||
sets.set(spec.id, record);
|
||||
return handle;
|
||||
}
|
||||
|
||||
const pickId = (record, item) => ({
|
||||
set: record.spec.id,
|
||||
id: item.id,
|
||||
meta: item.meta,
|
||||
});
|
||||
|
||||
function replaceHost(record, host) {
|
||||
if (record.host) scene.primitives.remove(record.host);
|
||||
record.host = host;
|
||||
host.show = record.spec.show !== false;
|
||||
scene.primitives.add(host);
|
||||
}
|
||||
|
||||
function buildIcons(record) {
|
||||
const host = new Cesium.BillboardCollection({ scene });
|
||||
for (const item of record.items.values()) {
|
||||
const position = toCartesian(item.pos);
|
||||
const options = {
|
||||
position,
|
||||
image: toImage(item.image),
|
||||
width: item.size[0] * (item.scale ?? 1),
|
||||
height: item.size[1] * (item.scale ?? 1),
|
||||
color: toColor(item.color, item.alpha ?? 1),
|
||||
verticalOrigin: ANCHORS[item.anchor ?? 'center'],
|
||||
disableDepthTestDistance: item.disableDepthTest ?? 0,
|
||||
show: item.show !== false,
|
||||
id: pickId(record, item),
|
||||
};
|
||||
if (item.rotationMode === 'screen') options.rotation = item.rotation ?? 0;
|
||||
if (item.rotationMode === 'course') {
|
||||
const enu = Cesium.Transforms.eastNorthUpToFixedFrame(position);
|
||||
options.alignedAxis = Cesium.Matrix4.multiplyByPointAsVector(
|
||||
enu,
|
||||
Cesium.Cartesian3.UNIT_Y,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
options.rotation = -Cesium.Math.toRadians(item.course ?? 0);
|
||||
}
|
||||
host.add(options);
|
||||
}
|
||||
replaceHost(record, host);
|
||||
}
|
||||
|
||||
function buildPoints(record) {
|
||||
const host = new Cesium.PointPrimitiveCollection();
|
||||
for (const item of record.items.values())
|
||||
host.add({
|
||||
position: toCartesian(item.pos),
|
||||
pixelSize: item.pixelSize,
|
||||
color: toColor(item.color),
|
||||
outlineColor: toColor(item.outlineColor ?? [0, 0, 0, 0]),
|
||||
outlineWidth: item.outlineWidth ?? 0,
|
||||
disableDepthTestDistance: item.disableDepthTest ?? 0,
|
||||
show: item.show !== false,
|
||||
id: pickId(record, item),
|
||||
});
|
||||
replaceHost(record, host);
|
||||
}
|
||||
|
||||
function lineMaterial(style) {
|
||||
switch (style.kind) {
|
||||
case 'solid':
|
||||
return Cesium.Material.fromType('Color', {
|
||||
color: toColor(style.color),
|
||||
});
|
||||
case 'glow':
|
||||
return Cesium.Material.fromType('PolylineGlow', {
|
||||
color: toColor(style.color),
|
||||
glowPower: style.power,
|
||||
taperPower: style.taper ?? 1,
|
||||
});
|
||||
case 'dash':
|
||||
return Cesium.Material.fromType('PolylineDash', {
|
||||
color: toColor(style.color),
|
||||
gapColor: toColor(style.gapColor ?? [0, 0, 0, 0]),
|
||||
dashLength: style.length,
|
||||
...(style.pattern16 !== undefined
|
||||
? { dashPattern: style.pattern16 }
|
||||
: {}),
|
||||
});
|
||||
case 'arrow':
|
||||
return Cesium.Material.fromType('PolylineArrow', {
|
||||
color: toColor(style.color),
|
||||
});
|
||||
default:
|
||||
throw new Error(`the scene renderer has no ${style.kind} lines`);
|
||||
}
|
||||
}
|
||||
|
||||
function linePositions(item) {
|
||||
const flat = item.positions instanceof Float64Array;
|
||||
const points = [];
|
||||
const n = flat ? item.positions.length / 3 : item.positions.length;
|
||||
for (let i = 0; i < n; i++)
|
||||
points.push(
|
||||
flat
|
||||
? new Cesium.Cartesian3(
|
||||
item.positions[i * 3],
|
||||
item.positions[i * 3 + 1],
|
||||
item.positions[i * 3 + 2],
|
||||
)
|
||||
: toCartesian(item.positions[i]),
|
||||
);
|
||||
if (item.mode !== 'geodesic') return points;
|
||||
const dense = [];
|
||||
for (let i = 0; i + 1 < points.length; i++) {
|
||||
const a = Cesium.Cartographic.fromCartesian(points[i]);
|
||||
const b = Cesium.Cartographic.fromCartesian(points[i + 1]);
|
||||
const geodesic = new Cesium.EllipsoidGeodesic(a, b);
|
||||
for (let k = 0; k < 64; k++) {
|
||||
const s = k / 64;
|
||||
const at = geodesic.interpolateUsingFraction(s);
|
||||
dense.push(
|
||||
Cesium.Cartesian3.fromRadians(
|
||||
at.longitude,
|
||||
at.latitude,
|
||||
a.height + (b.height - a.height) * s,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
dense.push(points.at(-1));
|
||||
return dense;
|
||||
}
|
||||
|
||||
function buildLines(record) {
|
||||
const host = new Cesium.PolylineCollection();
|
||||
for (const item of record.items.values()) {
|
||||
if (item.mode === 'draped')
|
||||
throw new Error('the scene renderer has no draped lines');
|
||||
host.add({
|
||||
positions: linePositions(item),
|
||||
width: item.widthPx,
|
||||
material: lineMaterial(item.style),
|
||||
show: item.show !== false,
|
||||
id: pickId(record, item),
|
||||
});
|
||||
}
|
||||
replaceHost(record, host);
|
||||
}
|
||||
|
||||
const heightOf = (pos) =>
|
||||
'x' in pos
|
||||
? Cesium.Cartographic.fromCartesian(toCartesian(pos)).height
|
||||
: (pos.height ?? 0);
|
||||
|
||||
function shapeGeometry(item) {
|
||||
const g = item.geometry;
|
||||
const extrudedHeight = item.extrudeHeight;
|
||||
switch (g.kind) {
|
||||
case 'polygon': {
|
||||
const [outer, ...holes] = g.rings.map((ring) => ring.map(toCartesian));
|
||||
const hierarchy = new Cesium.PolygonHierarchy(
|
||||
outer,
|
||||
holes.map((h) => new Cesium.PolygonHierarchy(h)),
|
||||
);
|
||||
return {
|
||||
fill: new Cesium.PolygonGeometry({
|
||||
polygonHierarchy: hierarchy,
|
||||
height: g.perPositionHeight ? undefined : heightOf(g.rings[0][0]),
|
||||
extrudedHeight,
|
||||
perPositionHeight: g.perPositionHeight,
|
||||
}),
|
||||
ring: outer,
|
||||
};
|
||||
}
|
||||
case 'ellipse':
|
||||
return {
|
||||
fill: new Cesium.EllipseGeometry({
|
||||
center: toCartesian(g.center),
|
||||
semiMajorAxis: g.semiMajor,
|
||||
semiMinorAxis: g.semiMinor,
|
||||
rotation: g.rotation ?? 0,
|
||||
height: heightOf(g.center),
|
||||
extrudedHeight,
|
||||
}),
|
||||
};
|
||||
case 'rectangle':
|
||||
return {
|
||||
fill: new Cesium.RectangleGeometry({
|
||||
rectangle: Cesium.Rectangle.fromDegrees(
|
||||
g.rect.west,
|
||||
g.rect.south,
|
||||
g.rect.east,
|
||||
g.rect.north,
|
||||
),
|
||||
height: g.height ?? 0,
|
||||
extrudedHeight,
|
||||
}),
|
||||
};
|
||||
case 'box': {
|
||||
const d = g.dimensions;
|
||||
return {
|
||||
fill: Cesium.BoxGeometry.fromDimensions({
|
||||
dimensions: new Cesium.Cartesian3(d.x, d.y, d.z),
|
||||
}),
|
||||
modelMatrix: Cesium.Transforms.eastNorthUpToFixedFrame(
|
||||
toCartesian(g.center),
|
||||
),
|
||||
};
|
||||
}
|
||||
default:
|
||||
throw new Error(`the scene renderer has no ${g.kind} shapes`);
|
||||
}
|
||||
}
|
||||
|
||||
function buildShapes(record) {
|
||||
const host = new Cesium.PrimitiveCollection();
|
||||
const outlines = new Cesium.PolylineCollection();
|
||||
for (const item of record.items.values()) {
|
||||
if (item.show === false) continue;
|
||||
if (
|
||||
item.fill &&
|
||||
typeof item.fill === 'object' &&
|
||||
!Array.isArray(item.fill)
|
||||
)
|
||||
throw new Error('the scene renderer fills shapes with colours only');
|
||||
const shape = shapeGeometry(item);
|
||||
const id = pickId(record, item);
|
||||
const solid =
|
||||
item.extrudeHeight !== undefined || item.geometry.kind === 'box';
|
||||
if (item.fill && !item.outlineOnly) {
|
||||
const color = toColor(item.fill);
|
||||
host.add(
|
||||
new Cesium.Primitive({
|
||||
geometryInstances: new Cesium.GeometryInstance({
|
||||
geometry: shape.fill,
|
||||
modelMatrix: shape.modelMatrix,
|
||||
attributes: {
|
||||
color: Cesium.ColorGeometryInstanceAttribute.fromColor(color),
|
||||
},
|
||||
id,
|
||||
}),
|
||||
appearance: new Cesium.PerInstanceColorAppearance({
|
||||
translucent: color.alpha < 1,
|
||||
closed: solid,
|
||||
// Flat shapes unlit: lit, their two triangles shade apart.
|
||||
flat: !solid,
|
||||
}),
|
||||
asynchronous: false,
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (item.outline && shape.ring)
|
||||
outlines.add({
|
||||
positions: [...shape.ring, shape.ring[0]],
|
||||
width: item.outlineWidth ?? 1,
|
||||
material: Cesium.Material.fromType('Color', {
|
||||
color: toColor(item.outline),
|
||||
}),
|
||||
id,
|
||||
});
|
||||
}
|
||||
host.add(outlines);
|
||||
replaceHost(record, host);
|
||||
}
|
||||
|
||||
function buildModels(record) {
|
||||
const host = new Cesium.PrimitiveCollection();
|
||||
replaceHost(record, host);
|
||||
// A model counts as loading until it is ready or fails, not when added;
|
||||
// each model settles once, and a failure is kept for `whenSettled`.
|
||||
host.loading = record.items.size;
|
||||
host.failed = 0;
|
||||
for (const item of record.items.values()) {
|
||||
let settled = false;
|
||||
const settle = (failed) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
host.loading -= 1;
|
||||
if (failed) host.failed += 1;
|
||||
scene.requestRender();
|
||||
};
|
||||
const o = item.orientation;
|
||||
const modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(
|
||||
toCartesian(item.pos),
|
||||
new Cesium.HeadingPitchRoll(o.heading, o.pitch, o.roll),
|
||||
);
|
||||
Cesium.Model.fromGltfAsync({
|
||||
url: item.url,
|
||||
modelMatrix,
|
||||
scale: item.scale ?? 1,
|
||||
minimumPixelSize: item.minPixelSize ?? 0,
|
||||
// All textures before ready, so a settled capture is the whole model.
|
||||
incrementallyLoadTextures: false,
|
||||
id: pickId(record, item),
|
||||
}).then(
|
||||
(model) => {
|
||||
if (record.host !== host) return model.destroy();
|
||||
host.add(model);
|
||||
model.readyEvent.addEventListener(() => {
|
||||
settle(false);
|
||||
for (const cb of record.listeners.ready.slice()) cb(item.id);
|
||||
});
|
||||
model.errorEvent.addEventListener(() => settle(true));
|
||||
scene.requestRender();
|
||||
},
|
||||
() => settle(true),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const draw = {
|
||||
icons: (spec) => makeSet('icons', { ...spec }, buildIcons),
|
||||
points: (spec) => makeSet('points', { ...spec }, buildPoints),
|
||||
lines: (spec) => makeSet('lines', { ...spec }, buildLines),
|
||||
shapes: (spec) => makeSet('shapes', { ...spec }, buildShapes),
|
||||
models: (spec) => makeSet('models', { ...spec }, buildModels),
|
||||
};
|
||||
|
||||
// --- Globe, camera, query --------------------------------------------------
|
||||
|
||||
const globe = {
|
||||
async setBase(base) {
|
||||
if (
|
||||
base.photoreal ||
|
||||
(base.terrain && base.terrain.kind !== 'ellipsoid') ||
|
||||
base.imagery?.length
|
||||
)
|
||||
throw new Error('the scene renderer draws a bare globe only');
|
||||
return { photoreal: { route: 'none' }, imagery: [], settled: true };
|
||||
},
|
||||
setGlobeVisible(visible) {
|
||||
scene.globe.show = visible;
|
||||
scene.requestRender();
|
||||
},
|
||||
sky: {
|
||||
get: () => ({ ...sky }),
|
||||
set(settings) {
|
||||
Object.assign(sky, settings);
|
||||
applySky();
|
||||
},
|
||||
},
|
||||
isSettled: () => scene.globe.tilesLoaded,
|
||||
};
|
||||
|
||||
const cameraPort = {
|
||||
setView(spec) {
|
||||
const d = spec.destination;
|
||||
if (d && 'rect' in d)
|
||||
throw new Error('the scene renderer takes position destinations only');
|
||||
const o = spec.orientation ?? {};
|
||||
camera.setView({
|
||||
...(d ? { destination: toCartesian(d) } : {}),
|
||||
orientation: {
|
||||
heading: o.heading ?? 0,
|
||||
pitch: o.pitch ?? -Math.PI / 2,
|
||||
roll: o.roll ?? 0,
|
||||
},
|
||||
});
|
||||
scene.requestRender();
|
||||
},
|
||||
pose() {
|
||||
const v = (c) => ({ x: c.x, y: c.y, z: c.z });
|
||||
return {
|
||||
position: v(camera.positionWC),
|
||||
direction: v(camera.directionWC),
|
||||
up: v(camera.upWC),
|
||||
right: v(camera.rightWC),
|
||||
heading: camera.heading,
|
||||
pitch: camera.pitch,
|
||||
roll: camera.roll,
|
||||
height: camera.positionCartographic.height,
|
||||
fovy: camera.frustum.fovy,
|
||||
aspectRatio: camera.frustum.aspectRatio,
|
||||
near: camera.frustum.near,
|
||||
far: camera.frustum.far,
|
||||
frameIsIdentity: Cesium.Matrix4.equals(
|
||||
camera.transform,
|
||||
Cesium.Matrix4.IDENTITY,
|
||||
),
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
const query = {
|
||||
pick(x, y) {
|
||||
const picked = scene.pick(new Cesium.Cartesian2(x, y));
|
||||
const id = picked?.id;
|
||||
return id && typeof id.set === 'string'
|
||||
? { ...id, surface: false }
|
||||
: null;
|
||||
},
|
||||
project(position) {
|
||||
const s = Cesium.SceneTransforms.worldToWindowCoordinates(
|
||||
scene,
|
||||
toCartesian(position),
|
||||
);
|
||||
return s ? { x: s.x, y: s.y } : null;
|
||||
},
|
||||
pickEllipsoid(x, y) {
|
||||
const c = camera.pickEllipsoid(new Cesium.Cartesian2(x, y));
|
||||
return c ? { x: c.x, y: c.y, z: c.z } : null;
|
||||
},
|
||||
};
|
||||
|
||||
// --- Renderer --------------------------------------------------------------
|
||||
|
||||
async function whenSettled({ timeoutMs = 20000 } = {}) {
|
||||
const models = () =>
|
||||
[...sets.values()].filter((r) => r.kind === 'models' && r.host);
|
||||
const end = performance.now() + timeoutMs;
|
||||
let quiet = 0;
|
||||
while (performance.now() < end) {
|
||||
await nextPostRender();
|
||||
if (models().some((r) => r.host.failed > 0))
|
||||
return { state: 'failed', blockers: ['models'] };
|
||||
const loading = models().some((r) => r.host.loading > 0);
|
||||
quiet = scene.globe.tilesLoaded && !loading ? quiet + 1 : 0;
|
||||
if (quiet >= 3) return { state: 'settled', blockers: [] };
|
||||
}
|
||||
const blockers = [];
|
||||
if (!scene.globe.tilesLoaded) blockers.push('globe');
|
||||
if (models().some((r) => r.host.loading > 0)) blockers.push('models');
|
||||
return { state: 'timeout', blockers };
|
||||
}
|
||||
|
||||
return {
|
||||
id: 'cesium',
|
||||
contractVersion: '0',
|
||||
capabilities,
|
||||
canvas: scene.canvas,
|
||||
frame,
|
||||
globe,
|
||||
draw,
|
||||
camera: cameraPort,
|
||||
query,
|
||||
async capture({ type = 'png' } = {}) {
|
||||
scene.render(widget.clock.currentTime);
|
||||
return new Promise((resolve) =>
|
||||
scene.canvas.toBlob(resolve, `image/${type}`),
|
||||
);
|
||||
},
|
||||
whenSettled,
|
||||
resize: () => widget.resize(),
|
||||
destroy() {
|
||||
for (const off of removers) off();
|
||||
widget.destroy();
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user