Read Street Level's camera and ground through the renderer's ports

The Street Level view maths no longer reads Cesium. The camera's place
comes from the camera port's pose; the visible box and the view focus
cast the query port's screen rays and meet them with the ground, the
WGS84 ellipsoid raised to the ground height, in geo; heights come from
the query port; the horizon cull for the marker and the cones tests
against an ellipsoid 1,000 m below WGS84 in geo and re-culls on the
frame port's pre-render. Ground clicks, the screen-centre aim, the
nearest-image point, photo framing, the coverage refresh and the ground
re-measure read the same ports and convert positions with geo.

geo's ellipsoid functions (toLonLatH, rayEllipsoid and those under them)
and horizonFrom take an optional ellipsoid, made with ellipsoidOf or
offsetEllipsoid; anything else, a map index included, means WGS84. Tests
check them against Cesium on raised and lowered ellipsoids.

view, index, cameraFraming, groundClick and selection leave the Cesium
allow-list. The Street Level entries are Cesium-free callers of the
unbound factories: given a viewer of a caller's own, with no renderer
bound, the caller loads ./render/cesium/unbound first. GhostMaps, which
uses the horizon cull and is still on the list, loads the camera factory
itself.

Differences the contract does not let us avoid:
- The ground under the camera no longer reads whether the globe is shown
  (the globe port has no visibility getter). Where 3D tiles answer under
  the camera they decide, as they did under Google 3D; elsewhere the
  terrain answers, then the drawn surface below the camera. Over 3D tiles
  shown on a map with the globe (and under other primitives at the
  nadir) those now count as ground.
- Without depth-texture support the query port answers no heights, the
  terrain included, so the ground then reads as the ellipsoid.
- selection and groundClick still read the click dispatcher's own pick
  for now; they move to draw-set hits with the coverage lines.
This commit is contained in:
Sameh Khamis committed 2026-11-07 01:40:32 -08:00
1 parent 30b6be1da0
commit a10f3a16e1
22 files changed
+567 -260

No files matched your search

