Draw the Global Context range ring and its pulses through the draw port

The ring, its sonar fronts and the band behind them leave Cesium: the
ring and each front are one solid line with an outline, the band is one
flat unpickable ellipse at the rim filled with a canvas painted from the
pulse radii. rangeRingCesium.js goes, and the Cesium allow-list is empty.

Solid lines take an optional outline ({color, widthPx}, inside the
line's width on each side), drawn in the line's own pass so a
translucent halo never darkens where it meets the core; both colours
change in place, and outlined lines never settle into the static batch.
An unclamped image fill takes a new canvas in place: copied into its
texture when the size is the same.

A parked view steps the pulses from the 80 ms timer before it asks for
the frame that draws them, so it draws one frame a step; frames the
camera moves in, or held drawing, step them every frame, and the band is
painted at most every 80 ms. The lines reference scene gains an outlined
line.
This commit is contained in:
Sameh Khamis committed 2026-11-10 07:48:07 -08:00
1 parent c8346c9fda
commit 74d6d97b29
16 files changed
+1379 -1191

No files matched your search

+106 -14
View File
@@ -1777,7 +1777,18 @@ export function createCesiumDraw({
source: FLOW_SOURCE,
};
case 'solid':
return { type: 'Color', uniforms: { color: toColor(style.color) } };
// An outlined line keeps its edge in one material: two lines
// stacked would darken where their translucent passes overlap.
return style.outline
? {
type: 'PolylineOutline',
uniforms: {
color: toColor(style.color),
outlineColor: toColor(style.outline.color),
outlineWidth: style.outline.widthPx,
},
}
: { type: 'Color', uniforms: { color: toColor(style.color) } };
case 'glow':
return {
type: 'PolylineGlow',
@@ -2030,7 +2041,7 @@ export function createCesiumDraw({
if (!item.overlaid || !deep || revealed(item)) return false;
if (!flatEquals(item.positions, part.overlayFrom)) return false;
// Only its colour may have changed (`patchLines` checked its look).
deep.appearance.material.uniforms.color = toColor(item.style.color);
paintLine(deep.appearance.material, item.style);
deep.depthFailAppearance.material.uniforms.color = toColor(
item.style.color,
);
@@ -2266,6 +2277,9 @@ export function createCesiumDraw({
// change it takes in place: kept for lines that can settle.
shape: settled ? looks.shape(item) : null,
positions: settled ? flatPositions(item.positions) : null,
// An outlined line, which never settles, takes its colours in
// place too: how it draws but for them.
outlined: item.style.outline ? looks.shape(item) : null,
id,
polyline,
reveal,
@@ -2343,6 +2357,23 @@ export function createCesiumDraw({
replaceHost(record, collect(own, ground), ground, own);
}
/** A line style but for its colours, which its material takes in place. */
const colourlessStyle = (style) =>
style.outline
? {
...style,
color: undefined,
outline: { ...style.outline, color: undefined },
}
: { ...style, color: undefined };
/** Give a line's material its style's colours, its outline's too. */
function paintLine(material, style) {
material.uniforms.color = toColor(style.color);
if (style.outline && 'outlineColor' in material.uniforms)
material.uniforms.outlineColor = toColor(style.outline.color);
}
/**
* How a line draws, but for where it runs, whether it shows and how much
* of it is revealed: lines that keep it can be changed in place.
@@ -2352,7 +2383,7 @@ export function createCesiumDraw({
item.mode,
item.widthPx,
item.clampTo,
colourless ? { ...item.style, color: undefined } : item.style,
colourless ? colourlessStyle(item.style) : item.style,
// The same depth-fail style in any key order.
item.depthFail
? [
@@ -2471,11 +2502,15 @@ export function createCesiumDraw({
/**
* Whether an undraped line that can settle (in the collection or a
* static batch) changes only its colour: it takes the new one in place.
* static batch), or an outlined one in the collection, changes only its
* colours: it takes the new ones in place.
*/
const recolourableSolid = (was, item, looks) =>
Boolean((was.polyline || was.batched) && was.shape) &&
was.shape === looks.shape(item);
(Boolean((was.polyline || was.batched) && was.shape) &&
was.shape === looks.shape(item)) ||
(Boolean(was.polyline && was.outlined) &&
!revealed(item) &&
was.outlined === looks.shape(item));
/**
* Whether a line drawn over the surface for a draped one changes only its
@@ -2588,8 +2623,7 @@ export function createCesiumDraw({
if (!shared) return false;
// Shared materials take their one colour once, before lines move off
// them.
for (const [material, item] of shared)
material.uniforms.color = toColor(item.style.color);
for (const [material, item] of shared) paintLine(material, item.style);
for (let k = 0; k < list.length; k++) {
const item = list[k];
const was = parts.get(item.id);
@@ -2600,8 +2634,7 @@ export function createCesiumDraw({
}
if (was.look !== looks[k] && was.deep === undefined) {
// A line drawing on a shared material took its colour above.
if (!was.shared || was.solo)
was.material.uniforms.color = toColor(item.style.color);
if (!was.shared || was.solo) paintLine(was.material, item.style);
was.look = looks[k];
}
if (was.deep !== undefined) {
@@ -2697,6 +2730,8 @@ export function createCesiumDraw({
item.frame !== 'inertial' &&
!item.depthFail &&
item.style.kind === 'solid' &&
// A batch draws one colour per line, no outline.
!item.style.outline &&
!revealed(item) &&
// The collection draws no line under half a pixel wide; a batch
// would.
@@ -2792,8 +2827,7 @@ export function createCesiumDraw({
const positions = linePositions(item);
was.polyline.positions = positions;
was.polyline.show = show;
if (recolour)
was.polyline.material.uniforms.color = toColor(item.style.color);
if (recolour) paintLine(was.polyline.material, item.style);
if (was.reveal)
was.reveal.uniforms.reveal = revealUniform(
item.revealFraction,
@@ -3575,6 +3609,7 @@ export function createCesiumDraw({
* hides it: a colour drawn opaque cannot become translucent in place.
*/
function solidPatchable(part, item) {
if (part.image) return imagePatchable(part, item);
if (!colourOnly(part.look, item)) return false;
if (part.fill?.isDestroyed?.() || part.outline?.isDestroyed?.())
return false;
@@ -3594,6 +3629,52 @@ export function createCesiumDraw({
);
}
/**
* Whether an unclamped image fill's upsert changes only its image (a
* canvas, under a new key for new content), its show or its `meta`: its
* texture takes a new canvas in place.
*/
function imagePatchable(part, item) {
const fill = item.fill;
if (!fill || typeof fill !== 'object' || Array.isArray(fill)) return false;
if (!fill.image?.canvas || item.animate || part.pulse) return false;
if (!part.fill || part.fill.isDestroyed() || !part.fill.ready) return false;
const { fill: was, ...look } = part.look;
const { fill: now, show, meta, ...next } = item;
return (
sameValues(look, next) &&
sameValues(was.repeat ?? null, fill.repeat ?? null)
);
}
/**
* An image fill's texture takes `canvas`: copied into the texture it has
* where the size is the same, else a texture of its own, which the
* material holds (and destroys) from this frame on. Without a drawing
* context (a scene with no WebGL), the material takes the canvas, as it
* did when built.
*/
function writeImage(part, canvas) {
const image = part.image;
const context = scene.context;
if (!context) {
image.material.uniforms.image = canvas;
return;
}
const texture = image.texture;
if (
texture &&
!texture.isDestroyed() &&
texture.width === canvas.width &&
texture.height === canvas.height
) {
texture.copyFrom({ source: canvas });
return;
}
image.texture = new Cesium.Texture({ context, source: canvas });
image.material.uniforms.image = image.texture;
}
/** An instance's colour, in a ready primitive. */
function colourOn(primitive, id, color) {
const attributes = primitive.getGeometryInstanceAttributes(id);
@@ -3605,6 +3686,9 @@ export function createCesiumDraw({
/** An unclamped shape changed in place: its colours, pulse, show and `meta`. */
function patchSolid(part, item) {
const image = part.image ? item.fill.image : null;
if (image && (image.key !== part.look.fill.image.key || !image.key))
writeImage(part, image.canvas);
part.look = lookOf(item);
part.pulse = pulseOf(item);
part.id.meta = item.meta;
@@ -3632,7 +3716,12 @@ export function createCesiumDraw({
? Cesium.Color.clone(base, result)
: base.withAlpha(alpha, result);
};
if (look.fill && part.fill?.ready && !part.fill.isDestroyed())
if (
look.fill &&
!part.image &&
part.fill?.ready &&
!part.fill.isDestroyed()
)
colourOn(part.fill, part.id, tint(look.fill, scratchColour));
if (look.outline) {
if (part.outline?.ready && !part.outline.isDestroyed())
@@ -3941,7 +4030,10 @@ export function createCesiumDraw({
const material = ownMaterial(
imageMaterial(toImage(imageFill.image), imageFill.repeat),
);
host.add(
// Its image is changed in place (`writeImage`); it never recolours.
part.image = { material, texture: null };
part.translucent.fill = true;
part.fill = host.add(
new Cesium.Primitive({
geometryInstances: new Cesium.GeometryInstance({
geometry: shape.fill,
+105 -11
View File
@@ -2208,19 +2208,24 @@ test('the context layer draws its static range ring as a haloed line on a viewer
const linesOf = (from = scene) => {
const [host] = from.added;
const lines = [];
for (let i = 0; i < host.length; i++) {
const collection = host.get(i);
if (!host) return lines;
// Lines that never settle are drawn by the collection alone.
const collections =
host instanceof Cesium.PolylineCollection
? [host]
: Array.from({ length: host.length }, (_, i) => host.get(i));
for (const collection of collections) {
if (!(collection instanceof Cesium.PolylineCollection)) continue;
for (let j = 0; j < collection.length; j++)
lines.push(collection.get(j));
}
return lines.sort((x, y) => y.width - x.width);
};
const [halo, core] = linesOf();
assert.equal(linesOf().length, 2, 'a halo and a core, nothing else');
assert.equal(halo.width, 4);
assert.equal(core.width, 3);
assert.equal(core.material.type, 'Color');
const [core] = linesOf();
assert.equal(linesOf().length, 1, 'one line, its halo its outline');
assert.equal(core.width, 4);
assert.equal(core.material.type, 'PolylineOutline');
assert.equal(core.material.uniforms.outlineWidth, 1);
assert.ok(
core.material.uniforms.color.equalsEpsilon(
Cesium.Color.fromCssColorString('rgba(98, 181, 255, 0.95)'),
@@ -2228,7 +2233,7 @@ test('the context layer draws its static range ring as a haloed line on a viewer
),
);
assert.ok(
halo.material.uniforms.color.equalsEpsilon(
core.material.uniforms.outlineColor.equalsEpsilon(
Cesium.Color.fromCssColorString('rgba(4, 12, 16, 0.8)'),
1e-9,
),
@@ -2247,15 +2252,15 @@ test('the context layer draws its static range ring as a haloed line on a viewer
// Small moves keep the ring; one past 250 m moves it.
const first = core.positions[0];
rendering.renderVisual(subject(0.001));
assert.ok(Cesium.Cartesian3.equals(linesOf()[1].positions[0], first));
assert.ok(Cesium.Cartesian3.equals(linesOf()[0].positions[0], first));
rendering.renderVisual(subject(0.01));
assert.ok(!Cesium.Cartesian3.equals(linesOf()[1].positions[0], first));
assert.ok(!Cesium.Cartesian3.equals(linesOf()[0].positions[0], first));
// Re-initialized on another viewer, the same subject's ring moves there.
const other = drawOver().scene;
state.viewer = { scene: other };
rendering.renderVisual(subject(0.01));
assert.equal(scene.added.length, 0, 'gone from the old viewer');
assert.equal(linesOf(other).length, 2, 'drawn on the new one');
assert.equal(linesOf(other).length, 1, 'drawn on the new one');
rendering.clearVisual();
assert.equal(linesOf(other).length, 0);
rendering.disposeVisual();
@@ -2929,6 +2934,43 @@ test('many lines left beside a static batch settle as soon as they are unchanged
set.dispose();
}));
test('an outlined solid line draws one outline material, recoloured in place, and never settles', () =>
withImageClasses(() => {
const s = settlingSet();
const outlined = (core, halo) =>
settleStem('ring', 100, {
widthPx: 4,
style: {
kind: 'solid',
color: core,
outline: { color: halo, widthPx: 1 },
},
});
s.set.set([outlined([0, 0.5, 1, 1], [0, 0, 0, 0.8]), settleStem('a', 200)]);
const line = () => {
const polylines = s.polylines();
for (let i = 0; i < polylines.length; i++)
if (polylines.get(i).id?.id === 'ring') return polylines.get(i);
return null;
};
const first = line();
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.material.uniforms.color.alpha, 0.4);
assert.ok(
Math.abs(first.material.uniforms.outlineColor.alpha - 0.32) < 1e-9,
);
s.settle();
const [batch] = s.batches();
assert.deepEqual(
batch.geometryInstances.map((one) => one.id.id),
['a'],
);
s.set.dispose();
}));
test('a line under half a pixel wide, which the collection does not draw, never settles', () =>
withImageClasses(() => {
const s = settlingSet();
@@ -4149,6 +4191,58 @@ test('an unclamped shape filled with an image draws a textured primitive and fre
);
}));
test('an unclamped image fill takes a new canvas in place; anything else about it rebuilds', () =>
withImageClasses(() => {
const scene = fakeScene();
const draw = createCesiumDraw({
scene,
report: (error) => assert.fail(error),
}).port;
const set = draw.shapes({ id: 'band' });
const canvases = [new HTMLCanvasElement(), new HTMLCanvasElement()];
const band = (k, extra = {}) => ({
id: 'band',
geometry: {
kind: 'ellipse',
center: { lon: 10, lat: 20, height: 2500 },
semiMajor: 50_000,
semiMinor: 50_000,
},
fill: { image: { canvas: canvases[k % 2], key: `band:${k}` } },
height: 'absolute',
...extra,
});
set.set([band(0)]);
const host = scene.added[0];
const primitive = host.get(0);
const material = primitive.appearance.material;
assert.equal(material.uniforms.image, canvases[0]);
// Not yet drawn, it rebuilds.
set.upsert([band(1)]);
assert.notEqual(scene.added[0], host, 'not yet drawn: rebuilt');
const drawn = scene.added[0].get(0);
drawn._ready = true;
const drawnMaterial = drawn.appearance.material;
// Drawn, a new canvas, its show and its meta change in place; with no
// drawing context the material takes the canvas itself.
set.upsert([band(2, { meta: 'm', show: false })]);
assert.equal(scene.added[0].get(0), drawn, 'changed in place');
assert.equal(drawnMaterial.uniforms.image, canvases[0]);
assert.equal(drawn.show, false);
set.upsert([band(3)]);
assert.equal(scene.added[0].get(0), drawn);
assert.equal(drawnMaterial.uniforms.image, canvases[1]);
assert.equal(drawn.show, true);
// The same key is the same image: nothing is written.
drawnMaterial.uniforms.image = 'untouched';
set.upsert([band(3)]);
assert.equal(drawnMaterial.uniforms.image, 'untouched');
// Another shape or height rebuilds.
set.upsert([{ ...band(4), height: 'clampGround' }]);
assert.notEqual(scene.added[0].get?.(0), drawn, 'rebuilt');
set.dispose();
}));
test('a draped rectangle turns, its image fill with it, on a ground primitive that owns its material', () =>
withImageClasses(() => {
const scene = fakeScene();