diff --git a/docs/RENDERER-CONTRACT.md b/docs/RENDERER-CONTRACT.md index 8fb575944..2c1478f3b 100644 --- a/docs/RENDERER-CONTRACT.md +++ b/docs/RENDERER-CONTRACT.md @@ -214,7 +214,9 @@ item of that id with the columns' fields over it, replacing every item as The Cesium port also draws `pattern` line styles (the image repeated `repeat` times along the line, in the translucent pass; draped too), lines in the `inertial` frame (turned each frame by the set's motion clock, a depth-fail -style included; never draped and never batched), segment colours and shows +style included; never batched; a draped one is draped where the sky is, +turned into the fixed frame, and draped again each second of its clock), +segment colours and shows on lines of every mode and style (a draped solid line as ground instances, any other as a primitive a segment with a material of its own, recoloured and shown in place; an arrow's head on its last segment, a pattern keeping diff --git a/src/render/cesium/draw.js b/src/render/cesium/draw.js index eeb9658eb..4de3bbfb2 100644 --- a/src/render/cesium/draw.js +++ b/src/render/cesium/draw.js @@ -298,6 +298,13 @@ export function pulseAlpha(pulse, now) { return pulse.from + (pulse.to - pulse.from) * (0.5 + 0.5 * Math.sin(turn)); } +/** + * How much of a set's motion clock passes before its draped lines in the + * sky's frame are draped again where the sky has turned to (the Earth turns + * about 465 m at the equator in a second). + */ +export const DRAPED_INERTIAL_STEP_MS = 1000; + /** The line styles the port drapes. */ const DRAPED_STYLES = new Set([ 'solid', @@ -659,6 +666,60 @@ export function createCesiumDraw({ dropClusters(record); dispose(); }; + // Draped lines in the sky's frame are draped where the sky is: held as + // given, drawn turned into the fixed frame, and draped again each + // `DRAPED_INERTIAL_STEP_MS` of their motion clock (`beforeRender`). + if (kind === 'lines') { + const { set, upsert, remove, clear } = handle; + record.inertialDraped = new Map(); + const fixedNow = (list) => { + const ms = motionMs(record); + let axes = null; + const given = new Map(); + const drawn = list.map((item) => { + if (item.frame !== 'inertial' || item.mode !== 'draped') return item; + given.set(item.id, item); + axes ??= temeToPseudoFixed(ms); + return fixedLine(item, axes); + }); + return { drawn, given, ms }; + }; + const keep = (list, given, ms) => { + for (const item of list) record.inertialDraped.delete(item.id); + for (const [id, item] of given) record.inertialDraped.set(id, item); + if (given.size) record.inertialTurnMs = ms; + }; + handle.set = (list) => { + const { drawn, given, ms } = fixedNow(list); + const result = set(drawn); + record.inertialDraped.clear(); + keep(list, given, ms); + return result; + }; + handle.upsert = (list) => { + const { drawn, given, ms } = fixedNow(list); + const result = upsert(drawn); + keep(list, given, ms); + return result; + }; + handle.remove = (ids) => { + const result = remove(ids); + for (const id of ids) record.inertialDraped.delete(id); + return result; + }; + handle.clear = () => { + const result = clear(); + record.inertialDraped.clear(); + return result; + }; + record.drapeInertial = () => { + if (!record.inertialDraped.size) return; + const ms = motionMs(record); + if (Math.abs(ms - record.inertialTurnMs) < DRAPED_INERTIAL_STEP_MS) + return; + handle.upsert([...record.inertialDraped.values()]); + }; + } // `columns`: column updates of an icon set, on the GPU. if (kind === 'icons' && capabilities.has('columns')) handle.setColumns = (columns) => { @@ -2275,18 +2336,46 @@ export function createCesiumDraw({ * Turn a line set's inertial lines to the sky at its motion clock's time * now: the frame rotated by sidereal time from the Earth-fixed one. */ + /** The time a set's motion clock reads now, epoch ms. */ + function motionMs(record) { + const clock = record.spec.motionClock; + return clock?.kind === 'scene' + ? Cesium.JulianDate.toDate( + scene.frameState?.time ?? Cesium.JulianDate.now(), + ).getTime() + : Date.now() - (clock?.delayMs ?? 0); + } + + /** + * A draped line in the sky's frame as a fixed one where the sky is at + * `axes` (`temeToPseudoFixed`): its positions turned into the fixed frame. + */ + function fixedLine(item, axes) { + const { x, y, z } = axes; + const flat = + item.positions instanceof Float64Array + ? item.positions + : item.positions.flatMap((p) => { + const c = toCartesian(p); + return [c.x, c.y, c.z]; + }); + const turned = new Float64Array(flat.length); + for (let i = 0; i < flat.length; i += 3) { + const a = flat[i]; + const b = flat[i + 1]; + const c = flat[i + 2]; + turned[i] = x.x * a + y.x * b + z.x * c; + turned[i + 1] = x.y * a + y.y * b + z.y * c; + turned[i + 2] = x.z * a + y.z * b + z.z * c; + } + return { ...item, frame: 'fixed', positions: turned }; + } + function turnInertial(record) { const lines = record.inertialLines; const pieces = record.inertialPieces ?? []; if ((!lines || lines.isDestroyed()) && !pieces.length) return; - const clock = record.spec.motionClock; - const ms = - clock?.kind === 'scene' - ? Cesium.JulianDate.toDate( - scene.frameState?.time ?? Cesium.JulianDate.now(), - ).getTime() - : Date.now() - (clock?.delayMs ?? 0); - const { x, y, z } = temeToPseudoFixed(ms); + const { x, y, z } = temeToPseudoFixed(motionMs(record)); const turn = Cesium.Matrix4.fromRotationTranslation( new Cesium.Matrix3(x.x, y.x, z.x, x.y, y.y, z.y, x.z, y.z, z.z), Cesium.Cartesian3.ZERO, @@ -2588,8 +2677,6 @@ export function createCesiumDraw({ for (const item of record.items.values()) if (item.mode === 'draped' && !DRAPED_STYLES.has(item.style.kind)) throw new Error(`the Cesium renderer has no ${item.style.kind} lines`); - else if (item.frame === 'inertial' && item.mode === 'draped') - throw new Error('the Cesium renderer draws inertial lines undraped'); else if ( item.mode !== 'draped' && item.depthFail && @@ -5052,9 +5139,11 @@ export function createCesiumDraw({ cullHorizon(); clusterIcons(); runFlows(); - for (const record of sets.values()) + for (const record of sets.values()) { if (record.inertialLines || record.inertialPieces?.length) turnInertial(record); + record.drapeInertial?.(); + } modelSets.beforeRender(sets.values()); quadSets.beforeRender(sets.values()); }, diff --git a/src/render/cesium/draw.test.mjs b/src/render/cesium/draw.test.mjs index 4baa24472..430f0995e 100644 --- a/src/render/cesium/draw.test.mjs +++ b/src/render/cesium/draw.test.mjs @@ -7326,20 +7326,6 @@ test('a pattern line draws its image along it, repeated, and an inertial line tu later.upsert([{ ...sky(true), id: 'parked' }]); assert.equal(scene.added.at(-1).get(1).length, 1); later.dispose(); - assert.throws( - () => - set.upsert([ - { - id: 'draped', - positions: ring, - widthPx: 3, - mode: 'draped', - style: { kind: 'solid', color: '#fff' }, - frame: 'inertial', - }, - ]), - /inertial lines undraped/, - ); set.dispose(); assert.equal(scene.added.length, 0); })); @@ -8086,3 +8072,66 @@ test('a points set takes columns as rows, over the items it holds', () => { assert.deepEqual(drawHitOf({ id: point.id })?.meta, { m: 1 }); set.dispose(); }); + +test('a draped line in the sky’s frame drapes where the sky is, again as its clock moves on', () => { + const { scene, draw, frame } = drawOver(); + const realNow = Date.now; + let now = Date.UTC(2026, 0, 1); + Date.now = () => now; + try { + const set = draw.lines({ id: 'sky-track' }); + const given = [ + Cesium.Cartesian3.fromDegrees(0, 0), + Cesium.Cartesian3.fromDegrees(1, 0), + ]; + set.set([ + { + id: 'track', + positions: given, + widthPx: 3, + mode: 'draped', + style: { kind: 'solid', color: '#fff' }, + frame: 'inertial', + meta: { m: 1 }, + }, + ]); + const grounds = () => { + const found = []; + for (const host of scene.added) + for (let i = 0; i < (host.length ?? 0); i++) + if (host.get(i) instanceof Cesium.GroundPolylinePrimitive) + found.push(host.get(i)); + return found; + }; + const start = (ground) => + Cesium.Math.toDegrees( + Cesium.Cartographic.fromCartesian( + ground.geometryInstances[0].geometry._positions[0], + ).longitude, + ); + const [first] = grounds(); + assert.ok(first, 'draped'); + assert.equal(first.geometryInstances[0].id.id, 'track'); + assert.deepEqual(first.geometryInstances[0].id.meta, { m: 1 }); + const turned = start(first); + assert.ok( + Math.abs(turned) > 1, + `turned off where it was given (${turned})`, + ); + // Within a step, nothing is draped again. + now += 500; + frame(); + assert.equal(grounds().at(-1), first); + // A minute later the sky has turned a quarter of a degree west. + now += 60_000; + frame(); + const again = grounds().at(-1); + assert.notEqual(again, first, 'draped again'); + const moved = start(again) - turned; + assert.ok(Math.abs(moved + 0.2507) < 0.01, `moved ${moved}°`); + assert.equal(set.size(), 1); + set.dispose(); + } finally { + Date.now = realNow; + } +});