Follow, drape and read the camera through the ports, not the viewer

- What the camera follows is the camera port's target only: no layer
  tracks an entity of its own any more, so followed.js stops reading and
  writing viewer.trackedEntity and the viewer's clock.
- Draped imagery's host is the globe port's imagery while the globe is
  shown (isGlobeVisible, or no shown base tileset where a renderer cannot
  say), else the shown base tileset; weather uses the same resolver.
- The World Packet reads the eye from the camera port's pose, and vessels
  ask again for the view on the camera port's moveEnd.
- The stub's canvas takes event listeners, and in a page is a real canvas
  filling the container it is given; createStubRendererPlugin makes it a
  renderer plugin.

The viewer access record is now empty.
This commit is contained in:
Sameh Khamis committed 2026-11-10 17:18:10 -08:00
1 parent ebbe006924
commit a72d4c3b1f
11 files changed
+230 -213

No files matched your search

+15 -34
View File
@@ -4,17 +4,13 @@
*
* A layer follows through the camera port with a FollowTarget that also
* carries what the app shows about it (key, display position, sprite, label).
* Until every layer does, a layer may still track an entity of its own
* through Cesium's `trackedEntity`; that entity is seen here through the
* same fields, read from its entity record. Readers ask this module, never
* `trackedEntity`, so they work either way. A layer that still draws its
* target as an entity follows through the port with `entityFollowTarget`,
* whose presentation fields read that same record.
* A layer that keeps its target as a holder of its own (an entity record)
* follows through the port with `entityFollowTarget`, whose presentation
* fields read that record. Readers ask this module, never the renderer.
*
* Its readers (the tracked card, Cockpit, the share link's thing view,
* north-up) still read the drawn entity and its `displayPosition`: a port
* target without them is followed by the camera but not shown by them,
* until those readers move with the drawing to the renderer.
* north-up) read the target's `displayPosition`: a port target without one
* is followed by the camera but not shown by them.
*/
import { findCamera } from '../render/rendererOf.js';
import { readEntityRecord } from './entityRecords.js';
@@ -29,8 +25,8 @@ import { readEntityRecord } from './entityRecords.js';
* @property {object} [sprite] Size of the drawn billboard.
* @property {object} [labelModel]
* @property {string} [selectionOrigin]
* @property {object} [entity] The tracked entity, for a layer that still
* tracks one itself.
* @property {object} [entity] The holder whose entity record the
* presentation fields read (`entityFollowTarget`).
*/
/** @type {WeakMap<object, Followed>} */
@@ -78,10 +74,7 @@ function entityView(entity) {
*/
export function followedOf(viewer) {
if (!viewer) return null;
const target = findCamera(viewer)?.followed;
if (target) return target;
const entity = viewer.trackedEntity;
return entity ? entityView(entity) : null;
return findCamera(viewer)?.followed ?? null;
}
/** Whether `viewer`'s camera follows anything. */
@@ -100,8 +93,7 @@ export function onFollowChanged(viewer, cb) {
let port = null;
let offPort = null;
// The port is looked up again on every change: a renderer bound after
// this subscribed replaces the one it listened to. A Cesium port's follow
// also sets `trackedEntity`, so the change still arrives through it.
// this subscribed replaces the one it listened to.
const listenToPort = () => {
const current = findCamera(viewer);
if (current === port) return;
@@ -117,20 +109,17 @@ export function onFollowChanged(viewer, cb) {
cb(now);
}
listenToPort();
const offTracking = viewer.trackedEntityChanged?.addEventListener(check);
return () => {
offPort?.();
offPort = null;
offTracking?.();
};
}
/** Stop the camera following anything, a layer's own tracking included. */
/** Stop the camera following anything. */
export function stopFollowing(viewer) {
if (!viewer) return;
const camera = findCamera(viewer);
if (camera?.followed) camera.follow(null);
else if (viewer.trackedEntity) viewer.trackedEntity = undefined;
}
/**
@@ -157,30 +146,22 @@ export function entityFollowTarget(entity, camera) {
}
/**
* Follow `followed` again after `stopFollowing`: a port target through the
* camera port, a layer's own tracked entity through `trackedEntity`.
* Follow `followed` again after `stopFollowing`, through the camera port.
* @param {object} viewer
* @param {Followed|null} followed What `followedOf` gave.
*/
export function resumeFollowing(viewer, followed) {
if (!viewer || !followed) return;
if (entityViews.get(followed.entity) === followed)
viewer.trackedEntity = followed.entity;
else findCamera(viewer)?.follow(followed);
findCamera(viewer)?.follow(followed);
}
/**
* Where `followed` is shown this frame: the layer's display cache, else its
* entity's position at the viewer's time; null when neither answers.
* Where `followed` is shown this frame: the layer's display cache; null
* when it has none, or it answers nothing.
* @param {object} viewer
* @param {Followed|null} followed
*/
export function followedPosition(viewer, followed) {
const display = followed?.displayPosition;
const shown = typeof display === 'function' ? display() : null;
return (
shown ||
followed?.entity?.position?.getValue?.(viewer?.clock?.currentTime) ||
null
);
return (typeof display === 'function' ? display() : null) || null;
}
+20 -85
View File
@@ -14,23 +14,6 @@ import {
import { bindRenderer } from '../render/rendererOf.js';
import { createCameraEvents } from '../render/camera/events.js';
/** A viewer whose `trackedEntity` behaves as Cesium's setter does. */
function trackingViewer() {
const trackedEntityChanged = new Cesium.Event();
let tracked;
return {
trackedEntityChanged,
get trackedEntity() {
return tracked;
},
set trackedEntity(value) {
if (tracked === value) return;
tracked = value;
trackedEntityChanged.raiseEvent(value);
},
};
}
/** A camera port that follows, for a viewer bound to a renderer. */
function followingPort() {
const events = createCameraEvents({ pose: () => ({}), report: () => {} });
@@ -48,62 +31,37 @@ function followingPort() {
};
}
test('a layer’s own tracked entity is seen through its entity record', () => {
const viewer = trackingViewer();
assert.equal(followedOf(viewer), null);
assert.equal(isFollowing(viewer), false);
const entity = new Cesium.Entity({ id: 'e1', name: 'N1' });
const record = entityRecord(entity);
record.trackedId = 'flights:abc';
record.displayPosition = () => ({ x: 1, y: 2, z: 3 });
record.sprite = { width: 28 };
viewer.trackedEntity = entity;
const followed = followedOf(viewer);
assert.equal(followed, followedOf(viewer), 'the same view each time');
assert.equal(followed.key, 'flights:abc');
assert.equal(followed.name, 'N1');
assert.deepEqual(followed.displayPosition(), { x: 1, y: 2, z: 3 });
assert.equal(followed.sprite.width, 28);
assert.equal(followed.entity, entity);
viewer.trackedEntity = new Cesium.Entity({ id: 'plain' });
assert.equal(followedOf(viewer).key, 'entity:plain', 'no tracked id');
});
test('a port follow comes before any tracked entity, and is the target itself', (t) => {
const viewer = trackingViewer();
test('what the camera port follows is the target itself', (t) => {
const viewer = {};
assert.equal(followedOf(viewer), null, 'nothing without a renderer');
const camera = followingPort();
t.after(bindRenderer(viewer, { camera }));
viewer.trackedEntity = new Cesium.Entity({ id: 'helper' });
assert.equal(followedOf(viewer), null);
assert.equal(isFollowing(viewer), false);
const target = { key: 'flights:abc', position: () => null };
camera.follow(target);
assert.equal(followedOf(viewer), target);
assert.equal(isFollowing(viewer), true);
});
test('onFollowChanged fires once per change, from either source', (t) => {
const viewer = trackingViewer();
test('onFollowChanged fires once per change', (t) => {
const viewer = {};
const camera = followingPort();
t.after(bindRenderer(viewer, { camera }));
const seen = [];
const off = onFollowChanged(viewer, (f) => seen.push(f?.key ?? null));
const target = { key: 'flights:abc' };
// A port follow on Cesium sets trackedEntity too: still one change.
camera.follow(target);
// Ending it, Cesium clears its tracking before the port announces.
viewer.trackedEntity = undefined;
camera.follow(target);
camera.follow(null);
const entity = new Cesium.Entity({ id: 'legacy' });
viewer.trackedEntity = entity;
viewer.trackedEntity = entity;
off();
viewer.trackedEntity = undefined;
assert.deepEqual(seen, ['flights:abc', null, 'entity:legacy']);
camera.follow({ key: 'after' });
assert.deepEqual(seen, ['flights:abc', null]);
});
test('stopFollowing ends a port follow, or a layer’s own tracking', (t) => {
const viewer = trackingViewer();
viewer.trackedEntity = new Cesium.Entity({ id: 'legacy' });
test('stopFollowing ends a port follow', (t) => {
const viewer = {};
stopFollowing(viewer);
assert.equal(viewer.trackedEntity, undefined);
const camera = followingPort();
t.after(bindRenderer(viewer, { camera }));
camera.follow({ key: 'k' });
@@ -112,19 +70,6 @@ test('stopFollowing ends a port follow, or a layer’s own tracking', (t) => {
stopFollowing(null);
});
test('a renderer bound after subscribing is still heard', (t) => {
const viewer = { trackedEntityChanged: new Cesium.Event() };
const seen = [];
onFollowChanged(viewer, (f) => seen.push(f?.key ?? null));
const camera = followingPort();
t.after(bindRenderer(viewer, { camera }));
// Any change re-resolves the port; a Cesium follow raises one.
viewer.trackedEntityChanged.raiseEvent();
camera.follow({ key: 'k' });
camera.follow(null);
assert.deepEqual(seen, ['k', null]);
});
test('an entity follow target reads its entity record live', () => {
const entity = new Cesium.Entity({ id: 'drawn', name: 'N2' });
const position = () => ({ x: 1, y: 2, z: 3 });
@@ -142,16 +87,8 @@ test('an entity follow target reads its entity record live', () => {
assert.equal(keyed.labelModel.title, 'N2');
});
test('resumeFollowing follows a port target again, or re-tracks an entity', (t) => {
const viewer = trackingViewer();
const entity = new Cesium.Entity({ id: 'legacy' });
viewer.trackedEntity = entity;
const legacy = followedOf(viewer);
stopFollowing(viewer);
resumeFollowing(viewer, legacy);
assert.equal(viewer.trackedEntity, entity);
assert.equal(followedOf(viewer), legacy);
viewer.trackedEntity = undefined;
test('resumeFollowing follows a port target again', (t) => {
const viewer = {};
const camera = followingPort();
t.after(bindRenderer(viewer, { camera }));
const target = entityFollowTarget(new Cesium.Entity({ id: 'p' }), {});
@@ -163,13 +100,11 @@ test('resumeFollowing follows a port target again, or re-tracks an entity', (t)
assert.equal(camera.followed, target);
});
test('followedPosition prefers the display cache over the entity position', () => {
const at = new Cesium.Cartesian3(4, 5, 6);
const viewer = { clock: { currentTime: Cesium.JulianDate.now() } };
const entity = new Cesium.Entity({ id: 'e', position: at });
test('followedPosition reads the display cache', () => {
const entity = new Cesium.Entity({ id: 'e' });
const view = entityFollowTarget(entity, {});
assert.deepEqual(followedPosition(viewer, view), at);
assert.equal(followedPosition({}, view), null, 'none without a cache');
entityRecord(entity).displayPosition = () => ({ x: 7, y: 8, z: 9 });
assert.deepEqual(followedPosition(viewer, view), { x: 7, y: 8, z: 9 });
assert.equal(followedPosition(viewer, null), null);
assert.deepEqual(followedPosition({}, view), { x: 7, y: 8, z: 9 });
assert.equal(followedPosition({}, null), null);
});