A line can be drawn as several runs (breaks) (#1895)

* A line can be drawn as several runs (`breaks`)

LineItem.breaks (capability lineBreaks) lists the positions that start a new
run of a line: the segment into each is not drawn nor picked, and the line
does not join across it, each run having ends of its own. Many lines in one
style, such as many moving objects' trails, can then be sent as one item.
Segment colours and shows stay one a pair of positions, age times one a
position, and the reveal runs along the runs in order. The Cesium port draws
a line in runs a piece a run, draped or not, and lists the capability; the
stub picks the runs only; the conformance check draw.lines.breaks pins it.

* A line in runs is cut by time run by run, whatever their order

The conformance check draw.lines.breaks.timeWindow draws a line in runs,
draped and not, whose first run is later than its second, and reads each
run drawn and cut by its own times as the scene clock moves.

It found that a plain line drawn in a polyline collection after an aged one
was the page's first of its style's type stopped the frame: Cesium keeps a
material type's uniform names from the first material of it, and a part of
a composite material has them renumbered. A part's type is now made on its
own first.

* Changelog for line breaks (#1895)
This commit is contained in:
Sameh Khamis authored and GitHub committed 2026-11-21 13:04:19 -08:00
1 parent e4caf5d3af
commit d9d79ebc4d
15 files changed
+868 -29

No files matched your search

+167 -26
View File
@@ -603,6 +603,47 @@ function checkAgeFade(item) {
);
}
/**
* Make a material of `fabric`'s type on its own once, before one is a part
* of a composite material. Cesium keeps a type's uniform names, for all the
* page's materials of that type, from the first it makes, and a part has
* them numbered as the composite's: a plain material of that type made
* later then fails in a polyline collection (`material._uniforms[uniform]
* is not a function`), stopping the frame.
* @param {{type: string}} fabric
*/
function ownUniforms(fabric) {
if (!Cesium.Material._uniformList[fabric.type])
new Cesium.Material({ fabric }).destroy();
}
/**
* A line's `breaks`, checked: strictly increasing whole position indices,
* each past the first position and within the line (`0 < i < count`).
* Refused otherwise.
* @param {import('../contract/draw').LineItem} item
*/
function checkBreaks(item) {
const breaks = item.breaks;
if (breaks == null) return;
const count =
item.positions instanceof Float64Array
? item.positions.length / 3
: item.positions.length;
let previous = 0;
const fits =
(Array.isArray(breaks) || breaks instanceof Uint32Array) &&
Array.prototype.every.call(breaks, (at) => {
const ok = Number.isInteger(at) && at > previous && at < count;
previous = at;
return ok;
});
if (!fits)
throw new TypeError(
'a line breaks at whole, increasing position indices after its first position and before its end',
);
}
/**
* The `flow` lines' clock, seconds, for a line flowing at `speed` cells a
* second: wrapped to one cell's time, which draws the same dashes, so the
@@ -683,6 +724,8 @@ export function createCesiumDraw({
const ageFades = capabilities.has('lineAgeFade');
// And draw only within a span of the scene clock (`LineAgeFade.showMs`).
const timeWindows = ageFades && capabilities.has('lineTimeWindow');
// And as several runs, not joined across their breaks (`LineItem.breaks`).
const lineBreaks = capabilities.has('lineBreaks');
// Fields share the sets' ids.
const fieldPart = createCesiumFields({
scene,
@@ -772,6 +815,7 @@ export function createCesiumDraw({
if (item.style?.kind === 'split' && item.mode !== 'draped')
throw new TypeError('a split line is draped');
if (ageFades) checkAgeFade(item);
if (lineBreaks) checkBreaks(item);
}
if (kind !== 'shapes') return;
for (const item of list) pulseOf(item);
@@ -2531,14 +2575,17 @@ export function createCesiumDraw({
? { translucent: true }
: {};
let built = template(fabric);
if (reveal !== undefined)
if (reveal !== undefined) {
ownUniforms(built);
built = {
type: `${REVEAL_MATERIAL}_${style.kind}`,
materials: { revealed: built },
uniforms: { reveal },
source: REVEAL_SOURCE,
};
if (age)
}
if (age) {
ownUniforms(built);
built = {
type: `${AGE_MATERIAL}_${reveal === undefined ? '' : 'revealed_'}${style.kind}`,
materials: { aged: built },
@@ -2549,6 +2596,7 @@ export function createCesiumDraw({
},
source: AGE_SOURCE,
};
}
const material = new Cesium.Material({ fabric: built, ...translucent });
const drawn = styleMaterial(material);
Object.assign(drawn.uniforms, fabric.uniforms);
@@ -3048,7 +3096,47 @@ export function createCesiumDraw({
!revealed(item) &&
!aged(item)
)) ||
(item.frame === 'inertial' && item.mode !== 'draped' && item.depthFail);
(item.frame === 'inertial' && item.mode !== 'draped' && item.depthFail) ||
// A line in runs with no segments of its own: a piece a run.
(broken(item) && !segmented(item));
/** Whether a line is drawn in runs, not joined across its breaks. */
const broken = (item) =>
lineBreaks && item.breaks != null && item.breaks.length > 0;
/** The positions that start a run of a line in runs, or null. */
const runStarts = (item) => (broken(item) ? new Set(item.breaks) : null);
/**
* Where a line drawn piece by piece is cut into pieces, by position
* index: `[a, b]` from position `a` to `b`, `j` its segment (its pair of
* positions, for its colour and show), `last` whether it ends its run (an
* arrow's head is drawn there). A segmented line has a piece a segment,
* but for those across a break; one in runs a piece a run; any other one
* piece. A run of one position draws nothing, and has no piece.
*/
function pieceSpans(item, count, bySegment) {
const starts = runStarts(item);
const spans = [];
if (bySegment) {
for (let j = 0; j + 1 < count; j++)
if (!starts?.has(j + 1))
spans.push({
a: j,
b: j + 1,
j,
last: j + 2 >= count || Boolean(starts?.has(j + 2)),
});
return spans;
}
if (!starts) return [{ a: 0, b: count - 1, j: 0, last: true }];
let a = 0;
for (const at of [...starts, count]) {
if (at - 1 > a) spans.push({ a, b: at - 1, j: -1, last: true });
a = at;
}
return spans;
}
/**
* The style a segment of a line draws in: the line's, in the segment's
@@ -3067,11 +3155,14 @@ export function createCesiumDraw({
* A line drawn piece by piece: a segment a piece for a segmented line
* (each its own primitive and material, under a pick id of its own that
* reads as the line's, so a segment's colour and show change in place),
* else one piece for the whole line. A revealed line reveals each piece
* by its share of the line's length. Draped pieces are ground polylines.
* Inertial pieces turn with the sky (`turnInertial`).
* a run a piece for a line in runs (`pieceSpans`), else one piece for
* the whole line. A revealed line reveals each piece by its share of the
* line's length. Draped pieces are ground polylines; the undraped pieces
* of a line in runs with no depth-fail style, in the Earth's frame, are
* polylines in the set's collection (`polylines`), the rest primitives
* in `host`. Inertial pieces turn with the sky (`turnInertial`).
*/
function linePieces(record, item, host, groundOut) {
function linePieces(record, item, host, groundOut, polylines) {
const id = pickId(record, item);
// Its own positions: a geodesic piece is cut along its arc as it is drawn.
const points = linePositions({ ...item, mode: '3d' });
@@ -3084,21 +3175,30 @@ export function createCesiumDraw({
pieces: [],
};
const bySegment = segmented(item);
const spans = bySegment
? points.slice(1).map((_, j) => [j, j + 1])
: [[0, points.length - 1]];
const lengths = spans.map(([a, b]) => {
const spans = pieceSpans(item, points.length, bySegment);
const lengths = spans.map(({ a, b }) => {
let length = 0;
for (let k = a; k < b; k++)
length += Cesium.Cartesian3.distance(points[k], points[k + 1]);
return length;
});
// The revealed length runs along the pieces, the gaps between runs
// having none.
const total = lengths.reduce((sum, length) => sum + length, 0);
// Several pieces of one line: each under a pick id of its own that
// reads as the line's.
const several = bySegment || spans.length > 1;
// Runs of a line drawn in the collection, as a line of one run is.
const inCollection =
broken(item) &&
item.mode !== 'draped' &&
!item.depthFail &&
item.frame !== 'inertial';
let start = 0;
spans.forEach(([a, b], j) => {
spans.forEach(({ a, b, j, last }, k) => {
const positions = points.slice(a, b + 1);
const begins = start;
start += lengths[j];
start += lengths[k];
if (!drawable(positions)) {
part.pieces.push(null);
return;
@@ -3109,16 +3209,16 @@ export function createCesiumDraw({
1,
Math.max(
0,
(item.revealFraction * total - begins) / (lengths[j] || 1),
(item.revealFraction * total - begins) / (lengths[k] || 1),
),
)
: 1;
const pieceId = bySegment ? Object.create(id) : id;
if (bySegment) record.pickIds.push(pieceId);
const pieceId = several ? Object.create(id) : id;
if (several) record.pickIds.push(pieceId);
const sub = {
...item,
positions,
style: pieceStyle(item, j, j === spans.length - 1),
style: pieceStyle(item, j, last),
...(revealed(item) && shown < 1 ? { revealFraction: shown } : {}),
// Its own points' times.
...(aged(item)
@@ -3130,6 +3230,7 @@ export function createCesiumDraw({
}
: {}),
};
delete sub.breaks;
if (!(revealed(item) && shown < 1)) delete sub.revealFraction;
const draped = item.mode === 'draped';
const showing = pieceShows(item, j, shown);
@@ -3138,17 +3239,33 @@ export function createCesiumDraw({
// would never show. Showing it later needs no build.
const primitive = draped
? groundPiece(record, sub, pieceId, true)
: depthFailLine(record, sub, pieceId);
: inCollection
? addPolyline(record, polylines, sub, pieceId).polyline
: depthFailLine(record, sub, pieceId);
if (!draped) primitive.show = showing;
// Ground pieces swap in with the set's other ground primitives.
if (draped) groundOut.push(primitive);
else host.add(primitive);
else if (!inCollection) host.add(primitive);
if (item.frame === 'inertial' && item.mode !== 'draped')
(record.inertialPieces ??= []).push(primitive);
// Drawn over the surface for a draped line: hidden past the horizon
// (`cullOverlayLines`), as a line of one run is.
const overlay =
item.overlaid && record.overlayLines
? {
part: { deep: primitive },
shown: showing,
ends: overlayEnds(positions),
}
: undefined;
if (overlay) record.overlayLines.push(overlay);
part.pieces.push({
primitive,
// The collection it is drawn in, for a polyline there.
home: inCollection ? polylines : null,
overlay,
j,
last: j === spans.length - 1,
last,
shown,
draped,
id: pieceId,
@@ -3158,6 +3275,9 @@ export function createCesiumDraw({
return part;
}
/** Whether a piece of a line is gone (with its viewer, say). */
const pieceGone = (piece) => (piece.home ?? piece.primitive).isDestroyed();
/** Whether a piece shows: its line, its segment and some of it revealed. */
const pieceShows = (item, j, shown = 1) =>
item.show !== false &&
@@ -3196,12 +3316,15 @@ export function createCesiumDraw({
/** A pieced line's colours and shows, written in place. */
function writePieces(part, item) {
for (const piece of part.pieces) {
if (!piece || piece.primitive.isDestroyed()) continue;
if (!piece || pieceGone(piece)) continue;
const style = pieceStyle(item, piece.j, piece.last);
const material = piece.primitive.appearance.material;
const material = piece.home
? piece.primitive.material
: piece.primitive.appearance.material;
if (style.kind !== 'pattern') paintLine(material, style);
const show = pieceShows(item, piece.j, piece.shown);
if (!piece.draped) piece.primitive.show = show;
if (piece.overlay) piece.overlay.shown = show;
else if (!piece.draped) piece.primitive.show = show;
else if (piece.primitive.ready) {
showOn(piece.primitive, piece.id, show);
piece.pendingShow = undefined;
@@ -3235,6 +3358,11 @@ export function createCesiumDraw({
if (first < 0) line.show = false;
else if (first > 0 || last < segmentShow.length - 1) {
line.positions = linePositions(item).slice(first, last + 2);
// Its runs' starts, among the positions it keeps.
if (broken(item))
line.breaks = Array.from(item.breaks)
.filter((at) => at > first && at < last + 2)
.map((at) => at - first);
if (item.ageFade)
line.ageFade = {
...item.ageFade,
@@ -3390,7 +3518,10 @@ export function createCesiumDraw({
for (const item of items) {
if (piecewise(item)) {
if (item.mode !== 'draped') pieces ??= new Cesium.PrimitiveCollection();
parts.set(item.id, linePieces(record, item, pieces, groundPieces));
parts.set(
item.id,
linePieces(record, item, pieces, groundPieces, polylines),
);
continue;
}
if (item.mode === 'draped') {
@@ -3626,6 +3757,8 @@ export function createCesiumDraw({
item.segmentShow != null,
revealed(item),
item.frame,
// Where its runs start.
broken(item) ? Array.from(item.breaks) : null,
// How it fades with age (its times change with its positions).
aged(item)
? [
@@ -3652,6 +3785,7 @@ export function createCesiumDraw({
(a.segmentShow != null) === (b.segmentShow != null) &&
revealed(a) === revealed(b) &&
a.frame === b.frame &&
(broken(a) ? a.breaks : null) === (broken(b) ? b.breaks : null) &&
sameAgeLook(a, b);
/** Whether two lines fade with age alike, but for their times. */
@@ -3864,7 +3998,7 @@ export function createCesiumDraw({
!flatEquals(item.positions, was.from) ||
!sameTimes(item, was.ageFrom) ||
item.revealFraction !== was.reveal ||
was.pieces.some((piece) => piece?.primitive.isDestroyed())
was.pieces.some((piece) => piece && pieceGone(piece))
)
return false;
looks.push(was.look);
@@ -4025,6 +4159,8 @@ export function createCesiumDraw({
item.mode !== 'draped' &&
item.frame !== 'inertial' &&
!item.depthFail &&
// A line in runs draws a piece a run.
!broken(item) &&
// A batch draws one colour per line, unfaded.
!aged(item) &&
item.style.kind === 'solid' &&
@@ -4475,10 +4611,15 @@ export function createCesiumDraw({
function segmentInstances(record, item, part) {
const points = linePositions(item);
const instances = [];
const starts = runStarts(item);
part.segments = [];
part.drawn = [];
for (let j = 0; j + 1 < points.length; j++) {
if (Cesium.Cartesian3.equals(points[j], points[j + 1])) {
// Nothing across a break, between two runs.
if (
starts?.has(j + 1) ||
Cesium.Cartesian3.equals(points[j], points[j + 1])
) {
part.segments.push(null);
part.drawn.push(null);
continue;