Draw transit vehicles through the renderer ports, off the Cesium list

Transit drew its vehicles into two Cesium billboard collections and culled,
turned and floored them through the Cesium scene. It now draws them as two
draw-port icon sets, still and moving, registered with the sprite order by
set id in the same places. Each marker is a plain record; a frame writes the
changed ones in one upsert per set, and markers taken out in one task go in
one removal. Clicks read the draw-port hit.

Visibility uses the query port's horizon test and the frustum planes of the
camera port's pose. Icons stay turned in screen space by an exact projection
through the query port, the same answer the Cesium helper gave. The flat
globe is told from terrain by asking the height service once per surface
change; floors are read again when the answer flips. The mesh floor sampler
sees through the layer's sets by id. The camera pose and surface version are
read once per task.

The Cesium draw port now moves and turns an icon without restyling it. The
GL upload probe moves to src/render/cesium/uploadProbe.js, which the QA
scripts hand to the frame probe; the QA scripts read the billboards drawn for
each set.
This commit is contained in:
Sameh Khamis committed 2026-11-08 21:44:22 -08:00
1 parent 00ae80908c
commit 347427f83d
24 files changed
+1618 -452

No files matched your search

+27 -14
View File
@@ -966,21 +966,29 @@ export function createCesiumDraw({
};
}
/** Whether `item` draws as `style` (`iconStyleOf`) says, but its position. */
function sameIconStyle(style, record, item) {
const 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;
};
const sameCurve = (a, c) =>
!a || !c
? !a && !c
: a[0] === c.near &&
/** 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] &&
@@ -1000,7 +1008,7 @@ export function createCesiumDraw({
sameCurve(style.scaleByDistance, item.scaleByDistance) &&
sameCurve(style.fadeByDistance, item.fadeByDistance) &&
style.rotationMode === item.rotationMode &&
style.rotation === item.rotation &&
(item.rotationMode === 'screen' || style.rotation === item.rotation) &&
style.course === item.course
);
}
@@ -1009,7 +1017,8 @@ export function createCesiumDraw({
const movedAxis = new Cesium.Cartesian3();
/**
* Move a drawn icon whose look is unchanged (`sameIconStyle`): its
* position, the course rotation that follows it, and its pick record.
* 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;
@@ -1024,6 +1033,10 @@ export function createCesiumDraw({
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);
+121
View File
@@ -2883,6 +2883,13 @@ test('icons that only move are moved on the billboards they have, and still rest
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) =>
@@ -6160,3 +6167,117 @@ test('a clamped ellipse filled with a radial rim drapes its material, finely cut
assert.deepEqual([...shows.show], [0]);
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();
});