Share the camera events and the stub's drawn positions with other cameras

The geo export now has geodesicAt and horizonFrom. The camera events module
is a public export (./render/camera/events) and holds move detection and the
command hooks as well as the per-listener changed measure; the stub camera
uses them. Each onCommand registration is now its own, so removing one twin
leaves the other. The stub's testing.drawnPosition says where it draws an
item, so a camera that is not the stub's can follow drawn items.
This commit is contained in:
Sameh Khamis committed 2026-11-08 19:22:42 -08:00
1 parent afd8dba67b
commit ef616b7905
9 files changed
+320 -70

No files matched your search

+14 -53
View File
@@ -47,7 +47,6 @@ import { createCameraEvents } from '../camera/events.js';
import { geodesicAt } from '../../geo/geodesic.js';
const DEFAULT_FOV = Math.PI / 3;
const MOVE_END_WAIT_MS = 500;
const MINIMUM_ZOOM = 100;
const DEFAULT_SPHERE_OFFSET = Object.freeze({
heading: 0,
@@ -98,17 +97,23 @@ export function createStubCamera({
const near = 1;
const far = 5e8;
const events = createCameraEvents({ pose, report, thresholds });
const { emit, on, stepChanged } = events;
const commandHooks = [];
const events = createCameraEvents({
pose,
world: () => ({
position: positionWC(),
direction: directionWC(),
up: upWC(),
}),
following: () => Boolean(followed),
report,
thresholds,
});
const { emit, on, stepChanged, command, onCommand } = events;
const holds = new Map();
let holdSeq = 0;
let flight = null;
let followed = null;
let followFramed = false;
let lastPose = null;
let moving = false;
let lastMovedAt = 0;
let coast = Object.freeze({ drag: 0.4, zoom: 0.2 });
const controls = {
@@ -202,17 +207,6 @@ export function createStubCamera({
};
}
function command(kind) {
for (const hook of commandHooks.slice()) {
if (!commandHooks.includes(hook)) continue;
try {
hook({ kind });
} catch (error) {
report(error);
}
}
}
/** Place the eye at `destination` (world) with `orientation` in its east-north-up frame. */
function placeAt(destination, hpr) {
const kept = frame;
@@ -727,35 +721,8 @@ export function createStubCamera({
// --- Events ----------------------------------------------------------------
function sameView(a, b) {
return (
a &&
b &&
distance(a.position, b.position) === 0 &&
dot(a.direction, b.direction) === dot(a.direction, a.direction) &&
dot(a.up, b.up) === dot(a.up, a.up)
);
}
function stepMoveDetection() {
const current = {
position: positionWC(),
direction: directionWC(),
up: upWC(),
};
const changed = lastPose && !sameView(lastPose, current);
lastPose = current;
const t = now();
if (changed) {
lastMovedAt = t;
if (!moving) {
moving = true;
emit('moveStart');
}
} else if (moving && !followed && t - lastMovedAt >= MOVE_END_WAIT_MS) {
moving = false;
emit('moveEnd');
}
events.stepMoveDetection(now());
}
function matrices() {
@@ -844,13 +811,7 @@ export function createStubCamera({
followOffset,
controls,
on,
onCommand(cb) {
commandHooks.push(cb);
return () => {
const i = commandHooks.indexOf(cb);
if (i >= 0) commandHooks.splice(i, 1);
};
},
onCommand,
matrices,
};
+8
View File
@@ -1600,6 +1600,13 @@ export function createStubRenderer(options = {}) {
canvas.width = viewport.width;
canvas.height = viewport.height;
},
/**
* Where a drawn item is drawn now (or `aheadMs` from now), ECEF, or
* null: what the stub's own camera follows for a target's `item`, for
* a camera of someone else's driving the stub (the `camera.followItem`
* conformance check).
*/
drawnPosition: (ref, aheadMs = 0) => drawnPosition(ref, aheadMs),
/** What a set holds. */
setRecord: (id) => sets.get(id) ?? null,
sets: () => [...sets.keys()],
@@ -1624,6 +1631,7 @@ export function createStubRenderer(options = {}) {
* @property {(point: object, height: number) => void} cacheHeight
* @property {(kind: string, value: boolean) => void} setSettled
* @property {(size: {width?: number, height?: number}) => void} setViewport
* @property {(ref: {set: string, id: string|number}, aheadMs?: number) => {x: number, y: number, z: number}|null} drawnPosition
* @property {(id: string) => object|null} setRecord
* @property {() => string[]} sets
* @property {Array<{port: string, member: string, args: unknown[]}>} calls
+53
View File
@@ -130,3 +130,56 @@ test('a draped rectangle across the antimeridian picks on both sides of it, turn
assert.ok(!picked(179.5, 0), 'and narrows it');
r.destroy();
});
test('the stub says where it draws an item, for a camera that is not its own', () => {
const r = createStubRenderer();
const t0 = r.frame.time();
const from = toEcef({ lon: -97.74, lat: 30.27, height: 1000 });
const to = toEcef({ lon: -97.7, lat: 30.27, height: 1000 });
const set = r.draw.icons({ id: 'drawn', motionClock: { kind: 'scene' } });
set.set([
{
id: 'm',
pos: from,
image: 'data:,',
size: [16, 16],
motion: { kind: 'lerp', from, to, t0, t1: t0 + 60000 },
},
]);
const at = (p) => [p.x, p.y, p.z];
assert.deepEqual(
at(r.testing.drawnPosition({ set: 'drawn', id: 'm' })),
at(from),
);
r.frame.setTime(t0 + 30000);
const mid = r.testing.drawnPosition({ set: 'drawn', id: 'm' });
for (const k of ['x', 'y', 'z'])
assert.ok(Math.abs(mid[k] - (from[k] + to[k]) / 2) < 1e-6, k);
const ahead = r.testing.drawnPosition({ set: 'drawn', id: 'm' }, 30000);
for (const k of ['x', 'y', 'z'])
assert.ok(Math.abs(ahead[k] - to[k]) < 1e-6, k);
assert.equal(r.testing.drawnPosition({ set: 'drawn', id: 'gone' }), null);
assert.equal(r.testing.drawnPosition({ set: 'nothing', id: 'm' }), null);
r.destroy();
});
test('the stub camera ends no move while it follows', () => {
const r = createStubRenderer();
const seen = [];
r.camera.on('moveStart', () => seen.push('start'));
r.camera.on('moveEnd', () => seen.push('end'));
r.testing.tick();
const target = toEcef({ lon: -97.74, lat: 30.27, height: 0 });
r.camera.follow({
key: 'still',
position: () => target,
frame: 'enu',
viewFrom: { x: 0, y: -500, z: 300 },
});
r.testing.advance(2000);
assert.deepEqual(seen, ['start'], 'moveEnd waits for the follow to end');
r.camera.follow(null);
r.testing.advance(1000);
assert.deepEqual(seen, ['start', 'end']);
r.destroy();
});