Colour and show draped line segments in place through the Cesium draw port

A draped solid line with segment colours or shows draws each segment as a
ground polyline instance of its own in the set's ground batch, under a
pick id that reads as the line's, so changing its colours or which
segments show writes their attributes without a rebuild; a change made
while the ground still builds is written as it swaps in. Undraped or
non-solid segmented lines are refused, and a segmented line of no length
builds no ground. The shared conformance suite changes a segmented
draped line every frame.
This commit is contained in:
Sameh Khamis committed 2026-11-07 07:41:00 -08:00
1 parent 8cefc8cc4a
commit e8872ab419
4 files changed
+298 -4

No files matched your search

+110 -2
View File
@@ -1317,6 +1317,15 @@ export function createCesiumDraw({
throw new Error(
'the Cesium renderer drapes solid, glow, dash and flow lines only',
);
else if (
segmented(item) &&
(item.mode !== 'draped' ||
item.style.kind !== 'solid' ||
revealed(item))
)
throw new Error(
'the Cesium renderer colours and shows segments of draped solid lines only',
);
const polylines = new Cesium.PolylineCollection();
const draped = new Map();
// Draped lines a batch cannot draw, each with a primitive of its own.
@@ -1349,6 +1358,15 @@ export function createCesiumDraw({
solo: false,
};
parts.set(item.id, part);
if (segmented(item)) {
// A line of no length builds no ground primitive (it would
// never be ready, and hold the swap).
const instances = segmentInstances(record, item, part);
if (!instances.length) continue;
if (!draped.has(clampTo)) draped.set(clampTo, []);
draped.get(clampTo).push(...instances);
continue;
}
if (!batchable(item)) {
alone.push({ item, part });
continue;
@@ -1430,8 +1448,9 @@ export function createCesiumDraw({
item.clampTo,
item.style,
item.depthFailColor,
item.segmentColors,
item.segmentShow,
// Segment colours and shows change in place.
item.segmentColors != null,
item.segmentShow != null,
revealed(item),
item.frame,
]);
@@ -1480,10 +1499,14 @@ export function createCesiumDraw({
for (const item of list) {
const was = parts.get(item.id);
if (!was || was.look !== lineLook(item)) return false;
// A segmented line that drew nothing is built anew.
if (was.segments && !was.ground) return false;
if (was.deep !== undefined && record.deepLines?.isDestroyed())
return false;
if (!was.clampTo) continue;
const flat = flatPositions(item.positions);
// A segmented line that moves is built anew, segment by segment.
if (was.segments && !samePositions(flat, was.positions)) return false;
if (!drapedPatchable(record, was, flat)) return false;
flats.set(item.id, flat);
}
@@ -1517,6 +1540,12 @@ export function createCesiumDraw({
continue;
}
was.id.meta = item.meta;
if (was.segments) {
// A newer write supersedes one queued while the ground built.
was.pendingItem = was.ground.ready ? undefined : item;
if (was.ground.ready) writeSegments(was, item);
continue;
}
const flat = flats.get(item.id);
if (!samePositions(flat, was.positions)) {
moveDraped(record, was, item, flat);
@@ -1614,6 +1643,85 @@ export function createCesiumDraw({
showPart(part, part.pendingShow);
part.pendingShow = undefined;
}
if (part.pendingItem && part.ground && !part.ground.isDestroyed()) {
writeSegments(part, part.pendingItem);
part.pendingItem = undefined;
}
}
}
/** Whether a line is drawn segment by segment, in colours or shows of their own. */
const segmented = (item) =>
item.segmentColors != null || item.segmentShow != null;
/** A segment's colour and whether it shows. */
const segmentLook = (item, j) => ({
color: toColor(item.segmentColors?.[j] ?? item.style.color),
show: item.show !== false && item.segmentShow?.[j] !== false,
});
/** What a segment draws, to tell whether an upsert changes it. */
const segmentKey = ({ color, show }) =>
`${color.red},${color.green},${color.blue},${color.alpha},${show}`;
/**
* A draped line drawn segment by segment: a ground polyline instance a
* segment, each with a pick id of its own that reads as the line's (and
* that height queries see through), so its colour and show change in
* place. A segment of no length draws nothing.
*/
function segmentInstances(record, item, part) {
const points = linePositions(item);
const instances = [];
part.segments = [];
part.drawn = [];
for (let j = 0; j + 1 < points.length; j++) {
if (Cesium.Cartesian3.equals(points[j], points[j + 1])) {
part.segments.push(null);
part.drawn.push(null);
continue;
}
const id = Object.create(part.id);
record.pickIds.push(id);
const look = segmentLook(item, j);
part.segments.push(id);
part.drawn.push(segmentKey(look));
instances.push(
new Cesium.GeometryInstance({
geometry: new Cesium.GroundPolylineGeometry({
positions: [points[j], points[j + 1]],
width: item.widthPx,
arcType: Cesium.ArcType.GEODESIC,
}),
attributes: {
color: Cesium.ColorGeometryInstanceAttribute.fromColor(look.color),
show: new Cesium.ShowGeometryInstanceAttribute(look.show),
},
id,
}),
);
}
return instances;
}
/** A segmented line's colours and shows, on its ready ground primitive. */
function writeSegments(part, item) {
for (let j = 0; j < part.segments.length; j++) {
const id = part.segments[j];
if (!id) continue;
const look = segmentLook(item, j);
const key = segmentKey(look);
if (key === part.drawn[j]) continue;
part.drawn[j] = key;
const attributes = part.ground.getGeometryInstanceAttributes(id);
attributes.color = Cesium.ColorGeometryInstanceAttribute.toValue(
look.color,
attributes.color,
);
attributes.show = Cesium.ShowGeometryInstanceAttribute.toValue(
look.show,
attributes.show,
);
}
}
+137 -2
View File
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import test from 'node:test';
import { bindRenderer, drawOf, findDraw } from '../rendererOf.js';
import { createCesiumDraw, revealUniform } from './draw.js';
import { createCesiumDraw, revealUniform, unboundDrawPart } from './draw.js';
import { drawHitOf } from './drawHit.js';
// Node has no WebGL context to report line-width limits.
@@ -1532,6 +1532,138 @@ test('lines that change only whether they show, or where an undraped one runs, c
set.dispose();
}));
test('a draped line with segment colours and shows draws a ground instance a segment, changed in place', () =>
withImageClasses(() => {
const { scene, draw, frame } = drawOver();
const set = draw.lines({ id: 'segments', pickable: false });
const trail = (alpha, shows) => ({
id: 'trail',
positions: [
{ lon: 0, lat: 0 },
{ lon: 0.001, lat: 0 },
{ lon: 0.001, lat: 0 },
{ lon: 0.002, lat: 0 },
],
widthPx: 3,
mode: 'draped',
style: { kind: 'solid', color: '#ffffff' },
segmentColors: [
[0, 1, 0, alpha],
[0, 1, 0, alpha],
[1, 0, 0, alpha],
],
segmentShow: shows,
});
set.set([
trail(0.8, [true, false, false]),
{
id: 'plain',
positions: [
{ lon: 1, lat: 0 },
{ lon: 1.001, lat: 0 },
],
widthPx: 2,
mode: 'draped',
style: { kind: 'solid', color: '#7fe6ed' },
},
]);
const host = scene.added[0];
const ground = host.get(1);
assert.ok(ground instanceof Cesium.GroundPolylinePrimitive);
assert.equal(ground.allowPicking, false);
// Two segments with length, and the plain line, in one primitive.
const instances = ground.geometryInstances;
assert.equal(instances.length, 3);
const [first, last] = instances;
assert.equal(first.id.id, 'trail', 'picks read as the line');
assert.equal(first.id.set, 'segments');
assert.notEqual(first.id, last.id);
assert.deepEqual(
[...first.attributes.color.value],
[0, 255, 0, Math.round(0.8 * 255)],
);
assert.deepEqual([...first.attributes.show.value], [1]);
assert.deepEqual([...last.attributes.color.value].slice(0, 3), [255, 0, 0]);
assert.deepEqual([...last.attributes.show.value], [0]);
const attributes = new Map(
instances.map((instance) => [
instance.id,
{
color: Uint8Array.from(instance.attributes.color.value),
show: Uint8Array.from(instance.attributes.show.value),
},
]),
);
let writes = 0;
ground.getGeometryInstanceAttributes = (id) => {
writes += 1;
return attributes.get(id);
};
// Still building: queued, and written as it swaps in.
set.upsert([trail(0.5, [true, false, true])]);
assert.equal(scene.added[0], host, 'not rebuilt');
assert.equal(writes, 0);
Object.defineProperty(ground, 'ready', { value: true });
frame();
assert.equal(attributes.get(last.id).color[3], Math.round(0.5 * 255));
assert.deepEqual([...attributes.get(last.id).show], [1]);
// Ready: only what changed is written.
writes = 0;
set.upsert([trail(0.5, [false, false, true])]);
assert.equal(scene.added[0], host);
assert.equal(writes, 1);
assert.deepEqual([...attributes.get(first.id).show], [0]);
writes = 0;
set.upsert([trail(0.5, [false, false, true])]);
assert.equal(writes, 0, 'nothing changed, nothing written');
// Shown in place after a queued write: the queued one does not return.
const again = draw.lines({ id: 'segments-again' });
again.set([trail(0.8, [true, true, true])]);
const queued = scene.added.at(-1).get(1);
const shows = new Map(
queued.geometryInstances.map((instance) => [
instance.id,
{ color: new Uint8Array(4), show: new Uint8Array([1]) },
]),
);
queued.getGeometryInstanceAttributes = (id) => shows.get(id);
again.upsert([trail(0.8, [false, false, false])]);
Object.defineProperty(queued, 'ready', { value: true });
again.upsert([trail(0.8, [true, true, true])]);
frame();
for (const { show } of shows.values()) assert.deepEqual([...show], [1]);
again.dispose();
// A line of no length draws nothing and holds no swap.
const none = draw.lines({ id: 'segments-none' });
const still = {
...trail(1, [true]),
positions: [
{ lon: 3, lat: 0 },
{ lon: 3, lat: 0 },
],
segmentColors: ['#fff'],
};
none.set([still]);
const empty = scene.added.at(-1);
assert.ok(
empty instanceof Cesium.PolylineCollection,
'no ground primitive',
);
none.upsert([{ ...still, positions: trail(1, []).positions }]);
assert.notEqual(scene.added.at(-1), empty, 'drawn once it has length');
none.dispose();
// Height queries see through every segment.
const { pickIds } = unboundDrawPart(draw).sets.get('segments');
assert.ok(pickIds.includes(first.id) && pickIds.includes(last.id));
// Undraped segmented lines are refused, and the set keeps drawing.
assert.throws(
() => set.upsert([{ ...trail(1, [true, true, true]), mode: '3d' }]),
/segments of draped solid lines only/,
);
assert.equal(scene.added[0], host);
set.dispose();
}));
test('a draped line shown after it is ready keeps that show through the swap', () =>
withImageClasses(() => {
const { draw, frame, scene } = drawOver();
@@ -3319,7 +3451,10 @@ test('a contact trail draws its history and moving head as depth-fail geodesic l
Cesium.Cartesian3.fromDegrees(-63, 30, 10000),
]);
const mixed = deep.get(0).geometryInstances.geometry._positions;
assert.ok(mixed.length > 400 + 25 && mixed.length < 400 + 40, `${mixed.length}`);
assert.ok(
mixed.length > 400 + 25 && mixed.length < 400 + 40,
`${mixed.length}`,
);
// The head moves every frame alone, under one pick id.
const head = (lon) => [
Cesium.Cartesian3.fromDegrees(-120, 30, 10000),
+1
View File
@@ -42,6 +42,7 @@ export const CESIUM_CHECKS = new Set([
'draw.icons.update',
'draw.lines.styles',
'draw.lines.drape',
'draw.lines.segments',
'draw.ids',
'draw.models',
'draw.motion',