diff --git a/src/layers/awareness/rangeRing.js b/src/layers/awareness/rangeRing.js index cdb88e65d..c351c6685 100644 --- a/src/layers/awareness/rangeRing.js +++ b/src/layers/awareness/rangeRing.js @@ -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; diff --git a/src/layers/awareness/rangeRing.test.mjs b/src/layers/awareness/rangeRing.test.mjs index a4dbe7e74..be436b2fe 100644 --- a/src/layers/awareness/rangeRing.test.mjs +++ b/src/layers/awareness/rangeRing.test.mjs @@ -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); diff --git a/src/render/cesium/draw.js b/src/render/cesium/draw.js index 8a33afc62..bb1f26d13 100644 --- a/src/render/cesium/draw.js +++ b/src/render/cesium/draw.js @@ -2824,8 +2824,12 @@ export function createCesiumDraw({ was.positions = flatPositions(item.positions); unsettle(record); } - const positions = linePositions(item); - was.polyline.positions = positions; + // A line sent again where it runs keeps its positions: writing them + // 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; if (recolour) paintLine(was.polyline.material, item.style); 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 * `SETTLE_FRAMES` drawn frames starts building its static batch, of all diff --git a/src/render/cesium/draw.test.mjs b/src/render/cesium/draw.test.mjs index 789da624c..8acfe7f07 100644 --- a/src/render/cesium/draw.test.mjs +++ b/src/render/cesium/draw.test.mjs @@ -2954,10 +2954,16 @@ test('an outlined solid line draws one outline material, recoloured in place, an return null; }; const first = line(); + const drawnPositions = first.positions; assert.equal(first.material.type, 'PolylineOutline'); assert.equal(first.material.uniforms.outlineWidth, 1); s.set.upsert([outlined([0, 0.5, 1, 0.4], [0, 0, 0, 0.32])]); 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.ok( Math.abs(first.material.uniforms.outlineColor.alpha - 0.32) < 1e-9,