Merge branch 'sameh/renderer-transit' into sameh/renderer-seam

# Conflicts:
#	src/render/cesium/draw.test.mjs
This commit is contained in:
Sameh Khamis committed 2026-11-09 01:00:43 -08:00
commit 9d4e438e63
27 files changed
+2016 -465

No files matched your search

+157
View File
@@ -949,6 +949,113 @@ export function createCesiumDraw({
return look;
}
/**
* What a billboard draws of an icon item but its position, image, `meta`
* and `lodGroup`, as plain values: the course rotation follows the
* position, so it is written with it.
*/
function iconStyleOf(record, item) {
const color = item.color;
const curve = (c) =>
c ? [c.near, c.nearValue, c.far, c.farValue] : undefined;
return {
width: item.size[0],
height: item.size[1],
scale: item.scale,
color:
Array.isArray(color) || ArrayBuffer.isView(color) ? [...color] : color,
alpha: item.alpha,
focus: item.focus,
anchor: item.anchor,
disableDepthTest: item.disableDepthTest,
heightMode: item.height,
clampTo: record.spec.clampTo,
show: item.show,
displayRange: item.displayRange ? [...item.displayRange] : undefined,
scaleByDistance: curve(item.scaleByDistance),
fadeByDistance: curve(item.fadeByDistance),
rotationMode: item.rotationMode,
rotation: item.rotation,
course: item.course,
};
}
/** Two lists, or two missing ones, alike element by element. */
function sameList(a, b) {
if (!a || !b) return a === b;
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
return true;
}
/** A curve as `iconStyleOf` keeps it, and one as an item gives it. */
function sameCurve(a, c) {
return !a || !c
? !a && !c
: a[0] === c.near &&
a[1] === c.nearValue &&
a[2] === c.far &&
a[3] === c.farValue;
}
/**
* Whether `item` draws as `style` (`iconStyleOf`) says, but its position
* and, turned on screen, its rotation (`moveIcon` writes both).
*/
function sameIconStyle(style, record, item) {
const color = item.color;
return (
style.width === item.size[0] &&
style.height === item.size[1] &&
style.scale === item.scale &&
(Array.isArray(color) || ArrayBuffer.isView(color)
? Array.isArray(style.color) && sameList(style.color, color)
: style.color === color) &&
style.alpha === item.alpha &&
style.focus === item.focus &&
style.anchor === item.anchor &&
style.disableDepthTest === item.disableDepthTest &&
style.heightMode === item.height &&
style.clampTo === record.spec.clampTo &&
style.show === item.show &&
sameList(style.displayRange, item.displayRange) &&
sameCurve(style.scaleByDistance, item.scaleByDistance) &&
sameCurve(style.fadeByDistance, item.fadeByDistance) &&
style.rotationMode === item.rotationMode &&
(item.rotationMode === 'screen' || style.rotation === item.rotation) &&
style.course === item.course
);
}
const movedEnu = new Cesium.Matrix4();
const movedAxis = new Cesium.Cartesian3();
/**
* Move a drawn icon whose look is unchanged (`sameIconStyle`): its
* position, the course rotation that follows it or the screen rotation
* it is given, and its pick record.
*/
function moveIcon(record, entry, item) {
const { billboard, placed } = entry;
const position = toCartesianInto(item.pos, placed.position);
billboard.position = position;
if (item.rotationMode === 'course') {
Cesium.Transforms.eastNorthUpToFixedFrame(position, undefined, movedEnu);
// The billboard copies the axis it is given.
billboard.alignedAxis = Cesium.Matrix4.multiplyByPointAsVector(
movedEnu,
Cesium.Cartesian3.UNIT_Y,
movedAxis,
);
}
if (item.rotationMode === 'screen') {
billboard.rotation = item.rotation ?? 0;
entry.style.rotation = item.rotation;
}
billboard.id.meta = item.meta;
billboard.id.lodGroup = item.lodGroup;
billboard.show = shownNow(record, placed);
}
/** Give a billboard its image: a generated one under its key. */
function setIconImage(record, billboard, image) {
if (isKeyed(image)) billboard.setImage(image.key, image.canvas);
@@ -971,6 +1078,8 @@ export function createCesiumDraw({
placed,
image: imageKeyOf(image),
canvas: image?.canvas,
// What it is drawn as, for `patchIcon`'s quick path.
style: iconStyleOf(record, item),
});
}
@@ -982,6 +1091,17 @@ export function createCesiumDraw({
*/
function patchIcon(record, entry, item, image) {
const { billboard, placed } = entry;
// Drawn as it was but for where it is: only that is written, so a set
// whose icons move every frame costs little more than the moves.
if (
entry.style &&
imageKeyOf(image) === entry.image &&
sameIconStyle(entry.style, record, item)
) {
moveIcon(record, entry, item);
return;
}
entry.style = iconStyleOf(record, item);
const position = toCartesian(item.pos);
const look = iconLook(record, item, position);
// Sized first: a new SVG image rasterises at the billboard's size.
@@ -1000,6 +1120,42 @@ export function createCesiumDraw({
billboard.show = shownNow(record, placed);
}
/**
* `syncIcons`' quick case: every item is drawn, every touched one keeps
* the image it is drawn with, and nothing drawn has gone, so each touched
* one is changed where it is and nothing is added, removed or reordered.
* False, changing nothing, otherwise.
* @param {object} record
* @param {readonly string[]} touched
*/
function patchDrawnIcons(record, touched) {
const drawn = record.billboards;
// Nothing to remove: every item drawn, and nothing drawn but items.
if (
!Array.isArray(touched) ||
touched.length === 0 ||
drawn.size !== record.items.size
)
return false;
for (const id of touched) {
const entry = drawn.get(id);
const item = record.items.get(id);
if (!entry || !item) return false;
const image = iconImage(record, item.image);
if (
image === undefined ||
imageKeyOf(image) !== entry.image ||
(isKeyed(image) && image.canvas !== entry.canvas)
)
return false;
}
for (const id of touched) {
const item = record.items.get(id);
patchIcon(record, drawn.get(id), item, iconImage(record, item.image));
}
return true;
}
/**
* Bring an icon set's billboards in line with its items without a
* rebuild: those of items gone (or whose image is gone) are removed, the
@@ -1029,6 +1185,7 @@ export function createCesiumDraw({
host.isDestroyed()
)
return false;
if (!reordered && patchDrawnIcons(record, touched)) return ICONS_KEPT;
const images = new Map();
for (const id of touched) {
const item = record.items.get(id);
+213 -4
View File
@@ -1963,7 +1963,10 @@ test('the installations layer draws its site dots, outlines and selected dot on
rendering.renderRecords();
const after = billboards(dotsId);
assert.equal(after.length, 2);
assert.ok(after.every((dot, i) => dot === dots[i]), 'the same billboards');
assert.ok(
after.every((dot, i) => dot === dots[i]),
'the same billboards',
);
assert.equal(after.find((dot) => dot.id.id === 'b').show, false);
assert.equal(after.find((dot) => dot.id.id === 'a').show, true);
const [selected] = billboards(state.sets.selected.id);
@@ -2020,9 +2023,7 @@ test('the satellites layer draws its dots as one point collection and its rings
assert.equal(points.get(0), iss);
assert.equal(iss.position.x, 6.9e6);
withImageClasses(() =>
rendering._showOrbitPath(25544, TRACKED_ORBIT_COLOR),
);
withImageClasses(() => rendering._showOrbitPath(25544, TRACKED_ORBIT_COLOR));
const all = (host) =>
host instanceof Cesium.PrimitiveCollection
? Array.from({ length: host.length }, (_, i) => all(host.get(i))).flat()
@@ -6460,3 +6461,211 @@ test('the recent imagery box preview drags its ground fill and dashed edge frame
);
assert.equal(await destroyed, 0, 'both sets let go');
});
test('icons that only move are moved on the billboards they have, and still restyle', () => {
const scene = fakeScene();
const part = createCesiumDraw({
scene,
report: (error) => assert.fail(error),
});
const set = part.port.icons({ id: 'icons:moving' });
const icon = (lon, extra = {}) => ({
id: 'a',
pos: Cesium.Cartesian3.fromDegrees(lon, 10, 20),
image: 'a.png',
size: [20, 20],
color: [1, 0, 0, 1],
rotationMode: 'screen',
rotation: 0.25,
disableDepthTest: Infinity,
meta: { lon },
...extra,
});
set.set([icon(0), { ...icon(5), id: 'b' }]);
const [host] = scene.added;
const a = host.get(0);
const version = part.version();
// Only moved: its look is not written again.
let restyles = 0;
const width = Object.getOwnPropertyDescriptor(
Object.getPrototypeOf(a),
'width',
);
Object.defineProperty(a, 'width', {
get: width.get,
set(value) {
restyles += 1;
width.set.call(this, value);
},
configurable: true,
});
for (const lon of [1, 2, 3]) set.upsert([icon(lon)]);
assert.equal(restyles, 0, 'moved, not restyled');
assert.equal(scene.added[0], host, 'the same collection');
assert.equal(host.get(0), a, 'the same billboard');
const at = Cesium.Cartographic.fromCartesian(a.position);
assert.ok(Math.abs(Cesium.Math.toDegrees(at.longitude) - 3) < 1e-9);
assert.ok(Math.abs(at.height - 20) < 1e-6);
assert.equal(a.rotation, 0.25);
assert.ok(Cesium.Color.equals(a.color, Cesium.Color.RED));
assert.deepEqual(a.id.meta, { lon: 3 }, 'picks read the latest meta');
assert.equal(part.version(), version, 'nothing added or removed');
// A look that changes after moves is drawn, then moves go on from it.
set.upsert([icon(3, { color: [0, 1, 0, 1], rotation: 1, size: [30, 30] })]);
assert.equal(restyles, 1, 'restyled once');
assert.ok(Cesium.Color.equals(a.color, Cesium.Color.LIME));
assert.equal(a.rotation, 1);
assert.equal(a.width, 30);
set.upsert([icon(4, { color: [0, 1, 0, 1], rotation: 1, size: [30, 30] })]);
assert.ok(Cesium.Color.equals(a.color, Cesium.Color.LIME));
assert.equal(a.width, 30);
const moved = Cesium.Cartographic.fromCartesian(a.position);
assert.ok(Math.abs(Cesium.Math.toDegrees(moved.longitude) - 4) < 1e-9);
// Turning on screen as it moves is written with the move, not restyled.
set.upsert([icon(5, { color: [0, 1, 0, 1], rotation: 2, size: [30, 30] })]);
assert.equal(restyles, 1, 'a screen turn is not a restyle');
assert.equal(a.rotation, 2);
set.upsert([icon(5, { color: [0, 1, 0, 1], rotation: 2, size: [31, 30] })]);
assert.equal(restyles, 2, 'a new size still restyles');
assert.equal(a.rotation, 2);
// A course-turned icon's axis follows where it moves.
const course = (lon) =>
icon(lon, { rotationMode: 'course', course: 90, rotation: undefined });
set.upsert([course(4)]);
set.upsert([course(60)]);
const north = Cesium.Matrix4.multiplyByPointAsVector(
Cesium.Transforms.eastNorthUpToFixedFrame(
Cesium.Cartesian3.fromDegrees(60, 10, 20),
),
Cesium.Cartesian3.UNIT_Y,
new Cesium.Cartesian3(),
);
assert.ok(Cesium.Cartesian3.equalsEpsilon(a.alignedAxis, north, 1e-12));
assert.equal(a.rotation, -Cesium.Math.toRadians(90));
// Hidden in place, while it moves.
set.upsert([{ ...course(61), show: false }]);
assert.equal(a.show, false);
set.upsert([{ ...course(62), show: false }]);
assert.equal(a.show, false);
set.upsert([course(63)]);
assert.equal(a.show, true);
set.dispose();
});
test('the transit layer draws its vehicles as two icon sets, moving a marker between them, on a viewer of its own', async () => {
const { createRendering } = await import('../../layers/transit/rendering.js');
const { createState } = await import('../../layers/transit/state.js');
const scene = fakeScene();
const viewer = { scene };
const noop = () => {};
const registered = [];
const services = {
overlays: {
setOverlayEntries: noop,
setOverlaySourceVisible: noop,
clearOverlaySource: noop,
},
render: {
governorRequestRender: noop,
holdContinuousRender: noop,
releaseContinuousRender: noop,
},
sprites: {
registerSpriteSet: (layerId, setId) => {
const entry = { drawSetId: setId };
registered.push([layerId, setId]);
return entry;
},
unregisterSpriteCollection: noop,
},
};
const state = createState({ services });
state._viewer = viewer;
const rendering = createRendering({
state,
services,
parts: { height: { nearGround: () => false }, trails: { update: noop } },
});
rendering.createMarkers(viewer);
const [stillId, movingId] = rendering.setIds();
assert.deepEqual(registered, [
['transit', stillId],
['transit-motion', movingId],
]);
const hostOf = (setId) => scene.added.find((h) => h.drawSetId === setId);
assert.ok(hostOf(stillId) instanceof Cesium.BillboardCollection);
assert.ok(hostOf(movingId) instanceof Cesium.BillboardCollection);
assert.equal(hostOf(stillId).show, false, 'hidden until the layer shows');
rendering.showMarkers();
const at = Cesium.Cartesian3.fromDegrees(-71.06, 42.36, 12);
const entry = { key: 'mbta:bus', mode: 'bus', toCart: at };
rendering.addMarker(entry.key, entry);
entry.marker.show = true;
entry.marker.rotation = 0.5;
rendering.markDirty(entry);
rendering.flushMarkers();
const still = hostOf(stillId);
assert.equal(still.length, 1);
const billboard = still.get(0);
assert.equal(billboard.id.set, stillId);
assert.equal(billboard.id.id, entry.key);
assert.deepEqual(billboard.id.meta, { layer: 'transit', key: entry.key });
assert.ok(Cesium.Cartesian3.equalsEpsilon(billboard.position, at, 0, 1e-6));
assert.equal(billboard.rotation, 0.5);
assert.equal(billboard.show, true);
assert.equal(billboard.disableDepthTestDistance, Number.POSITIVE_INFINITY);
assert.equal(billboard.width, entry.marker.width);
assert.equal(
rendering.setNameOf(
drawHitOf({ primitive: billboard, id: billboard.id }).set,
),
'still',
);
// Moving it moves the billboard it has.
entry.marker.position.z += 10;
rendering.markDirty(entry);
rendering.flushMarkers();
assert.equal(still.get(0), billboard);
assert.ok(Math.abs(billboard.position.z - (at.z + 10)) < 1e-6);
// Starting to move takes it to the moving set and out of the still one.
entry.animated = true;
rendering.markDirty(entry);
rendering.flushMarkers();
const moving = hostOf(movingId);
assert.equal(moving.length, 1);
assert.equal(moving.get(0).id.id, entry.key);
assert.equal(moving.get(0).id.set, movingId);
const stillNow = hostOf(stillId);
const keysInStill = [];
for (let i = 0; i < stillNow.length; i += 1)
keysInStill.push(stillNow.get(i).id?.id);
assert.deepEqual(keysInStill, [], 'no longer drawn in the still set');
// Markers taken out in one task go in one removal, at its end.
const other = { key: 'mbta:tram', mode: 'tram', toCart: at };
rendering.addMarker(other.key, other);
other.animated = true;
rendering.markDirty(other);
rendering.flushMarkers();
assert.equal(moving.length, 2);
const removals = [];
const remove = state._movingIcons.remove;
state._movingIcons.remove = function (ids) {
removals.push([...ids]);
return remove.call(this, ids);
};
rendering.removeMarker(entry);
rendering.removeMarker(other);
assert.equal(removals.length, 0, 'not yet');
await Promise.resolve();
assert.deepEqual(removals, [[entry.key, other.key]]);
assert.equal(hostOf(movingId).length, 0);
rendering.destroyMarkers(viewer);
assert.equal(hostOf(stillId), undefined);
assert.equal(hostOf(movingId), undefined);
});
+91
View File
@@ -0,0 +1,91 @@
/**
* @module render/cesium/uploadProbe
* @description A QA probe: the bytes the WebGL context is handed
* (`bufferData`, `bufferSubData`) while the hosts of some draw sets update,
* frame by frame. A layer's frame probe takes it as its `uploads` (the
* transit layer's `_measureTransitFramesForTest`), so the layer itself
* stays off the default renderer. Diagnostics only: it wraps the context's
* methods and the hosts' `update` until `stop()`.
*/
import { webglContext } from './internals.js';
/**
* Count the bytes uploaded while the hosts of `setIds` update. Hosts are
* found by the `drawSetId` the draw port names them with, again at each
* `reset()`, so a host a rebuild puts in a set's place is counted too.
* @param {object} viewer A Cesium viewer.
* @param {readonly string[]} setIds
* @returns {{reset: () => void, bytes: () => number, stop: () => void}}
*/
export function countDrawSetUploads(viewer, setIds) {
const scene = viewer.scene;
const ids = new Set(setIds);
const gl = webglContext(scene);
let active = false;
let upload = 0;
const restores = [];
if (gl) {
for (const name of ['bufferData', 'bufferSubData']) {
const original = gl[name];
gl[name] = function (...args) {
if (active) {
const value = args[name === 'bufferData' ? 1 : 2];
const offset = args[3] || 0;
const length = args[4];
upload +=
typeof value === 'number'
? value
: value
? length !== undefined
? length * (value.BYTES_PER_ELEMENT || 1)
: value.byteLength - offset * (value.BYTES_PER_ELEMENT || 1)
: 0;
}
return original.apply(this, args);
};
restores.push(() => {
gl[name] = original;
});
}
}
// Hosts wrapped so far, and how to give each its own `update` back (an
// inherited one by deleting the wrapper).
const wrapped = new Map();
function wrapHosts() {
const primitives = scene.primitives;
for (let i = 0; i < (primitives?.length ?? 0); i++) {
const host = primitives.get(i);
if (!ids.has(host?.drawSetId) || wrapped.has(host)) continue;
const original = host.update;
const own = Object.prototype.hasOwnProperty.call(host, 'update');
host.update = function (frameState) {
active = true;
try {
return original.call(this, frameState);
} finally {
active = false;
}
};
wrapped.set(host, { original, own, wrapper: host.update });
}
}
wrapHosts();
return {
reset() {
upload = 0;
wrapHosts();
},
bytes: () => upload,
stop() {
for (const restore of restores) restore();
restores.length = 0;
for (const [host, { original, own, wrapper }] of wrapped) {
// A host whose update was replaced meanwhile keeps the new one.
if (host.update !== wrapper) continue;
if (own) host.update = original;
else delete host.update;
}
wrapped.clear();
},
};
}
+105
View File
@@ -0,0 +1,105 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { countDrawSetUploads } from './uploadProbe.js';
/** A scene whose context counts nothing itself, and hosts that upload. */
function sceneWith(hosts) {
const gl = {
bufferData() {},
bufferSubData() {},
};
const scene = {
context: { _gl: gl },
primitives: {
get: (i) => hosts[i],
get length() {
return hosts.length;
},
},
};
return { scene, gl };
}
function uploader(gl, drawSetId, bytes) {
return {
drawSetId,
update() {
gl.bufferData(0, new Float32Array(bytes / 4), 0);
gl.bufferSubData(0, 0, new Uint8Array(bytes), 2, 3);
},
};
}
test('only the named sets’ uploads count, from each reset, and stop gives everything back', () => {
const hosts = [];
const { scene, gl } = sceneWith(hosts);
const ownBufferData = gl.bufferData;
const mine = uploader(gl, 'transit:1:still', 16);
const other = uploader(gl, 'traffic:1', 64);
hosts.push(mine, other);
const ownUpdate = mine.update;
const uploads = countDrawSetUploads({ scene }, [
'transit:1:still',
'transit:1:moving',
]);
uploads.reset();
mine.update();
other.update();
// 16 bytes of data, then 3 of the 16-byte view from element 2.
assert.equal(uploads.bytes(), 16 + 3);
// A host that comes later (a rebuild) is found at the next reset.
const later = uploader(gl, 'transit:1:moving', 8);
const laterUpdate = later.update;
hosts.push(later);
uploads.reset();
assert.equal(uploads.bytes(), 0);
later.update();
assert.equal(uploads.bytes(), 8 + 3);
// An update replaced while counting is the one kept.
const replaced = function () {};
mine.update = replaced;
uploads.stop();
assert.equal(gl.bufferData, ownBufferData);
assert.equal(later.update, laterUpdate, 'its own update back');
assert.equal(mine.update, replaced, 'an update replaced meanwhile stays');
assert.notEqual(mine.update, ownUpdate);
// Nothing counts once stopped.
later.update();
assert.equal(uploads.bytes(), 8 + 3);
});
test('an inherited update is given back by removing the wrapper', () => {
const hosts = [];
const { scene, gl } = sceneWith(hosts);
class Host {
update() {
gl.bufferData(0, 4, 0);
}
}
const host = Object.assign(new Host(), { drawSetId: 's' });
hosts.push(host);
const uploads = countDrawSetUploads({ scene }, ['s']);
assert.ok(Object.hasOwn(host, 'update'));
uploads.reset();
host.update();
assert.equal(uploads.bytes(), 4);
uploads.stop();
assert.equal(Object.hasOwn(host, 'update'), false);
assert.equal(host.update, Host.prototype.update);
});
test('a size-only allocation counts its size', () => {
const hosts = [];
const { scene, gl } = sceneWith(hosts);
hosts.push({
drawSetId: 's',
update() {
gl.bufferData(0, 100, 0);
},
});
const uploads = countDrawSetUploads({ scene }, ['s']);
uploads.reset();
hosts[0].update();
assert.equal(uploads.bytes(), 100);
uploads.stop();
});
-1
View File
@@ -8,7 +8,6 @@
"src/cyberSonarScene.js",
"src/data/geoImportPins.js",
"src/layers/traffic/rendering.js",
"src/layers/transit/rendering.js",
"src/layers/weather/shellRendering.js",
"src/layers/wind/gpuRendering.js",
"src/layers/wind/relief.js",
+6
View File
@@ -120,6 +120,12 @@ const DRAW_FROM_CALLER = new Set([
// Its pins, footprints and outlines draw through `drawOf`; its horizon
// cull reads the camera and frame ports.
'./layers/ghostmaps',
// Its moving and still vehicle icons draw through `drawOf`; it projects,
// culls, follows and samples floors through the ports.
'./layers/transit',
// Not Cesium-free (its legacy mesh floor sampler is), but its layer draws
// and reads the view through the ports alone.
'./layers/transit/standalone',
]);
/**