mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-19 09:42:21 +00:00
Focusing a mission does nothing in the cockpit, so the layer row's arrows select the next mission without moving the camera, as its label and dot already do. Co-authored-by: Manjunath Paparaju <[email protected]>
208 lines
6.7 KiB
JavaScript
208 lines
6.7 KiB
JavaScript
import { WGS84, fromDegrees } from '../../geo/ellipsoid.js';
|
|
import {
|
|
PI_OVER_TWO,
|
|
add,
|
|
cross,
|
|
distance,
|
|
dot,
|
|
negate,
|
|
normalize,
|
|
scale,
|
|
toRadians,
|
|
vec,
|
|
} from '../../geo/vector.js';
|
|
import {
|
|
MISSION_GLOBE_VIEW_RANGE_M,
|
|
MISSION_FOCUS_RANGE_M,
|
|
cockpitActive,
|
|
} from './policy.js';
|
|
import { cameraOf, canvasOf, findCamera } from '../../render/rendererOf.js';
|
|
|
|
export function createSelection({
|
|
state: layerState,
|
|
services,
|
|
parts,
|
|
source,
|
|
}) {
|
|
const { getKeyholeGeometry } = services.geometry;
|
|
|
|
/** @returns {boolean} Whether the flight started. */
|
|
function focusFullGlobe(viewer, { duration = 2.4, complete, cancel } = {}) {
|
|
const canvas = canvasOf(viewer);
|
|
const height = canvas?.clientHeight || canvas?.height;
|
|
const width = canvas?.clientWidth || canvas?.width;
|
|
if (!(width > 0) || !(height > 0)) return;
|
|
const pose = findCamera(viewer)?.pose();
|
|
const fovy = pose?.fovy;
|
|
if (
|
|
!pose ||
|
|
!Number.isFinite(pose.lon) ||
|
|
!Number.isFinite(fovy) ||
|
|
fovy <= 0 ||
|
|
fovy >= Math.PI
|
|
)
|
|
return false;
|
|
const earthRadius = WGS84.radii.x;
|
|
const keyholeRadius = getKeyholeGeometry(width, height).radius;
|
|
const targetScreenRadius = keyholeRadius * 0.61;
|
|
const angularRadius = Math.atan(
|
|
(targetScreenRadius / (height * 0.5)) * Math.tan(fovy * 0.5),
|
|
);
|
|
const distance = earthRadius / Math.max(Math.sin(angularRadius), 1e-4);
|
|
const altitude = Math.max(earthRadius * 1.55, distance - earthRadius);
|
|
const camera = cameraOf(viewer);
|
|
layerState._cameraReleased = false;
|
|
camera.flyTo(
|
|
{ lon: pose.lon, lat: pose.lat, height: altitude },
|
|
{
|
|
orientation: {
|
|
heading: pose.heading,
|
|
pitch: -PI_OVER_TWO,
|
|
roll: 0,
|
|
},
|
|
durationS: duration,
|
|
easing: 'cubicInOut',
|
|
onComplete: complete,
|
|
onCancel: cancel,
|
|
},
|
|
);
|
|
return true;
|
|
}
|
|
|
|
function setSelectedMission(launchId, isolate = true) {
|
|
// Once the layer is going off (its Satellites restore may still be
|
|
// running) a roster click selects nothing and moves no camera.
|
|
if (launchId && !layerState._enabled) return;
|
|
if (launchId) parts.panel.clearMissionRosterHover();
|
|
if (
|
|
layerState._replayCameraLaunchId &&
|
|
layerState._replayCameraLaunchId !== launchId
|
|
)
|
|
parts.replay.stopMissionReplay();
|
|
if (
|
|
layerState._missionZoomAnchorId &&
|
|
layerState._missionZoomAnchorId !== launchId
|
|
)
|
|
stopMissionZoomAnchor();
|
|
layerState._selectedLaunchId = launchId;
|
|
layerState._explicitSelection = Boolean(launchId && isolate);
|
|
if (launchId) layerState._animationStarts.set(launchId, Date.now());
|
|
if (!launchId && layerState._viewer) {
|
|
stopMissionZoomAnchor();
|
|
parts.rendering.selectLaunchEntity(null);
|
|
}
|
|
if (!layerState._missionSets) return;
|
|
parts.rendering.isolateMission(launchId);
|
|
parts.orbitRendering.syncMissionOrbitPrimitiveVisibility();
|
|
parts.overlays.syncMissionOverlayEntries();
|
|
parts.panel.renderMissionPanel();
|
|
}
|
|
|
|
function stopMissionZoomAnchor() {
|
|
if (layerState._missionZoomAnchorRemover)
|
|
layerState._missionZoomAnchorRemover();
|
|
layerState._missionZoomAnchorRemover = null;
|
|
layerState._missionZoomAnchorId = null;
|
|
if (!layerState._replayCameraLaunchId)
|
|
findCamera(layerState._viewer)?.release();
|
|
}
|
|
|
|
function startMissionZoomAnchor(launch, initialRange) {
|
|
if (!layerState._viewer || !launch || layerState._replayCameraLaunchId)
|
|
return;
|
|
stopMissionZoomAnchor();
|
|
const target = fromDegrees(launch.lon, launch.lat);
|
|
const range = Math.max(
|
|
2000,
|
|
Number(initialRange) || MISSION_GLOBE_VIEW_RANGE_M,
|
|
);
|
|
layerState._missionZoomAnchorId = launch.id;
|
|
cameraOf(layerState._viewer).lookAt(target, {
|
|
heading: 0,
|
|
pitch: parts.camera.missionZoomPitch(range),
|
|
range,
|
|
});
|
|
}
|
|
|
|
function focusMission(launch) {
|
|
if (!layerState._viewer || !launch || !layerState._enabled) return;
|
|
// The cockpit holds the camera: a mission chosen there, by its label,
|
|
// its dot or the layer row's arrows, is selected without flying to it.
|
|
if (cockpitActive()) return;
|
|
stopMissionZoomAnchor();
|
|
parts.rendering.selectLaunchEntity(launch.id);
|
|
const camera = cameraOf(layerState._viewer);
|
|
const orbitPositions = parts.rendering.projectedOrbitPath(launch.id);
|
|
const launchPosition = fromDegrees(launch.lon, launch.lat);
|
|
let range = 18000000;
|
|
if (orbitPositions?.length > 1) {
|
|
const canvas = canvasOf(layerState._viewer);
|
|
const fovy = camera.pose().fovy || toRadians(60);
|
|
const aspect = Math.max(
|
|
0.1,
|
|
(canvas.clientWidth || canvas.width) /
|
|
Math.max(1, canvas.clientHeight || canvas.height),
|
|
);
|
|
const fovx = 2 * Math.atan(Math.tan(fovy * 0.5) * aspect);
|
|
const paddedHalfAngle = Math.min(fovy, fovx) * 0.5 * 0.62;
|
|
const orbitExtent = orbitPositions.reduce(
|
|
(largest, point) => Math.max(largest, distance(launchPosition, point)),
|
|
0,
|
|
);
|
|
range = Math.max(
|
|
orbitExtent / Math.max(Math.sin(paddedHalfAngle), 0.1),
|
|
18000000,
|
|
);
|
|
}
|
|
const targetNormal = normalize(launchPosition);
|
|
const destination = add(launchPosition, scale(targetNormal, range));
|
|
const direction = negate(targetNormal);
|
|
let right = cross(direction, vec(0, 0, 1));
|
|
if (dot(right, right) < 1e-8) right = cross(direction, vec(0, 1, 0));
|
|
right = normalize(right);
|
|
const up = normalize(cross(right, direction));
|
|
layerState._cameraReleased = false;
|
|
camera.flyTo(destination, {
|
|
orientation: { direction, up },
|
|
durationS: 1.1,
|
|
onComplete: () => {
|
|
if (layerState._selectedLaunchId === launch.id)
|
|
startMissionZoomAnchor(launch, range);
|
|
},
|
|
});
|
|
}
|
|
|
|
function focusLaunchSite(launch) {
|
|
if (!layerState._viewer || !launch || !layerState._enabled) return;
|
|
parts.replay.stopMissionReplay();
|
|
stopMissionZoomAnchor();
|
|
parts.rendering.selectLaunchEntity(launch.id);
|
|
layerState._cameraReleased = false;
|
|
cameraOf(layerState._viewer).flyToBounds(
|
|
{ center: { lon: launch.lon, lat: launch.lat }, radius: 0 },
|
|
{
|
|
offset: {
|
|
heading: 0,
|
|
pitch: toRadians(-42),
|
|
range: MISSION_FOCUS_RANGE_M,
|
|
},
|
|
durationS: 1.4,
|
|
easing: 'cubicInOut',
|
|
onComplete: () => {
|
|
if (layerState._selectedLaunchId === launch.id) {
|
|
startMissionZoomAnchor(launch, MISSION_FOCUS_RANGE_M);
|
|
}
|
|
},
|
|
},
|
|
);
|
|
}
|
|
return {
|
|
focusFullGlobe,
|
|
setSelectedMission,
|
|
stopMissionZoomAnchor,
|
|
startMissionZoomAnchor,
|
|
focusMission,
|
|
focusLaunchSite,
|
|
};
|
|
}
|