mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
fix(cockpit): the orbital view gives the field of view back on exit
Cesium's fov was saved and put back when the orbital view closed, so a later resize or rotation kept Cesium's own behaviour. The exit held the saved vertical angle through setFov instead, which the camera port kept for the rest of the session: rotated to portrait, the view stayed zoomed in. setFov(null) gives the field of view back: Cesium's fov as it was before the first hold, the stub's own angle.
This commit is contained in:
1 parent
03a7d8827a
commit
4a781f0480
6 files changed
+52
-12
No files matched your search
@@ -240,6 +240,8 @@ export function createCesiumCamera({
|
|||||||
* view, it waits.
|
* view, it waits.
|
||||||
*/
|
*/
|
||||||
let heldFovy = null;
|
let heldFovy = null;
|
||||||
|
/** Cesium's own `fov` before the first hold, put back on release. */
|
||||||
|
let ownFov = null;
|
||||||
function applyFov() {
|
function applyFov() {
|
||||||
const frustum = camera.frustum;
|
const frustum = camera.frustum;
|
||||||
const aspect = frustum?.aspectRatio;
|
const aspect = frustum?.aspectRatio;
|
||||||
@@ -388,10 +390,22 @@ export function createCesiumCamera({
|
|||||||
camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
|
camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
|
||||||
scene.requestRender();
|
scene.requestRender();
|
||||||
},
|
},
|
||||||
setFov({ vertical }) {
|
setFov(fov) {
|
||||||
|
const frustum = camera.frustum;
|
||||||
|
if (fov === null) {
|
||||||
|
// Given back: Cesium's own angle again, kept as it keeps it.
|
||||||
|
if (heldFovy !== null && ownFov !== null && 'fov' in frustum)
|
||||||
|
frustum.fov = ownFov;
|
||||||
|
heldFovy = null;
|
||||||
|
ownFov = null;
|
||||||
|
scene.requestRender();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const { vertical } = fov;
|
||||||
// Cesium would take a bad angle and draw a garbage projection.
|
// Cesium would take a bad angle and draw a garbage projection.
|
||||||
if (!(vertical > 0 && vertical < Math.PI))
|
if (!(vertical > 0 && vertical < Math.PI))
|
||||||
throw new RangeError(`no vertical field of view of ${vertical}`);
|
throw new RangeError(`no vertical field of view of ${vertical}`);
|
||||||
|
if (heldFovy === null && 'fov' in frustum) ownFov = frustum.fov;
|
||||||
heldFovy = vertical;
|
heldFovy = vertical;
|
||||||
applyFov();
|
applyFov();
|
||||||
scene.requestRender();
|
scene.requestRender();
|
||||||
|
|||||||
@@ -305,6 +305,26 @@ test('setFov holds the vertical angle across resizes, and waits for a first one'
|
|||||||
assert.throws(() => port.setFov({ vertical: NaN }), RangeError);
|
assert.throws(() => port.setFov({ vertical: NaN }), RangeError);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('setFov(null) gives the angle back: Cesium’s own fov again, kept as Cesium keeps it', () => {
|
||||||
|
const { scene, camera } = headlessCamera();
|
||||||
|
scene.postUpdate = new Cesium.Event();
|
||||||
|
scene.requestRender = () => {};
|
||||||
|
camera.frustum.aspectRatio = 16 / 9;
|
||||||
|
const own = camera.frustum.fov;
|
||||||
|
const { port } = createCesiumCamera({ scene, camera, report: () => {} });
|
||||||
|
port.setFov({ vertical: 0.5 });
|
||||||
|
port.setFov({ vertical: 0.3 });
|
||||||
|
scene.postUpdate.raiseEvent(scene);
|
||||||
|
port.setFov(null);
|
||||||
|
assert.equal(camera.frustum.fov, own, 'the fov it had before the first hold');
|
||||||
|
// Rotated to portrait: Cesium keeps its fov, which is now the vertical
|
||||||
|
// angle, rather than the held one.
|
||||||
|
camera.frustum.aspectRatio = 9 / 16;
|
||||||
|
scene.postUpdate.raiseEvent(scene);
|
||||||
|
assert.equal(camera.frustum.fov, own);
|
||||||
|
assert.ok(Math.abs(camera.frustum.fovy - own) < 1e-12);
|
||||||
|
});
|
||||||
|
|
||||||
test('a pose measures heading, pitch and roll as Cesium does, and leaves the camera as it was', () => {
|
test('a pose measures heading, pitch and roll as Cesium does, and leaves the camera as it was', () => {
|
||||||
const { camera, port } = headlessPort();
|
const { camera, port } = headlessPort();
|
||||||
const bits = () =>
|
const bits = () =>
|
||||||
|
|||||||
@@ -917,9 +917,10 @@ export function cameraChecks() {
|
|||||||
{
|
{
|
||||||
id: 'camera.fov',
|
id: 'camera.fov',
|
||||||
port: 'camera',
|
port: 'camera',
|
||||||
title: 'setFov sets the vertical field of view',
|
title: 'setFov sets the vertical field of view; null gives it back',
|
||||||
async run(h) {
|
async run(h) {
|
||||||
const r = await fresh(h);
|
const r = await fresh(h);
|
||||||
|
const own = r.camera.pose().fovy;
|
||||||
r.camera.setFov({ vertical: 0.5 });
|
r.camera.setFov({ vertical: 0.5 });
|
||||||
near(r.camera.pose().fovy, 0.5, 1e-12, 'fovy');
|
near(r.camera.pose().fovy, 0.5, 1e-12, 'fovy');
|
||||||
near(
|
near(
|
||||||
@@ -928,6 +929,8 @@ export function cameraChecks() {
|
|||||||
1e-9,
|
1e-9,
|
||||||
'aspect',
|
'aspect',
|
||||||
);
|
);
|
||||||
|
r.camera.setFov(null);
|
||||||
|
near(r.camera.pose().fovy, own, 1e-9, 'the renderer’s own again');
|
||||||
r.destroy();
|
r.destroy();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Vendored
+6
-1
@@ -240,7 +240,12 @@ export interface CameraPort {
|
|||||||
* sent many times over.
|
* sent many times over.
|
||||||
*/
|
*/
|
||||||
setView(view: ViewSpec): void;
|
setView(view: ViewSpec): void;
|
||||||
setFov(fov: { vertical: number }): void;
|
/**
|
||||||
|
* Hold the vertical field of view, across resizes, until `null` gives the
|
||||||
|
* field of view back to the renderer: it is then as it was before the
|
||||||
|
* first hold, and follows the view's shape as the renderer's own does.
|
||||||
|
*/
|
||||||
|
setFov(fov: { vertical: number } | null): void;
|
||||||
/**
|
/**
|
||||||
* The geodetic rectangle in view: the bounds of the canvas corners on the
|
* The geodetic rectangle in view: the bounds of the canvas corners on the
|
||||||
* ellipsoid, a corner that misses it taken where the horizon is, and the
|
* ellipsoid, a corner that misses it taken where the horizon is, and the
|
||||||
|
|||||||
@@ -798,8 +798,9 @@ export function createStubCamera({
|
|||||||
const port = {
|
const port = {
|
||||||
pose,
|
pose,
|
||||||
setView,
|
setView,
|
||||||
setFov({ vertical }) {
|
setFov(fov) {
|
||||||
fovy = vertical;
|
// null: the stub's own angle again, the wider one kept as it was.
|
||||||
|
fovy = fov === null ? null : fov.vertical;
|
||||||
requestRender('camera');
|
requestRender('camera');
|
||||||
},
|
},
|
||||||
viewRectangle: () => null,
|
viewRectangle: () => null,
|
||||||
|
|||||||
@@ -72,11 +72,10 @@ const IDLE_TRANSITION = Object.freeze({
|
|||||||
const NADIR_LOOK = Object.freeze({ yawDeg: 0, pitchDeg: 0 });
|
const NADIR_LOOK = Object.freeze({ yawDeg: 0, pitchDeg: 0 });
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start an orbital session's state. Keeps the camera's own field of view so
|
* Start an orbital session's state. The exit gives the field of view back
|
||||||
* the exit can put it back exactly.
|
* to the renderer, as it was before the session.
|
||||||
*/
|
*/
|
||||||
export function beginOrbital() {
|
export function beginOrbital() {
|
||||||
const fovy = cameraOf(this.viewer).pose().fovy;
|
|
||||||
const state = {
|
const state = {
|
||||||
imagery: acquireOrbitImagery(this.services),
|
imagery: acquireOrbitImagery(this.services),
|
||||||
zoom: ORBITAL_DEFAULT_ZOOM,
|
zoom: ORBITAL_DEFAULT_ZOOM,
|
||||||
@@ -85,8 +84,6 @@ export function beginOrbital() {
|
|||||||
// A zoom step in flight: the magnification it started from and when.
|
// A zoom step in flight: the magnification it started from and when.
|
||||||
transition: null,
|
transition: null,
|
||||||
previousUp: null,
|
previousUp: null,
|
||||||
// The vertical field of view the camera had, put back on exit.
|
|
||||||
savedFov: Number.isFinite(fovy) ? fovy : null,
|
|
||||||
altitudeM: null,
|
altitudeM: null,
|
||||||
verticalFov: null,
|
verticalFov: null,
|
||||||
lastBriefMs: 0,
|
lastBriefMs: 0,
|
||||||
@@ -116,8 +113,8 @@ export function endOrbital() {
|
|||||||
if (!state) return;
|
if (!state) return;
|
||||||
this.orbital = null;
|
this.orbital = null;
|
||||||
state.placeTracker?.reset();
|
state.placeTracker?.reset();
|
||||||
if (state.savedFov !== null)
|
// The renderer's own field of view again, following the view's shape.
|
||||||
cameraOf(this.viewer).setFov({ vertical: state.savedFov });
|
cameraOf(this.viewer).setFov(null);
|
||||||
// Back to the map the operator had, unless they picked another meanwhile.
|
// Back to the map the operator had, unless they picked another meanwhile.
|
||||||
state.imagery?.release?.()?.catch?.(() => {});
|
state.imagery?.release?.()?.catch?.(() => {});
|
||||||
this.publishOrbital(null);
|
this.publishOrbital(null);
|
||||||
|
|||||||
Reference in new issue
Block a user