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
+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;
|
||||
}
|
||||
Reference in new issue
Block a user