mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
refactor(ui): navigation, orientation, share links and HUD use the globe engine
The application shell keeps the page's globe engine and hands it to the navigation controller, location navigation (search, city and POI flights, orbit, globe reset), the tilt and north-up controls, share-link save and restore, share restoration's first-gesture listeners and the HUD. The viewer is passed only where entity tracking still lives on it (phase 1c) and to the HUD's basemap label context (voice); every such use is null-safe. Share links and captured shots write heading and roll as Cesium's camera reports them (src/cameraAngles.js): the contract's [0, 2π) pose maps an exact 0 back to Cesium's 2π, except the roll of a vertical view, so links stay byte-identical (heading=360, roll=360) on every engine. Source-reading tests follow the moved code with the same checks.
This commit is contained in:
1 parent
16e63f4c50
commit
ec38e6ef16
19 files changed
+594
-226
No files matched your search
+151
-108
@@ -1,6 +1,21 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import * as geo from '../geo/index.js';
|
||||
import { isPickedWorldPosition } from '../data/scenePick.js';
|
||||
import { isGlobeEngine } from '../globe/contract.js';
|
||||
import { engineFromViewer } from '../globe/cesium/engineFromViewer.js';
|
||||
|
||||
// The camera is the globe engine's. Entity tracking stays on the Cesium viewer
|
||||
// until phase 1c, so the viewer, when there is one, is only read for its
|
||||
// tracked entity: callers pass an engine and that viewer, or a viewer whose
|
||||
// engine is registered (engineFromViewer).
|
||||
const engineOf = (target) => engineFromViewer(target);
|
||||
const trackerOf = (target, tracker) =>
|
||||
tracker ?? (target && !isGlobeEngine(target) ? target : null);
|
||||
|
||||
// Cesium's EasingFunction.CUBIC_IN_OUT (tween.js Cubic.InOut).
|
||||
function cubicInOut(amount) {
|
||||
if ((amount *= 2) < 1) return 0.5 * amount * amount * amount;
|
||||
return 0.5 * ((amount -= 2) * amount * amount + 2);
|
||||
}
|
||||
|
||||
function trackedTarget(viewer) {
|
||||
const entity = viewer.trackedEntity;
|
||||
@@ -21,69 +36,66 @@ function normalizedHeading(heading) {
|
||||
return Math.abs(wrapped - geo.math.TWO_PI) < geo.math.EPSILON10 ? 0 : wrapped;
|
||||
}
|
||||
|
||||
/** Return the world position under the center of the canvas, when available. */
|
||||
export function pickViewTarget(viewer) {
|
||||
const scene = viewer?.scene;
|
||||
const camera = viewer?.camera;
|
||||
const canvas = scene?.canvas;
|
||||
if (!scene || !camera || !canvas) return null;
|
||||
const width = canvas.clientWidth || canvas.width || 0;
|
||||
const height = canvas.clientHeight || canvas.height || 0;
|
||||
/**
|
||||
* Return the world position under the center of the canvas, when available.
|
||||
* @param {import('../globe/contract.js').GlobeEngine | object} target The
|
||||
* globe engine, or a viewer registered for one.
|
||||
*/
|
||||
export function pickViewTarget(target) {
|
||||
const engine = engineOf(target);
|
||||
if (!engine) return null;
|
||||
const { width, height } = engine.host.size();
|
||||
if (!width || !height) return null;
|
||||
const center = new Cesium.Cartesian2(width / 2, height / 2);
|
||||
let target = null;
|
||||
|
||||
if (scene.pickPositionSupported && typeof scene.pickPosition === 'function') {
|
||||
const center = { x: width / 2, y: height / 2 };
|
||||
const { picker, camera } = engine;
|
||||
// Depth first, then terrain before the ellipsoid: the globe pick
|
||||
// intersects the rendered surface, so over keyless elevated terrain it
|
||||
// returns the ground the operator is looking at. pickEllipsoid answers with
|
||||
// sea level, which on a plateau sits far below the view and would swing the
|
||||
// camera on every tilt.
|
||||
const stages = [
|
||||
() =>
|
||||
picker.pickPositionSupported ? picker.pickPosition(center) : undefined,
|
||||
() => picker.pickGlobe(center),
|
||||
() => camera.pickEllipsoid(center),
|
||||
];
|
||||
for (const stage of stages) {
|
||||
let picked = null;
|
||||
try {
|
||||
target = scene.pickPosition(center);
|
||||
picked = stage();
|
||||
} catch {
|
||||
target = null;
|
||||
picked = null;
|
||||
}
|
||||
if (isPickedWorldPosition(picked)) return picked;
|
||||
}
|
||||
// Terrain before the ellipsoid: globe.pick intersects the rendered surface,
|
||||
// so over keyless elevated terrain it returns the ground the operator is
|
||||
// looking at. pickEllipsoid answers with sea level, which on a plateau sits
|
||||
// far below the view and would swing the camera on every tilt.
|
||||
if (
|
||||
!isPickedWorldPosition(target) &&
|
||||
typeof camera.getPickRay === 'function'
|
||||
) {
|
||||
try {
|
||||
target = scene.globe?.pick(camera.getPickRay(center), scene) || null;
|
||||
} catch {
|
||||
target = null;
|
||||
}
|
||||
}
|
||||
if (
|
||||
!isPickedWorldPosition(target) &&
|
||||
typeof camera.pickEllipsoid === 'function'
|
||||
) {
|
||||
try {
|
||||
target = camera.pickEllipsoid(center, Cesium.Ellipsoid.WGS84);
|
||||
} catch {
|
||||
target = null;
|
||||
}
|
||||
}
|
||||
return isPickedWorldPosition(target) ? target : null;
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Describe the camera as an orbit around the current viewport center. */
|
||||
export function readCameraTargetFrame(viewer) {
|
||||
const camera = viewer?.camera;
|
||||
const target = viewer?.trackedEntity
|
||||
/**
|
||||
* Describe the camera as an orbit around the current viewport center.
|
||||
* @param {import('../globe/contract.js').GlobeEngine | object} target The
|
||||
* globe engine, or a viewer registered for one.
|
||||
* @param {object} [tracker] The viewer whose tracked entity is the orbit
|
||||
* target (defaults to `target` when that is a viewer).
|
||||
*/
|
||||
export function readCameraTargetFrame(target, tracker) {
|
||||
const engine = engineOf(target);
|
||||
if (!engine) return null;
|
||||
const viewer = trackerOf(target, tracker);
|
||||
const center = viewer?.trackedEntity
|
||||
? trackedTarget(viewer)
|
||||
: pickViewTarget(viewer);
|
||||
if (!camera || !target || !isPickedWorldPosition(camera.positionWC))
|
||||
return null;
|
||||
: pickViewTarget(engine);
|
||||
const pose = engine.camera.pose();
|
||||
if (!center || !isPickedWorldPosition(pose.position)) return null;
|
||||
|
||||
const transform = geo.transforms.eastNorthUpToFixedFrame(target);
|
||||
const transform = geo.transforms.eastNorthUpToFixedFrame(center);
|
||||
const inverse = geo.matrix4.inverseTransformation(
|
||||
transform,
|
||||
geo.matrix4.create(),
|
||||
);
|
||||
const localOffset = geo.matrix4.multiplyByPoint(
|
||||
inverse,
|
||||
camera.positionWC,
|
||||
pose.position,
|
||||
geo.cartesian3.create(),
|
||||
);
|
||||
const range = geo.cartesian3.magnitude(localOffset);
|
||||
@@ -97,42 +109,43 @@ export function readCameraTargetFrame(viewer) {
|
||||
// make north-up rotate the wrong way from three quarters of the compass.
|
||||
const targetHeading = Math.atan2(-localOffset.x, -localOffset.y);
|
||||
const heading = normalizedHeading(
|
||||
pitch < geo.math.toRadians(-88.5) ? camera.heading : targetHeading,
|
||||
pitch < geo.math.toRadians(-88.5) ? pose.heading : targetHeading,
|
||||
);
|
||||
return { target, range, heading, pitch };
|
||||
return { target: center, range, heading, pitch };
|
||||
}
|
||||
|
||||
/** Apply an orbit frame, then return the camera to Cesium's fixed-world frame. */
|
||||
export function setCameraTargetFrame(viewer, frame) {
|
||||
const camera = viewer?.camera;
|
||||
if (!camera || !frame?.target || !Number.isFinite(frame.range)) return false;
|
||||
/**
|
||||
* Apply an orbit frame, then return the camera to the fixed-world frame.
|
||||
* @param {import('../globe/contract.js').GlobeEngine | object} target The
|
||||
* globe engine, or a viewer registered for one.
|
||||
* @param {object} frame From {@link readCameraTargetFrame}.
|
||||
* @param {object} [tracker] As in readCameraTargetFrame.
|
||||
*/
|
||||
export function setCameraTargetFrame(target, frame, tracker) {
|
||||
const engine = engineOf(target);
|
||||
if (!engine || !frame?.target || !Number.isFinite(frame.range)) return false;
|
||||
const viewer = trackerOf(target, tracker);
|
||||
const { camera } = engine;
|
||||
try {
|
||||
const trackedTransform = viewer.trackedEntity
|
||||
? geo.matrix4.clone(camera.transform)
|
||||
: null;
|
||||
camera.lookAt(
|
||||
frame.target,
|
||||
new Cesium.HeadingPitchRange(
|
||||
normalizedHeading(frame.heading),
|
||||
frame.pitch,
|
||||
frame.range,
|
||||
),
|
||||
);
|
||||
const trackedTransform = viewer?.trackedEntity ? camera.transform() : null;
|
||||
camera.lookAt(frame.target, {
|
||||
heading: normalizedHeading(frame.heading),
|
||||
pitch: frame.pitch,
|
||||
range: frame.range,
|
||||
});
|
||||
if (trackedTransform) {
|
||||
// Preserve EntityView's reference frame (including satellite frames).
|
||||
// Its next update carries this new local offset along with the target.
|
||||
camera.lookAtTransform(trackedTransform);
|
||||
viewer.scene?.requestRender?.();
|
||||
engine.render.request('camera-orientation');
|
||||
return true;
|
||||
}
|
||||
const destination = geo.cartesian3.clone(camera.positionWC);
|
||||
const direction = geo.cartesian3.clone(camera.directionWC);
|
||||
const up = geo.cartesian3.clone(camera.upWC);
|
||||
camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
|
||||
const { position: destination, direction, up } = camera.pose();
|
||||
camera.release();
|
||||
if (destination && direction && up) {
|
||||
camera.setView({ destination, orientation: { direction, up } });
|
||||
}
|
||||
viewer.scene?.requestRender?.();
|
||||
engine.render.request('camera-orientation');
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
@@ -158,32 +171,40 @@ export function frameIsTilted(frame) {
|
||||
}
|
||||
|
||||
/** Toggle between a straight-down map and a useful oblique map angle. */
|
||||
export function toggleCameraTilt(viewer) {
|
||||
const frame = readCameraTargetFrame(viewer);
|
||||
export function toggleCameraTilt(target, tracker) {
|
||||
const frame = readCameraTargetFrame(target, tracker);
|
||||
if (!frame) return false;
|
||||
const tilted = frameIsTilted(frame);
|
||||
const pitch = tilted ? STRAIGHT_DOWN_PITCH : OBLIQUE_PITCH;
|
||||
return setCameraTargetFrame(viewer, { ...frame, pitch })
|
||||
return setCameraTargetFrame(target, { ...frame, pitch }, tracker)
|
||||
? { tilted: !tilted, pitch }
|
||||
: false;
|
||||
}
|
||||
|
||||
/** Rotate around the viewport center until north is at the top. */
|
||||
export function resetCameraNorth(viewer) {
|
||||
const frame = readCameraTargetFrame(viewer);
|
||||
export function resetCameraNorth(target, tracker) {
|
||||
const frame = readCameraTargetFrame(target, tracker);
|
||||
if (!frame) return false;
|
||||
return setCameraTargetFrame(viewer, { ...frame, heading: 0 });
|
||||
return setCameraTargetFrame(target, { ...frame, heading: 0 }, tracker);
|
||||
}
|
||||
|
||||
function headingDegrees(camera) {
|
||||
return geo.math.toDegrees(normalizedHeading(camera?.heading));
|
||||
function headingDegrees(engine) {
|
||||
return geo.math.toDegrees(normalizedHeading(engine?.camera.pose().heading));
|
||||
}
|
||||
|
||||
/** Ease an orbit without handing off the entity that owns the follow camera. */
|
||||
/**
|
||||
* Ease an orbit without handing off the entity that owns the follow camera.
|
||||
* @param {import('../globe/contract.js').GlobeEngine | object} target The
|
||||
* globe engine, or a viewer registered for one.
|
||||
* @param {{ now?: () => number, duration?: number, tracker?: object }} [options]
|
||||
* `tracker` as in readCameraTargetFrame.
|
||||
*/
|
||||
export function createCameraOrientationAnimator(
|
||||
viewer,
|
||||
{ now = () => performance.now(), duration = 650 } = {},
|
||||
target,
|
||||
{ now = () => performance.now(), duration = 650, tracker } = {},
|
||||
) {
|
||||
const engine = engineOf(target);
|
||||
const viewer = trackerOf(target, tracker);
|
||||
let remove = null;
|
||||
let pending = null;
|
||||
const cancel = () => {
|
||||
@@ -193,37 +214,42 @@ export function createCameraOrientationAnimator(
|
||||
};
|
||||
function animate(frame, destination) {
|
||||
cancel();
|
||||
if (!viewer.scene?.preUpdate?.addEventListener || duration <= 0)
|
||||
return setCameraTargetFrame(viewer, { ...frame, ...destination });
|
||||
const entity = viewer.trackedEntity;
|
||||
if (!engine) return false;
|
||||
if (duration <= 0)
|
||||
return setCameraTargetFrame(engine, { ...frame, ...destination }, viewer);
|
||||
const entity = viewer?.trackedEntity;
|
||||
pending = destination;
|
||||
const start = now();
|
||||
const headingDelta = geo.math.negativePiToPi(
|
||||
destination.heading - frame.heading,
|
||||
);
|
||||
remove = viewer.scene.preUpdate.addEventListener(() => {
|
||||
if (viewer.isDestroyed?.() || viewer.trackedEntity !== entity) {
|
||||
remove = engine.frames.preUpdate.addEventListener(() => {
|
||||
if (engine.isDestroyed() || viewer?.trackedEntity !== entity) {
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
const progress = geo.math.clamp((now() - start) / duration, 0, 1);
|
||||
const eased = Cesium.EasingFunction.CUBIC_IN_OUT(progress);
|
||||
const target = entity ? trackedTarget(viewer) : frame.target;
|
||||
const eased = cubicInOut(progress);
|
||||
const center = entity ? trackedTarget(viewer) : frame.target;
|
||||
if (
|
||||
!isPickedWorldPosition(target) ||
|
||||
!setCameraTargetFrame(viewer, {
|
||||
...frame,
|
||||
target,
|
||||
heading: frame.heading + headingDelta * eased,
|
||||
pitch: geo.math.lerp(frame.pitch, destination.pitch, eased),
|
||||
})
|
||||
!isPickedWorldPosition(center) ||
|
||||
!setCameraTargetFrame(
|
||||
engine,
|
||||
{
|
||||
...frame,
|
||||
target: center,
|
||||
heading: frame.heading + headingDelta * eased,
|
||||
pitch: geo.math.lerp(frame.pitch, destination.pitch, eased),
|
||||
},
|
||||
viewer,
|
||||
)
|
||||
) {
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
if (progress === 1) cancel();
|
||||
});
|
||||
viewer.scene.requestRender?.();
|
||||
engine.render.request('camera-orientation');
|
||||
return true;
|
||||
}
|
||||
return {
|
||||
@@ -235,12 +261,25 @@ export function createCameraOrientationAnimator(
|
||||
};
|
||||
}
|
||||
|
||||
/** Bind the two map-orientation actions and keep their accessible state current. */
|
||||
/**
|
||||
* Bind the two map-orientation actions and keep their accessible state current.
|
||||
* @param {{
|
||||
* engine?: import('../globe/contract.js').GlobeEngine,
|
||||
* viewer?: object,
|
||||
* elements: object,
|
||||
* runNavigation: Function,
|
||||
* showToast?: Function,
|
||||
* animationMs?: number,
|
||||
* }} options `engine` defaults to the one registered for `viewer`; the viewer
|
||||
* is otherwise only read for its tracked entity (phase 1c).
|
||||
*/
|
||||
export function bindCameraOrientationControls({
|
||||
viewer,
|
||||
viewer = null,
|
||||
engine = engineOf(viewer),
|
||||
elements,
|
||||
runNavigation,
|
||||
showToast,
|
||||
animationMs,
|
||||
}) {
|
||||
const tiltButton = elements?.tiltButton;
|
||||
const northButton = elements?.northButton;
|
||||
@@ -251,12 +290,16 @@ export function bindCameraOrientationControls({
|
||||
// of the screen: 4 ms median and up to 12 ms, measured on this machine. It is
|
||||
// therefore held between refreshes rather than recomputed per frame.
|
||||
let tilted = false;
|
||||
const animator = createCameraOrientationAnimator(viewer);
|
||||
const animator = createCameraOrientationAnimator(engine, {
|
||||
tracker: viewer,
|
||||
...(animationMs === undefined ? {} : { duration: animationMs }),
|
||||
});
|
||||
const readFrame = () => readCameraTargetFrame(engine, viewer);
|
||||
|
||||
/** Recompute the tilt state exactly, paying for one pick. */
|
||||
const refreshTilt = () => {
|
||||
if (destroyed) return;
|
||||
tilted = frameIsTilted(readCameraTargetFrame(viewer));
|
||||
tilted = frameIsTilted(readFrame());
|
||||
sync();
|
||||
};
|
||||
|
||||
@@ -272,7 +315,7 @@ export function bindCameraOrientationControls({
|
||||
if (destroyed) return;
|
||||
const next = {
|
||||
tilted,
|
||||
heading: Math.round(headingDegrees(viewer?.camera)) % 360,
|
||||
heading: Math.round(headingDegrees(engine)) % 360,
|
||||
};
|
||||
if (
|
||||
applied &&
|
||||
@@ -308,7 +351,7 @@ export function bindCameraOrientationControls({
|
||||
listen(tiltButton, () => {
|
||||
const pending = animator.destination;
|
||||
const result = runNavigation('camera', () => {
|
||||
const frame = readCameraTargetFrame(viewer);
|
||||
const frame = readFrame();
|
||||
if (!frame) return false;
|
||||
const nextTilted = !frameIsTilted(pending || frame);
|
||||
const pitch = nextTilted ? OBLIQUE_PITCH : STRAIGHT_DOWN_PITCH;
|
||||
@@ -330,7 +373,7 @@ export function bindCameraOrientationControls({
|
||||
listen(northButton, () => {
|
||||
const pending = animator.destination;
|
||||
const result = runNavigation('camera', () => {
|
||||
const frame = readCameraTargetFrame(viewer);
|
||||
const frame = readFrame();
|
||||
return (
|
||||
frame &&
|
||||
animator.animate(frame, {
|
||||
@@ -350,13 +393,13 @@ export function bindCameraOrientationControls({
|
||||
// The needle follows every frame the scene draws. `camera.changed` is NOT
|
||||
// enough: it only fires past `camera.percentageChanged`, so a rotation of a
|
||||
// few degrees leaves the needle pointing at the old bearing.
|
||||
subscribe(viewer?.scene?.preRender, sync);
|
||||
subscribe(engine?.frames.preRender, sync);
|
||||
// The exact tilt state is refreshed once per gesture, not once per frame —
|
||||
// moveEnd fires when the camera stops, which is where the pick is affordable.
|
||||
subscribe(viewer?.camera?.moveEnd, refreshTilt);
|
||||
subscribe(engine?.camera.moveEnd, refreshTilt);
|
||||
subscribe(viewer?.trackedEntityChanged, animator.cancel);
|
||||
for (const type of ['pointerdown', 'wheel']) {
|
||||
const canvas = viewer?.scene?.canvas;
|
||||
const canvas = engine?.host.canvas;
|
||||
canvas?.addEventListener?.(type, animator.cancel, { passive: true });
|
||||
removers.push(() => canvas?.removeEventListener?.(type, animator.cancel));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user