mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Draw the traffic heat-lines through the draw port, batching alike draped glow lines in the Cesium draw port
This commit is contained in:
1 parent
bbd6049e21
commit
5715bf9df3
11 files changed
+608
-193
No files matched your search
+115
-6
@@ -1612,6 +1612,9 @@ export function createCesiumDraw({
|
||||
);
|
||||
const polylines = new Cesium.PolylineCollection();
|
||||
const draped = new Map();
|
||||
// Draped glow lines that look alike: one ground primitive and one
|
||||
// material for them all, so they recolour together in place.
|
||||
const glows = new Map();
|
||||
// Draped lines a batch cannot draw, each with a primitive of its own.
|
||||
const alone = [];
|
||||
// Each line as drawn, for upserts that change it in place.
|
||||
@@ -1652,6 +1655,11 @@ export function createCesiumDraw({
|
||||
draped.get(clampTo).push(...instances);
|
||||
continue;
|
||||
}
|
||||
if (item.style.kind === 'glow' && !revealed(item)) {
|
||||
if (!glows.has(part.look)) glows.set(part.look, []);
|
||||
glows.get(part.look).push({ item, part });
|
||||
continue;
|
||||
}
|
||||
if (!batchable(item)) {
|
||||
alone.push({ item, part });
|
||||
continue;
|
||||
@@ -1693,7 +1701,7 @@ export function createCesiumDraw({
|
||||
own.add(deep);
|
||||
}
|
||||
record.deepLines = deep;
|
||||
if (!draped.size && !alone.length) {
|
||||
if (!draped.size && !glows.size && !alone.length) {
|
||||
noteFlows(record);
|
||||
return replaceHost(record, own);
|
||||
}
|
||||
@@ -1714,6 +1722,26 @@ export function createCesiumDraw({
|
||||
part.ground = byClamp.get(part.clampTo);
|
||||
part.batch = part.ground;
|
||||
}
|
||||
for (const lines of glows.values()) {
|
||||
const [{ item, part: first }] = lines;
|
||||
const material = lineMaterial(record, item.style);
|
||||
const primitive = new Cesium.GroundPolylinePrimitive({
|
||||
geometryInstances: lines.map(({ item, part }) =>
|
||||
drapedInstance(item, part, false),
|
||||
),
|
||||
appearance: new Cesium.PolylineMaterialAppearance({ material }),
|
||||
classificationType: classificationOf(first.clampTo),
|
||||
allowPicking: record.spec.pickable !== false,
|
||||
});
|
||||
// A batch as the solid ones are: a line moved leaves it, hidden.
|
||||
for (const { part } of lines) {
|
||||
part.ground = primitive;
|
||||
part.batch = primitive;
|
||||
part.material = material;
|
||||
part.shared = true;
|
||||
}
|
||||
ground.push(primitive);
|
||||
}
|
||||
for (const { item, part } of alone) {
|
||||
part.ground = drapedPrimitive(record, item, part, false);
|
||||
ground.push(part.ground);
|
||||
@@ -1744,6 +1772,48 @@ export function createCesiumDraw({
|
||||
item.frame,
|
||||
]);
|
||||
|
||||
/**
|
||||
* Whether two lines have the same `lineLook` by the identity of what it
|
||||
* is drawn from, read without stringifying either.
|
||||
*/
|
||||
const sameLookFrom = (a, b) =>
|
||||
a.mode === b.mode &&
|
||||
Object.is(a.widthPx, b.widthPx) &&
|
||||
a.clampTo === b.clampTo &&
|
||||
a.style === b.style &&
|
||||
a.depthFailColor === b.depthFailColor &&
|
||||
(a.segmentColors != null) === (b.segmentColors != null) &&
|
||||
(a.segmentShow != null) === (b.segmentShow != null) &&
|
||||
revealed(a) === revealed(b) &&
|
||||
a.frame === b.frame;
|
||||
|
||||
/**
|
||||
* `lineLook`, kept for the last item asked for: within one upsert, lines
|
||||
* sent with one style object (lines pulsing together) are stringified
|
||||
* once. Not across upserts, where a caller may have changed the style
|
||||
* object it sends again.
|
||||
*/
|
||||
function lineLooks() {
|
||||
let last = null;
|
||||
let look = '';
|
||||
let shape = '';
|
||||
return {
|
||||
look(item) {
|
||||
if (!last || !sameLookFrom(last, item)) {
|
||||
last = item;
|
||||
look = lineLook(item);
|
||||
shape = '';
|
||||
}
|
||||
return look;
|
||||
},
|
||||
shape(item) {
|
||||
this.look(item);
|
||||
shape ||= lineLook(item, true);
|
||||
return shape;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** A line's positions as one flat array of numbers, to compare. */
|
||||
function flatPositions(positions) {
|
||||
if (positions instanceof Float64Array) return Array.from(positions);
|
||||
@@ -1797,10 +1867,40 @@ export function createCesiumDraw({
|
||||
* nor revealed or in segments) changes only its colour: its material
|
||||
* takes the new one in place.
|
||||
*/
|
||||
const recolourable = (was, item) =>
|
||||
const recolourable = (was, item, looks) =>
|
||||
Boolean(was.clampTo && was.material && !was.segments) &&
|
||||
!revealed(item) &&
|
||||
was.shape === lineLook(item, true);
|
||||
was.shape === (looks ? looks.shape(item) : lineLook(item, true));
|
||||
|
||||
/**
|
||||
* The colours the glow lines sharing a material that `list` recolours
|
||||
* are given together: each such material and the item whose colour it
|
||||
* takes, when every line it draws ends the upsert with one look (its
|
||||
* last in `list`); null when they would not, as the material draws them
|
||||
* all. Lines that move then draw on a material of their own.
|
||||
*/
|
||||
function sharedRecolours(parts, list, looks) {
|
||||
const recolours = new Map();
|
||||
// Each shared line's last look in `list`, and the item it is from.
|
||||
let final = null;
|
||||
for (let k = 0; k < list.length; k++) {
|
||||
const was = parts.get(list[k].id);
|
||||
if (was.shared && !was.solo) (final ??= new Map()).set(list[k].id, k);
|
||||
}
|
||||
if (!final) return recolours;
|
||||
const lookOf = new Map();
|
||||
for (const [id, k] of final) {
|
||||
const was = parts.get(id);
|
||||
if (looks[k] === was.look) continue;
|
||||
recolours.set(was.material, list[k]);
|
||||
lookOf.set(was.material, looks[k]);
|
||||
}
|
||||
for (const [id, part] of parts) {
|
||||
if (!part.shared || part.solo || !lookOf.has(part.material)) continue;
|
||||
if (looks[final.get(id)] !== lookOf.get(part.material)) return null;
|
||||
}
|
||||
return recolours;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a draped line's upsert can change it in place: its ground
|
||||
@@ -1832,12 +1932,13 @@ export function createCesiumDraw({
|
||||
const flats = new Map();
|
||||
// Each item's look, in order: an item sent twice is changed twice.
|
||||
const looks = [];
|
||||
const lookOf = lineLooks();
|
||||
for (const item of list) {
|
||||
const was = parts.get(item.id);
|
||||
if (!was) return false;
|
||||
const look = lineLook(item);
|
||||
const look = lookOf.look(item);
|
||||
// Only its colour may change: its material takes it in place.
|
||||
if (was.look !== look && !recolourable(was, item)) return false;
|
||||
if (was.look !== look && !recolourable(was, item, lookOf)) return false;
|
||||
looks.push(look);
|
||||
if (was.deep !== undefined && record.deepLines?.isDestroyed())
|
||||
return false;
|
||||
@@ -1854,12 +1955,20 @@ export function createCesiumDraw({
|
||||
if (!drapedPatchable(record, was, flat)) return false;
|
||||
flats.set(item.id, flat);
|
||||
}
|
||||
const shared = sharedRecolours(parts, list, looks);
|
||||
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 (let k = 0; k < list.length; k++) {
|
||||
const item = list[k];
|
||||
const was = parts.get(item.id);
|
||||
const show = item.show !== false;
|
||||
if (was.look !== looks[k]) {
|
||||
was.material.uniforms.color = toColor(item.style.color);
|
||||
// A line drawing on a shared material took its colour above.
|
||||
if (!was.shared || was.solo)
|
||||
was.material.uniforms.color = toColor(item.style.color);
|
||||
was.look = looks[k];
|
||||
}
|
||||
if (was.polyline) {
|
||||
|
||||
@@ -3705,6 +3705,12 @@ test('a draped line drawn by its own material changes colour in place; a batched
|
||||
assert.equal(glow.appearance.material.uniforms.color.alpha, 0.7);
|
||||
set.upsert([jam(0.3)]);
|
||||
assert.equal(glow.appearance.material.uniforms.color.alpha, 0.3);
|
||||
// A style object the caller changes and sends again is read anew.
|
||||
const again = jam(0.4);
|
||||
set.upsert([again]);
|
||||
again.style.color[3] = 0.6;
|
||||
set.upsert([again]);
|
||||
assert.equal(glow.appearance.material.uniforms.color.alpha, 0.6);
|
||||
// Another width is another line: rebuilt.
|
||||
set.upsert([{ ...jam(0.7), widthPx: 10 }]);
|
||||
assert.notEqual(scene.added[0], host, 'a new width rebuilds');
|
||||
@@ -3714,6 +3720,126 @@ test('a draped line drawn by its own material changes colour in place; a batched
|
||||
set.dispose();
|
||||
}));
|
||||
|
||||
test('draped glow lines that look alike share one ground primitive and recolour together in place', () =>
|
||||
withImageClasses(() => {
|
||||
const { scene, draw } = drawOver();
|
||||
const set = draw.lines({ id: 'glows', clampTo: 'tiles' });
|
||||
const at = (lon) => Cesium.Cartesian3.fromDegrees(lon, 0);
|
||||
const glow = (id, lon, alpha, extra = {}) => ({
|
||||
id,
|
||||
positions: [at(lon), at(lon + 0.01)],
|
||||
widthPx: 9,
|
||||
mode: 'draped',
|
||||
style: { kind: 'glow', color: [0.9, 0.3, 0.3, alpha], power: 0.25 },
|
||||
...extra,
|
||||
});
|
||||
const grounds = () => {
|
||||
const host = scene.added[0];
|
||||
return [...Array(host.length).keys()]
|
||||
.map((i) => host.get(i))
|
||||
.filter((p) => p instanceof Cesium.GroundPolylinePrimitive);
|
||||
};
|
||||
set.set([glow('a', 0, 0.5), glow('b', 1, 0.5), glow('c', 2, 0.5)]);
|
||||
const host = scene.added[0];
|
||||
const [batch] = grounds();
|
||||
assert.equal(grounds().length, 1, 'one primitive for the three');
|
||||
assert.equal(batch.geometryInstances.length, 3);
|
||||
const uniforms = batch.appearance.material.uniforms;
|
||||
// All recoloured alike: in place, on the one material.
|
||||
set.upsert([glow('a', 0, 0.7), glow('b', 1, 0.7), glow('c', 2, 0.7)]);
|
||||
assert.equal(scene.added[0], host, 'in place');
|
||||
assert.ok(!batch.isDestroyed());
|
||||
assert.equal(uniforms.color.alpha, 0.7);
|
||||
// A line moved off the batch, sent twice in one upsert, takes its last
|
||||
// colour on a material of its own; its peers keep theirs.
|
||||
const moved = (alpha) =>
|
||||
glow('a', 0, alpha, { positions: [at(0.5), at(0.51)] });
|
||||
withTerrainHeights(() => set.upsert([moved(0.2), moved(0.7)]));
|
||||
assert.equal(scene.added[0], host, 'moved in place');
|
||||
assert.equal(uniforms.color.alpha, 0.7, 'the peers keep their colour');
|
||||
// Two recoloured and one not: it would change colour with them, so
|
||||
// the set is rebuilt.
|
||||
set.upsert([glow('a', 0, 0.2), glow('b', 1, 0.2)]);
|
||||
assert.notEqual(scene.added[0], host, 'rebuilt');
|
||||
assert.equal(grounds().length, 2, 'one for each look');
|
||||
const rebuilt = scene.added[0];
|
||||
// Another width is another look: drawn apart.
|
||||
set.upsert([glow('c', 2, 0.7, { widthPx: 4 })]);
|
||||
assert.notEqual(scene.added[0], rebuilt);
|
||||
assert.equal(grounds().length, 2);
|
||||
set.dispose();
|
||||
}));
|
||||
|
||||
test('the traffic layer drapes its heat-lines on the tiles and pulses the jam ones in place', async () => {
|
||||
const { createRendering } = await import('../../layers/traffic/rendering.js');
|
||||
const policy = await import('../../layers/traffic/policy.js');
|
||||
await withImageClasses(() => {
|
||||
const { scene, draw } = drawOver();
|
||||
const at = (lon) => Cesium.Cartesian3.fromDegrees(lon, 30.26);
|
||||
const road = (level, lon) => ({
|
||||
flow: { level },
|
||||
segmentDist: [100],
|
||||
waypoints: [at(lon), at(lon + 0.001)],
|
||||
});
|
||||
const state = {
|
||||
_viewer: {},
|
||||
_liveMode: true,
|
||||
_presetDots: 'off',
|
||||
_heatJam: [],
|
||||
_heatLineCount: 0,
|
||||
_heatSupported: true,
|
||||
_heatSet: draw.lines({ id: 'traffic:1:heat', clampTo: 'tiles' }),
|
||||
};
|
||||
const rendering = createRendering({
|
||||
state,
|
||||
services: {},
|
||||
parts: {
|
||||
style: { heatlineOn: () => true },
|
||||
animation: { syncRenderDemand() {} },
|
||||
},
|
||||
source: {},
|
||||
});
|
||||
rendering.rebuildHeatLines([
|
||||
road(0.2, -97.74),
|
||||
road(0.25, -97.735),
|
||||
road(0.6, -97.73),
|
||||
]);
|
||||
const host = scene.added[0];
|
||||
const grounds = [...Array(host.length).keys()]
|
||||
.map((i) => host.get(i))
|
||||
.filter((p) => p instanceof Cesium.GroundPolylinePrimitive);
|
||||
assert.equal(grounds.length, 2, 'the jam lines glowing, the slow ones');
|
||||
assert.ok(
|
||||
grounds.every(
|
||||
(p) =>
|
||||
p.classificationType === Cesium.ClassificationType.CESIUM_3D_TILE,
|
||||
),
|
||||
'draped on the tiles alone',
|
||||
);
|
||||
const glow = grounds.find(
|
||||
(p) => p.appearance?.material?.type === 'PolylineGlow',
|
||||
);
|
||||
assert.ok(glow, 'the jam line glows');
|
||||
assert.equal(
|
||||
glow.appearance.material.uniforms.color.alpha,
|
||||
policy.HEAT_JAM_BASE_ALPHA,
|
||||
);
|
||||
rendering.pulseHeatLines(260 * (Math.PI / 2));
|
||||
assert.equal(scene.added[0], host, 'pulsed where it is');
|
||||
assert.ok(!glow.isDestroyed());
|
||||
assert.ok(
|
||||
Math.abs(
|
||||
glow.appearance.material.uniforms.color.alpha -
|
||||
(policy.HEAT_JAM_BASE_ALPHA + policy.HEAT_JAM_PULSE_ALPHA),
|
||||
) < 1e-6,
|
||||
);
|
||||
rendering.removeHeatLines();
|
||||
assert.equal(state._heatSet.size(), 0);
|
||||
state._heatSet.dispose();
|
||||
assert.equal(scene.added.length, 0);
|
||||
});
|
||||
});
|
||||
|
||||
test('revealFraction draws a line up to that fraction of its length, and changes in place', () =>
|
||||
withImageClasses(() => {
|
||||
const { scene, draw } = drawOver();
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* @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