+59 -19
View File
@@ -47,11 +47,45 @@ export const WGS84 = Object.freeze({
}),
});
/**
* An ellipsoid with these radii, in the form `WGS84` has: for code that
* works on a surface other than WGS84 (the ground raised to a height, a
* horizon taken below it). Pass it as the trailing argument of the
* functions that take one.
* @param {{x: number, y: number, z: number}} radii
*/
export function ellipsoidOf({ x, y, z }) {
return Object.freeze({
radii: Object.freeze({ x, y, z }),
radiiSquared: Object.freeze({ x: x * x, y: y * y, z: z * z }),
oneOverRadii: Object.freeze({ x: 1 / x, y: 1 / y, z: 1 / z }),
oneOverRadiiSquared: Object.freeze({
x: 1 / (x * x),
y: 1 / (y * y),
z: 1 / (z * z),
}),
});
}
/** WGS84 with every radius `metres` longer (shorter, when negative). */
export function offsetEllipsoid(metres) {
const { x, y, z } = WGS84.radii;
return ellipsoidOf({ x: x + metres, y: y + metres, z: z + metres });
}
/**
* The ellipsoid a trailing argument names: one from `ellipsoidOf`, else
* WGS84 (so these functions can still be handed to `map`, whose index
* lands there).
*/
const ellipsoidArg = (ellipsoid) =>
ellipsoid?.oneOverRadiiSquared ? ellipsoid : WGS84;
const EPSILON12 = 1e-12;
const CENTER_TOLERANCE_SQUARED = 0.1;
export function geodeticSurfaceNormal(p) {
const o = WGS84.oneOverRadiiSquared;
export function geodeticSurfaceNormal(p, ellipsoid) {
const o = ellipsoidArg(ellipsoid).oneOverRadiiSquared;
return normalize(vec(p.x * o.x, p.y * o.y, p.z * o.z));
}
@@ -77,11 +111,12 @@ export const fromDegrees = (lon, lat, height = 0) =>
fromRadians(toRadians(lon), toRadians(lat), height);
/** The point on the ellipsoid along the geodetic normal, or null at the centre. */
export function scaleToGeodeticSurface(p) {
export function scaleToGeodeticSurface(p, ellipsoid) {
const e = ellipsoidArg(ellipsoid);
const { x: positionX, y: positionY, z: positionZ } = p;
const oneOverRadiiX = WGS84.oneOverRadii.x;
const oneOverRadiiY = WGS84.oneOverRadii.y;
const oneOverRadiiZ = WGS84.oneOverRadii.z;
const oneOverRadiiX = e.oneOverRadii.x;
const oneOverRadiiY = e.oneOverRadii.y;
const oneOverRadiiZ = e.oneOverRadii.z;
const x2 = positionX * positionX * oneOverRadiiX * oneOverRadiiX;
const y2 = positionY * positionY * oneOverRadiiY * oneOverRadiiY;
const z2 = positionZ * positionZ * oneOverRadiiZ * oneOverRadiiZ;
@@ -91,9 +126,9 @@ export function scaleToGeodeticSurface(p) {
if (squaredNorm < CENTER_TOLERANCE_SQUARED)
return Number.isFinite(ratio) ? intersection : null;
const oneOverRadiiSquaredX = WGS84.oneOverRadiiSquared.x;
const oneOverRadiiSquaredY = WGS84.oneOverRadiiSquared.y;
const oneOverRadiiSquaredZ = WGS84.oneOverRadiiSquared.z;
const oneOverRadiiSquaredX = e.oneOverRadiiSquared.x;
const oneOverRadiiSquaredY = e.oneOverRadiiSquared.y;
const oneOverRadiiSquaredZ = e.oneOverRadiiSquared.z;
const gradient = vec(
intersection.x * oneOverRadiiSquaredX * 2.0,
intersection.y * oneOverRadiiSquaredY * 2.0,
@@ -130,11 +165,14 @@ export function scaleToGeodeticSurface(p) {
);
}
/** ECEF to geodetic `{lon, lat}` radians and `height` metres, or null at the centre. */
export function toCartographicRadians(p) {
const surface = scaleToGeodeticSurface(p);
/**
* ECEF to geodetic `{lon, lat}` radians and `height` metres, or null at the
* centre; on `ellipsoid` (from `ellipsoidOf`) when given, else WGS84.
*/
export function toCartographicRadians(p, ellipsoid) {
const surface = scaleToGeodeticSurface(p, ellipsoid);
if (!surface) return null;
const n = geodeticSurfaceNormal(surface);
const n = geodeticSurfaceNormal(surface, ellipsoid);
const h = sub(p, surface);
return {
lon: Math.atan2(n.y, n.x),
@@ -143,21 +181,23 @@ export function toCartographicRadians(p) {
};
}
/** ECEF to `{lon, lat}` degrees and `height` metres. */
export function toLonLatH(p) {
const c = toCartographicRadians(p);
/** ECEF to `{lon, lat}` degrees and `height` metres, on `ellipsoid` as `toCartographicRadians`. */
export function toLonLatH(p, ellipsoid) {
const c = toCartographicRadians(p, ellipsoid);
if (!c) return null;
return { lon: toDegrees(c.lon), lat: toDegrees(c.lat), height: c.height };
}
/**
* Where a ray first meets the ellipsoid ahead of its origin, as Cesium's
* `camera.pickEllipsoid` takes the hit, or null when it misses.
* `camera.pickEllipsoid` takes the hit, or null when it misses; on
* `ellipsoid` (from `ellipsoidOf`) when given, else WGS84.
* @param {{origin: {x: number, y: number, z: number},
* direction: {x: number, y: number, z: number}}} ray
* @param {typeof WGS84} [ellipsoid]
*/
export function rayEllipsoid(ray) {
const r = WGS84.oneOverRadii;
export function rayEllipsoid(ray, ellipsoid) {
const r = ellipsoidArg(ellipsoid).oneOverRadii;
const q = vec(ray.origin.x * r.x, ray.origin.y * r.y, ray.origin.z * r.z);
const w = vec(
ray.direction.x * r.x,
+66
View File
@@ -6,9 +6,11 @@ import {
distance,
distanceSquared,
eastNorthUp,
ellipsoidOf,
fromDegrees,
geodeticSurfaceNormal,
lerpInto,
offsetEllipsoid,
rayEllipsoid,
scaleToGeodeticSurface,
toCartographicRadians,
@@ -245,3 +247,67 @@ test('in-place vector helpers match the default renderer bit for bit', () => {
{ ...Cesium.Cartesian3.lerp(ca, cb, 0.25, new Cesium.Cartesian3()) },
);
});
test('on another ellipsoid, rays and geodetic positions match the default renderer’s', () => {
const rand = random(7);
for (const metres of [1600, -1000, 30]) {
const ours = offsetEllipsoid(metres);
const theirs = new Cesium.Ellipsoid(
Cesium.Ellipsoid.WGS84.radii.x + metres,
Cesium.Ellipsoid.WGS84.radii.y + metres,
Cesium.Ellipsoid.WGS84.radii.z + metres,
);
for (let i = 0; i < 200; i++) {
const origin = Cesium.Cartesian3.fromDegrees(
rand() * 360 - 180,
rand() * 170 - 85,
100 + rand() * 2e5,
);
const target = Cesium.Cartesian3.fromDegrees(
rand() * 360 - 180,
rand() * 170 - 85,
);
const direction = Cesium.Cartesian3.normalize(
Cesium.Cartesian3.subtract(target, origin, new Cesium.Cartesian3()),
new Cesium.Cartesian3(),
);
const ray = new Cesium.Ray(origin, direction);
const span = Cesium.IntersectionTests.rayEllipsoid(ray, theirs);
const hit = rayEllipsoid({ origin, direction }, ours);
if (!span) {
assert.equal(hit, null);
continue;
}
const expected = Cesium.Ray.getPoint(
ray,
span.start > 0 ? span.start : span.stop,
);
assert.ok(distance(hit, expected) < 1e-3, `ray ${i} at ${metres} m`);
const carto = Cesium.Cartographic.fromCartesian(expected, theirs);
const deg = toLonLatH(expected, ours);
assert.ok(Math.abs(deg.lon - toDegrees(carto.longitude)) < 1e-9);
assert.ok(Math.abs(deg.lat - toDegrees(carto.latitude)) < 1e-9);
assert.ok(Math.abs(deg.height - carto.height) < 1e-3);
}
}
});
test('an ellipsoid argument that is not one (a map index) means WGS84', () => {
const p = fromDegrees(10, 20, 30);
assert.deepEqual(toLonLatH(p, 1), toLonLatH(p));
assert.deepEqual(
rayEllipsoid(
{ origin: { x: 1e7, y: 0, z: 0 }, direction: { x: -1, y: 0, z: 0 } },
0,
),
rayEllipsoid({
origin: { x: 1e7, y: 0, z: 0 },
direction: { x: -1, y: 0, z: 0 },
}),
);
assert.deepEqual(ellipsoidOf({ x: 1, y: 2, z: 4 }).oneOverRadiiSquared, {
x: 1,
y: 0.25,
z: 0.0625,
});
});
+5 -3
View File
@@ -8,10 +8,12 @@ import { WGS84 } from './ellipsoid.js';
/**
* A horizon test from `eye` (ECEF): `isPointVisible(p)` is false for a
* point the ellipsoid hides from it.
* point the ellipsoid hides from it. `ellipsoid` (from `ellipsoidOf`) puts
* the horizon on another surface, as the occluder takes one.
*/
export function horizonFrom(eye) {
const o = WGS84.oneOverRadii;
export function horizonFrom(eye, ellipsoid) {
// Anything but an ellipsoid (a map index, say) means WGS84.
const o = (ellipsoid?.oneOverRadii ? ellipsoid : WGS84).oneOverRadii;
const cx = eye.x * o.x;
const cy = eye.y * o.y;
const cz = eye.z * o.z;
+31 -1
View File
@@ -1,7 +1,7 @@
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import test from 'node:test';
import { fromDegrees } from './ellipsoid.js';
import { fromDegrees, offsetEllipsoid } from './ellipsoid.js';
import { horizonFrom } from './horizon.js';
test('the horizon hides what the default renderer’s occluder hides', () => {
@@ -31,3 +31,33 @@ test('the horizon hides what the default renderer’s occluder hides', () => {
}
}
});
test('a horizon on a lowered ellipsoid hides what the occluder on it hides', () => {
let seed = 11;
const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
const radii = Cesium.Ellipsoid.WGS84.radii;
const lowered = new Cesium.Ellipsoid(
radii.x - 1000,
radii.y - 1000,
radii.z - 1000,
);
for (let i = 0; i < 30; i++) {
const eye = fromDegrees(
random() * 360 - 180,
random() * 170 - 85,
i === 0 ? -1500 : random() * 2e5 - 500,
);
const occluder = new Cesium.EllipsoidalOccluder(
lowered,
new Cesium.Cartesian3(eye.x, eye.y, eye.z),
);
const horizon = horizonFrom(eye, offsetEllipsoid(-1000));
for (let k = 0; k < 50; k++) {
const p = fromDegrees(random() * 360 - 180, random() * 180 - 90, 0);
assert.equal(
horizon.isPointVisible(p),
occluder.isPointVisible(new Cesium.Cartesian3(p.x, p.y, p.z)),
);
}
}
});