mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
refactor(camera): use the globe engine
The startup fly-in, orbit, world-target focus, the arrival ground guard, arrival ownership, camera sensitivity claims, landmark/viewport/globe flights and place search (with its view bias and near-view recovery) take the globe engine. Callers that still hold a viewer (voice, layers until phase 1c) reach the same engine through engineFromViewer, and arrival ownership is keyed by the engine whichever one a caller names. The ground guard yields on camera.commanded instead of patching the viewer camera itself; on Cesium that event hooks the same command methods and tracking changes, so direct Cesium callers still end it. Landmark framing reads the frustum fov from the pose's fovy and aspectRatio. Tests that built viewer doubles now run on the fake engine (or on the Cesium engine over the same double), with the same expectations.
This commit is contained in:
1 parent
a3e929870d
commit
16e63f4c50
19 files changed
+601
-215
No files matched your search
+85
-49
@@ -1,8 +1,8 @@
|
||||
import { isUnavailableCapability } from './sources/capability.js';
|
||||
import { createOverpassFeatureSource } from './sources/overpassFeatures.js';
|
||||
import { applicationServices } from './services/application.js';
|
||||
import * as Cesium from 'cesium';
|
||||
import * as geo from './geo/index.js';
|
||||
import { engineFromViewer } from './globe/cesium/engineFromViewer.js';
|
||||
import {
|
||||
viewportBias,
|
||||
placesNearViewRecovery,
|
||||
@@ -538,16 +538,21 @@ export const GLOBE_VIEW = Object.freeze({
|
||||
/**
|
||||
* Fly straight out to the full-earth globe view, keeping the current sub-camera
|
||||
* point centered so the user's continent stays in front of them.
|
||||
* @param {Cesium.Viewer} viewer
|
||||
* @param {import('./globe/contract.js').GlobeEngine | object} target The globe
|
||||
* engine, or a viewer registered for one.
|
||||
* @param {{duration?: number, onComplete?: Function, onCancel?: Function}} options
|
||||
* @returns {{latitude: number, longitude: number, heightM: number}}
|
||||
* @returns {{latitude: number, longitude: number, heightM: number} | null}
|
||||
* Null without an engine.
|
||||
*/
|
||||
export function flyToGlobeView(viewer, options = {}) {
|
||||
const carto = viewer.camera.positionCartographic;
|
||||
export function flyToGlobeView(target, options = {}) {
|
||||
const engine = engineFromViewer(target);
|
||||
if (!engine) return null;
|
||||
const { camera } = engine;
|
||||
const carto = camera.pose().cartographic;
|
||||
const longitude = geo.math.toDegrees(carto.longitude);
|
||||
const latitude = geo.math.toDegrees(carto.latitude);
|
||||
viewer.camera.cancelFlight();
|
||||
viewer.camera.flyTo({
|
||||
camera.cancelFlight();
|
||||
camera.flyTo({
|
||||
destination: geo.cartesian3.fromDegrees(
|
||||
longitude,
|
||||
latitude,
|
||||
@@ -559,9 +564,10 @@ export function flyToGlobeView(viewer, options = {}) {
|
||||
roll: 0,
|
||||
},
|
||||
duration: finitePositive(options.duration) || GLOBE_VIEW.durationS,
|
||||
endTransform: Cesium.Matrix4.IDENTITY,
|
||||
// Cesium's Camera.flyTo reads `complete`/`cancel`. `onComplete`/`onCancel`
|
||||
// are this module's OWN option names and are silently ignored by Cesium —
|
||||
endTransform: geo.matrix4.IDENTITY,
|
||||
// The engine's flyTo reads `complete`/`cancel`, as Cesium's Camera.flyTo
|
||||
// does. `onComplete`/`onCancel` are this module's OWN option names and were
|
||||
// silently ignored by Cesium —
|
||||
// spelling them through to flyTo meant the reset never resolved on the
|
||||
// flight's own events and every caller fell back to its watchdog timeout.
|
||||
complete: options.onComplete,
|
||||
@@ -584,7 +590,8 @@ export const LOCATIONS = Object.entries(CITY_POIS).map(([id, city]) => ({
|
||||
* Fly the camera to a landmark using lookAt-based targeting.
|
||||
* Guarantees the target is centered in viewport via flyToBoundingSphere + lookAt.
|
||||
*
|
||||
* @param {Cesium.Viewer} viewer
|
||||
* @param {import('./globe/contract.js').GlobeEngine | object} target The
|
||||
* globe engine, or a viewer registered for one.
|
||||
* @param {number} lat - Latitude in degrees
|
||||
* @param {number} lon - Longitude in degrees
|
||||
* @param {object} options
|
||||
@@ -596,10 +603,14 @@ export const LOCATIONS = Object.entries(CITY_POIS).map(([id, city]) => ({
|
||||
* @param {number} options.duration - Flight duration in seconds (default 3.0)
|
||||
* @param {{cachedGroundFloor?: Function, warmGroundFloor?: Function}} [options.ground] -
|
||||
* Optional ground-floor service; its cached elevation outranks `groundElevation`
|
||||
* @returns {{ targetPosition: Cesium.Cartesian3 }} The computed target for orbit use
|
||||
* @returns {{ targetPosition: {x: number, y: number, z: number} } | null} The
|
||||
* computed target for orbit use; null without an engine.
|
||||
*/
|
||||
export function flyToLandmark(viewer, lat, lon, options = {}) {
|
||||
cancelCameraArrival(viewer);
|
||||
export function flyToLandmark(target, lat, lon, options = {}) {
|
||||
const engine = engineFromViewer(target);
|
||||
if (!engine) return null;
|
||||
const { camera } = engine;
|
||||
cancelCameraArrival(engine);
|
||||
const {
|
||||
range = 500,
|
||||
pitch = -30,
|
||||
@@ -616,7 +627,7 @@ export function flyToLandmark(viewer, lat, lon, options = {}) {
|
||||
|
||||
// Sample terrain height (sync — uses loaded tiles; 0 if globe/terrain not ready)
|
||||
const targetCartographic = geo.cartographic.fromDegrees(lon, lat);
|
||||
const sampledHeight = viewer.scene.globe?.getHeight(targetCartographic);
|
||||
const sampledHeight = engine.surface.terrainHeight(targetCartographic);
|
||||
|
||||
// Use sampled height if available, otherwise the cached ground-floor
|
||||
// elevation (same ellipsoidal datum), then the pre-baked city elevation.
|
||||
@@ -646,16 +657,16 @@ export function flyToLandmark(viewer, lat, lon, options = {}) {
|
||||
const boundingRadius = bounds ? buildingBoundingRadius(bounds) : 0;
|
||||
const framingRange = bounds
|
||||
? Math.max(
|
||||
rangeForBoundingSphere(viewer, boundingRadius),
|
||||
rangeForBoundingSphere(engine, boundingRadius),
|
||||
boundingRadius * 1.35,
|
||||
)
|
||||
: range;
|
||||
|
||||
const hpr = new Cesium.HeadingPitchRange(
|
||||
geo.math.toRadians(heading),
|
||||
geo.math.toRadians(pitch),
|
||||
framingRange,
|
||||
);
|
||||
const hpr = {
|
||||
heading: geo.math.toRadians(heading),
|
||||
pitch: geo.math.toRadians(pitch),
|
||||
range: framingRange,
|
||||
};
|
||||
|
||||
if (typeof onStart === 'function') {
|
||||
try {
|
||||
@@ -666,20 +677,20 @@ export function flyToLandmark(viewer, lat, lon, options = {}) {
|
||||
}
|
||||
|
||||
// Fly to target, then lock with lookAt for guaranteed centering
|
||||
viewer.camera.flyToBoundingSphere(
|
||||
new Cesium.BoundingSphere(targetPosition, boundingRadius),
|
||||
camera.flyToSphere(
|
||||
{ center: targetPosition, radius: boundingRadius },
|
||||
{
|
||||
offset: hpr,
|
||||
...hpr,
|
||||
duration,
|
||||
complete: () => {
|
||||
viewer.camera.lookAt(targetPosition, hpr);
|
||||
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
|
||||
camera.lookAt(targetPosition, hpr);
|
||||
camera.release();
|
||||
// Framing used a predicted ground height. Once tiles stream in, measure
|
||||
// the rendered surface and lift the eye if it is buried or pressed
|
||||
// against it; a newer arrival or a manual gesture ends the check.
|
||||
try {
|
||||
guardCameraAboveGround(
|
||||
viewer,
|
||||
engine,
|
||||
{ lat, lon },
|
||||
{ isStale: () => generation !== arrivalGeneration },
|
||||
);
|
||||
@@ -718,7 +729,7 @@ export function flyToLandmark(viewer, lat, lon, options = {}) {
|
||||
* Fly to a preset location by ID (uses the first POI as default).
|
||||
* Returns target position for orbit controller.
|
||||
*/
|
||||
export function flyToPresetLocation(viewer, locationId, options = {}) {
|
||||
export function flyToPresetLocation(target, locationId, options = {}) {
|
||||
const city = CITY_POIS[locationId];
|
||||
if (!city) return null;
|
||||
if (
|
||||
@@ -726,7 +737,7 @@ export function flyToPresetLocation(viewer, locationId, options = {}) {
|
||||
!finitePositive(options.range) &&
|
||||
city.viewBounds
|
||||
) {
|
||||
return flyToViewportBounds(viewer, city.viewBounds, {
|
||||
return flyToViewportBounds(engineFromViewer(target), city.viewBounds, {
|
||||
duration: options.duration,
|
||||
onStart: options.onStart,
|
||||
onComplete: options.onComplete,
|
||||
@@ -735,7 +746,7 @@ export function flyToPresetLocation(viewer, locationId, options = {}) {
|
||||
});
|
||||
}
|
||||
const poi = city.pois[0];
|
||||
return flyToLandmark(viewer, poi.lat, poi.lon, {
|
||||
return flyToLandmark(target, poi.lat, poi.lon, {
|
||||
range: poi.alt,
|
||||
pitch: poi.pitch,
|
||||
heading: poi.heading || 0,
|
||||
@@ -750,11 +761,11 @@ export function flyToPresetLocation(viewer, locationId, options = {}) {
|
||||
* Fly to a specific POI within a city.
|
||||
* Returns target position for orbit controller.
|
||||
*/
|
||||
export function flyToPOI(viewer, cityId, poiIndex, options = {}) {
|
||||
export function flyToPOI(target, cityId, poiIndex, options = {}) {
|
||||
const city = CITY_POIS[cityId];
|
||||
if (!city || !city.pois[poiIndex]) return null;
|
||||
const poi = city.pois[poiIndex];
|
||||
return flyToLandmark(viewer, poi.lat, poi.lon, {
|
||||
return flyToLandmark(target, poi.lat, poi.lon, {
|
||||
range: poi.alt,
|
||||
pitch: poi.pitch,
|
||||
heading: poi.heading || 0,
|
||||
@@ -811,15 +822,18 @@ export const CANCELLED_SEARCH = Object.freeze({ cancelled: true });
|
||||
* appropriate to the request. Countries and cities use their viewport by
|
||||
* default; precise landmarks/buildings use close landmark framing.
|
||||
*/
|
||||
export async function searchAndFlyTo(viewer, query, options = {}) {
|
||||
export async function searchAndFlyTo(target, query, options = {}) {
|
||||
const { placeSearch = unavailablePlaceSearch, signal } = options;
|
||||
// The globe engine, or (voice, until phase 1c) a viewer registered for one.
|
||||
const engine = engineFromViewer(target);
|
||||
if (!engine) return null;
|
||||
signal?.throwIfAborted();
|
||||
const beforeFly =
|
||||
typeof options.beforeFly === 'function' ? options.beforeFly : null;
|
||||
const mayFly = () =>
|
||||
!signal?.aborted && (beforeFly === null || beforeFly() !== false);
|
||||
const outcome = await placeSearch.geocode(query, {
|
||||
bias: viewportBias(viewer),
|
||||
bias: viewportBias(engine),
|
||||
signal,
|
||||
});
|
||||
signal?.throwIfAborted();
|
||||
@@ -838,7 +852,7 @@ export async function searchAndFlyTo(viewer, query, options = {}) {
|
||||
const recovered = result?.exact
|
||||
? null
|
||||
: await (options.recoverNearView || placesNearViewRecovery)(
|
||||
viewer,
|
||||
engine,
|
||||
query,
|
||||
result && !outcome.fallbackUsed ? { lat, lon: lng } : null,
|
||||
signal,
|
||||
@@ -874,7 +888,7 @@ export async function searchAndFlyTo(viewer, query, options = {}) {
|
||||
navigationMode === 'area-overview' ? regionFramingPlan(viewport) : null;
|
||||
if (swath?.mode === 'swath') {
|
||||
if (!mayFly()) return CANCELLED_SEARCH;
|
||||
flyToLandmark(viewer, swath.centerLat, swath.centerLng, {
|
||||
flyToLandmark(engine, swath.centerLat, swath.centerLng, {
|
||||
range: swath.rangeM,
|
||||
pitch: swath.pitchDeg,
|
||||
heading: swath.headingDeg,
|
||||
@@ -910,7 +924,7 @@ export async function searchAndFlyTo(viewer, query, options = {}) {
|
||||
const framedViewport = gateFraming
|
||||
? placeFramingViewport(viewport, lat, lng, types)
|
||||
: viewport;
|
||||
const flight = flyToViewportBounds(viewer, framedViewport, {
|
||||
const flight = flyToViewportBounds(engine, framedViewport, {
|
||||
duration,
|
||||
navigationMode,
|
||||
beforeFly: mayFly,
|
||||
@@ -956,7 +970,7 @@ export async function searchAndFlyTo(viewer, query, options = {}) {
|
||||
]);
|
||||
const searchGroundElevation = ground?.cachedGroundFloor?.(flyLat, flyLon);
|
||||
if (!mayFly()) return CANCELLED_SEARCH;
|
||||
const flight = flyToLandmark(viewer, flyLat, flyLon, {
|
||||
const flight = flyToLandmark(engine, flyLat, flyLon, {
|
||||
range,
|
||||
pitch: buildingPitch(buildingBounds),
|
||||
heading: 30,
|
||||
@@ -1274,7 +1288,7 @@ function shouldFrameGeocodeViewport(mode) {
|
||||
);
|
||||
}
|
||||
|
||||
function flyToViewportBounds(viewer, viewport, options = {}) {
|
||||
function flyToViewportBounds(engine, viewport, options = {}) {
|
||||
const {
|
||||
duration = 3.0,
|
||||
beforeFly = null,
|
||||
@@ -1298,8 +1312,9 @@ function flyToViewportBounds(viewer, viewport, options = {}) {
|
||||
// that crosses the antimeridian: Alaska (sw 172.3E, ne -130.0) subtracts to
|
||||
// -302, and a 0.4-degree metro box straddling the dateline subtracts to -359.6,
|
||||
// whose 12% padding alone is 43 degrees — that box framed 86.7 degrees of ocean
|
||||
// instead of a city. Cesium's Rectangle does NOT normalize past +/-180, so the
|
||||
// padded edges are wrapped here into a proper east<west crossing rectangle.
|
||||
// instead of a city. A rectangle destination (Cesium's Rectangle) does NOT
|
||||
// normalize past +/-180, so the padded edges are wrapped here into a proper
|
||||
// east<west crossing rectangle.
|
||||
const metrics = viewportMetrics(viewport);
|
||||
const latitudePadding = Math.max(0.05, Math.abs(metrics.latSpanDeg) * 0.12);
|
||||
const longitudePadding = Math.max(0.05, metrics.lonSpanDeg * 0.12);
|
||||
@@ -1308,8 +1323,8 @@ function flyToViewportBounds(viewer, viewport, options = {}) {
|
||||
const north = Math.min(89.9, northeast.lat + latitudePadding);
|
||||
const rectangle =
|
||||
paddedLonSpan >= 360
|
||||
? Cesium.Rectangle.fromDegrees(-180, south, 180, north)
|
||||
: Cesium.Rectangle.fromDegrees(
|
||||
? rectangleFromDegrees(-180, south, 180, north)
|
||||
: rectangleFromDegrees(
|
||||
wrapLongitude(southwest.lng - longitudePadding),
|
||||
south,
|
||||
wrapLongitude(southwest.lng + metrics.lonSpanDeg + longitudePadding),
|
||||
@@ -1324,10 +1339,10 @@ function flyToViewportBounds(viewer, viewport, options = {}) {
|
||||
/* no-op */
|
||||
}
|
||||
}
|
||||
viewer.camera.flyTo({
|
||||
engine.camera.flyTo({
|
||||
destination: rectangle,
|
||||
duration,
|
||||
endTransform: Cesium.Matrix4.IDENTITY,
|
||||
endTransform: geo.matrix4.IDENTITY,
|
||||
complete: () => {
|
||||
if (typeof onComplete === 'function') {
|
||||
try {
|
||||
@@ -1378,10 +1393,31 @@ function buildingBoundingRadius(bounds) {
|
||||
return Math.hypot(halfHeight, halfWidth, halfDepth) * 1.18;
|
||||
}
|
||||
|
||||
function rangeForBoundingSphere(viewer, radius) {
|
||||
const frustum = viewer.camera.frustum;
|
||||
const verticalFov = Number(frustum?.fov) || geo.math.toRadians(60);
|
||||
const aspectRatio = Math.max(0.5, Number(frustum?.aspectRatio) || 1);
|
||||
/** A {west, south, east, north} radians rectangle, as Cesium's Rectangle.fromDegrees. */
|
||||
function rectangleFromDegrees(west, south, east, north) {
|
||||
return {
|
||||
west: geo.math.toRadians(west),
|
||||
south: geo.math.toRadians(south),
|
||||
east: geo.math.toRadians(east),
|
||||
north: geo.math.toRadians(north),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The perspective frustum's `fov` (Cesium's PerspectiveFrustum: the angle
|
||||
* across the wider side) from the pose's vertical fov and aspect ratio.
|
||||
*/
|
||||
function frustumFov({ fovy, aspectRatio }) {
|
||||
if (!Number.isFinite(fovy) || !Number.isFinite(aspectRatio)) return NaN;
|
||||
return aspectRatio <= 1
|
||||
? fovy
|
||||
: 2 * Math.atan(Math.tan(fovy / 2) * aspectRatio);
|
||||
}
|
||||
|
||||
function rangeForBoundingSphere(engine, radius) {
|
||||
const pose = engine.camera.pose();
|
||||
const verticalFov = frustumFov(pose) || geo.math.toRadians(60);
|
||||
const aspectRatio = Math.max(0.5, Number(pose.aspectRatio) || 1);
|
||||
const horizontalFov = 2 * Math.atan(Math.tan(verticalFov / 2) * aspectRatio);
|
||||
const limitingFov = Math.min(verticalFov, horizontalFov);
|
||||
// Target 57% sphere occupancy so perspective still leaves at least 40%
|
||||
|
||||
Reference in new issue
Block a user