From 3276a3346ece319dfd901bac70843f9817b4e8e3 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Sun, 8 Nov 2026 20:49:08 -0800 Subject: [PATCH] Draw a moving clamped fill every frame in the Cesium draw port A clamped colour fill (a polygon, a fixed-radius ellipse or a rectangle) whose upsert changed only its geometry rebuilt its set's ground primitive batches asynchronously, and each upsert dropped the build in flight, so a shape dragged every frame never drew. Once Cesium's terrain heights are loaded, such a fill is now drawn alone where it now is, on a ground primitive built in the same frame with the same pick id, and hidden in its batch and in a host still retiring. That mirrors how a moved draped line is drawn. A set draws at most MOVING_SHAPES fills alone, counting those a retiring host still draws; an upsert that would draw more rebuilds as before. A set whose moved fills have been still for SETTLE_FRAMES drawn frames rebuilds once, so they go back into its batches. If a second rebuild drops the host drawing a fill alone before that host is ready, the fill shows again where the retiring host draws it, unless it is hidden or gone. --- src/render/cesium/draw.js | 216 +++++++++++++++++++- src/render/cesium/draw.test.mjs | 339 ++++++++++++++++++++++++++++++++ 2 files changed, 545 insertions(+), 10 deletions(-) diff --git a/src/render/cesium/draw.js b/src/render/cesium/draw.js index 210db8ec4..abb161b80 100644 --- a/src/render/cesium/draw.js +++ b/src/render/cesium/draw.js @@ -323,6 +323,13 @@ export const SETTLE_FRAMES = 30; export const FEW_LOOSE_LINES = 64; export const LONG_SETTLE_FRAMES = 300; +/** + * Clamped fills a set draws alone at most, each where it last moved + * (`moveShape`): an upsert that would draw more alone rebuilds the set's + * batches instead, as each costs a primitive and is built in the frame. + */ +export const MOVING_SHAPES = 16; + /** What `syncIcons` did: icons added or removed, or only changed in place. */ const ICONS_CHANGED = 'changed'; const ICONS_KEPT = 'kept'; @@ -446,8 +453,8 @@ export function createCesiumDraw({ return; } // Lines that only change how they show or where an undraped one - // runs, and clamped shapes that only show or hide, are changed - // where they are. + // runs, and clamped shapes that only show or hide, or a few colour + // fills that only move (`moveShape`), are changed where they are. if ( patchLines(record, list) || patchShapes(record, list) || @@ -661,7 +668,12 @@ export function createCesiumDraw({ ) { record.retiring = old; if (record.own) old.remove(record.own); - } else if (old) scene.primitives.remove(old); + } else if (old) { + scene.primitives.remove(old); + // Shapes drawn alone in the host that goes show again where the host + // still retiring draws them, until the new one is ready. + showRetiringShapes(record); + } if (!waiting || !groundShown) retire(record); // Hidden one by one: a hidden collection would not build them. if (waiting) for (const primitive of waiting) primitive.show = false; @@ -754,6 +766,8 @@ export function createCesiumDraw({ record.retiring = null; record.retiringFlows = null; record.retiringParts = null; + record.retiringShapeParts = null; + record.retiringHiddenShapes = null; } /** @@ -2625,7 +2639,10 @@ export function createCesiumDraw({ * and the lines drawn alone since, hidden in their batches. */ function showPending(record) { - for (const part of record.shapeParts?.values() ?? []) + for (const part of record.shapeParts?.values() ?? []) { + // A shape drawn alone since it moved stays hidden in its batch. + if (part.batchHidden && part.batch && !part.batch.isDestroyed()) + showOn(part.batch, part.id, false); if ( part.pendingShow !== undefined && part.ground && @@ -2634,6 +2651,7 @@ export function createCesiumDraw({ showOn(part.ground, part.id, part.pendingShow); part.pendingShow = undefined; } + } for (const part of record.lineParts?.values() ?? []) { if (part.batchHidden && part.batch && !part.batch.isDestroyed()) showOn(part.batch, part.id, false); @@ -2869,6 +2887,9 @@ export function createCesiumDraw({ // Each clamped item as drawn, for upserts that only show or hide it; // the set keeps them once every one is built. const parts = new Map(); + // The shapes as the host this build replaces draws them, while it + // still draws; a host already retiring keeps its own. + if (!record.retiring) record.retiringShapeParts = record.shapeParts ?? null; record.shapeParts = null; for (const item of items) { if (item.outline) @@ -3084,29 +3105,198 @@ export function createCesiumDraw({ * Upsert clamped shapes in place, without a rebuild, when every item is * drawn now and looks the same: then only whether it shows and its * `meta` change. A ground primitive still building takes the show once - * it is ready (`swapReady`). False, changing nothing, otherwise. + * it is ready (`swapReady`). A plain colour fill whose geometry alone + * changed is drawn alone where it now is (`moveShape`), while no more + * than `MOVING_SHAPES` would be. False, changing nothing, otherwise. */ function patchShapes(record, list) { const parts = record.shapeParts; if (record.kind !== 'shapes' || !parts) return false; + // Each item's way in: `true` moves it (`moveShape`), else it only + // shows, hides or takes its `meta`. + const moves = []; for (const item of list) { const was = parts.get(item.id); - if (!was || !sameLook(was, item)) return false; - if (was.ground?.isDestroyed()) return false; + if (!was) return false; + if (was.ground?.isDestroyed() || was.batch?.isDestroyed()) return false; + if (sameLook(was, item)) { + moves.push(false); + continue; + } + if (!movable(record, was, item)) return false; + moves.push(true); } - for (const item of list) { + // Drawn alone after this upsert: a few at most, or the batches rebuild. + const alone = new Set(); + for (const [id, part] of parts) if (part.solo) alone.add(id); + list.forEach((item, k) => moves[k] && alone.add(item.id)); + // Those the host still retiring draws alone count too, until it goes. + let retiringAlone = 0; + if (record.retiring) + for (const part of record.retiringShapeParts?.values() ?? []) + if (part.solo && part.ground && !part.ground.isDestroyed()) + retiringAlone += 1; + if (alone.size + retiringAlone > MOVING_SHAPES) return false; + list.forEach((item, k) => { const was = parts.get(item.id); - if (!was.ground) continue; + if (moves[k]) return moveShape(record, was, item); + if (!was.ground) return; was.id.meta = item.meta; const show = item.show !== false; if (was.ground.ready) { showOn(was.ground, was.id, show); was.pendingShow = undefined; + } else if (was.solo) { + // Not yet drawn, it is built again showing as asked. + moveShape(record, was, item); } else was.pendingShow = show; - } + }); return true; } + /** Whether a clamped item is a plain colour fill, drawn in a batch. */ + const colourFill = (item) => + item.height === 'clampGround' && + Boolean(item.fill) && + !item.outlineOnly && + !item.outline && + item.extrudeHeight === undefined && + (typeof item.fill !== 'object' || Array.isArray(item.fill)); + + /** How a clamped shape draws, but for its geometry, show and `meta`. */ + const placedLookOf = (item) => { + const { show, meta, geometry, ...look } = item; + return plainCopy(look); + }; + + /** + * Whether a clamped colour fill's upsert moves or reshapes it and nothing + * else: then it is drawn alone where it now is, built in this frame, + * which needs Cesium's terrain heights loaded. + */ + function movable(record, was, item) { + if (!was.id || !was.ground || !colourFill(item)) return false; + if (!colourFill(was.look)) return false; + if (!sameValues(placedLookOf(was.look), placedLookOf(item))) return false; + // Refused geometry (an ellipse radius by camera height) rebuilds, to throw. + const g = item.geometry; + if ( + g?.kind === 'ellipse' && + (typeof g.semiMajor !== 'number' || typeof g.semiMinor !== 'number') + ) + return false; + if (!['polygon', 'ellipse', 'rectangle'].includes(g?.kind)) return false; + return groundSyncs() && typeof record.host?.add === 'function'; + } + + /** + * Draw a clamped colour fill where it now is, in this frame: on a ground + * primitive of its own built at once, in place of its own one (or of its + * place in its batch, hidden there), as a moved draped line is drawn + * (`moveDraped`). A shape dragged every frame draws every frame, where a + * rebuild would build its batch anew each time and never finish. + */ + function moveShape(record, part, item) { + const host = record.host; + if (part.solo) { + if (part.ground && !part.ground.isDestroyed()) host.remove(part.ground); + } else { + part.batch = part.ground; + part.batchHidden = true; + // Its batch draws on without it, hidden now or once it is ready. + if (part.batch.ready) showOn(part.batch, part.id, false); + } + hideRetiringShape(record, item.id); + record.shapesStill = 0; + part.solo = true; + part.pendingShow = undefined; + part.look = lookOf(item); + part.id.meta = item.meta; + const pickable = record.spec.pickable !== false; + const Ground = pickable + ? Cesium.GroundPrimitive + : UnpickableGroundPrimitive; + part.ground = new Ground({ + geometryInstances: [ + new Cesium.GeometryInstance({ + geometry: groundGeometry(item), + attributes: { + color: Cesium.ColorGeometryInstanceAttribute.fromColor( + toColor(item.fill), + ), + show: new Cesium.ShowGeometryInstanceAttribute(item.show !== false), + }, + id: part.id, + }), + ], + classificationType: classificationOf( + item.clampTo ?? record.spec.clampTo ?? 'both', + ), + allowPicking: pickable, + asynchronous: false, + }); + host.add(part.ground); + } + + /** + * Hide where the host a set is rebuilding from still draws a clamped + * shape, once it is drawn where it now is: else both would draw (and + * pick) until the rebuild is ready. + */ + function hideRetiringShape(record, id) { + const part = record.retiring && record.retiringShapeParts?.get(id); + const ground = part?.ground; + if (!ground || ground.isDestroyed() || !ground.ready) return; + const hidden = (record.retiringHiddenShapes ??= new Set()); + // Only one it drew: one hidden there already stays hidden. + if (!hidden.has(id) && !shownOn(ground, part.id)) return; + showOn(ground, part.id, false); + hidden.add(id); + } + + /** Whether the instance under pick id `id` in a ready ground primitive shows. */ + const shownOn = (ground, id) => + ground.getGeometryInstanceAttributes(id).show[0] !== 0; + + /** + * Show again the shapes `hideRetiringShape` hid where the retiring host + * draws them: the host that drew them alone is gone before it was ready. + */ + function showRetiringShapes(record) { + const hidden = record.retiringHiddenShapes; + record.retiringHiddenShapes = null; + if (!record.retiring || !hidden) return; + for (const id of hidden) { + const ground = record.retiringShapeParts?.get(id)?.ground; + if (!ground || ground.isDestroyed() || !ground.ready) continue; + // As it is now: one since hidden or removed stays hidden. + const item = record.items.get(id); + if (!item || item.show === false) continue; + showOn(ground, record.retiringShapeParts.get(id).id, true); + } + } + + /** + * After a draw: a shape set whose shapes drawn alone have not moved for + * `SETTLE_FRAMES` drawn frames rebuilds, so they go back into its + * batches; the host drawing them alone draws until the rebuild is ready. + */ + function settleShapes(record) { + if (record.pending || !record.shapeParts) return; + let alone = false; + for (const part of record.shapeParts.values()) + if (part.solo) { + alone = true; + break; + } + if (!alone) return; + record.shapesStill = (record.shapesStill ?? 0) + 1; + if (record.shapesStill < SETTLE_FRAMES) return; + record.shapesStill = 0; + record.rebuild(); + requestRender(); + } + /** Whether a hidden clamped item is a plain colour fill `groundFills` builds. */ const hiddenFillBuilds = (item) => Boolean(item.fill) && @@ -3395,6 +3585,12 @@ export function createCesiumDraw({ record.lineBox = null; report(error); } + else if (record.kind === 'shapes') + try { + settleShapes(record); + } catch (error) { + report(error); + } // Rows moving on a live clock, and flowing lines, draw on in the // frames that follow. const playing = quadSets.afterRender(sets.values()); diff --git a/src/render/cesium/draw.test.mjs b/src/render/cesium/draw.test.mjs index 85963d727..50846e25d 100644 --- a/src/render/cesium/draw.test.mjs +++ b/src/render/cesium/draw.test.mjs @@ -6,6 +6,7 @@ import { createCesiumDraw, FEW_LOOSE_LINES, LONG_SETTLE_FRAMES, + MOVING_SHAPES, revealUniform, SETTLE_FRAMES, unboundDrawPart, @@ -5917,3 +5918,341 @@ test('a clamped ellipse filled with a radial rim drapes its material, finely cut assert.deepEqual([...shows.show], [0]); set.dispose(); })); + +test('a clamped fill that moves or resizes is drawn where it now is in the same frame, once terrain heights are loaded', () => + withTerrainHeights(() => { + const { scene, draw, frame } = drawOver(); + const set = draw.shapes({ id: 'dragged' }); + const box = (id, east, extra = {}) => ({ + id, + geometry: { + kind: 'rectangle', + rect: { west: 0, south: 0, east, north: 1 }, + }, + fill: [0.5, 0.9, 1, 0.12], + height: 'clampGround', + ...extra, + }); + set.set([ + box('a', 1), + box('b', 3, { + geometry: { + kind: 'rectangle', + rect: { west: 2, south: 0, east: 3, north: 1 }, + }, + }), + ]); + const host = scene.added[0]; + const groundOf = (h) => { + for (let i = 0; i < h.length; i++) + if (h.get(i) instanceof Cesium.GroundPrimitive) return h.get(i); + return null; + }; + const batch = groundOf(host); + const shows = new Map(); + const attributesOf = (primitive) => { + primitive.getGeometryInstanceAttributes = (id) => { + const key = `${primitive.geometryInstances.length}:${id.id}`; + if (!shows.has(key)) shows.set(key, { show: new Uint8Array([1]) }); + return shows.get(key); + }; + }; + attributesOf(batch); + // Resized while its batch still builds: drawn alone, built at once. + set.upsert([box('a', 1.5, { meta: 'dragged' })]); + assert.equal(scene.added[0], host, 'the set is not rebuilt'); + const solo = host.get(host.length - 1); + assert.ok(solo instanceof Cesium.GroundPrimitive); + assert.equal(solo.asynchronous, false, 'built in the frame it is drawn'); + const instance = solo.geometryInstances[0]; + assert.equal(instance.id.id, 'a'); + assert.equal(instance.id.meta, 'dragged'); + assert.equal( + instance.id, + batch.geometryInstances.find((i) => i.id.id === 'a').id, + 'the same pick id', + ); + assert.ok( + Cesium.Rectangle.equalsEpsilon( + instance.geometry._rectangle, + Cesium.Rectangle.fromDegrees(0, 0, 1.5, 1), + 1e-12, + ), + ); + // Its place in the batch is hidden once the batch is ready; the rest draws. + Object.defineProperty(batch, 'ready', { value: true }); + frame(); + assert.deepEqual([...shows.get('2:a').show], [0], 'hidden in the batch'); + assert.equal(shows.get('2:b'), undefined, 'the other untouched'); + // Moved again: the one built for it is replaced. + set.upsert([box('a', 2)]); + const again = host.get(host.length - 1); + assert.notEqual(again, solo); + assert.ok(solo.isDestroyed(), 'the old one goes'); + // A show change on a ready one is in place; one not yet drawn is built again. + Object.defineProperty(again, 'ready', { value: true }); + attributesOf(again); + set.upsert([box('a', 2, { show: false })]); + assert.equal(host.get(host.length - 1), again); + assert.deepEqual([...shows.get('1:a').show], [0]); + // A new colour is a new look: the set rebuilds. + set.upsert([box('a', 2, { fill: [1, 0, 0, 0.5] })]); + assert.notEqual(scene.added.at(-1), host, 'rebuilt'); + set.dispose(); + assert.equal(scene.added.length, 0); + })); + +test('a moved clamped fill is hidden where a rebuild in flight still draws it', () => + withTerrainHeights(() => { + const { scene, draw, frame } = drawOver(); + const set = draw.shapes({ id: 'retiring' }); + const box = (id, east, fill = [0.5, 0.9, 1, 0.12]) => ({ + id, + geometry: { + kind: 'rectangle', + rect: { west: 0, south: 0, east, north: 1 }, + }, + fill, + height: 'clampGround', + }); + set.set([box('a', 1)]); + const first = scene.added[0]; + const groundOf = (h) => { + for (let i = 0; i < h.length; i++) + if (h.get(i) instanceof Cesium.GroundPrimitive) return h.get(i); + return null; + }; + const old = groundOf(first); + Object.defineProperty(old, 'ready', { value: true }); + frame(); + const shown = { show: new Uint8Array([1]) }; + old.getGeometryInstanceAttributes = () => shown; + // A new colour rebuilds; the old host draws on until the new is ready. + set.upsert([box('a', 1, [1, 0, 0, 0.5])]); + const second = scene.added.find((p) => p !== first); + assert.ok( + second && scene.added.includes(first), + 'both drawn while it builds', + ); + // Moved meanwhile: drawn alone in the new host, and gone from the old one. + set.upsert([box('a', 2, [1, 0, 0, 0.5])]); + assert.ok(second.get(second.length - 1).asynchronous === false); + assert.deepEqual( + [...shown.show], + [0], + 'hidden where the old host draws it', + ); + set.dispose(); + })); + +/** A clamped colour rectangle from 0° to `east`°, for the move tests. */ +const groundBox = (id, east, extra = {}) => ({ + id, + geometry: { + kind: 'rectangle', + rect: { west: 0, south: 0, east, north: 1 }, + }, + fill: [0.5, 0.9, 1, 0.12], + height: 'clampGround', + ...extra, +}); + +/** Every ground primitive in a host built, with a show to read and set. */ +const groundsReady = (host) => { + for (let i = 0; i < host.length; i++) { + const ground = host.get(i); + if (!(ground instanceof Cesium.GroundPrimitive)) continue; + Object.defineProperty(ground, 'ready', { value: true }); + const show = { show: new Uint8Array([1]) }; + ground.getGeometryInstanceAttributes = () => show; + } +}; + +/** The first ground primitive in a host, or null. */ +const firstGround = (host) => { + for (let i = 0; i < host.length; i++) + if (host.get(i) instanceof Cesium.GroundPrimitive) return host.get(i); + return null; +}; + +test('moved clamped fills go back into a rebuilt batch once they have been still for a while', () => + withTerrainHeights(() => { + const { scene, draw, frame } = drawOver(); + const set = draw.shapes({ id: 'settling' }); + set.set([groundBox('a', 1), groundBox('b', 2)]); + const host = scene.added[0]; + groundsReady(host); + frame(); + set.upsert([groundBox('a', 1.5)]); + for (let i = 0; i < SETTLE_FRAMES - 1; i++) frame(); + // Moved again before it settled: it waits from there. + set.upsert([groundBox('a', 1.6)]); + for (let i = 0; i < SETTLE_FRAMES - 1; i++) frame(); + assert.deepEqual(scene.added, [host], 'not rebuilt while it moves'); + frame(); + assert.equal(scene.added.length, 2, 'rebuilt once still'); + const rebuilt = scene.added.find((p) => p !== host); + const instances = []; + for (let i = 0; i < rebuilt.length; i++) + if (rebuilt.get(i) instanceof Cesium.GroundPrimitive) + instances.push(...rebuilt.get(i).geometryInstances); + assert.deepEqual( + instances.map((i) => i.id.id).sort(), + ['a', 'b'], + 'both in the new batches', + ); + assert.ok( + rebuilt.get(0).asynchronous !== false, + 'built as a batch, not alone', + ); + assert.ok( + Cesium.Rectangle.equalsEpsilon( + instances.find((i) => i.id.id === 'a').geometry._rectangle, + Cesium.Rectangle.fromDegrees(0, 0, 1.6, 1), + 1e-12, + ), + 'where it last moved', + ); + // The host drawing it alone draws until the rebuild is ready. + assert.ok(scene.added.includes(host)); + for (let i = 0; i < SETTLE_FRAMES * 2; i++) frame(); + assert.equal(scene.added.length, 2, 'it settles once'); + set.dispose(); + })); + +test('an upsert that would draw more clamped fills alone than MOVING_SHAPES rebuilds instead', () => + withTerrainHeights(() => { + const { scene, draw, frame } = drawOver(); + const set = draw.shapes({ id: 'many', pickable: false }); + const ids = Array.from({ length: MOVING_SHAPES + 1 }, (_, i) => `s${i}`); + set.set(ids.map((id) => groundBox(id, 1))); + const host = scene.added[0]; + groundsReady(host); + frame(); + // A set that does not pick draws one alone without picking. + set.upsert([groundBox('s0', 1.5)]); + const solo = host.get(host.length - 1); + assert.equal(solo.asynchronous, false); + assert.equal(solo.allowPicking, false); + assert.deepEqual(scene.added, [host]); + set.upsert(ids.slice(1, MOVING_SHAPES).map((id) => groundBox(id, 1.5))); + assert.deepEqual(scene.added, [host], 'MOVING_SHAPES alone at most'); + set.upsert([groundBox(ids.at(-1), 1.5)]); + assert.equal(scene.added.length, 2, 'one more rebuilds the set'); + set.dispose(); + })); + +test('a clamped fill moved before terrain heights load rebuilds its set', () => { + const heights = Cesium.ApproximateTerrainHeights; + if (heights.initialized) return; + const { scene, draw } = drawOver(); + const set = draw.shapes({ id: 'no-heights' }); + set.set([groundBox('a', 1)]); + const host = scene.added[0]; + set.upsert([groundBox('a', 1.5)]); + assert.ok( + !scene.added.includes(host) || scene.added.length > 1, + 'rebuilt, not drawn alone', + ); + set.dispose(); +}); + +test('a moved clamped fill shows again where the retiring host draws it when the host drawing it alone goes unbuilt', () => + withTerrainHeights(() => { + const { scene, draw, frame } = drawOver(); + const set = draw.shapes({ id: 'twice' }); + set.set([groundBox('a', 1)]); + const first = scene.added[0]; + const old = firstGround(first); + Object.defineProperty(old, 'ready', { value: true }); + frame(); + const shown = { show: new Uint8Array([1]) }; + old.getGeometryInstanceAttributes = () => shown; + set.upsert([groundBox('a', 1, { fill: [1, 0, 0, 0.5] })]); + set.upsert([groundBox('a', 2, { fill: [1, 0, 0, 0.5] })]); + assert.deepEqual([...shown.show], [0], 'hidden while drawn alone'); + // A second rebuild drops the host drawing it alone, still building. + set.upsert([groundBox('a', 2, { fill: [0, 1, 0, 0.5] })]); + assert.ok(scene.added.includes(first), 'the retiring host draws on'); + assert.equal(scene.added.length, 2); + assert.deepEqual([...shown.show], [1], 'drawn where it was, not gone'); + set.dispose(); + })); + +/** Ground primitives in a host built, each instance's show kept by id. */ +const groundsReadyById = (host, shows = new Map()) => { + for (let i = 0; i < host.length; i++) { + const ground = host.get(i); + if (!(ground instanceof Cesium.GroundPrimitive)) continue; + Object.defineProperty(ground, 'ready', { value: true }); + ground.getGeometryInstanceAttributes = (id) => { + if (!shows.has(id.id)) { + const shown = ground.geometryInstances.find((g) => g.id === id) + ?.attributes.show.value[0]; + shows.set(id.id, { show: new Uint8Array([shown ?? 1]) }); + } + return shows.get(id.id); + }; + } + return shows; +}; + +test('a hidden clamped fill moved while its set rebuilds stays hidden when a second rebuild comes', () => + withTerrainHeights(() => { + const { scene, draw, frame } = drawOver(); + const set = draw.shapes({ id: 'hidden-twice' }); + set.set([ + groundBox('a', 1, { show: false }), + groundBox('b', 1, { + geometry: { + kind: 'rectangle', + rect: { west: 5, south: 0, east: 6, north: 1 }, + }, + }), + ]); + const first = scene.added[0]; + const shows = groundsReadyById(first); + frame(); + const b = (fill) => + groundBox('b', 1, { + geometry: { + kind: 'rectangle', + rect: { west: 5, south: 0, east: 6, north: 1 }, + }, + fill, + }); + set.upsert([b([1, 0, 0, 0.5])]); + set.upsert([groundBox('a', 2, { show: false })]); + set.upsert([b([0, 1, 0, 0.5])]); + assert.ok(scene.added.includes(first), 'the retiring host draws on'); + assert.deepEqual([...shows.get('a').show], [0], 'a stays hidden'); + set.dispose(); + })); + +test('clamped fills drawn alone by a host still retiring count toward MOVING_SHAPES', () => + withTerrainHeights(() => { + const { scene, draw, frame } = drawOver(); + const set = draw.shapes({ id: 'cap-retiring' }); + const ids = Array.from( + { length: MOVING_SHAPES * 2 + 1 }, + (_, i) => `s${i}`, + ); + set.set(ids.map((id) => groundBox(id, 1))); + const first = scene.added[0]; + groundsReady(first); + frame(); + // Half the cap moved, then a rebuild while they are drawn alone. + set.upsert(ids.slice(0, MOVING_SHAPES).map((id) => groundBox(id, 1.5))); + set.upsert([groundBox(ids.at(-1), 1, { fill: [1, 0, 0, 0.5] })]); + assert.equal(scene.added.length, 2, 'rebuilding, the old host retiring'); + const second = scene.added.find((p) => p !== first); + const before = second.length; + // Others moved while it builds: over the cap counted with the retiring + // host's, so the set rebuilds rather than draw them alone. + set.upsert([groundBox(ids[MOVING_SHAPES], 1.5)]); + assert.ok( + !scene.added.includes(second) || second.length === before, + 'nothing more drawn alone', + ); + set.dispose(); + }));