Cut the range ring's frame cost while the camera moves

The pulse level is worked out once a step, so the camera pose is read
once rather than on every call. The band's alpha is tabulated along the
radius and each pixel looks it up (0.78 ms a paint down to 0.21 ms in
Node). A line sent again where it already runs keeps its positions, so the
polyline collection does not rewrite the ring's vertices on every step.

The band test now compares images byte by byte. A failing deepEqual on a
256x256 image builds a diff of every byte, and that ran a test process
out of memory.
This commit is contained in:
Sameh Khamis committed 2026-11-10 07:59:23 -08:00
1 parent 74d6d97b29
commit e4af6c8ed7
4 files changed
+103 -18

No files matched your search

+43 -15
View File
@@ -233,30 +233,39 @@ export function paintAwarenessBand(image, band) {
const cx = size / 2 + offset.east / ((2 * spanM) / size); const cx = size / 2 + offset.east / ((2 * spanM) / size);
// Canvas rows run north to south. // Canvas rows run north to south.
const cy = size / 2 - offset.north / ((2 * spanM) / size); const cy = size / 2 - offset.north / ((2 * spanM) / size);
const count = radii.length; // The band depends on the distance from the subject alone: its alpha
// byte is tabulated once along the radius, finer than a pixel, and each
// pixel looks it up.
const opacity = pulse.gradientOpacity; const opacity = pulse.gradientOpacity;
let outer = 0;
for (const radius of radii) outer = Math.max(outer, radius);
const bins = 4 * size;
const table = new Uint8Array(bins + 1);
for (let bin = 0; bin <= bins; bin++) {
const at = (bin / bins) * outer;
let sum = 0;
for (let i = 0; i < radii.length; i++) {
const inside = radii[i] - at;
if (inside >= 0 && inside <= depth) {
const t = 1 - inside / depth;
sum += alphas[i] * t * t;
}
}
// `Math.round` of a non-negative value.
table[bin] = (Math.min(1, sum * opacity) * 255 + 0.5) | 0;
}
const toBin = outer > 0 ? bins / outer : 0;
for (let y = 0; y < size; y++) { for (let y = 0; y < size; y++) {
const dy = (y + 0.5 - cy) * perPixel; const dy = (y + 0.5 - cy) * perPixel;
const dy2 = dy * dy; const dy2 = dy * dy;
for (let x = 0; x < size; x++) { for (let x = 0; x < size; x++) {
const dx = (x + 0.5 - cx) * perPixel; const dx = (x + 0.5 - cx) * perPixel;
// Not `Math.hypot`, many times slower: this runs on every frame the
// camera moves while the pulses run.
const at = Math.sqrt(dx * dx + dy2); const at = Math.sqrt(dx * dx + dy2);
let sum = 0;
for (let i = 0; i < count; i++) {
const inside = radii[i] - at;
if (inside >= 0 && inside <= depth) {
const t = 1 - inside / depth;
sum += alphas[i] * t * t;
}
}
const k = (y * size + x) * 4; const k = (y * size + x) * 4;
data[k] = r; data[k] = r;
data[k + 1] = g; data[k + 1] = g;
data[k + 2] = b; data[k + 2] = b;
// `Math.round` of a non-negative value. data[k + 3] = at > outer ? 0 : table[(at * toBin + 0.5) | 0];
data[k + 3] = (Math.min(1, sum * opacity) * 255 + 0.5) | 0;
} }
} }
} }
@@ -413,8 +422,27 @@ export function createRangeRing({
// sky, so the pulses are hidden there and rest with it: no flash, no // sky, so the pulses are hidden there and rest with it: no flash, no
// echoes, no frames. // echoes, no frames.
const belowRim = () => cameraHeightM(viewer) < CONTEXT_RIM_HEIGHT_M; const belowRim = () => cameraHeightM(viewer) < CONTEXT_RIM_HEIGHT_M;
const levelAt = (nowMs) => // One step asks for the level at one time several times over; the
wake && !belowRim() ? awarenessPulseLevel(nowMs, wake) : 0; // camera's pose is read once for them.
let levelMemo = { nowMs: NaN, startMs: NaN, untilMs: NaN, level: 0 };
const levelAt = (nowMs) => {
if (!wake) return 0;
const memo = levelMemo;
if (
memo.nowMs === nowMs &&
memo.startMs === wake.startMs &&
memo.untilMs === wake.untilMs
)
return memo.level;
const level = belowRim() ? 0 : awarenessPulseLevel(nowMs, wake);
levelMemo = {
nowMs,
startMs: wake.startMs,
untilMs: wake.untilMs,
level,
};
return level;
};
const ring = AWARENESS_RANGE_RING_STYLE; const ring = AWARENESS_RANGE_RING_STYLE;
const pulse = AWARENESS_RANGE_PULSE; const pulse = AWARENESS_RANGE_PULSE;
+13 -1
View File
@@ -76,6 +76,16 @@ function fakeCanvas() {
return canvas; return canvas;
} }
/**
* The first index where two byte arrays differ, -1 when they match: a
* bounded comparison for whole images.
*/
function firstMismatch(a, b) {
if (a.length !== b.length) return Math.min(a.length, b.length);
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return i;
return -1;
}
/** A Cesium scene double with the frame events the frame port listens to. */ /** A Cesium scene double with the frame events the frame port listens to. */
const sceneDouble = () => ({ const sceneDouble = () => ({
preRender: new Cesium.Event(), preRender: new Cesium.Event(),
@@ -1105,7 +1115,9 @@ test('the band is one flat unpickable ellipse at the rim, drawn anew each step a
rangeM: AWARENESS_RADIUS_M, rangeM: AWARENESS_RADIUS_M,
offset: { east: 0, north: 0 }, offset: { east: 0, north: 0 },
}); });
assert.deepEqual(next.canvas.painted.data, expected.data); // Compared byte by byte: a failing deepEqual on a 256×256 image builds a
// diff of every byte, which ran a test process out of memory.
assert.equal(firstMismatch(next.canvas.painted.data, expected.data), -1);
// Each front is a haloed line at its radius, faded with its pulse. // Each front is a haloed line at its radius, faded with its pulse.
for (let index = 0; index < AWARENESS_RANGE_PULSE.count; index++) { for (let index = 0; index < AWARENESS_RANGE_PULSE.count; index++) {
const core = drawn(ring).pulse(index); const core = drawn(ring).pulse(index);
+41 -2
View File
@@ -2824,8 +2824,12 @@ export function createCesiumDraw({
was.positions = flatPositions(item.positions); was.positions = flatPositions(item.positions);
unsettle(record); unsettle(record);
} }
const positions = linePositions(item); // A line sent again where it runs keeps its positions: writing them
was.polyline.positions = positions; // would have the collection rewrite its vertices.
const drawn = was.polyline.positions;
const unmoved = drawnAt(drawn, item.positions);
const positions = unmoved ? drawn : linePositions(item);
if (!unmoved) was.polyline.positions = positions;
was.polyline.show = show; was.polyline.show = show;
if (recolour) paintLine(was.polyline.material, item.style); if (recolour) paintLine(was.polyline.material, item.style);
if (was.reveal) if (was.reveal)
@@ -2835,6 +2839,41 @@ export function createCesiumDraw({
); );
} }
/**
* Whether `positions` (an item's, ECEF points or packed triples) are the
* Cartesians a polyline already draws. False for any given by longitude
* and latitude, which are converted, not compared.
*/
function drawnAt(drawn, positions) {
if (!Array.isArray(drawn)) return false;
if (positions instanceof Float64Array) {
if (positions.length !== drawn.length * 3) return false;
for (let i = 0; i < drawn.length; i++) {
const p = drawn[i];
if (
!Object.is(p.x, positions[i * 3]) ||
!Object.is(p.y, positions[i * 3 + 1]) ||
!Object.is(p.z, positions[i * 3 + 2])
)
return false;
}
return true;
}
if (positions.length !== drawn.length) return false;
for (let i = 0; i < drawn.length; i++) {
const p = positions[i];
const q = drawn[i];
if (
!('x' in p) ||
!Object.is(p.x, q.x) ||
!Object.is(p.y, q.y) ||
!Object.is(p.z, q.z)
)
return false;
}
return true;
}
/** /**
* After a draw: a line set whose lines have not changed for * After a draw: a line set whose lines have not changed for
* `SETTLE_FRAMES` drawn frames starts building its static batch, of all * `SETTLE_FRAMES` drawn frames starts building its static batch, of all
+6
View File
@@ -2954,10 +2954,16 @@ test('an outlined solid line draws one outline material, recoloured in place, an
return null; return null;
}; };
const first = line(); const first = line();
const drawnPositions = first.positions;
assert.equal(first.material.type, 'PolylineOutline'); assert.equal(first.material.type, 'PolylineOutline');
assert.equal(first.material.uniforms.outlineWidth, 1); assert.equal(first.material.uniforms.outlineWidth, 1);
s.set.upsert([outlined([0, 0.5, 1, 0.4], [0, 0, 0, 0.32])]); s.set.upsert([outlined([0, 0.5, 1, 0.4], [0, 0, 0, 0.32])]);
assert.equal(line(), first, 'the same line'); assert.equal(line(), first, 'the same line');
assert.equal(
first.positions,
drawnPositions,
'positions sent again where they were are not rewritten',
);
assert.equal(first.material.uniforms.color.alpha, 0.4); assert.equal(first.material.uniforms.color.alpha, 0.4);
assert.ok( assert.ok(
Math.abs(first.material.uniforms.outlineColor.alpha - 0.32) < 1e-9, Math.abs(first.material.uniforms.outlineColor.alpha - 0.32) < 1e-9,