mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Follow aircraft and satellites through the camera port
Flights and military follow their tracked aircraft with an entityFollowTarget: framed in ENU from viewFrom, never closer than 150 m, presentation read from the drawn entity's record. Satellites follow in Cesium's autodetect frame. The layers' own framing (applyTrackedCameraFrame) goes; the port's follow has the same close-range rules, and trackedCamera.js no longer imports Cesium. The port's helper entity draws nothing: its point's outline is transparent too, and click and voice picks of it count as picks of the target's drawn entity. The fake camera binder follows when the fake has Cesium's entity tracking.
This commit is contained in:
1 parent
99f9a701d9
commit
e3a850bc9c
22 files changed
+291
-332
No files matched your search
@@ -3,6 +3,8 @@
|
||||
// Pure function — no viewer/DOM needed; imported directly.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { followedOf } from './followed.js';
|
||||
import { bindFakeCesiumCamera } from '../testSupport/fakeCameraPort.mjs';
|
||||
import * as Cesium from 'cesium';
|
||||
import flightsLayer, {
|
||||
_addFlightTrackingCandidateForTest,
|
||||
@@ -439,6 +441,7 @@ test('real civil track path creates no native label and publishes every cached h
|
||||
},
|
||||
});
|
||||
viewer.scene.camera = viewer.camera;
|
||||
bindFakeCesiumCamera(viewer);
|
||||
const billboard = {
|
||||
position,
|
||||
rotation: 0.3,
|
||||
@@ -512,7 +515,7 @@ test('real civil track path creates no native label and publishes every cached h
|
||||
flightsLayer.trackById(icao24, { origin: 'programmatic' }),
|
||||
true,
|
||||
);
|
||||
const entity = viewer.trackedEntity;
|
||||
const entity = followedOf(viewer)?.entity;
|
||||
assert.ok(
|
||||
entity instanceof Cesium.Entity,
|
||||
'trackById must create the real Cesium entity',
|
||||
@@ -554,7 +557,7 @@ test('real civil track path creates no native label and publishes every cached h
|
||||
assert.equal(flightsLayer.refocusTrackedById(icao24), true);
|
||||
viewer.scene.preUpdate.raiseEvent();
|
||||
assert.equal(
|
||||
viewer.trackedEntity,
|
||||
followedOf(viewer)?.entity,
|
||||
entity,
|
||||
'refocus must retain the exact tracked entity',
|
||||
);
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
// Pure function — no viewer/DOM needed; imported directly.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { followedOf } from './followed.js';
|
||||
import { bindFakeCesiumCamera } from '../testSupport/fakeCameraPort.mjs';
|
||||
import * as Cesium from 'cesium';
|
||||
import militaryFlightsLayer, {
|
||||
_addMilitaryTrackingCandidateForTest,
|
||||
@@ -367,6 +369,7 @@ test('real military track path creates no native label and publishes every cache
|
||||
isDestroyed: () => false,
|
||||
};
|
||||
viewer.scene.camera = viewer.camera;
|
||||
bindFakeCesiumCamera(viewer);
|
||||
Object.defineProperty(viewer, 'trackedEntity', {
|
||||
get: () => trackedEntity,
|
||||
set(value) {
|
||||
@@ -434,7 +437,7 @@ test('real military track path creates no native label and publishes every cache
|
||||
militaryFlightsLayer.trackById(icao24, { origin: 'programmatic' }),
|
||||
true,
|
||||
);
|
||||
const entity = viewer.trackedEntity;
|
||||
const entity = followedOf(viewer)?.entity;
|
||||
assert.ok(
|
||||
entity instanceof Cesium.Entity,
|
||||
'trackById must create the real Cesium entity',
|
||||
@@ -482,7 +485,7 @@ test('real military track path creates no native label and publishes every cache
|
||||
assert.equal(militaryFlightsLayer.refocusTrackedById(icao24), true);
|
||||
viewer.scene.preUpdate.raiseEvent();
|
||||
assert.equal(
|
||||
viewer.trackedEntity,
|
||||
followedOf(viewer)?.entity,
|
||||
entity,
|
||||
'refocus must retain the exact tracked entity',
|
||||
);
|
||||
|
||||
@@ -16,6 +16,8 @@ import {
|
||||
_trackIssForTest,
|
||||
createIssOverlayEntry,
|
||||
} from './satellites.js';
|
||||
import { followedOf } from './followed.js';
|
||||
import { bindRenderer } from '../render/rendererOf.js';
|
||||
import satellitesLayer from './satellites.js';
|
||||
import { createTrackedOverlayEntry } from './trackedReadout.js';
|
||||
import { readEntityRecord, withEntityRecord } from './entityRecords.js';
|
||||
@@ -25,6 +27,22 @@ const L1 =
|
||||
const L2 =
|
||||
'2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.72125391563537';
|
||||
|
||||
|
||||
/** A camera port that only follows, for viewers without a Cesium camera. */
|
||||
function followingPort() {
|
||||
let followed = null;
|
||||
return {
|
||||
on: () => () => {},
|
||||
get followed() {
|
||||
return followed;
|
||||
},
|
||||
follow(target) {
|
||||
followed = target;
|
||||
},
|
||||
cancelFlight() {},
|
||||
};
|
||||
}
|
||||
|
||||
test('partial and dense catalogs do not make an early false missing decision', async () => {
|
||||
_setSatelliteTrackingRefreshOutcomeForTest({
|
||||
status: 'partial',
|
||||
@@ -90,7 +108,7 @@ test('satellite first update aborts every parallel catalog request', async () =>
|
||||
}
|
||||
});
|
||||
|
||||
test('selected satellite params survive delayed arrival and yield to newer explicit intent', () => {
|
||||
test('selected satellite params survive delayed arrival and yield to newer explicit intent', (t) => {
|
||||
const point = {
|
||||
position: Cesium.Cartesian3.fromDegrees(-97.7, 30.2, 420_000),
|
||||
show: true,
|
||||
@@ -108,6 +126,7 @@ test('selected satellite params survive delayed arrival and yield to newer expli
|
||||
primitives: { remove() {} },
|
||||
},
|
||||
};
|
||||
t.after(bindRenderer(viewer, { camera: followingPort() }));
|
||||
_setSatelliteLabelLifecycleStateForTest({
|
||||
viewer,
|
||||
satrec: twoline2satrec(L1, L2),
|
||||
@@ -118,7 +137,7 @@ test('selected satellite params survive delayed arrival and yield to newer expli
|
||||
satellitesLayer.setParams({ selectedSatTrackingId: 25544 }),
|
||||
);
|
||||
assert.equal(satellitesLayer.getParams().selectedSatTrackingId, 25544);
|
||||
assert.equal(readEntityRecord(viewer.trackedEntity)?.trackedId, 'satellites:25544');
|
||||
assert.equal(followedOf(viewer)?.key, 'satellites:25544');
|
||||
|
||||
satellitesLayer.stopTracking();
|
||||
_removeSatelliteTrackingCandidateForTest(25544);
|
||||
@@ -288,7 +307,7 @@ test('satellite pre-render refreshes the tracked altitude on each propagated fra
|
||||
);
|
||||
});
|
||||
|
||||
test('ISS ambient and tracked lifecycle uses cached host entries with no native labels or duplicate text', () => {
|
||||
test('ISS ambient and tracked lifecycle uses cached host entries with no native labels or duplicate text', (t) => {
|
||||
const calls = [];
|
||||
const overlayHost = {
|
||||
setEntries: (...args) => calls.push(['entries', ...args]),
|
||||
@@ -312,6 +331,7 @@ test('ISS ambient and tracked lifecycle uses cached host entries with no native
|
||||
primitives: { remove() {} },
|
||||
},
|
||||
};
|
||||
t.after(bindRenderer(viewer, { camera: followingPort() }));
|
||||
let cleaned = false;
|
||||
_setSatelliteLabelLifecycleStateForTest({
|
||||
viewer,
|
||||
|
||||
+5
-143
@@ -1,9 +1,8 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { readEntityRecord } from './entityRecords.js';
|
||||
import { clampFollowEye, holdFollowZoom } from '../render/cesium/follow.js';
|
||||
|
||||
const MAX_FRAME_ATTEMPTS = 120;
|
||||
const MIN_TRACKED_RANGE_M = 150;
|
||||
/**
|
||||
* Closest a follow camera comes to a tracked aircraft, metres: the camera
|
||||
* port's `minRange` for the aircraft layers' FollowTargets.
|
||||
*/
|
||||
export const FOLLOW_MIN_RANGE_M = 150;
|
||||
|
||||
/**
|
||||
* Preserve real-world model scale except when a very close tracked camera
|
||||
@@ -47,140 +46,3 @@ export function trackedModelScaleForPixelCap({
|
||||
if (projectedDiameterPx <= maximumPixelSize) return baseScale;
|
||||
return baseScale * (maximumPixelSize / projectedDiameterPx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the exact position already consumed by the tracked visual whenever
|
||||
* the layer exposes one, without advancing its CallbackProperty again.
|
||||
*
|
||||
* @param {Cesium.Entity} entity Tracked entity.
|
||||
* @param {Cesium.JulianDate} time Current viewer time.
|
||||
* @param {Cesium.Cartesian3} result Destination.
|
||||
* @returns {Cesium.Cartesian3|undefined} Camera-frame position.
|
||||
*/
|
||||
export function trackedDisplayPositionForCamera(entity, time, result) {
|
||||
const displayedPosition = readEntityRecord(entity)?.displayPosition?.();
|
||||
return displayedPosition
|
||||
? Cesium.Cartesian3.clone(displayedPosition, result)
|
||||
: entity?.position?.getValue(time, result);
|
||||
}
|
||||
|
||||
function acquireStableTrackedZoom(controller, entity) {
|
||||
return holdFollowZoom(controller, entity, MIN_TRACKED_RANGE_M);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep a tracked-frame camera on the same side of its target and outside the
|
||||
* minimum readable range.
|
||||
*
|
||||
* @param {{position: Cesium.Cartesian3, direction: Cesium.Cartesian3}} camera Camera-like object.
|
||||
* @param {Cesium.Cartesian3} previousPosition Previous tracked-frame camera position.
|
||||
* @param {number} [minimumRangeM=MIN_TRACKED_RANGE_M] Minimum target range.
|
||||
* @returns {boolean} Whether the camera position was corrected.
|
||||
*/
|
||||
export function clampTrackedCameraPosition(
|
||||
camera,
|
||||
previousPosition,
|
||||
minimumRangeM = MIN_TRACKED_RANGE_M,
|
||||
) {
|
||||
return clampFollowEye(camera, previousPosition, minimumRangeM);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply an entity's requested follow offset once, then let Cesium's ENU
|
||||
* EntityView exclusively own continuous following.
|
||||
*
|
||||
* Cesium can retain the previous world-space camera position when tracking
|
||||
* switches across a large distance. The view then only rotates toward the new
|
||||
* aircraft, and the next zoom can drive the camera through the ellipsoid.
|
||||
* Applying the exact frame for the handoff makes selection deterministic. We
|
||||
* deliberately do not rebuild it every frame: Cesium's EntityView already
|
||||
* follows the same ENU entity, and two writers create sub-pixel camera jitter
|
||||
* that becomes visible at minimum range.
|
||||
*
|
||||
* @param {Cesium.Viewer} viewer Active viewer.
|
||||
* @param {Cesium.Entity} entity Newly tracked entity.
|
||||
* @param {Cesium.Cartesian3} viewFrom Desired camera offset in the tracked frame.
|
||||
* @returns {(() => void)|undefined} Disposer for this one tracked-frame owner.
|
||||
*/
|
||||
export function applyTrackedCameraFrame(viewer, entity, viewFrom) {
|
||||
if (!viewer || !entity || !viewFrom) return;
|
||||
let attempts = 0;
|
||||
const trackedTransform = new Cesium.Matrix4();
|
||||
const trackedPosition = new Cesium.Cartesian3();
|
||||
const resolvedViewFrom = new Cesium.Cartesian3();
|
||||
const cameraOffset = new Cesium.Cartesian3();
|
||||
const previousCameraPosition = new Cesium.Cartesian3();
|
||||
let framed = false;
|
||||
const restoreZoomInertia = acquireStableTrackedZoom(
|
||||
viewer.scene.screenSpaceCameraController,
|
||||
entity,
|
||||
);
|
||||
let stopped = false;
|
||||
let remove = null;
|
||||
const stop = () => {
|
||||
if (stopped) return;
|
||||
stopped = true;
|
||||
remove?.();
|
||||
restoreZoomInertia();
|
||||
};
|
||||
|
||||
// preUpdate is deliberate. preRender fires after Cesium has prepared
|
||||
// billboard/model draw state, so changing the tracked transform there makes
|
||||
// the camera and target visual disagree for one frame (visible as a
|
||||
// front/back oscillation). The controller then enforces the minimum range
|
||||
// during the same update pass.
|
||||
remove = viewer.scene.preUpdate.addEventListener(() => {
|
||||
if (viewer.isDestroyed() || viewer.trackedEntity !== entity) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
attempts += framed ? 0 : 1;
|
||||
// Prefer the display cache exposed by the flight layer. Calling the
|
||||
// CallbackProperty from preUpdate can advance dead reckoning again while
|
||||
// Cesium's billboard/label still holds the prior visualizer sample. At a
|
||||
// 150 m follow range that sub-frame difference becomes visible as the
|
||||
// label swinging around the icon during an orbit, especially immediately
|
||||
// after a 2D→3D→2D handoff.
|
||||
if (!framed) {
|
||||
const position = trackedDisplayPositionForCamera(
|
||||
entity,
|
||||
viewer.clock.currentTime,
|
||||
trackedPosition,
|
||||
);
|
||||
if (!position) {
|
||||
if (attempts >= MAX_FRAME_ATTEMPTS) stop();
|
||||
return;
|
||||
}
|
||||
const transform = Cesium.Transforms.eastNorthUpToFixedFrame(
|
||||
position,
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
trackedTransform,
|
||||
);
|
||||
const offset =
|
||||
typeof viewFrom.getValue === 'function'
|
||||
? viewFrom.getValue(viewer.clock.currentTime, resolvedViewFrom)
|
||||
: Cesium.Cartesian3.clone(viewFrom, resolvedViewFrom);
|
||||
if (
|
||||
!offset ||
|
||||
Cesium.Cartesian3.magnitudeSquared(offset) < Cesium.Math.EPSILON12
|
||||
) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
viewer.camera.lookAtTransform(transform, offset);
|
||||
Cesium.Cartesian3.clone(viewer.camera.position, previousCameraPosition);
|
||||
framed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Normal frames are intentionally read-only: EntityView is the sole
|
||||
// continuous camera writer. Only a programmatic zoom that bypasses the
|
||||
// controller's minimumZoomDistance needs correction.
|
||||
if (clampTrackedCameraPosition(viewer.camera, previousCameraPosition)) {
|
||||
Cesium.Cartesian3.clone(viewer.camera.position, cameraOffset);
|
||||
viewer.camera.lookAtTransform(viewer.camera.transform, cameraOffset);
|
||||
}
|
||||
Cesium.Cartesian3.clone(viewer.camera.position, previousCameraPosition);
|
||||
});
|
||||
return stop;
|
||||
}
|
||||
@@ -1,12 +1,8 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
clampTrackedCameraPosition,
|
||||
trackedDisplayPositionForCamera,
|
||||
trackedModelScaleForPixelCap,
|
||||
} from './trackedCamera.js';
|
||||
import { withEntityRecord } from './entityRecords.js';
|
||||
import { trackedModelScaleForPixelCap } from './trackedCamera.js';
|
||||
import { clampFollowEye as clampTrackedCameraPosition } from '../render/cesium/follow.js';
|
||||
|
||||
test('tracked camera origin crossing returns to the prior side at the minimum range', () => {
|
||||
const camera = {
|
||||
@@ -71,22 +67,3 @@ test('tracked model shrinks smoothly when close enough to exceed the pixel cap',
|
||||
assert.ok(scale < 24);
|
||||
assert.ok(Math.abs(projectedDiameterPx - 104) < 1e-9);
|
||||
});
|
||||
|
||||
test('tracked camera prefers the already-rendered display cache', () => {
|
||||
let callbackReads = 0;
|
||||
const cached = new Cesium.Cartesian3(1, 2, 3);
|
||||
const entity = withEntityRecord({ position: {
|
||||
getValue: () => {
|
||||
callbackReads += 1;
|
||||
return new Cesium.Cartesian3(4, 5, 6);
|
||||
},
|
||||
} }, { displayPosition: () => cached });
|
||||
const result = trackedDisplayPositionForCamera(
|
||||
entity,
|
||||
Cesium.JulianDate.now(),
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
assert.deepEqual(result, cached);
|
||||
assert.notEqual(result, cached);
|
||||
assert.equal(callbackReads, 0);
|
||||
});
|
||||
Reference in new issue
Block a user