Files
workinf_Blender_Wasm/web/protocol/viewport-camera.ts
mes123456 0fe8d2bb56
Some checks are pending
M6 deployable RC / quick (push) Waiting to run
M6 deployable RC / chromium (push) Blocked by required conditions
M6 deployable RC / release (push) Blocked by required conditions
Advance M8-M11 parity workflows
2026-08-17 04:37:07 -04:00

59 lines
2.2 KiB
TypeScript

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<ViewportOrbitState> = 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<ViewportOrbitState, "yaw" | "pitch" | "distance" | "target">): [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],
};
}