Draw the traffic dots through the draw port and read the road surface through renderer ports

The traffic layer's vehicle dots are now a points set on the draw port,
moved, added to and taken from in place each frame, instead of a Cesium
point collection. Road surface heights and readiness read the query,
camera, frame and globe ports and the heights service instead of the
scene; the surface identity and "any tileset drawn" checks, which the
contract cannot answer, move to rendering.js with the heat lines.

animation.js, style.js and surface.js leave the Cesium allow-list.

The Cesium draw port compares a moved point's look without allocating:
colours by index, and a frozen colour or curve of plain data by identity,
so a thousand dots moved every frame cost about what direct writes did.
The traffic QA scripts read the dots' draw items.
This commit is contained in:
Sameh Khamis committed 2026-11-07 23:16:11 -08:00
1 parent d9db5199ed
commit bbd6049e21
24 files changed
+872 -467

No files matched your search

+60 -64
View File
@@ -1,7 +1,7 @@
import { DOT_FADE_MS } from './retention.js';
import { flowBucket, flowSpeedScale } from '../../data/trafficFlowStyle.js';
import { presetDotOutline } from '../../data/trafficPresetStyle.js';
import * as Cesium from 'cesium';
import { lerpInto } from '../../geo/vector.js';
import { findFrame } from '../../render/rendererOf.js';
import { queuePlatoons, locateAlongRoad } from '../../data/trafficQueue.js';
import {
SPEED_MPS,
@@ -10,8 +10,6 @@ import {
CREEP_MOVE_MS,
CREEP_STOP_MS,
CREEP_BURST,
HEAT_JAM_BASE_ALPHA,
HEAT_JAM_PULSE_ALPHA,
} from './policy.js';
export function createAnimation({
@@ -20,13 +18,24 @@ export function createAnimation({
parts,
source,
}) {
/** Scratch point for the per-frame lerp, so a frame allocates nothing. */
const scratchLerp = new Cesium.Cartesian3();
/** The dots' point items, gathered once a frame for one upsert. */
const frameItems = [];
/**
* Send every dot's point item to the draw set: the set moves and
* restyles the points it has, and adds the new ones, in place.
*/
function flushDots() {
const dots = layerState._dots;
frameItems.length = dots.length;
for (let i = 0; i < dots.length; i++) frameItems[i] = dots[i].point;
layerState._dotSet?.upsert(frameItems);
}
function syncRenderDemand() {
const needed = Boolean(
layerState._enabled &&
(layerState._dots.length || layerState._heatJamPrim),
(layerState._dots.length || parts.rendering.heatPulsing?.()),
);
// Height easing only runs in a held frame. Settle it before releasing, so
// a population emptied mid-correction cannot report loading forever.
@@ -36,7 +45,7 @@ export function createAnimation({
if (needed) {
layerState._lastAnimTime = 0;
services.render.holdContinuousRender('traffic');
layerState._viewer?.scene?.requestRender?.();
findFrame(layerState._viewer)?.requestRender('traffic');
} else services.render.releaseContinuousRender('traffic');
}
@@ -78,18 +87,7 @@ export function createAnimation({
(bucket === 'jam' ? 1 : 0) +
parts.style.activeSizeDelta(bucket);
// Dark-halo outline under styled presets (null = shipped no-outline).
const outlineSpec =
bucket && layerState._presetDots === 'on'
? presetDotOutline(layerState._stylePreset, bucket)
: null;
const outlineColor = outlineSpec
? new Cesium.Color(
outlineSpec.rgba[0] / 255,
outlineSpec.rgba[1] / 255,
outlineSpec.rgba[2] / 255,
outlineSpec.rgba[3],
)
: null;
const outline = bucket ? parts.style.outlineOf(bucket) : null;
const flowSpeed = flow ? flowSpeedScale(flow.level) : 1;
const now = Date.now();
@@ -113,8 +111,9 @@ export function createAnimation({
}
}
const spawned = [];
for (let i = 0; i < count; i++) {
if (layerState._dots.length >= MAX_DOTS) return;
if (layerState._dots.length >= MAX_DOTS) break;
// Random start position: pick a random segment and offset within it —
// unless this is a queued jam dot with a platoon placement.
@@ -140,43 +139,44 @@ export function createAnimation({
? 1
: -1;
// Compute initial Cartesian3 position via linear interpolation
Cesium.Cartesian3.lerp(
road.waypoints[segIdx],
road.waypoints[segIdx + 1],
t,
scratchLerp,
);
// Grounded traffic respects building/terrain occlusion. The former
// depth override made distant roads look like vehicles crossing roofs.
const jamProminent = bucket === 'jam' && parts.style.jamDensityOn();
const id = layerState._nextDotId++;
const point = layerState._pointCollection.add({
position: Cesium.Cartesian3.clone(scratchLerp),
// The dot's point item, kept and changed in place: its draw id stays
// while the dot's vehicle identity (`id`) is renewed at each recycle.
const point = {
id: `dot:${id}`,
// Its initial position, by linear interpolation.
pos: lerpInto(road.waypoints[segIdx], road.waypoints[segIdx + 1], t, {
x: 0,
y: 0,
z: 0,
}),
pixelSize,
// No flow data → today's exact simulated white.
color: parts.style.dotColor(bucket),
scaleByDistance: new Cesium.NearFarScalar(
100,
1.5,
layerState._fadeScaleFar,
jamProminent ? JAM_DOT_FAR_SCALE : 0.3,
),
translucencyByDistance: new Cesium.NearFarScalar(
100,
1.0,
layerState._fadeTransFar,
0.0,
),
// Frozen, as the colours are: the draw port then tells them
// unchanged by identity when the dots move each frame.
scaleByDistance: Object.freeze({
near: 100,
nearValue: 1.5,
far: layerState._fadeScaleFar,
farValue: jamProminent ? JAM_DOT_FAR_SCALE : 0.3,
}),
fadeByDistance: Object.freeze({
near: 100,
nearValue: 1.0,
far: layerState._fadeTransFar,
farValue: 0.0,
}),
// Respect the sampled mesh at every viewing distance.
disableDepthTestDistance: 0,
// Preset dark halo (spread only when present — the keyless/normal
// path passes the exact shipped option set).
...(outlineSpec
? { outlineColor, outlineWidth: outlineSpec.width }
: {}),
});
disableDepthTest: 0,
// Preset dark halo (only when present — the keyless/normal path
// passes the shipped look).
...outline,
};
spawned.push(point);
layerState._bucketCounts[bucket || 'sim'] += 1;
layerState._motion.added++;
@@ -207,6 +207,8 @@ export function createAnimation({
: null,
});
}
// Drawn at once, as they join; each frame moves them on.
if (spawned.length) layerState._dotSet?.upsert(spawned);
syncRenderDemand();
}
@@ -307,23 +309,16 @@ export function createAnimation({
if (now < dot.stoppedUntil) break;
}
// Lerp between pre-computed Cartesian3 waypoints (no trig needed).
// Pass the scratch directly: PointPrimitive's position setter clones the
// value into its own storage (and skips the VBO dirty flag when equal), so
// the extra defensive clone here allocated 360–720k Cartesian3/s of pure
// garbage across 6000 dots. (perf item 5)
// Lerp between pre-computed ECEF waypoints (no trig needed), into the
// point item's own position: the draw set copies it.
const a = dot.waypoints[dot.segIdx];
const b = dot.waypoints[dot.segIdx + 1];
Cesium.Cartesian3.lerp(a, b, dot.t, scratchLerp);
dot.point.position = scratchLerp;
lerpInto(a, b, dot.t, dot.point.pos);
}
flushDots();
// Jam heat-lines throb (~1.6 s period) — one shared material uniform for
// the whole jam batch, no geometry rebuild. Null outside live heatline mode.
if (layerState._heatJamPrim?.appearance) {
layerState._heatJamPrim.appearance.material.uniforms.color.alpha =
HEAT_JAM_BASE_ALPHA + HEAT_JAM_PULSE_ALPHA * Math.sin(now / 260);
}
// Jam heat-lines throb (~1.6 s period); none outside live heatline mode.
parts.rendering.pulseHeatLines?.(now);
layerState._animFrame++;
}
@@ -354,7 +349,7 @@ export function createAnimation({
services.credits?.hideOsmCredit?.(layerState._viewer, 'traffic');
parts.retention.clear();
if (layerState._dots.length) layerState._motion.rebuilds++;
if (layerState._pointCollection) layerState._pointCollection.removeAll();
layerState._dotSet?.clear();
parts.rendering.removeHeatLines();
layerState._dots = [];
layerState._roads = [];
@@ -369,5 +364,6 @@ export function createAnimation({
maybeStopLight,
clearDots,
syncRenderDemand,
flushDots,
};
}