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:
German Khokhlov committed 2026-10-15 19:47:43 +04:00
1 parent a3e929870d
commit 16e63f4c50
19 files changed
+601 -215

No files matched your search

+49 -2
View File
@@ -28,6 +28,7 @@ import {
} from '../data/dataCredits.js';
import { unavailablePlaceSearch } from '../search/placeSearch.js';
import { isPickedWorldPosition } from '../data/scenePick.js';
import { isGlobeEngine } from '../globe/contract.js';
/** Ms the analyst region lookup waits on geocode + admin boundary. */
export const REGION_FALLBACK_BUDGET_MS = 3_000;
@@ -1834,7 +1835,10 @@ export function createAnnotationResolver({
*/
function viewportProximity(viewer) {
try {
const carto = viewer?.camera?.positionCartographic;
// A globe engine (search) or a Cesium viewer (annotations until 1c).
const carto = isGlobeEngine(viewer)
? viewer.camera.pose().cartographic
: viewer?.camera?.positionCartographic;
if (!carto) return null;
const lat = geo.math.toDegrees(carto.latitude);
const lon = geo.math.toDegrees(carto.longitude);
@@ -1849,7 +1853,9 @@ export function createAnnotationResolver({
/** Exported for searchAndFlyTo (src/locations.js), which shares this bias. */
function viewportBias(viewer) {
try {
const rect = viewer?.camera?.computeViewRectangle?.();
const rect = isGlobeEngine(viewer)
? viewer.camera.viewRectangle()
: viewer?.camera?.computeViewRectangle?.();
if (!rect) return null;
const swLat = geo.math.toDegrees(rect.south).toFixed(4);
const swLng = geo.math.toDegrees(rect.west).toFixed(4);
@@ -1899,6 +1905,46 @@ export function createAnnotationResolver({
return hit && hit.distanceM <= PLACES_MAX_DISTANCE_M ? hit : null;
}
/** World lon/lat/height from a picked Cartesian, or null for a missed pick. */
function pickedPlace(cart) {
if (!isPickedWorldPosition(cart)) return null;
const carto = geo.cartographic.fromCartesian(cart);
if (!carto) return null;
return {
lat: geo.math.toDegrees(carto.latitude),
lon: geo.math.toDegrees(carto.longitude),
height: Number.isFinite(carto.height) ? carto.height : 0,
};
}
/** pickWorldFromScreen through a globe engine: the same cascade. */
function enginePickWorldFromScreen(engine, nx, ny) {
const { width: w, height: h } = engine.host.size();
if (!w || !h) return null;
const screen = {
x: Math.max(0, Math.min(1, nx)) * w,
y: Math.max(0, Math.min(1, ny)) * h,
};
const stages = [
() =>
engine.picker.pickPositionSupported
? engine.picker.pickPosition(screen)
: undefined,
() => engine.camera.pickEllipsoid(screen),
() => engine.picker.pickGlobe(screen),
];
for (const stage of stages) {
let cart = null;
try {
cart = stage();
} catch {
cart = null;
}
if (isPickedWorldPosition(cart)) return pickedPlace(cart);
}
return null;
}
/**
* Convert a NORMALIZED screen point (x,y in [0,1] of the current viewport) into
* a world lon/lat using the depth-aware pick cascade — the inverse of the
@@ -1907,6 +1953,7 @@ export function createAnnotationResolver({
* name the place, and we anchor the mark to the actual world point under it.
*/
function pickWorldFromScreen(viewer, nx, ny) {
if (isGlobeEngine(viewer)) return enginePickWorldFromScreen(viewer, nx, ny);
const scene = viewer?.scene;
if (!scene) return null;
const canvas = scene.canvas;
+78
View File
@@ -0,0 +1,78 @@
// The view-centred search helpers read the camera through a globe engine as
// well as through a viewer (src/locations.js searchAndFlyTo hands them the
// engine).
import test from 'node:test';
import assert from 'node:assert/strict';
import * as geo from '../geo/index.js';
import { createFakeEngine } from '../globe/testing/fakeEngine.js';
import { FEATURE_SOURCE_METHODS } from '../sources/featureSource.js';
import { createAnnotationResolver } from './resolver.js';
const resolver = createAnnotationResolver({
featureSource: Object.fromEntries(
FEATURE_SOURCE_METHODS.map((method) => [method, async () => null]),
),
});
function lookingDownAt(lon, lat, height = 20000) {
const engine = createFakeEngine();
engine.fake.setPose({
position: geo.cartesian3.fromDegrees(lon, lat, height),
heading: 0,
pitch: -Math.PI / 2,
roll: 0,
});
return engine;
}
test('viewportBias reads the engine view rectangle', () => {
const engine = lookingDownAt(-97.7431, 30.2672);
engine.fake.script('viewRectangle', {
west: geo.math.toRadians(-98),
south: geo.math.toRadians(30),
east: geo.math.toRadians(-97.5),
north: geo.math.toRadians(30.5),
});
assert.equal(resolver.viewportBias(engine), '30.0000,-98.0000|30.5000,-97.5000');
engine.fake.script('viewRectangle', undefined);
assert.equal(resolver.viewportBias(engine), null);
});
test('pickWorldFromScreen tries the depth pick, then the ellipsoid, then the globe', () => {
const engine = lookingDownAt(-97.7431, 30.2672);
const depth = geo.cartesian3.fromDegrees(-97.7, 30.3, 210);
engine.fake.script('pickPosition', depth);
const picked = resolver.pickWorldFromScreen(engine, 0.5, 0.5);
assert.ok(Math.abs(picked.lat - 30.3) < 1e-9 && Math.abs(picked.height - 210) < 1e-6);
const [screen] = engine.fake.callsOf('picker.pickPosition')[0].args;
assert.deepEqual([screen.x, screen.y], [512, 384]);
engine.fake.script('pickPosition', undefined);
const ellipsoid = resolver.pickWorldFromScreen(engine, 0.5, 0.5);
assert.ok(Math.abs(ellipsoid.lon - -97.7431) < 0.01);
assert.ok(Math.abs(ellipsoid.height) < 1e-3);
engine.fake.script('pickEllipsoid', undefined);
engine.fake.script('pickGlobe', geo.cartesian3.fromDegrees(-97.71, 30.25, 300));
const globe = resolver.pickWorldFromScreen(engine, 0.5, 0.5);
assert.ok(Math.abs(globe.height - 300) < 1e-6);
engine.fake.script('pickGlobe', undefined);
assert.equal(resolver.pickWorldFromScreen(engine, 0.5, 0.5), null);
});
test('near-view recovery centres on the engine camera when nothing is picked', async () => {
const engine = lookingDownAt(2.35, 48.85, 10000);
for (const name of ['pickPosition', 'pickEllipsoid', 'pickGlobe'])
engine.fake.script(name, undefined);
const centres = [];
const placeSearch = {
textSearch: async (query, centre) => {
centres.push(centre);
return [];
},
};
await resolver.placesNearViewRecovery(engine, 'cafe', null, undefined, placeSearch);
assert.equal(centres.length, 1);
assert.ok(Math.abs(centres[0].latitude - 48.85) < 1e-9);
assert.ok(Math.abs(centres[0].longitude - 2.35) < 1e-9);
});