export interface ViewportOrbitState { yaw: number; pitch: number; distance: number; target: [number, number, number]; } export interface ViewportCameraState extends ViewportOrbitState { position: [number, number, number]; } export const VIEWPORT_DEFAULT_ORBIT: Readonly = Object.freeze({ yaw: -Math.PI / 4, pitch: 0.55, distance: 7, target: [0, 0, 0] as [number, number, number], }); export const VIEWPORT_ORBIT_ROTATE_SENSITIVITY = 0.008; export const VIEWPORT_ORBIT_ZOOM_SENSITIVITY = 0.001; export const VIEWPORT_ORBIT_MIN_DISTANCE = 0.2; export const VIEWPORT_ORBIT_MAX_DISTANCE = 500; export function orbitPosition(state: Pick): [number, number, number] { const horizontal = state.distance * Math.cos(state.pitch); return [ state.target[0] + horizontal * Math.cos(state.yaw), state.target[1] + horizontal * Math.sin(state.yaw), state.target[2] + state.distance * Math.sin(state.pitch), ]; } export function cameraState(state: ViewportOrbitState): ViewportCameraState { return { ...state, target: [...state.target] as [number, number, number], position: orbitPosition(state) }; } export function orbitStateFromPosition(position: readonly number[], target: readonly number[] = [0, 0, 0]): ViewportOrbitState { const dx = position[0] - target[0]; const dy = position[1] - target[1]; const dz = position[2] - target[2]; const distance = Math.max(VIEWPORT_ORBIT_MIN_DISTANCE, Math.hypot(dx, dy, dz)); return { yaw: Math.atan2(dy, dx), pitch: Math.asin(Math.max(-1, Math.min(1, dz / distance))), distance, target: [target[0], target[1], target[2]], }; } export function applyOrbitDelta(state: ViewportOrbitState, deltaX: number, deltaY: number, zoom: number): ViewportOrbitState { return { ...state, yaw: state.yaw - deltaX * VIEWPORT_ORBIT_ROTATE_SENSITIVITY, pitch: Math.max(-1.45, Math.min(1.45, state.pitch + deltaY * VIEWPORT_ORBIT_ROTATE_SENSITIVITY)), distance: Math.max(VIEWPORT_ORBIT_MIN_DISTANCE, Math.min(VIEWPORT_ORBIT_MAX_DISTANCE, state.distance * Math.exp(zoom * VIEWPORT_ORBIT_ZOOM_SENSITIVITY))), target: [...state.target] as [number, number, number], }; }