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);
// Canvas rows run north to south.
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;
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++) {
const dy = (y + 0.5 - cy) * perPixel;
const dy2 = dy * dy;
for (let x = 0; x < size; x++) {
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);
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;
data[k] = r;
data[k + 1] = g;
data[k + 2] = b;
// `Math.round` of a non-negative value.
data[k + 3] = (Math.min(1, sum * opacity) * 255 + 0.5) | 0;
data[k + 3] = at > outer ? 0 : table[(at * toBin + 0.5) | 0];
}
}
}
@@ -413,8 +422,27 @@ export function createRangeRing({
// sky, so the pulses are hidden there and rest with it: no flash, no
// echoes, no frames.
const belowRim = () => cameraHeightM(viewer) < CONTEXT_RIM_HEIGHT_M;
const levelAt = (nowMs) =>
wake && !belowRim() ? awarenessPulseLevel(nowMs, wake) : 0;
// One step asks for the level at one time several times over; the
// 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 pulse = AWARENESS_RANGE_PULSE;
+13 -1
View File
@@ -76,6 +76,16 @@ function fakeCanvas() {
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. */
const sceneDouble = () => ({
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,
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.
for (let index = 0; index < AWARENESS_RANGE_PULSE.count; index++) {
const core = drawn(ring).pulse(index);