Files
rayd1o 05377bf5c6
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
ci / delivery (push) Has been cancelled
release / images (push) Has been cancelled
release: bump version to 0.76.0
2026-09-30 17:49:29 +08:00

72 lines
3.5 KiB
JavaScript

import * as THREE from "three";
// Magnify the apparent sphere radius, not the camera's distance to its center.
// At zoom=1 the existing camera pose is unchanged; larger zoom never enters it.
export function distanceForGlobeZoom(zoom, radius, defaultDistance, minimumAltitude = 10) {
const tangentDistanceSquared = defaultDistance ** 2 - radius ** 2;
return Math.max(radius + minimumAltitude,
Math.sqrt(radius ** 2 + tangentDistanceSquared / zoom ** 2));
}
export function globeZoomForDistance(distance, radius, defaultDistance) {
return Math.sqrt((defaultDistance ** 2 - radius ** 2) /
Math.max(0.0001, distance ** 2 - radius ** 2));
}
// Keep the legacy pixel sensitivity until the globe fills the viewport. At
// extreme zoom cap it by the surface distance, with no pointer/sphere gating.
export function globeDragRotationFactor(zoom, camera, viewport, config) {
const legacy = config.dragRotationFactorBase * THREE.MathUtils.clamp(
Math.pow(Math.max(zoom, 0.01), -config.dragRotationZoomExponent),
config.dragRotationScaleMin, config.dragRotationScaleMax,
);
const distance = camera.position.length();
const radius = config.earthRadius;
const tangent = Math.tan(THREE.MathUtils.degToRad(camera.fov) / 2);
const diameter = radius / Math.sqrt(Math.max(1, distance * distance - radius * radius)) * viewport.height / tangent;
const coverage = diameter / Math.max(1, Math.min(viewport.width, viewport.height));
const blend = THREE.MathUtils.smoothstep(coverage, 1, 1.5);
const surface = 2 * Math.max(config.minimumCameraAltitude, distance - radius) * tangent / (Math.max(1, viewport.height) * radius);
return legacy + (Math.min(legacy, surface) - legacy) * blend;
}
// All pointer drags are pixel-driven, including outside the silhouette. Rotate
// around the geographic axis horizontally and camera right vertically. Keep
// quaternion math so Euler rebasing cannot reverse drag or add unwanted bank.
export function createScreenSpaceDrag() {
const pending = new THREE.Vector2();
const cameraRotation = new THREE.Quaternion();
const parentInverse = new THREE.Quaternion();
const axis = new THREE.Vector3();
const screenUp = new THREE.Vector3();
const north = new THREE.Vector3();
const localNorth = new THREE.Vector3(0, 1, 0);
const change = new THREE.Quaternion();
return {
move(yaw, pitch) { pending.x += yaw; pending.y += pitch; },
stop() { pending.set(0, 0); },
advance(earth, camera, smoothing) {
const length = pending.length();
if (!length) return;
// Bound a delayed frame without throwing away accumulated pointer motion.
const fraction = Math.min(smoothing, (Math.PI / 4) / length);
camera.getWorldQuaternion(cameraRotation);
parentInverse.identity();
if (earth.parent) earth.parent.getWorldQuaternion(parentInverse).invert();
screenUp.set(0, 1, 0).applyQuaternion(cameraRotation).applyQuaternion(parentInverse);
north.copy(localNorth).applyQuaternion(earth.quaternion);
const direction = north.dot(screenUp) < 0 ? -1 : 1;
change.setFromAxisAngle(localNorth, pending.x * fraction * direction);
earth.quaternion.multiply(change);
axis.set(1, 0, 0).applyQuaternion(cameraRotation).applyQuaternion(parentInverse);
change.setFromAxisAngle(axis, pending.y * fraction);
earth.quaternion.premultiply(change).normalize();
pending.multiplyScalar(1 - fraction);
},
};
}
export function isGlobeWheelTarget(target, canvas) {
return Boolean(target && (target === canvas || target.closest?.(".map-clusters")));
}