Draw Street Level, traffic and directions through one display each

- Street Level: coverage sequences are ground corridor sets of up to
  120, draped on terrain and tiles as Cesium's ground polylines were;
  the marker and view cones are sprite movers turned to their compass
  bearing on screen and culled 1 km under WGS84. The layer no longer
  loads the Cesium SDK on Navara.
- Traffic: vehicles are two point mover sets (an outline disc under each
  fill) scaled and faded with Cesium's NearFarScalar maths; congestion
  heat lines are ground corridor sets draped on tiles. Heights, depth,
  readiness and the surface key go through the engine, and Navara no
  longer loads the Cesium SDK for traffic.
- Directions: the route is one ground corridor set in the annotation
  route's dash pattern, maneuvers are point movers and the A/B markers a
  disc and a letter sprite; the in-layer Navara branch and the Cesium
  point collection go.

Known gaps against Cesium, to close in the contract: the traffic heat
lines lose their glow and pulse and the route its flowing dashes
(no animated ground fills yet), and dense Street Level coverage on
Navara is costly as ground corridors (about 80 to 190 ms a frame at
6,000 to 14,400 sequences) until corridors are binned set-wide.

Coupling: Navara renderer lines fall from 3349 to 2767, Cesium-importing
files from 51 to 32, `viewer` lines from 1591 to 1373, Cesium SDK loads
on Navara from 6 to 4.
This commit is contained in:
German Khokhlov committed 2026-11-07 08:03:25 +04:00
1 parent 2fbdca62be
commit 1559bd57f4
78 files changed
+4762 -3719

No files matched your search

