Cesium draw port pulses shape alpha and recolours shapes in place

Shapes take an alpha pulse (from, to, periodMs, forever), run before each
frame, with frames asked for while anything drawn pulses. Any other pulse,
or one on a fill that is not a colour, is refused before anything changes.
An upsert that changes only colours, pulse, show or meta is written into
the drawn primitives: unclamped shapes through their instance colours and
ring materials, clamped colour fills through their ground primitive (or
once it is ready). Shapes not yet drawn, or drawn opaque and turning
translucent, rebuild as before.
This commit is contained in:
Sameh Khamis committed 2026-11-09 09:50:30 -08:00
1 parent 1d813a8055
commit c148e2888f
2 files changed
+590 -33

No files matched your search

+278 -14
View File
@@ -452,14 +452,22 @@ test('a clamped fill that only shows, hides or changes meta is changed in place'
});
frame();
const moved = scene.added.at(-1);
// Anything else rebuilds: a new colour, a new item.
// A new colour is written in place too.
const colours = new Map();
groundOf(moved).getGeometryInstanceAttributes = (id) => {
if (!colours.has(id.id))
colours.set(id.id, {
color: new Uint8Array(4),
show: new Uint8Array([1]),
});
return colours.get(id.id);
};
set.upsert([{ ...fill('a', true), fill: [0, 1, 0, 0.3] }]);
assert.notEqual(scene.added.at(-1), moved, 'a new look rebuilds');
const rebuilt = scene.added.at(-1);
Object.defineProperty(groundOf(rebuilt), 'ready', { value: true });
frame();
assert.equal(scene.added.at(-1), moved, 'a new colour does not rebuild');
assert.deepEqual([...colours.get('a').color], [0, 255, 0, 76]);
// A new item rebuilds.
set.upsert([fill('c', true)]);
assert.notEqual(scene.added.at(-1), rebuilt, 'a new item rebuilds');
assert.notEqual(scene.added.at(-1), moved, 'a new item rebuilds');
set.dispose();
assert.equal(scene.added.length, 0);
});
@@ -813,6 +821,262 @@ test('rings drawn alone by a host still retiring count toward the cap when the c
set.dispose();
}));
test('shapes that only change colour, pulse, show or meta change in place; a pulse runs on the frames', () =>
withTerrainHeights(() => {
const scene = fakeScene();
const asked = [];
scene.requestRender = () => asked.push(1);
const { draw, frame } = drawOver(scene);
const set = draw.shapes({ id: 'fading' });
const ring = [
{ lon: 0, lat: 0, height: 10 },
{ lon: 0.001, lat: 0, height: 10 },
{ lon: 0.001, lat: 0.001, height: 10 },
];
const pulse = (alpha) => ({
property: 'alpha',
from: alpha * 0.6,
to: alpha,
periodMs: 1000,
});
const column = (alpha, extra = {}) => ({
id: 'column',
geometry: { kind: 'polygon', rings: [ring] },
fill: [1, 0.7, 0.3, 0.45 * alpha],
animate: pulse(0.45 * alpha),
extrudeHeight: 100,
height: 'absolute',
...extra,
});
const cage = (alpha) => ({
id: 'cage',
geometry: { kind: 'polygon', rings: [ring] },
outline: [1, 0.7, 0.3, 0.32 * alpha],
outlineOnly: true,
animate: pulse(0.32 * alpha),
extrudeHeight: 100,
height: 'absolute',
});
const fill = (alpha) => ({
id: 'fill',
geometry: {
kind: 'polygon',
rings: [ring.map(({ lon, lat }) => ({ lon: lon + 1, lat }))],
},
fill: [0.4, 1, 0.6, 0.2 * alpha],
animate: pulse(0.2 * alpha),
height: 'clampGround',
});
set.set([column(0), cage(0), fill(0)]);
const host = scene.added[0];
const solids = host.get(0);
const [columnPrimitive, cagePrimitive] = [solids.get(0), solids.get(1)];
let ground = null;
for (let i = 0; i < host.length; i++)
if (host.get(i) instanceof Cesium.GroundPrimitive) ground = host.get(i);
// A pulsing colour draws translucent, whatever its alpha now.
assert.equal(columnPrimitive.appearance.translucent, true);
assert.equal(cagePrimitive.appearance.translucent, true);
// Ready, with instance attributes to write.
const colours = new Map();
const ready = (primitive, key) => {
Object.defineProperty(primitive, 'ready', { value: true });
primitive.getGeometryInstanceAttributes = () => {
if (!colours.has(key))
colours.set(key, {
color: new Uint8Array(4),
show: new Uint8Array([1]),
});
return colours.get(key);
};
};
ready(columnPrimitive, 'column');
ready(cagePrimitive, 'cage');
ready(ground, 'fill');
frame();
// Faded in: the same primitives take it.
set.upsert([column(1, { meta: 'm' }), cage(1), fill(1)]);
assert.equal(scene.added[0], host, 'not rebuilt');
assert.equal(solids.get(0), columnPrimitive);
assert.equal(columnPrimitive.geometryInstances.id.meta, 'm');
// The pulse writes each frame between 60% and all of the alpha, and asks
// for the next frame.
const seen = { column: [], cage: [], fill: [] };
const realNow = performance.now;
let now = 0;
performance.now = () => now;
try {
for (now = 0; now < 1000; now += 50) {
asked.length = 0;
frame();
assert.ok(asked.length > 0, 'a pulse asks for frames');
for (const key of Object.keys(seen))
seen[key].push(colours.get(key).color[3] / 255);
}
} finally {
performance.now = realNow;
}
const range = (values) => [Math.min(...values), Math.max(...values)];
const near = (a, b) => Math.abs(a - b) < 0.01;
assert.ok(
near(range(seen.column)[0], 0.27) && near(range(seen.column)[1], 0.45),
`${range(seen.column)}`,
);
assert.ok(
near(range(seen.cage)[0], 0.192) && near(range(seen.cage)[1], 0.32),
`${range(seen.cage)}`,
);
assert.ok(
near(range(seen.fill)[0], 0.12) && near(range(seen.fill)[1], 0.2),
`${range(seen.fill)}`,
);
// Hidden in place: its primitives hide, and stop pulsing.
set.upsert([column(1, { show: false })]);
assert.equal(scene.added[0], host);
assert.equal(columnPrimitive.show, false);
// An opaque colour drawn translucent is fine in place; a shape that
// changes more than its colours rebuilds.
set.upsert([column(1, { extrudeHeight: 200 })]);
assert.notEqual(scene.added.at(-1), host, 'rebuilt');
set.dispose();
}));
test('a pulse the Cesium renderer cannot run is refused before anything changes', () => {
const { scene, draw } = drawOver();
const set = draw.shapes({ id: 'pulses' });
const disc = (animate) => ({
id: 'a',
geometry: {
kind: 'ellipse',
center: { lon: 0, lat: 0 },
semiMajor: 1000,
semiMinor: 1000,
},
fill: '#fff',
height: 'clampGround',
animate,
});
set.set([disc(undefined)]);
const host = scene.added[0];
assert.throws(
() => set.set([disc({ property: 'radius', from: 1, to: 2, periodMs: 1 })]),
/pulses alpha only, forever/,
);
assert.throws(
() =>
set.upsert([
disc({ property: 'alpha', from: 0, to: 1, periodMs: 1, repeat: 2 }),
]),
/pulses alpha only, forever/,
);
assert.equal(scene.added[0], host, 'still drawn as it was');
// Behind an item that would rebuild, and in a whole new list, too.
const radius = { property: 'radius', from: 1, to: 2, periodMs: 1 };
assert.throws(
() => set.upsert([{ ...disc(undefined), id: 'b' }, disc(radius)]),
/pulses alpha only, forever/,
);
assert.throws(() => set.set([disc(radius)]), /pulses alpha only, forever/);
assert.equal(scene.added[0], host, 'still drawn as it was');
assert.doesNotThrow(() => set.upsert([{ ...disc(undefined), id: 'c' }]));
// Nor numbers it cannot draw, nor a fill that is not a colour.
assert.throws(
() =>
set.upsert([disc({ property: 'alpha', from: 0, to: 1, periodMs: 0 })]),
/positive period/,
);
assert.throws(
() =>
set.upsert([
{
...disc({ property: 'alpha', from: 0, to: 1, periodMs: 1000 }),
fill: { image: 'x.png' },
},
]),
/colour fills and outlines only/,
);
set.dispose();
});
test('a pulsing ring outline changes in place; a pulsing shape that draws nothing asks for no frames', () =>
withImageClasses(() => {
const scene = fakeScene();
const asked = [];
scene.requestRender = () => asked.push(1);
const { draw, frame } = drawOver(scene);
const set = draw.shapes({ id: 'rings-pulse' });
const ring = [
{ lon: 0, lat: 0, height: 10 },
{ lon: 0.001, lat: 0, height: 10 },
{ lon: 0.001, lat: 0.001, height: 10 },
];
const pulse = { property: 'alpha', from: 0.2, to: 0.5, periodMs: 1000 };
const outlined = (extra = {}) => ({
id: 'o',
geometry: { kind: 'polygon', rings: [ring] },
outline: [1, 1, 1, 0.5],
outlineOnly: true,
animate: pulse,
height: 'absolute',
...extra,
});
set.set([outlined()]);
const host = scene.added[0];
set.upsert([outlined({ meta: 'm' })]);
assert.equal(scene.added[0], host, 'not rebuilt');
set.set([
{
id: 'nothing',
geometry: { kind: 'polygon', rings: [ring] },
animate: pulse,
height: 'absolute',
},
]);
frame();
asked.length = 0;
frame();
assert.equal(asked.length, 0, 'nothing drawn, no frames asked');
set.dispose();
}));
test('an unclamped shape recoloured before it is drawn rebuilds; frames stop once nothing pulses', () =>
withTerrainHeights(() => {
const scene = fakeScene();
const asked = [];
scene.requestRender = () => asked.push(1);
const { draw, frame } = drawOver(scene);
const set = draw.shapes({ id: 'early' });
const ring = [
{ lon: 0, lat: 0, height: 10 },
{ lon: 0.001, lat: 0, height: 10 },
{ lon: 0.001, lat: 0.001, height: 10 },
];
const column = (alpha, extra = {}) => ({
id: 'column',
geometry: { kind: 'polygon', rings: [ring] },
fill: [1, 0.7, 0.3, alpha],
extrudeHeight: 100,
height: 'absolute',
...extra,
});
set.set([column(0.4)]);
const host = scene.added[0];
set.upsert([column(0.5)]);
assert.notEqual(scene.added.at(-1), host, 'not yet drawn: rebuilt');
// Pulsing, it asks for frames; hidden, it stops.
const pulse = { property: 'alpha', from: 0.2, to: 0.5, periodMs: 1000 };
set.set([column(0.5, { animate: pulse })]);
asked.length = 0;
frame();
assert.ok(asked.length > 0, 'a pulse asks for frames');
set.set([column(0.5, { animate: pulse, show: false })]);
frame();
asked.length = 0;
frame();
assert.equal(asked.length, 0, 'nothing pulses, no frames asked');
set.dispose();
}));
test('the earthquakes layer draws its discs as ground fills on a viewer of its own', async () => {
const { createEarthquakesLayer } =
await import('../../layers/earthquakes/index.js');
@@ -6661,8 +6925,8 @@ test('a clamped fill that moves or resizes is drawn where it now is in the same
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] })]);
// A new item: the set rebuilds.
set.upsert([box('a', 2), box('z', 3)]);
assert.notEqual(scene.added.at(-1), host, 'rebuilt');
set.dispose();
assert.equal(scene.added.length, 0);
@@ -6693,8 +6957,8 @@ test('a moved clamped fill is hidden where a rebuild in flight still draws it',
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])]);
// A new item rebuilds; the old host draws on until the new is ready.
set.upsert([box('a', 1, [1, 0, 0, 0.5]), box('z', 0.5)]);
const second = scene.added.find((p) => p !== first);
assert.ok(
second && scene.added.includes(first),
@@ -6834,11 +7098,11 @@ test('a moved clamped fill shows again where the retiring host draws it when the
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] })]);
set.upsert([groundBox('a', 1), groundBox('z', 0.5)]);
set.upsert([groundBox('a', 2)]);
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] })]);
set.upsert([groundBox('a', 2), groundBox('y', 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');
@@ -6909,7 +7173,7 @@ test('clamped fills drawn alone by a host still retiring count toward MOVING_SHA
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] })]);
set.upsert([groundBox('new', 1)]);
assert.equal(scene.added.length, 2, 'rebuilding, the old host retiring');
const second = scene.added.find((p) => p !== first);
const before = second.length;