Read the followed target, not trackedEntity, in the follow readers

The tracked card, screen annotations, north-up, share links and Cockpit
ask data/followed.js what the camera follows, so they work the same when
a layer follows through the camera port. followed.js gains
entityFollowTarget (a FollowTarget whose presentation reads the drawn
entity's record), resumeFollowing and followedPosition.
This commit is contained in:
Sameh Khamis committed 2026-11-06 15:31:36 -08:00
1 parent 7eec2f828c
commit 99f9a701d9
16 files changed
+215 -84

No files matched your search

+2 -2
View File
@@ -196,8 +196,8 @@ export class StyleManager extends ShellFacade {
this._showGlobalStatusNotice(message, options),
feedback: this._feedback,
updateFeedback: () => this._updateGlobalLoadingFeedback(),
applyFollowOffset: (entity, offset) =>
applyShareFollowOffset(viewer, entity, offset),
applyFollowOffset: (followed, offset) =>
applyShareFollowOffset(viewer, followed, offset),
});
this._visualSettings = new VisualSettings({
+15 -14
View File
@@ -1,16 +1,17 @@
import * as Cesium from 'cesium';
import { isPickedWorldPosition } from '../data/scenePick.js';
import { t } from '../i18n/index.js';
import { readEntityRecord } from '../data/entityRecords.js';
import {
followedOf,
followedPosition,
isFollowing,
onFollowChanged,
} from '../data/followed.js';
function trackedTarget(viewer) {
const entity = viewer.trackedEntity;
// Read the visual's sample first; sampling a flight CallbackProperty twice
// can advance dead reckoning ahead of the displayed aircraft.
return (
readEntityRecord(entity)?.displayPosition?.() ||
entity?.position?.getValue(viewer.clock?.currentTime)
);
// The visual's sample first: sampling a flight CallbackProperty twice can
// advance dead reckoning ahead of the displayed aircraft.
return followedPosition(viewer, followedOf(viewer));
}
export const OBLIQUE_PITCH = Cesium.Math.toRadians(-35);
@@ -75,7 +76,7 @@ export function pickViewTarget(viewer) {
/** Describe the camera as an orbit around the current viewport center. */
export function readCameraTargetFrame(viewer) {
const camera = viewer?.camera;
const target = viewer?.trackedEntity
const target = isFollowing(viewer)
? trackedTarget(viewer)
: pickViewTarget(viewer);
if (!camera || !target || !isPickedWorldPosition(camera.positionWC))
@@ -112,7 +113,7 @@ function setCameraTargetFrame(viewer, frame) {
const camera = viewer?.camera;
if (!camera || !frame?.target || !Number.isFinite(frame.range)) return false;
try {
const trackedTransform = viewer.trackedEntity
const trackedTransform = isFollowing(viewer)
? Cesium.Matrix4.clone(camera.transform)
: null;
camera.lookAt(
@@ -200,20 +201,20 @@ export function createCameraOrientationAnimator(
cancel();
if (!viewer.scene?.preUpdate?.addEventListener || duration <= 0)
return setCameraTargetFrame(viewer, { ...frame, ...destination });
const entity = viewer.trackedEntity;
const followed = followedOf(viewer);
pending = destination;
const start = now();
const headingDelta = Cesium.Math.negativePiToPi(
destination.heading - frame.heading,
);
remove = viewer.scene.preUpdate.addEventListener(() => {
if (viewer.isDestroyed?.() || viewer.trackedEntity !== entity) {
if (viewer.isDestroyed?.() || followedOf(viewer) !== followed) {
cancel();
return;
}
const progress = Cesium.Math.clamp((now() - start) / duration, 0, 1);
const eased = Cesium.EasingFunction.CUBIC_IN_OUT(progress);
const target = entity ? trackedTarget(viewer) : frame.target;
const target = followed ? trackedTarget(viewer) : frame.target;
if (
!isPickedWorldPosition(target) ||
!setCameraTargetFrame(viewer, {
@@ -338,7 +339,7 @@ export function bindCameraOrientationControls({
// 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(viewer?.trackedEntityChanged, animator.cancel);
if (viewer) removers.push(onFollowChanged(viewer, animator.cancel));
for (const type of ['pointerdown', 'wheel']) {
const canvas = viewer?.scene?.canvas;
canvas?.addEventListener?.(type, animator.cancel, { passive: true });
+4 -1
View File
@@ -1,4 +1,5 @@
/** Coordinate Cockpit lifecycle through supplied aircraft, terrain, rendering and briefing operations. */
import { onFollowChanged } from '../data/followed.js';
import {
readAircraftInfo,
dispatchCockpitModeChanged,
@@ -87,6 +88,8 @@ export class CockpitViewController {
/** Gives camera input back; held from enter to exit. */
this.releaseCameraInput = null;
this.trackedEntity = null;
/** What the camera followed before Cockpit took it, to follow again. */
this.trackedFollow = null;
this.trackedEntityWasShown = true;
this.heading = null;
this.lastFrameMs = 0;
@@ -201,7 +204,7 @@ export class CockpitViewController {
// create a self-sustaining refinement loop under a moving cockpit camera.
this._listenerRemovers.push(
viewer.scene.preUpdate.addEventListener(() => this.update()),
viewer.trackedEntityChanged.addEventListener(() => {
onFollowChanged(viewer, () => {
if (this.active) this._adoptTrackedEntity(performance.now());
else this.syncEntry();
}),
+3 -2
View File
@@ -1,3 +1,4 @@
import { followedOf } from '../data/followed.js';
import { nextTick } from 'vue';
import { CockpitViewController, CockpitDisplayPortal } from './cockpit.js';
import { cockpitEntryAllowed } from '../contextModePolicy.js';
@@ -124,12 +125,12 @@ export class CockpitCoordinator {
const gateOpen = Boolean(this.cockpitView?.isEntryAllowed?.());
// "Could Cockpit be ENTERED right now" — so it is false while already
// inside, unconditionally. Cockpit takes the entity off
// `viewer.trackedEntity` on entry and NEXT puts one back, which made this
// the camera's follow on entry and NEXT puts one back, which made this
// flip true/false between calls while `active` stayed true; readers
// (including the voice model) read that as a broken half-entered state.
const entryAllowed =
!active &&
Boolean(gateOpen && info && this.viewer?.trackedEntity?.position);
Boolean(gateOpen && info && followedOf(this.viewer)?.entity?.position);
return {
active,
entryAllowed,
+2 -1
View File
@@ -1,4 +1,5 @@
/** Route Cockpit keys while preserving the active disclosure and form-control owner. */
import { followedOf } from '../data/followed.js';
export function onKeyDown(event) {
if (this.destroyed) return false;
@@ -49,7 +50,7 @@ export function onKeyDown(event) {
if (key === 'c' && !event.metaKey && !event.ctrlKey && !event.altKey) {
if (!this.active) {
const cockpitAttempt = !!(
this.readAircraftInfo() && this.viewer.trackedEntity?.position
this.readAircraftInfo() && followedOf(this.viewer)?.entity?.position
);
if (!cockpitAttempt) return;
}
+21 -11
View File
@@ -6,17 +6,21 @@ import {
normalizeHeading,
resolveTrackedAircraftInfo,
} from '../cockpitMath.js';
import { readEntityRecord } from '../data/entityRecords.js';
import {
followedOf,
resumeFollowing,
stopFollowing,
} from '../data/followed.js';
import { inputFor } from '../input/dispatcher.js';
export function readAircraftInfo() {
// In cockpit mode the controller takes the entity off `viewer.trackedEntity`
// (see update()), so the cockpit's own handle is the tracked identity there.
const trackedEntity = this.viewer?.trackedEntity || this.trackedEntity;
// In cockpit mode the controller stops the camera following (see enter()),
// so the cockpit's own handle is the tracked identity there.
const followed = followedOf(this.viewer) || this.trackedFollow;
return resolveTrackedAircraftInfo({
civilian: this.services.flightsLayer.getTrackedInfo?.() || null,
military: this.services.militaryFlightsLayer.getTrackedInfo?.() || null,
trackedId: readEntityRecord(trackedEntity)?.trackedId || '',
trackedId: followed?.key || '',
});
}
@@ -80,7 +84,7 @@ export function syncEntry() {
if (this.destroyed) return false;
if (this.active) return;
const info = this.readAircraftInfo();
const trackedContact = !!(info && this.viewer.trackedEntity?.position);
const trackedContact = !!(info && followedOf(this.viewer)?.entity?.position);
this.syncTr3bToggle(trackedContact ? info : null);
const available = !!(this.isEntryAllowed() && trackedContact);
// Change-only publishes: this runs on a preUpdate cadence, and a fresh
@@ -111,7 +115,8 @@ export function navigateContext(direction, options = {}) {
}
export function _adoptTrackedEntity(nowMs, suppliedInfo = null) {
const nextEntity = this.viewer.trackedEntity;
const nextFollowed = followedOf(this.viewer);
const nextEntity = nextFollowed?.entity;
if (
!this.active ||
!nextEntity?.position ||
@@ -124,9 +129,10 @@ export function _adoptTrackedEntity(nowMs, suppliedInfo = null) {
this.trackedEntity.show = this.trackedEntityWasShown;
}
this.trackedEntity = nextEntity;
this.trackedFollow = nextFollowed;
this.trackedEntityWasShown = nextEntity.show;
nextEntity.show = false;
this.viewer.trackedEntity = undefined;
stopFollowing(this.viewer);
this.cockpitAnchorValid = false;
this.heading = normalizeHeading(info.track ?? 0);
this.lastFrameMs = nowMs;
@@ -143,7 +149,8 @@ export function enter() {
if (this.active) return false;
if (!this.isEntryAllowed()) return false;
const info = this.readAircraftInfo();
const entity = this.viewer.trackedEntity;
const followed = followedOf(this.viewer);
const entity = followed?.entity;
if (!info || !entity?.position) return false;
this.entryFocusOrigin =
document.activeElement instanceof HTMLElement
@@ -153,6 +160,7 @@ export function enter() {
this.onCameraTakeover?.();
cameraOf(this.viewer).cancelFlight();
this.trackedEntity = entity;
this.trackedFollow = followed;
this.trackedEntityWasShown = entity.show;
entity.show = false;
this.heading = normalizeHeading(info.track ?? 0);
@@ -172,7 +180,7 @@ export function enter() {
// runs on rendered frames, so idle mode would freeze the cockpit solid.
// (perf wave 2)
this.services.holdContinuousRender('cockpit');
this.viewer.trackedEntity = undefined;
stopFollowing(this.viewer);
this.releaseCameraInput?.();
this.releaseCameraInput = inputFor(this.viewer).suspendCamera('cockpit');
document.body.classList.add('cockpit-mode');
@@ -217,9 +225,11 @@ export function enter() {
export function exit({ restoreTracking = true } = {}) {
if (!this.active) return false;
const entity = this.trackedEntity;
const followed = this.trackedFollow;
this.active = false;
this.services.releaseContinuousRender('cockpit');
this.trackedEntity = null;
this.trackedFollow = null;
this.heading = null;
this.cockpitAnchorValid = false;
this.surfaceWaitStartedMs = 0;
@@ -248,7 +258,7 @@ export function exit({ restoreTracking = true } = {}) {
this.trackedEntityWasShown = true;
this.dispatchCockpitModeChanged(false);
if (restoreTracking && entity && this.viewer.entities.contains(entity)) {
this.viewer.trackedEntity = entity;
resumeFollowing(this.viewer, followed);
this.restoreTrackingFrame(entity);
}
this.syncEntry();
+27 -26
View File
@@ -1,11 +1,11 @@
import * as Cesium from 'cesium';
import { trackedDisplayPositionForCamera } from '../data/trackedCamera.js';
import { findCamera } from '../render/rendererOf.js';
import { followedOf, followedPosition } from '../data/followed.js';
import {
SHARE_VIEW_ENTITY_KINDS,
followOffsetFromEnu,
followOffsetToEnu,
} from '../share/follow.js';
import { readEntityRecord } from '../data/entityRecords.js';
/**
* The bridge between the camera following an entity and a share link's thing
@@ -18,6 +18,12 @@ const KIND_BY_LAYER = new Map(
);
const MAX_FRAMES = 120;
/** Where `followed` is shown now, as a Cesium position, or null. */
function shownAt(viewer, followed) {
const p = followedPosition(viewer, followed);
return p ? new Cesium.Cartesian3(p.x, p.y, p.z) : null;
}
/** `position`'s local east/north/up offset from `origin`, in metres. */
function enuOffset(origin, position) {
const frame = Cesium.Transforms.eastNorthUpToFixedFrame(origin);
@@ -39,19 +45,13 @@ function enuOffset(origin, position) {
export function readShareFollowSubject({ viewer, nowMs = Date.now() }) {
const cameraPosition = viewer?.camera?.positionWC;
if (!cameraPosition) return null;
const tracked = viewer.trackedEntity;
const trackedId = String(readEntityRecord(tracked)?.trackedId || '');
const followed = followedOf(viewer);
const trackedId = String(followed?.key || '');
const separator = trackedId.indexOf(':');
if (separator <= 0) return null;
const kind = KIND_BY_LAYER.get(trackedId.slice(0, separator));
const id = trackedId.slice(separator + 1);
const target = kind
? trackedDisplayPositionForCamera(
tracked,
viewer.clock.currentTime,
new Cesium.Cartesian3(),
)
: null;
const target = kind ? shownAt(viewer, followed) : null;
if (!kind || !id || !target) return null;
const offset = followOffsetFromEnu(enuOffset(target, cameraPosition));
const fix = Cesium.Cartographic.fromCartesian(target);
@@ -73,14 +73,14 @@ export function readShareFollowSubject({ viewer, nowMs = Date.now() }) {
const MAX_TURN_PER_FRAME = Math.PI / 3;
/**
* Put the camera at a shared follow offset from `entity` once following it
* has framed the camera. Cesium keeps the offset afterwards, as it keeps one
* Put the camera at a shared follow offset from `followed` (what
* `followedOf` gave) once following it has framed the camera. Cesium keeps the offset afterwards, as it keeps one
* the user orbits to. The camera swings round the entity at most 60° a frame:
* the follow's own guard treats a jump to the far side as the camera crossing
* through the entity and puts it back. Returns a disposer.
*/
export function applyShareFollowOffset(viewer, entity, offset) {
if (!viewer?.scene?.preUpdate || !entity || !(offset?.range > 0))
export function applyShareFollowOffset(viewer, followed, offset) {
if (!viewer?.scene?.preUpdate || !followed || !(offset?.range > 0))
return () => {};
const local = followOffsetToEnu(offset);
let frames = 0;
@@ -90,23 +90,20 @@ export function applyShareFollowOffset(viewer, entity, offset) {
remove = null;
};
remove = viewer.scene.preUpdate.addEventListener(() => {
if (viewer.isDestroyed?.() || viewer.trackedEntity !== entity) {
if (viewer.isDestroyed?.() || followedOf(viewer) !== followed) {
stop();
return;
}
frames += 1;
const camera = viewer.camera;
// Let the follow frame the camera first (its first frame), then move it.
const port = findCamera(viewer);
const framed =
frames > 1 &&
!Cesium.Matrix4.equals(camera.transform, Cesium.Matrix4.IDENTITY);
const target = framed
? trackedDisplayPositionForCamera(
entity,
viewer.clock.currentTime,
new Cesium.Cartesian3(),
)
: null;
(port?.followed === followed
? port.followFramed
: !Cesium.Matrix4.equals(camera.transform, Cesium.Matrix4.IDENTITY));
const target = framed ? shownAt(viewer, followed) : null;
if (!target) {
if (frames >= MAX_FRAMES) stop();
return;
@@ -136,8 +133,12 @@ export function applyShareFollowOffset(viewer, entity, offset) {
: 0;
const done = !(angle > MAX_TURN_PER_FRAME);
const next = done ? goal : turnToward(from, goal, MAX_TURN_PER_FRAME);
camera.lookAtTransform(camera.transform, next);
viewer.scene.requestRender?.();
// Through the port when it follows: its close-range guard keeps the eye.
if (port?.followed === followed) port.followOffset.set(next);
else {
camera.lookAtTransform(camera.transform, next);
viewer.scene.requestRender?.();
}
if (done) stop();
});
return stop;
+2 -1
View File
@@ -20,6 +20,7 @@ import {
} from '../share/viewCodec.js';
import { entityRecord, withEntityRecord } from '../data/entityRecords.js';
import { bindFakeCesiumCamera } from '../testSupport/fakeCameraPort.mjs';
import { followedOf } from '../data/followed.js';
const NOW_MS = Date.now();
const FIX = { lat: 37.6213, lon: -122.379, alt: 3200 };
@@ -494,7 +495,7 @@ test('a shared offset on the far side swings round, never jumping through the en
};
// Looking south at the nose: the opposite side of the app's framing.
const offset = { range: 5800, heading: 180, pitch: -35 };
applyShareFollowOffset(viewer, entity, offset);
applyShareFollowOffset(viewer, followedOf(viewer), offset);
let previous = viewer.camera.position;
for (let frame = 0; frame < 10 && listeners.length; frame += 1) {
listeners[0]();
+4 -5
View File
@@ -4,7 +4,7 @@ import { canPresentDeferredStatusNotice } from '../loadingFeedback.js';
import { t, msg, formatDate } from '../i18n/index.js';
import { SHARE_VIEW_ENTITY_KINDS } from '../share/follow.js';
import { UiLifetime } from './uiLifetime.js';
import { readEntityRecord } from '../data/entityRecords.js';
import { followedOf } from '../data/followed.js';
/** How long a shared vessel may take to appear in the vessel feed. */
export const SHARED_VESSEL_WAIT_MS = 120_000;
@@ -420,17 +420,16 @@ export class ShareRestoration {
/** Frame a shared entity the camera now follows from the shared offset. */
_applySharedFollowOffset(result) {
const view = this._sharedThingFor(result);
const entity = this.viewer?.trackedEntity;
const followed = followedOf(this.viewer);
// Only a link made while following carries a real offset (with the fix);
// a long link's follow keeps the app's own framing.
if (
!view?.lastKnown ||
String(readEntityRecord(entity)?.trackedId || '') !==
`${result.layerId}:${result.targetId}`
String(followed?.key || '') !== `${result.layerId}:${result.targetId}`
)
return;
this._stopFollowOffset?.();
this._stopFollowOffset = this.applyFollowOffset?.(entity, view.offset);
this._stopFollowOffset = this.applyFollowOffset?.(followed, view.offset);
}
cancelSelection() {