mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Traffic surface keys and readiness through the globe port; draped lines fall back to overlay where the scene cannot drape
The traffic layer keys its cached road heights by the globe port's
surfaceVersion() and waits on isSettled('surface') instead of reading the
scene through render/cesium/surfaceState.js, which is gone. Heat-lines are
always asked for draped; the layer no longer checks whether the scene can
drape and drops them. The traffic layer and application/sources no longer
reach Cesium, and src/layers/traffic/rendering.js leaves the Cesium list.
Where the Cesium renderer cannot drape (no depth textures), its draw port
now draws every draped line over the surface, as the contract asks: along
the surface's curve between its positions, in its colour where the surface
hides it, hidden past the horizon (tested along its arc, shown in 2D). A
segmented line runs from its first shown segment to its last, in its style
colour. A line whose positions hold what they held takes a new colour in
place, so pulsing jam lines build nothing per frame; one that moves is
built anew alone.
This commit is contained in:
1 parent
f53f691a80
commit
26633106fc
15 files changed
+543
-169
No files matched your search
+196
-12
@@ -790,6 +790,7 @@ export function createCesiumDraw({
|
||||
// eye is hidden, as layers drawn without depth test used to do themselves.
|
||||
let occluder = null;
|
||||
function cullHorizon() {
|
||||
cullOverlayLines();
|
||||
for (const record of sets.values()) {
|
||||
if (!record.spec.horizonCull || !record.placed.length) continue;
|
||||
const horizon = horizonNow();
|
||||
@@ -801,6 +802,26 @@ export function createCesiumDraw({
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draped lines drawn over the surface (where the scene cannot drape) show
|
||||
* while any of their ends or middle is this side of the horizon.
|
||||
*/
|
||||
function cullOverlayLines() {
|
||||
let horizon;
|
||||
for (const record of sets.values()) {
|
||||
if (!record.overlayLines?.length) continue;
|
||||
horizon ??= horizonNow();
|
||||
for (const line of record.overlayLines) {
|
||||
// With no horizon (2D) every line shows as it was asked to.
|
||||
const show =
|
||||
line.shown &&
|
||||
(!horizon || line.ends.some((p) => horizon.isPointVisible(p)));
|
||||
if (line.part.deep && line.part.deep.show !== show)
|
||||
line.part.deep.show = show;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The horizon from the eye as it is now; null where there is none (2D). */
|
||||
function horizonNow() {
|
||||
const eye = scene.camera?.positionWC;
|
||||
@@ -1849,8 +1870,11 @@ export function createCesiumDraw({
|
||||
width: item.widthPx,
|
||||
vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT,
|
||||
// Straight between the positions, as the collection's lines are
|
||||
// (geodesic ones come densified).
|
||||
arcType: Cesium.ArcType.NONE,
|
||||
// (geodesic ones come densified); one drawn for a draped line
|
||||
// follows the surface's curve between them, as draping does.
|
||||
arcType: item.overlaid
|
||||
? Cesium.ArcType.GEODESIC
|
||||
: Cesium.ArcType.NONE,
|
||||
}),
|
||||
id,
|
||||
}),
|
||||
@@ -1958,6 +1982,125 @@ export function createCesiumDraw({
|
||||
);
|
||||
}
|
||||
|
||||
/** Whether the scene can drape lines (Cesium's ground polylines need
|
||||
* depth textures, which every WebGL 2 context has). */
|
||||
const drapes = () => scene.frameState?.context?.depthTexture !== false;
|
||||
|
||||
/**
|
||||
* A draped line as drawn where the scene cannot drape: undraped along its
|
||||
* positions, in its colour where the surface hides it (so it draws over
|
||||
* the surface), as one colour where it had segments of its own. One with
|
||||
* segments runs from its first shown segment to its last, and is hidden
|
||||
* with none shown.
|
||||
*/
|
||||
function overlayLine(item) {
|
||||
if (item.mode !== 'draped') return item;
|
||||
const { segmentColors, segmentShow, clampTo, ...rest } = item;
|
||||
const line = {
|
||||
...rest,
|
||||
mode: '3d',
|
||||
depthFailColor: item.style.color,
|
||||
overlaid: true,
|
||||
};
|
||||
if (segmentShow?.length) {
|
||||
const first = segmentShow.indexOf(true);
|
||||
const last = segmentShow.lastIndexOf(true);
|
||||
if (first < 0) line.show = false;
|
||||
else if (first > 0 || last < segmentShow.length - 1)
|
||||
line.positions = linePositions(item).slice(first, last + 2);
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recolour, in place, a line drawn over the surface for a draped one whose
|
||||
* positions are the ones it was built from. False, changing nothing, for
|
||||
* one that moved or is revealed.
|
||||
*/
|
||||
function overlayRecoloured(record, part, item) {
|
||||
const deep = part.deep;
|
||||
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);
|
||||
deep.depthFailAppearance.material.uniforms.color = toColor(
|
||||
item.style.color,
|
||||
);
|
||||
const line = record.overlayLines.find((entry) => entry.part === part);
|
||||
if (line) line.shown = item.show !== false;
|
||||
else deep.show = item.show !== false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/** An overlay line's look but its colours (its depth-fail colour is its
|
||||
* colour too). */
|
||||
const overlayShape = (item) =>
|
||||
lineLook({ ...item, depthFailColor: null }, true);
|
||||
|
||||
/** Stop tracking a line no longer drawn over the surface for a draped
|
||||
* one. */
|
||||
function overlayLeft(record, part) {
|
||||
const lines = record.overlayLines;
|
||||
const at = lines ? lines.findIndex((line) => line.part === part) : -1;
|
||||
if (at >= 0) lines.splice(at, 1);
|
||||
part.overlayFrom = undefined;
|
||||
part.overlayShape = undefined;
|
||||
}
|
||||
|
||||
/** Track a patched overlay line for the horizon cull, as built now. */
|
||||
function overlayMoved(record, part, item) {
|
||||
part.overlayFrom = flatPositions(item.positions);
|
||||
part.overlayShape = overlayShape(item);
|
||||
const lines = record.overlayLines;
|
||||
const at = lines.findIndex((line) => line.part === part);
|
||||
if (at >= 0) lines.splice(at, 1);
|
||||
if (!part.deep) return;
|
||||
lines.push({
|
||||
part,
|
||||
shown: item.show !== false,
|
||||
ends: overlayEnds(item.positions),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Points along a line for the horizon test: up to 16 of its positions,
|
||||
* evenly spread, and between each two the point on the surface's curve
|
||||
* halfway (so a long arc under the eye is not taken as past the horizon).
|
||||
*/
|
||||
function overlayEnds(positions) {
|
||||
const all = linePositions({ positions });
|
||||
if (!all.length) return [];
|
||||
const picks = Math.min(all.length, 16);
|
||||
const probes = [];
|
||||
let previous = null;
|
||||
for (let k = 0; k < picks; k++) {
|
||||
const at =
|
||||
all[Math.round((k * (all.length - 1)) / Math.max(1, picks - 1))];
|
||||
if (previous) {
|
||||
const mid = Cesium.Cartesian3.midpoint(
|
||||
previous,
|
||||
at,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const radius =
|
||||
(Cesium.Cartesian3.magnitude(previous) +
|
||||
Cesium.Cartesian3.magnitude(at)) /
|
||||
2;
|
||||
if (Cesium.Cartesian3.magnitude(mid) > 0)
|
||||
probes.push(
|
||||
Cesium.Cartesian3.multiplyByScalar(
|
||||
Cesium.Cartesian3.normalize(mid, mid),
|
||||
radius,
|
||||
mid,
|
||||
),
|
||||
);
|
||||
}
|
||||
probes.push(at);
|
||||
previous = at;
|
||||
}
|
||||
return probes;
|
||||
}
|
||||
|
||||
function buildLines(record) {
|
||||
// Refused before anything changes, so the set keeps drawing as it was.
|
||||
for (const item of record.items.values())
|
||||
@@ -1981,6 +2124,14 @@ export function createCesiumDraw({
|
||||
throw new Error(
|
||||
'the Cesium renderer colours and shows segments of draped solid lines only',
|
||||
);
|
||||
// Where the scene cannot drape at all, draped lines draw over the
|
||||
// surface instead (`overlay`): undraped, in their own colour where the
|
||||
// surface hides them, and hidden past the horizon (`cullOverlayLines`).
|
||||
const overlay = !drapes();
|
||||
const items = overlay
|
||||
? [...record.items.values()].map(overlayLine)
|
||||
: [...record.items.values()];
|
||||
record.overlayLines = overlay ? [] : null;
|
||||
const polylines = new Cesium.PolylineCollection();
|
||||
// Lines in the inertial frame, turned with the sky each frame.
|
||||
let inertial = null;
|
||||
@@ -2003,7 +2154,7 @@ export function createCesiumDraw({
|
||||
// Lines with a depth-fail colour, beside the collection.
|
||||
let deep = null;
|
||||
const looks = lineLooks();
|
||||
for (const item of record.items.values()) {
|
||||
for (const item of items) {
|
||||
if (item.mode === 'draped') {
|
||||
const clampTo = item.clampTo ?? record.spec.clampTo ?? 'both';
|
||||
const part = {
|
||||
@@ -2047,7 +2198,18 @@ export function createCesiumDraw({
|
||||
const id = pickId(record, item);
|
||||
const primitive = depthFailLine(record, item, id);
|
||||
if (primitive) deep.add(primitive);
|
||||
parts.set(item.id, { look: lineLook(item), id, deep: primitive });
|
||||
const part = { look: lineLook(item), id, deep: primitive };
|
||||
if (item.overlaid) {
|
||||
part.overlayFrom = flatPositions(item.positions);
|
||||
part.overlayShape = overlayShape(item);
|
||||
}
|
||||
parts.set(item.id, part);
|
||||
if (item.overlaid && primitive)
|
||||
record.overlayLines.push({
|
||||
part,
|
||||
shown: item.show !== false,
|
||||
ends: overlayEnds(item.positions),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
const id = pickId(record, item);
|
||||
@@ -2101,7 +2263,7 @@ export function createCesiumDraw({
|
||||
let own = polylines;
|
||||
// Lines in the collection that can settle into a static batch draw
|
||||
// beside it once they do.
|
||||
const settling = [...record.items.values()].some(settles);
|
||||
const settling = items.some(settles);
|
||||
if (deep || settling || inertial) {
|
||||
own = new Cesium.PrimitiveCollection();
|
||||
own.add(polylines);
|
||||
@@ -2295,6 +2457,17 @@ export function createCesiumDraw({
|
||||
Boolean((was.polyline || was.batched) && was.shape) &&
|
||||
was.shape === looks.shape(item);
|
||||
|
||||
/**
|
||||
* Whether a line drawn over the surface for a draped one changes only its
|
||||
* colour where it runs: it takes the new one in place
|
||||
* (`overlayRecoloured`).
|
||||
*/
|
||||
const recolourableOverlay = (was, item, looks) =>
|
||||
Boolean(item.overlaid && was.deep && was.overlayShape) &&
|
||||
flatEquals(item.positions, was.overlayFrom) &&
|
||||
!revealed(item) &&
|
||||
was.overlayShape === overlayShape(item);
|
||||
|
||||
/**
|
||||
* The colours the glow lines sharing a material that `list` recolours
|
||||
* are given together: each such material and the item whose colour it
|
||||
@@ -2351,6 +2524,10 @@ export function createCesiumDraw({
|
||||
function patchLines(record, list) {
|
||||
const parts = record.lineParts;
|
||||
if (record.kind !== 'lines' || !parts) return false;
|
||||
// Where the scene cannot drape, draped lines are patched as the lines
|
||||
// drawn over the surface for them, each built anew alone where it
|
||||
// changes (as any line with a depth-fail colour is).
|
||||
if (record.overlayLines) list = list.map(overlayLine);
|
||||
// Each draped line's positions, flattened once for both passes.
|
||||
const flats = new Map();
|
||||
// Each item's look, in order: an item sent twice is changed twice.
|
||||
@@ -2364,7 +2541,8 @@ export function createCesiumDraw({
|
||||
if (
|
||||
was.look !== look &&
|
||||
!recolourable(was, item, lookOf) &&
|
||||
!recolourableSolid(was, item, lookOf)
|
||||
!recolourableSolid(was, item, lookOf) &&
|
||||
!recolourableOverlay(was, item, lookOf)
|
||||
)
|
||||
return false;
|
||||
// A viewer destroyed under the set took its batch with it.
|
||||
@@ -2400,7 +2578,7 @@ export function createCesiumDraw({
|
||||
patchUndraped(record, was, item, looks[k]);
|
||||
continue;
|
||||
}
|
||||
if (was.look !== looks[k]) {
|
||||
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);
|
||||
@@ -2408,6 +2586,10 @@ export function createCesiumDraw({
|
||||
}
|
||||
if (was.deep !== undefined) {
|
||||
was.id.meta = item.meta;
|
||||
was.look = looks[k];
|
||||
// One drawn for a draped line that runs where it ran takes its new
|
||||
// look in place (heat-lines pulse every frame).
|
||||
if (overlayRecoloured(record, was, item)) continue;
|
||||
// Built anew where it now runs, under the same pick id.
|
||||
const deep = record.deepLines;
|
||||
const old = was.deep;
|
||||
@@ -2418,6 +2600,8 @@ export function createCesiumDraw({
|
||||
was.deep = depthFailLine(record, item, was.id);
|
||||
if (was.deep) deep.add(was.deep);
|
||||
if (old) deep.remove(old);
|
||||
if (item.overlaid) overlayMoved(record, was, item);
|
||||
else if (was.overlayFrom) overlayLeft(record, was);
|
||||
continue;
|
||||
}
|
||||
was.id.meta = item.meta;
|
||||
@@ -3653,13 +3837,13 @@ export function createCesiumDraw({
|
||||
* drape them onto 3D tiles yet, so over shown tiles it is `overlay`.
|
||||
* Terrain and the ellipsoid take draped items wherever Cesium drapes at
|
||||
* all: its draped lines and image fills need depth textures, which every
|
||||
* WebGL 2 context has; without them it says `overlay`. (It still builds
|
||||
* them as ground primitives there: the overlay drawing itself is not
|
||||
* built, since only a WebGL 1 context without the extension lacks them.)
|
||||
* WebGL 2 context has; without them it says `overlay`, and draws draped
|
||||
* lines over the surface (`overlayLine`). Clamped shapes there are still
|
||||
* built as ground primitives: only a WebGL 1 context without the
|
||||
* extension lacks depth textures.
|
||||
*/
|
||||
const clampState = () =>
|
||||
scene.frameState?.context?.depthTexture === false ||
|
||||
tilesets().some((tileset) => tileset.show)
|
||||
!drapes() || tilesets().some((tileset) => tileset.show)
|
||||
? 'overlay'
|
||||
: 'draped';
|
||||
const clampListeners = listenerList(report);
|
||||
|
||||
@@ -4594,7 +4594,6 @@ test('the traffic layer drapes its heat-lines on the tiles and pulses the jam on
|
||||
_presetDots: 'off',
|
||||
_heatJam: [],
|
||||
_heatLineCount: 0,
|
||||
_heatSupported: true,
|
||||
_heatSet: draw.lines({ id: 'traffic:1:heat', clampTo: 'tiles' }),
|
||||
};
|
||||
const rendering = createRendering({
|
||||
@@ -4647,6 +4646,237 @@ test('the traffic layer drapes its heat-lines on the tiles and pulses the jam on
|
||||
});
|
||||
});
|
||||
|
||||
test('where the scene cannot drape, draped lines draw over the surface and hide past the horizon', () =>
|
||||
withImageClasses(() => {
|
||||
const scene = fakeScene();
|
||||
scene.frameState.context = { depthTexture: false };
|
||||
const { draw, frame } = drawOver(scene);
|
||||
assert.equal(draw.clampState(), 'overlay');
|
||||
const set = draw.lines({ id: 'heat', clampTo: 'tiles' });
|
||||
const at = (lon) => Cesium.Cartesian3.fromDegrees(lon, 0, 0);
|
||||
const firstPositions = [at(0), at(0.01)];
|
||||
set.set([
|
||||
{
|
||||
id: 'jam',
|
||||
positions: firstPositions,
|
||||
widthPx: 4,
|
||||
mode: 'draped',
|
||||
style: { kind: 'glow', color: [1, 0, 0, 0.8], power: 0.25 },
|
||||
},
|
||||
]);
|
||||
const host = scene.added[0];
|
||||
const all = [];
|
||||
const walk = (p) => {
|
||||
all.push(p);
|
||||
if (typeof p?.get === 'function' && Number.isFinite(p.length))
|
||||
for (let i = 0; i < p.length; i++) walk(p.get(i));
|
||||
};
|
||||
walk(host);
|
||||
assert.ok(
|
||||
!all.some((p) => p instanceof Cesium.GroundPolylinePrimitive),
|
||||
'no ground polyline',
|
||||
);
|
||||
const line = all.find(
|
||||
(p) => p instanceof Cesium.Primitive && p.depthFailAppearance,
|
||||
);
|
||||
assert.ok(line, 'drawn where the surface hides it too');
|
||||
const failColor = line.depthFailAppearance.material.uniforms.color;
|
||||
assert.ok(Math.abs(failColor.red - 1) < 1e-6 && failColor.green === 0);
|
||||
// The eye over the line's side of the Earth: shown.
|
||||
scene.camera.positionWC = new Cesium.Cartesian3(2e7, 0, 0);
|
||||
frame();
|
||||
assert.equal(line.show, true);
|
||||
// The eye over the far side: past the horizon, hidden.
|
||||
scene.camera.positionWC = new Cesium.Cartesian3(-2e7, 0, 0);
|
||||
frame();
|
||||
assert.equal(line.show, false);
|
||||
// A recolour where it runs (a pulse) takes the colour in place, and the
|
||||
// line stays hidden past the horizon: nothing is built anew.
|
||||
const hostBefore = scene.added[0];
|
||||
set.upsert([
|
||||
{
|
||||
id: 'jam',
|
||||
positions: firstPositions,
|
||||
widthPx: 4,
|
||||
mode: 'draped',
|
||||
style: { kind: 'glow', color: [1, 0, 0, 0.4], power: 0.25 },
|
||||
},
|
||||
]);
|
||||
frame();
|
||||
assert.equal(line.show, false, 'still past the horizon');
|
||||
const after = [];
|
||||
const walkAfter = (p) => {
|
||||
after.push(p);
|
||||
if (typeof p?.get === 'function' && Number.isFinite(p.length))
|
||||
for (let i = 0; i < p.length; i++) walkAfter(p.get(i));
|
||||
};
|
||||
walkAfter(scene.added[0]);
|
||||
assert.equal(scene.added[0], hostBefore, 'the set is not rebuilt');
|
||||
assert.ok(after.includes(line), 'the same line draws');
|
||||
assert.ok(
|
||||
Math.abs(line.appearance.material.uniforms.color.alpha - 0.4) < 1e-6,
|
||||
);
|
||||
assert.ok(
|
||||
Math.abs(line.depthFailAppearance.material.uniforms.color.alpha - 0.4) <
|
||||
1e-6,
|
||||
);
|
||||
// In 2D there is no horizon: it shows as asked.
|
||||
scene.mode = Cesium.SceneMode.SCENE2D;
|
||||
frame();
|
||||
assert.equal(line.show, true, 'shown in 2D');
|
||||
scene.mode = Cesium.SceneMode.SCENE3D;
|
||||
scene.camera.positionWC = new Cesium.Cartesian3(2e7, 0, 0);
|
||||
frame();
|
||||
// Moved, it alone is built anew, still over the surface and culled.
|
||||
set.upsert([
|
||||
{
|
||||
id: 'jam',
|
||||
positions: [at(0), at(0.02)],
|
||||
widthPx: 4,
|
||||
mode: 'draped',
|
||||
style: { kind: 'glow', color: [1, 0, 0, 0.4], power: 0.25 },
|
||||
},
|
||||
]);
|
||||
assert.equal(scene.added[0], hostBefore, 'patched, not rebuilt');
|
||||
const moved = [];
|
||||
const walkMoved = (p) => {
|
||||
moved.push(p);
|
||||
if (typeof p?.get === 'function' && Number.isFinite(p.length))
|
||||
for (let i = 0; i < p.length; i++) walkMoved(p.get(i));
|
||||
};
|
||||
walkMoved(scene.added[0]);
|
||||
const rebuilt = moved.find(
|
||||
(p) => p instanceof Cesium.Primitive && p.depthFailAppearance,
|
||||
);
|
||||
assert.ok(rebuilt && rebuilt !== line, 'built anew where it moved');
|
||||
assert.ok(!moved.includes(line), 'the old line is gone');
|
||||
scene.camera.positionWC = new Cesium.Cartesian3(-2e7, 0, 0);
|
||||
frame();
|
||||
assert.equal(rebuilt.show, false);
|
||||
set.dispose();
|
||||
assert.equal(scene.added.length, 0);
|
||||
}));
|
||||
|
||||
test('where the scene cannot drape, an overlay line is culled by its whole arc, and patched by what its positions hold', () =>
|
||||
withImageClasses(() => {
|
||||
const scene = fakeScene();
|
||||
scene.frameState.context = { depthTexture: false };
|
||||
const { draw, frame } = drawOver(scene);
|
||||
const set = draw.lines({ id: 'arc', clampTo: 'tiles' });
|
||||
const find = () => {
|
||||
const all = [];
|
||||
const walk = (p) => {
|
||||
all.push(p);
|
||||
if (typeof p?.get === 'function' && Number.isFinite(p.length))
|
||||
for (let i = 0; i < p.length; i++) walk(p.get(i));
|
||||
};
|
||||
walk(scene.added[0]);
|
||||
return all.find(
|
||||
(p) => p instanceof Cesium.Primitive && p.depthFailAppearance,
|
||||
);
|
||||
};
|
||||
// Its ends are past the horizon, but the arc runs under the eye.
|
||||
const a = Cesium.Cartesian3.fromDegrees(-80, 0, 0);
|
||||
const b = Cesium.Cartesian3.fromDegrees(80, 0, 0);
|
||||
const positions = new Float64Array([a.x, a.y, a.z, b.x, b.y, b.z]);
|
||||
const item = (style) => ({
|
||||
id: 'arc',
|
||||
positions,
|
||||
widthPx: 2,
|
||||
mode: 'draped',
|
||||
style: { kind: 'solid', color: style },
|
||||
});
|
||||
set.set([item([1, 0, 0, 1])]);
|
||||
scene.camera.positionWC = new Cesium.Cartesian3(2e7, 0, 0);
|
||||
frame();
|
||||
const line = find();
|
||||
assert.equal(line.show, true, 'shown: its middle is in view');
|
||||
// The same buffer, changed in place: built anew where it now runs.
|
||||
const c = Cesium.Cartesian3.fromDegrees(10, 0, 0);
|
||||
positions.set([c.x, c.y, c.z], 3);
|
||||
set.upsert([item([1, 0, 0, 1])]);
|
||||
const moved = find();
|
||||
assert.notEqual(moved, line, 'built anew');
|
||||
// Moved past the horizon, then upserted as an ordinary line in view:
|
||||
// it is no longer culled by where the overlay was.
|
||||
const far = new Float64Array(6);
|
||||
const f1 = Cesium.Cartesian3.fromDegrees(179, 0, 0);
|
||||
const f2 = Cesium.Cartesian3.fromDegrees(179.5, 0, 0);
|
||||
far.set([f1.x, f1.y, f1.z, f2.x, f2.y, f2.z]);
|
||||
set.upsert([{ ...item([1, 0, 0, 1]), positions: far }]);
|
||||
frame();
|
||||
assert.equal(find().show, false, 'past the horizon');
|
||||
set.upsert([
|
||||
{
|
||||
id: 'arc',
|
||||
positions: [
|
||||
Cesium.Cartesian3.fromDegrees(0, 0, 0),
|
||||
Cesium.Cartesian3.fromDegrees(0.5, 0, 0),
|
||||
],
|
||||
widthPx: 2,
|
||||
mode: '3d',
|
||||
depthFailColor: [1, 0, 0, 1],
|
||||
style: { kind: 'solid', color: [1, 0, 0, 1] },
|
||||
},
|
||||
]);
|
||||
const plain = find();
|
||||
frame();
|
||||
assert.equal(plain.show, true, 'not culled by the overlay it was');
|
||||
set.dispose();
|
||||
}));
|
||||
|
||||
test('where the scene cannot drape, a segmented draped line runs over its shown segments only', () =>
|
||||
withImageClasses(() => {
|
||||
const scene = fakeScene();
|
||||
scene.frameState.context = { depthTexture: false };
|
||||
const { draw } = drawOver(scene);
|
||||
const set = draw.lines({ id: 'trail', clampTo: 'tiles' });
|
||||
const at = (lon) => Cesium.Cartesian3.fromDegrees(lon, 0, 0);
|
||||
const positions = [at(0), at(0.01), at(0.02), at(0.03)];
|
||||
const red = [1, 0, 0, 1];
|
||||
const find = () => {
|
||||
const all = [];
|
||||
const walk = (p) => {
|
||||
all.push(p);
|
||||
if (typeof p?.get === 'function' && Number.isFinite(p.length))
|
||||
for (let i = 0; i < p.length; i++) walk(p.get(i));
|
||||
};
|
||||
walk(scene.added[0]);
|
||||
return all.find(
|
||||
(p) => p instanceof Cesium.Primitive && p.depthFailAppearance,
|
||||
);
|
||||
};
|
||||
set.set([
|
||||
{
|
||||
id: 'trail',
|
||||
positions,
|
||||
widthPx: 2,
|
||||
mode: 'draped',
|
||||
style: { kind: 'solid', color: red },
|
||||
segmentColors: [red, red, red],
|
||||
segmentShow: [true, true, false],
|
||||
},
|
||||
]);
|
||||
const line = find();
|
||||
assert.ok(line, 'drawn over the surface');
|
||||
const drawn = line.geometryInstances.geometry._positions;
|
||||
assert.equal(drawn.length, 3, 'up to its last shown segment');
|
||||
assert.ok(Cesium.Cartesian3.equals(drawn[2], positions[2]));
|
||||
set.set([
|
||||
{
|
||||
id: 'trail',
|
||||
positions,
|
||||
widthPx: 2,
|
||||
mode: 'draped',
|
||||
style: { kind: 'solid', color: red },
|
||||
segmentColors: [red, red, red],
|
||||
segmentShow: [false, false, false],
|
||||
},
|
||||
]);
|
||||
assert.equal(find()?.show ?? false, false, 'hidden with none shown');
|
||||
set.dispose();
|
||||
}));
|
||||
|
||||
test('revealFraction draws a line up to that fraction of its length, and changes in place', () =>
|
||||
withImageClasses(() => {
|
||||
const { scene, draw } = drawOver();
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
/**
|
||||
* @module render/cesium/surfaceState
|
||||
*
|
||||
* What a layer that samples heights off the drawn surface, or drapes lines
|
||||
* on it, needs to know of it and the renderer contract cannot say: whether
|
||||
* any 3D tileset is drawn (the globe port's settle states hold for none),
|
||||
* which surface it is (the contract names no surface identity), and
|
||||
* whether lines can be draped at all. Read off the Cesium scene.
|
||||
*/
|
||||
import * as Cesium from 'cesium';
|
||||
|
||||
/**
|
||||
* Whether the scene can drape lines on the surface: Cesium's ground
|
||||
* polylines need depth textures.
|
||||
* @param {object|undefined} viewer
|
||||
*/
|
||||
export function drapesLines(viewer) {
|
||||
const scene = viewer?.scene;
|
||||
return (
|
||||
Boolean(scene?.frameState) &&
|
||||
Cesium.GroundPolylinePrimitive.isSupported(scene)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the drawn surface has loaded what is in view: the
|
||||
* globe's tiles while it is shown, otherwise every shown 3D tileset (and
|
||||
* there is one).
|
||||
* @param {object|undefined} viewer
|
||||
*/
|
||||
export function surfaceTilesReady(viewer) {
|
||||
const scene = viewer?.scene;
|
||||
if (!scene) return false;
|
||||
if (scene.globe?.show) return scene.globe.tilesLoaded !== false;
|
||||
let found = false;
|
||||
for (let i = 0; i < (scene.primitives?.length || 0); i++) {
|
||||
const primitive = scene.primitives.get(i);
|
||||
if (!primitive.show || typeof primitive.tilesLoaded !== 'boolean') continue;
|
||||
found = true;
|
||||
if (!primitive.tilesLoaded) return false;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
// Stable ids for the objects heights are sampled from (tilesets, terrain).
|
||||
const surfaceIds = new WeakMap();
|
||||
let nextSurfaceId = 1;
|
||||
const surfaceId = (object) => {
|
||||
if (!object || typeof object !== 'object') return 0;
|
||||
if (!surfaceIds.has(object)) surfaceIds.set(object, nextSurfaceId++);
|
||||
return surfaceIds.get(object);
|
||||
};
|
||||
|
||||
/**
|
||||
* Name what heights are sampled against: the terrain provider when the globe
|
||||
* is shown, otherwise the set of visible 3D tilesets. A map-provider switch
|
||||
* changes it and invalidates every cached height.
|
||||
* @param {object|undefined} viewer
|
||||
*/
|
||||
const surfaceKeys = new WeakMap();
|
||||
export function surfaceKey(viewer) {
|
||||
const scene = viewer?.scene;
|
||||
if (!scene) return '';
|
||||
let state = surfaceKeys.get(scene);
|
||||
if (!state) {
|
||||
state = { globe: null, terrain: null, tiles: [], key: '' };
|
||||
surfaceKeys.set(scene, state);
|
||||
}
|
||||
const globe = Boolean(scene.globe?.show);
|
||||
const terrain = globe ? scene.globe.terrainProvider : null;
|
||||
let changed = state.globe !== globe || state.terrain !== terrain;
|
||||
let count = 0;
|
||||
if (!globe) {
|
||||
for (let i = 0; i < (scene.primitives?.length || 0); i++) {
|
||||
const primitive = scene.primitives.get(i);
|
||||
if (!primitive?.show || typeof primitive.tilesLoaded !== 'boolean')
|
||||
continue;
|
||||
if (state.tiles[count] !== primitive) {
|
||||
state.tiles[count] = primitive;
|
||||
changed = true;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count !== state.tiles.length) {
|
||||
state.tiles.length = count;
|
||||
changed = true;
|
||||
}
|
||||
if (changed) {
|
||||
state.globe = globe;
|
||||
state.terrain = terrain;
|
||||
// Allocate only when the actual map surface changes, never per frame.
|
||||
state.key = globe
|
||||
? `globe:${surfaceId(terrain)}`
|
||||
: `tiles:${state.tiles.map(surfaceId).join(',')}`;
|
||||
}
|
||||
return state.key;
|
||||
}
|
||||
Reference in new issue
Block a user