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

+90
View File
@@ -747,3 +747,93 @@ test('the perspective helper is continuous through the sub-pixel band, not a 23
`exact alone at ${s.separation.toFixed(2)} px`,
);
});
test('the port-side perspective rotation matches the Cesium one through the whole band', async () => {
// The transit layer turns its icons with `perspectiveCourseRotation`, which
// projects through the query port and blends with the camera-port basis;
// given the same projection and basis it must answer as the scene helper.
const { perspectiveCourseRotation } = await import('./screenRotation.js');
let compared = 0;
for (let altitude = 500; altitude <= 600_000; altitude *= 1.05) {
const scene = obliqueScene(altitude, 45);
const project = (p) => {
const s = Cesium.SceneTransforms.worldToWindowCoordinates(
scene,
new Cesium.Cartesian3(p.x, p.y, p.z),
);
return s ? { x: s.x, y: s.y } : null;
};
const basis = { right: scene.camera.rightWC, up: scene.camera.upWC };
for (const [east, north] of [
[0.7, 1.2],
[-0.4, 0.9],
[0, 0.5],
[0, -2],
])
for (const course of [0, 37, 90, 181, 300]) {
const contact = Cesium.Cartesian3.fromDegrees(
(east * altitude) / 111_320,
(north * altitude) / 111_320,
0,
);
for (const previous of [null, 0.25]) {
const expected = perspectiveProjectedRotation(
scene,
contact,
course,
previous,
);
const actual = perspectiveCourseRotation(
project,
basis,
{ x: contact.x, y: contact.y, z: contact.z },
course,
previous,
);
if (expected === null || actual === null)
assert.equal(actual, expected);
else
assert.ok(
Math.abs(actual - expected) < 1e-9,
`${altitude.toFixed(0)} m, course ${course}: ${actual} vs ${expected}`,
);
compared += 1;
}
}
}
assert.ok(compared > 1000);
assert.equal(perspectiveCourseRotation(null, null, null, 0, 0.5), 0.5);
});
test('the port-side perspective rotation probes along the east-north frame, poles included', async () => {
const { perspectiveCourseRotation, PERSPECTIVE_PROBE_M: probeM } =
await import('./screenRotation.js');
const { eastNorthUp, frameVector } = await import('../geo/frames.js');
const { fromDegrees } = await import('../geo/ellipsoid.js');
for (const position of [
fromDegrees(-71.06, 42.36, 12),
fromDegrees(150, -60, 0),
{ x: 0, y: 0, z: 6_356_752.314245 },
])
for (const course of [0, 45, 200]) {
const seen = [];
perspectiveCourseRotation(
(p) => (seen.push({ x: p.x, y: p.y, z: p.z }), { x: 0, y: 0 }),
null,
position,
course,
);
const rad = (course * Math.PI) / 180;
const forward = frameVector(eastNorthUp(position), {
x: Math.sin(rad) * probeM,
y: Math.cos(rad) * probeM,
z: 0,
});
const [, ahead] = seen;
for (const axis of ['x', 'y', 'z'])
assert.ok(
Math.abs(ahead[axis] - (position[axis] + forward[axis])) < 1e-6,
`${axis} at course ${course}`,
);
}
});
+115
View File
@@ -6,6 +6,7 @@
* render noise. See `iconOrientation.js` for why the course is projected
* onto the camera basis. No engine.
*/
import { WGS84 } from '../geo/ellipsoid.js';
import { eastNorthUp, frameVector } from '../geo/frames.js';
import { dot, toRadians, vec } from '../geo/vector.js';
@@ -79,3 +80,117 @@ export function stabilizeScreenRotation(
);
return Math.abs(delta) < Math.max(0, deadbandRad) ? previous : next;
}
/**
* Metres ahead `perspectiveCourseRotation` probes: short, so the projection
* is local to the contact (a two kilometre probe on a bus seen from six
* hundred metres would sample a part of the view the bus is nowhere near).
*/
export const PERSPECTIVE_PROBE_M = 30;
/**
* Probe separations (CSS px) across which the exact and camera-basis
* answers are blended: at or below the floor the camera-basis answer alone,
* at or above the ceiling the exact one.
*/
export const PERSPECTIVE_BLEND_MIN_PX = 0.5;
export const PERSPECTIVE_BLEND_MAX_PX = 3;
/**
* The icon rotation (as `courseScreenRotation`'s) that points a SURFACE
* contact along `courseDeg`, projected exactly, perspective division
* included: the contact and a point `PERSPECTIVE_PROBE_M` ahead of it are
* projected through `project` (a query port's `project`) and the angle
* between them read on screen. The camera-basis answer is orthographic,
* exact at the view's centre and wrong by twenty-odd degrees for a
* street-level contact away from it in an oblique view. Where either point
* cannot be projected it falls back to the camera-basis answer; below a few
* pixels of separation, where projection noise rules the exact one, the
* two are blended the short way round, so the rotation is continuous.
* @param {(p: {x: number, y: number, z: number}) => ({x: number, y: number}|null)} project
* @param {{right?: object, up?: object}|null} basis The camera's axes.
* @param {{x: number, y: number, z: number}|null} position ECEF.
* @param {number} courseDeg Degrees clockwise from north.
* @param {number|null} [previous] Kept when the rotation is unknown.
* @returns {number|null}
*/
export function perspectiveCourseRotation(
project,
basis,
position,
courseDeg,
previous = null,
) {
if (!project || !position || !Number.isFinite(courseDeg)) return previous;
if (!courseProbeInto(position, courseDeg, probeScratch)) {
// At a pole, through the frame `eastNorthUp` defines there.
const courseRad = toRadians(courseDeg);
const forward = frameVector(
eastNorthUp(position),
vec(
Math.sin(courseRad) * PERSPECTIVE_PROBE_M,
Math.cos(courseRad) * PERSPECTIVE_PROBE_M,
0,
),
);
probeScratch.x = position.x + forward.x;
probeScratch.y = position.y + forward.y;
probeScratch.z = position.z + forward.z;
}
const at = project(position);
const atX = at?.x;
const atY = at?.y;
const ahead = at ? project(probeScratch) : null;
if (!at || !ahead)
return courseScreenRotation(basis, position, courseDeg, previous);
const dx = ahead.x - atX;
const dy = ahead.y - atY;
const separation = Math.hypot(dx, dy);
// Window y grows downward; rotation 0 points the icon screen-up.
const exact = Math.atan2(-dx, -dy);
if (separation >= PERSPECTIVE_BLEND_MAX_PX) return exact;
const camera = courseScreenRotation(basis, position, courseDeg, previous);
if (!Number.isFinite(camera)) return exact;
if (separation <= PERSPECTIVE_BLEND_MIN_PX) return camera;
const weight =
(separation - PERSPECTIVE_BLEND_MIN_PX) /
(PERSPECTIVE_BLEND_MAX_PX - PERSPECTIVE_BLEND_MIN_PX);
const delta = Math.atan2(Math.sin(exact - camera), Math.cos(exact - camera));
return camera + delta * weight;
}
// The probe point, reused: the rotation pass visits every visible contact.
const probeScratch = { x: 0, y: 0, z: 0 };
/**
* The point `PERSPECTIVE_PROBE_M` ahead of `position` along `courseDeg` on
* its east-north plane (as `eastNorthUp` and `frameVector` give it), into
* `out`, allocating nothing. False at a pole, where `eastNorthUp` picks
* its own east.
*/
function courseProbeInto(position, courseDeg, out) {
const { x, y, z } = position;
// `eastNorthUp`'s own test for a pole.
if (Math.abs(x) <= 1e-14 && Math.abs(y) <= 1e-14) return false;
const eastLength = Math.hypot(x, y);
const o = WGS84.oneOverRadiiSquared;
let ux = x * o.x,
uy = y * o.y,
uz = z * o.z;
const upLength = Math.sqrt(ux * ux + uy * uy + uz * uz);
ux /= upLength;
uy /= upLength;
uz /= upLength;
const ex = -y / eastLength,
ey = x / eastLength;
// North is up × east (east has no z).
const nx = -uz * ey,
ny = uz * ex,
nz = ux * ey - uy * ex;
const courseRad = toRadians(courseDeg);
const e = Math.sin(courseRad) * PERSPECTIVE_PROBE_M;
const n = Math.cos(courseRad) * PERSPECTIVE_PROBE_M;
out.x = x + ex * e + nx * n;
out.y = y + ey * e + ny * n;
out.z = z + nz * n;
return true;
}