Files
gods-eye-view/src/layers/streetLevel/cameraFraming.js
T
Sameh Khamis a10f3a16e1 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.
2026-11-07 01:40:32 -08:00

89 lines
3.1 KiB
JavaScript

import { plausibleSurfaceHeight, sampledSurfaceHeight } from './view.js';
import { toRadians } from '../../geo/index.js';
import { cameraOf, findQuery } from '../../render/rendererOf.js';
/** Fly the globe camera once to frame a photo after it opens. */
export function createCameraFraming({ state }) {
/**
* The application's camera authority: `reassert(begin())` succeeds only if
* nothing newer took the camera. Without it, framing flies directly.
*/
let navigation = null;
/** Our framing flight while it is current; Cesium calls `cancel` when another flight starts. */
let flight = null;
function attachNavigation(next) {
navigation = next || null;
}
/** Ask for the camera as a photo starts opening: a ticket for `frame`, null when refused (cockpit). */
function begin() {
if (!navigation?.begin || !navigation.reassert) return { generation: null };
const generation = navigation.begin('photo');
return generation === false ? null : { generation };
}
/**
* Ground under a photo. `sampleHeight` can return kilometres below the
* ellipsoid before tiles load, so only a plausible height counts.
*/
function groundHeightAt(lon, lat, fallback) {
const viewer = state.viewer;
const point = { lon, lat };
let height = sampledSurfaceHeight(viewer, point);
if (height === null && viewer)
height =
findQuery(viewer)?.sampleHeightNow(point, { against: 'terrain' }) ??
null;
if (!plausibleSurfaceHeight(height)) height = fallback;
return plausibleSurfaceHeight(height) ? height : 0;
}
/**
* Frame the open photo from a short distance behind it, unless another
* action took the camera since `ticket` (from `begin`) was issued.
*/
function frame(ticket) {
const { position, bearing, altitude } = state.street;
if (!ticket || !position || !state.viewer) return;
// Detached (shell teardown) since the ticket was issued: nothing to frame for.
if (ticket.generation !== null && !navigation?.reassert(ticket.generation))
return;
const ground = groundHeightAt(position.lon, position.lat, altitude);
// Owned before the call: starting it cancels the previous flight (ours
// included), and a zero-length one completes inside the call.
const current = {};
flight = current;
const release = () => {
if (flight === current) flight = null;
};
const started = cameraOf(state.viewer).flyToBounds(
{
center: { lon: position.lon, lat: position.lat, height: ground + 2 },
radius: 4,
},
{
offset: {
heading: toRadians(bearing || 0),
pitch: toRadians(-32),
range: 140,
},
durationS: 1.6,
onComplete: release,
onCancel: release,
},
);
if (flight === current) current.handle = started;
}
/** Stop our framing flight if it is still in the air; another feature's flight is left alone. */
function cancel() {
if (!flight) return;
const { handle } = flight;
flight = null;
handle?.cancel();
}
return { attachNavigation, begin, frame, cancel };
}