Files
gods-eye-view/src/orbit.js
T
German Khokhlov d1fdb9eb45 Let the shell's share link, HUD, orbit and navigation follow another globe engine
The shell reads its viewer, engine and map stack from the attached scene. ShareLinkManager, IntelHUD and OrbitController gain attachEngine, NavigationController setScene and LocationNavigation attachScene/detachScene, so a switch can keep them and move only their camera listeners; between engines they write and read nothing. The voice runner and session voice modules are owned by their packages, which check:boundaries needs since the voice commit.
2026-11-03 17:48:08 +04:00

114 lines
3.4 KiB
JavaScript

import * as geo from './geo/index.js';
import { engineFromViewer } from './globe/cesium/engineFromViewer.js';
import {
holdContinuousRender,
releaseContinuousRender,
} from './renderGovernor.js';
/**
* OrbitController — smooth orbit around a target point.
* Uses the engine's preRender frames for frame-rate-independent updates.
* Toggle with O key; auto-stops on POI/city change.
*/
export class OrbitController {
/**
* @param {import('./globe/contract.js').GlobeEngine | object} engine The
* globe engine, or a viewer registered for one.
*/
constructor(engine) {
this.engine = engineFromViewer(engine);
this.active = false;
this.target = null;
this.radius = 500;
this.pitch = -30;
this.speed = geo.math.toRadians(6); // ~6°/sec → full rotation in ~60s
this.angle = 0;
this._removeListener = null;
}
/**
* Start orbiting around a target position.
* @param {{x: number, y: number, z: number}} targetCartesian - The point to orbit around
* @param {object} options
* @param {number} options.radius - Distance from target in meters
* @param {number} options.pitch - Tilt angle in degrees (negative = looking down)
* @param {number} options.speed - Degrees per second (default 6)
*/
start(targetCartesian, options = {}) {
if (!targetCartesian || !this.engine) return;
const { camera, frames } = this.engine;
this.target = targetCartesian;
this.radius = options.radius || this.radius;
this.pitch = options.pitch || this.pitch;
this.speed = geo.math.toRadians(options.speed || 6);
this.active = true;
// Orbit mutates the camera from preRender — without a hold the loop
// starves after its first idle frame. (perf wave 2)
holdContinuousRender('camera-orbit');
// Start from the camera's current heading for seamless transition
this.angle = camera.pose().heading;
let lastTime = Date.now();
this._removeListener = frames.preRender.addEventListener(() => {
if (!this.active) return;
const now = Date.now();
const dt = (now - lastTime) / 1000;
lastTime = now;
this.angle += this.speed * dt;
camera.lookAt(this.target, {
heading: this.angle,
pitch: geo.math.toRadians(this.pitch),
range: this.radius,
});
});
}
/**
* Stop orbiting. Camera freezes at current position and user regains control.
*/
stop() {
this.active = false;
releaseContinuousRender('camera-orbit');
if (this._removeListener) {
this._removeListener();
this._removeListener = null;
}
// Unlock camera for free interaction
this.engine?.camera.release();
}
/**
* Orbit on another globe engine's camera (an engine switch), or none
* (null). A running orbit stops: its frames belonged to the outgoing one.
*/
attachEngine(engine) {
if (this.active || this._removeListener) {
this.active = false;
releaseContinuousRender('camera-orbit');
this._removeListener?.();
this._removeListener = null;
}
this.engine = engine ? engineFromViewer(engine) : null;
}
/**
* Toggle orbit on/off.
* @param {{x: number, y: number, z: number}} targetCartesian - Required when starting
* @param {object} options - Passed to start()
* @returns {boolean} Whether orbit is now active
*/
toggle(targetCartesian, options) {
if (this.active) {
this.stop();
} else {
this.start(targetCartesian, options);
}
return this.active;
}
}