+88 -166
View File
@@ -1,10 +1,28 @@
import * as Cesium from 'cesium';
// What Street Level reads of the camera and the ground, on the globe
// contract: `view` is the layer's display (render/display.js), whose
// `engine` it asks. Screen rays meet an ellipsoid raised to the ground
// under the camera, as Cesium's camera.pickEllipsoid does with that
// ellipsoid, so a hidden globe (Google 3D) still has a ground to aim at.
import * as geo from '../../geo/index.js';
import { intersectEllipsoid } from '../../globe/cameraMath.js';
import { wrapLon } from './tileMath.js';
const engineOf = (view) => view?.engine ?? null;
/** The camera's Cartographic (radians, metres), or null without an engine. */
export const cameraLocation = (view) =>
view?.engine?.camera.pose().cartographic ??
view?.camera?.positionCartographic;
const canvasOf = (view) => view?.engine?.host.canvas ?? view?.scene?.canvas;
engineOf(view)?.camera.pose().cartographic ?? null;
/** The canvas size in CSS px ({0, 0} without an engine). */
function canvasSize(view) {
const size = engineOf(view)?.host?.size?.();
return { width: size?.width || 0, height: size?.height || 0 };
}
/** Whether Google 3D tiles are the surface shown (the globe is hidden). */
const tilesShown = (engine) => engine?.maps?.photorealActive?.() === true;
/** Equirectangular metres between {lon, lat} points, the short way round ±180°; street scale. */
export function metresBetween(a, b) {
const lat = (((a.lat + b.lat) / 2) * Math.PI) / 180;
@@ -24,82 +42,83 @@ export function plausibleSurfaceHeight(height) {
* when unsampleable or implausible (`sampleHeight` reads kilometres
* underground before tiles load).
*/
export function sampledSurfaceHeight(scene, carto) {
export function sampledSurfaceHeight(engine, carto) {
let height = null;
try {
if (scene?.sampleHeightSupported) height = scene.sampleHeight(carto);
if (engine?.surface?.supported) height = engine.surface.sampleHeight(carto);
} catch {
/* not sampleable yet */
}
return plausibleSurfaceHeight(height) ? height : null;
}
/** Height (m) of the terrain at `carto`, or null. */
export function terrainSurfaceHeight(engine, carto) {
let height;
try {
if (engine?.surface?.terrainHeightSupported)
height = engine.surface.terrainHeight(carto);
} catch {
height = undefined;
}
return Number.isFinite(height) ? height : null;
}
/**
* Ellipsoidal height (m) of the ground under the camera, or null. Under
* Google 3D (globe hidden) the rendered surface counts, minus any roof above
* the camera.
*/
export function groundUnderCamera(viewer) {
const carto = cameraLocation(viewer);
const scene = viewer?.scene;
if (carto && viewer?.engine) {
const surface = viewer.engine.surface;
const h = surface.sampleHeight(carto) ?? surface.terrainHeight(carto);
return plausibleSurfaceHeight(h) && h < carto.height ? h : null;
export function groundUnderCamera(view) {
const engine = engineOf(view);
const carto = cameraLocation(view);
if (!carto) return null;
if (!tilesShown(engine)) {
const ground = terrainSurfaceHeight(engine, carto);
if (ground !== null) return ground;
}
if (!carto || !scene) return null;
const globe = scene.globe;
if (globe && globe.show !== false) {
const ground = globe.getHeight?.(carto);
if (Number.isFinite(ground)) return ground;
}
const sampled = sampledSurfaceHeight(scene, carto);
const sampled = sampledSurfaceHeight(engine, carto);
return sampled !== null && sampled < carto.height ? sampled : null;
}
/** The ellipsoid `height` metres above `ellipsoid` (the ground at that height). */
function raisedEllipsoid(ellipsoid, height) {
if (!Number.isFinite(height) || Math.abs(height) < 1) return ellipsoid;
const { x, y, z } = ellipsoid.radii;
return new Cesium.Ellipsoid(x + height, y + height, z + height);
/** WGS84 raised by `height` metres (the ground at that height), else WGS84. */
function raisedEllipsoid(height) {
const wgs84 = geo.ellipsoid.WGS84;
if (!Number.isFinite(height) || Math.abs(height) < 1)
return { ellipsoid: wgs84, height: 0 };
const { x, y, z } = wgs84.radii;
return {
ellipsoid: geo.ellipsoid.create(x + height, y + height, z + height),
height,
};
}
/**
* Where a screen point's ray meets the ground, as a Cartographic, or null.
* `maxRange` (metres from the camera) drops hits near the horizon.
*/
function groundHit(camera, point, ellipsoid, maxRange, engine) {
function groundHit(engine, point, { ellipsoid, height }, maxRange) {
let cartesian = null;
try {
cartesian = engine
? engine.picker.pickGlobe(point)
: camera.pickEllipsoid(point, ellipsoid);
const { origin, direction } = engine.camera.pickRay(point);
cartesian = intersectEllipsoid(origin, direction, height);
} catch {
cartesian = null;
}
if (!cartesian) return null;
if (
Number.isFinite(maxRange) &&
(engine?.camera.livePosition || camera.positionWC) &&
Cesium.Cartesian3.distance(
cartesian,
engine?.camera.livePosition ?? camera.positionWC,
) > maxRange
geo.cartesian3.distance(cartesian, engine.camera.livePosition) > maxRange
)
return null;
return (
Cesium.Cartographic.fromCartesian(
cartesian,
engine ? Cesium.Ellipsoid.WGS84 : ellipsoid,
) || null
);
return geo.cartographic.fromCartesian(cartesian, ellipsoid) || null;
}
/** Camera height (m) above the ground under it. */
export function cameraHeightAboveGround(viewer) {
const carto = cameraLocation(viewer);
export function cameraHeightAboveGround(view) {
const carto = cameraLocation(view);
if (!carto) return null;
return carto.height - (groundUnderCamera(viewer) ?? 0);
return carto.height - (groundUnderCamera(view) ?? 0);
}
/** Screen rays per side, less one, that `visibleBbox` casts. */
@@ -111,32 +130,22 @@ const BBOX_GRID = 5;
* to the ground (Denver is 1,600 m up); `maxRange` (m) drops far hits.
*/
export function visibleBbox(
viewer,
view,
{ groundHeight = null, maxRange = null } = {},
) {
const scene = viewer?.scene;
const camera = viewer?.camera;
if ((!scene && !viewer?.engine) || !camera) return null;
const canvas = canvasOf(viewer);
const width = canvas?.clientWidth || canvas?.width || 0;
const height = canvas?.clientHeight || canvas?.height || 0;
const engine = engineOf(view);
if (!engine) return null;
const { width, height } = canvasSize(view);
const hits = [];
if (width > 0 && height > 0) {
const ellipsoid = raisedEllipsoid(
scene?.globe?.ellipsoid || Cesium.Ellipsoid.WGS84,
groundHeight,
);
const point = new Cesium.Cartesian2();
const ellipsoid = raisedEllipsoid(groundHeight);
for (let i = 0; i <= BBOX_GRID; i++) {
for (let j = 0; j <= BBOX_GRID; j++) {
point.x = (width * i) / BBOX_GRID;
point.y = (height * j) / BBOX_GRID;
const carto = groundHit(
camera,
point,
engine,
{ x: (width * i) / BBOX_GRID, y: (height * j) / BBOX_GRID },
ellipsoid,
maxRange,
viewer.engine,
);
if (carto) hits.push(carto);
}
@@ -148,8 +157,8 @@ export function visibleBbox(
let east = -Infinity;
let north = -Infinity;
for (const carto of hits) {
const lon = Cesium.Math.toDegrees(carto.longitude);
const lat = Cesium.Math.toDegrees(carto.latitude);
const lon = geo.math.toDegrees(carto.longitude);
const lat = geo.math.toDegrees(carto.latitude);
west = Math.min(west, lon);
east = Math.max(east, lon);
south = Math.min(south, lat);
@@ -162,7 +171,7 @@ export function visibleBbox(
let west360 = Infinity;
let east360 = -Infinity;
for (const carto of hits) {
const lon = Cesium.Math.toDegrees(carto.longitude);
const lon = geo.math.toDegrees(carto.longitude);
const shifted = lon < 0 ? lon + 360 : lon;
west360 = Math.min(west360, shifted);
east360 = Math.max(east360, shifted);
@@ -178,12 +187,12 @@ export function visibleBbox(
}
/** The point on the ground under the camera, as {lon, lat} degrees. */
export function cameraNadir(viewer) {
const carto = cameraLocation(viewer);
export function cameraNadir(view) {
const carto = cameraLocation(view);
if (!carto) return null;
return {
lon: Cesium.Math.toDegrees(carto.longitude),
lat: Cesium.Math.toDegrees(carto.latitude),
lon: geo.math.toDegrees(carto.longitude),
lat: geo.math.toDegrees(carto.latitude),
};
}
@@ -191,30 +200,20 @@ export function cameraNadir(viewer) {
* Where coverage tiles are ranked from: halfway between the nadir and the
* screen centre's ground, or the nadir when that is out of `maxRange`.
*/
export function viewFocus(
viewer,
{ groundHeight = null, maxRange = null } = {},
) {
const nadir = cameraNadir(viewer);
const scene = viewer?.scene;
const canvas = canvasOf(viewer);
const width = canvas?.clientWidth || canvas?.width || 0;
const height = canvas?.clientHeight || canvas?.height || 0;
export function viewFocus(view, { groundHeight = null, maxRange = null } = {}) {
const nadir = cameraNadir(view);
const { width, height } = canvasSize(view);
if (!nadir || !width || !height) return nadir;
const hit = groundHit(
viewer.camera,
new Cesium.Cartesian2(width / 2, height / 2),
raisedEllipsoid(
scene?.globe?.ellipsoid || Cesium.Ellipsoid.WGS84,
groundHeight,
),
engineOf(view),
{ x: width / 2, y: height / 2 },
raisedEllipsoid(groundHeight),
maxRange,
viewer.engine,
);
if (!hit) return nadir;
const ahead = {
lon: Cesium.Math.toDegrees(hit.longitude),
lat: Cesium.Math.toDegrees(hit.latitude),
lon: geo.math.toDegrees(hit.longitude),
lat: geo.math.toDegrees(hit.latitude),
};
return {
lon: wrapLon(nadir.lon + wrapLon(ahead.lon - nadir.lon) / 2),
@@ -222,82 +221,10 @@ export function viewFocus(
};
}
/**
* Horizon occluder depth below WGS84 (m): ground can lie below the ellipsoid
* (NYC −22 m), and an occluder at 0 m would hide the cones around it.
*/
const HORIZON_CULL_DEPTH_M = 1000;
let HORIZON_CULL_ELLIPSOID;
/**
* Hide billboards and points behind the horizon: they skip the depth test, so
* nothing else stops the far side drawing through the globe. Re-culls on
* pre-render after the camera moves; stops when nothing is left to cull.
* @param {{getViewer: () => object|null, items: () => Iterable<{position: object, show: boolean}>, onChange?: () => void}} options
*/
export function createHorizonCull({ getViewer, items, onChange }) {
let occluder;
let from = null;
let stopListening = null;
/** Show what is in front of the horizon, hide the rest; the count seen. */
function cull(list, position) {
HORIZON_CULL_ELLIPSOID ??= new Cesium.Ellipsoid(
Cesium.Ellipsoid.WGS84.radii.x - HORIZON_CULL_DEPTH_M,
Cesium.Ellipsoid.WGS84.radii.y - HORIZON_CULL_DEPTH_M,
Cesium.Ellipsoid.WGS84.radii.z - HORIZON_CULL_DEPTH_M,
);
occluder ??= new Cesium.EllipsoidalOccluder(HORIZON_CULL_ELLIPSOID);
occluder.cameraPosition = position;
let seen = 0;
let changed = false;
for (const item of list) {
seen++;
const show = occluder.isPointVisible(item.position);
if (item.show !== show) {
item.show = show;
changed = true;
}
}
if (changed) onChange?.();
return seen;
}
/** Cull everything from the camera at `position`; the count seen. */
function cullAll(position) {
from = Cesium.Cartesian3.clone(position, from);
return cull(items(), position);
}
function onPreRender() {
const position = getViewer()?.camera?.positionWC;
if (!position || (from && Cesium.Cartesian3.equals(from, position))) return;
if (!cullAll(position)) stop();
}
/** Cull `added` items (or all) now, then all again whenever the camera moves. */
function update(added) {
const viewer = getViewer();
const position = viewer?.camera?.positionWC;
if (position && !(added ? cull(added, position) : cullAll(position)))
return;
stopListening ||=
viewer?.scene?.preRender?.addEventListener(onPreRender) || null;
}
function stop() {
stopListening?.();
stopListening = null;
from = null;
}
return { update, stop };
}
/** The middle of the visible ground, else the point under the camera; null without a camera. */
export function viewCentre(viewer) {
if (!viewer) return null;
const bbox = visibleBbox(viewer);
export function viewCentre(view) {
if (!engineOf(view)) return null;
const bbox = visibleBbox(view);
if (bbox) {
// Across the date line (west > east) the middle is on the far side of 0°.
const span = bbox[2] - bbox[0] + (bbox[0] > bbox[2] ? 360 : 0);
@@ -305,10 +232,5 @@ export function viewCentre(viewer) {
if (lon > 180) lon -= 360;
return { lat: (bbox[1] + bbox[3]) / 2, lon };
}
const carto = cameraLocation(viewer);
if (!carto) return null;
return {
lat: Cesium.Math.toDegrees(carto.latitude),
lon: Cesium.Math.toDegrees(carto.longitude),
};
return cameraNadir(view);
}