Files
planet/frontend/public/earth/js/globe-navigation.test.js
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

138 lines
6.4 KiB
JavaScript

import { expect, test } from "bun:test";
import * as THREE from "three";
import { CONFIG, EARTH_SURFACE_TEXTURE_ALTITUDE_OFFSET } from "./constants.js";
import { createScreenSpaceDrag, globeDragRotationFactor, distanceForGlobeZoom, globeZoomForDistance } from "./globe-navigation.js";
const radius = CONFIG.earthRadius + EARTH_SURFACE_TEXTURE_ALTITUDE_OFFSET;
const distance = zoom => distanceForGlobeZoom(zoom, CONFIG.earthRadius,
CONFIG.defaultCameraZ, CONFIG.minimumCameraAltitude);
test("the entire zoom range stays outside the surface with near-plane clearance and an invertible scale", () => {
expect(distance(1)).toBe(CONFIG.defaultCameraZ);
let previous = Infinity;
for (let zoom = CONFIG.minZoom; zoom <= CONFIG.maxZoom; zoom += 0.025) {
const z = distance(zoom);
expect(z - 0.1).toBeGreaterThan(radius + 6);
expect(z).toBeLessThan(previous);
expect(globeZoomForDistance(z, CONFIG.earthRadius, CONFIG.defaultCameraZ)).toBeCloseTo(zoom, 10);
previous = z;
}
expect(distance(5)).toBeGreaterThan(110);
});
function scene(zoom, width, height) {
const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 5000);
camera.position.z = distance(zoom);
camera.updateMatrixWorld();
const earth = new THREE.Object3D();
earth.rotation.set(0.6, -1.2, 0.2);
earth.updateMatrixWorld();
const viewport = { left: 17, top: 29, width, height };
const project = local => {
earth.updateMatrixWorld();
const p = local.clone().applyMatrix4(earth.matrixWorld).project(camera);
return { x: viewport.left + (p.x + 1) * width / 2, y: viewport.top + (1 - p.y) * height / 2 };
};
return { camera, earth, viewport, project };
}
for (const [width, height] of [[1440, 900], [390, 844]]) {
test(`screen drag keeps pointer direction after multiple revolutions and quaternion rebasing at ${width}px`, () => {
const f = scene(0.5, width, height);
const drag = createScreenSpaceDrag();
for (const turns of [-12, -3, 0, 5, 20]) for (const pitch of [0.3, 2.6, -2.7]) {
for (const [dx, dy] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) {
f.earth.rotation.set(pitch, turns * Math.PI * 2 + 1.8, 0.7);
// Camera focusing writes quaternions; this
// canonicalizes XYZ angles, reproducing the legacy reversed Y axis.
f.earth.quaternion.copy(f.earth.quaternion.clone());
const point = new THREE.Vector3(0, 0, radius).applyQuaternion(f.earth.quaternion.clone().invert());
const before = f.project(point);
drag.stop();
drag.move(dx * 0.02, dy * 0.02);
drag.advance(f.earth, f.camera, 0.25);
const after = f.project(point);
if (dx) expect((after.x - before.x) * dx).toBeGreaterThan(0);
if (dy) expect((after.y - before.y) * dy).toBeGreaterThan(0);
expect(f.earth.quaternion.length()).toBeCloseTo(1, 10);
}
}
});
}
test("screen drag follows a rotated camera with a transformed parent and retains damped motion", () => {
const f = scene(0.5, 1440, 900), drag = createScreenSpaceDrag();
const parent = new THREE.Object3D();
parent.rotation.set(0.8, 1.2, -0.7); parent.add(f.earth);
f.camera.position.set(250, 100, 400); f.camera.lookAt(0, 0, 0); f.camera.updateMatrixWorld();
parent.updateMatrixWorld(true);
const point = f.camera.position.clone().normalize().multiplyScalar(radius);
f.earth.worldToLocal(point);
let previous = f.project(point);
drag.move(0.05, 0.02);
for (let i = 0; i < 12; i++) {
drag.advance(f.earth, f.camera, 0.25);
const current = f.project(point);
expect(current.x).toBeGreaterThan(previous.x);
expect(current.y).toBeGreaterThan(previous.y);
previous = current;
}
drag.stop();
const rotation = f.earth.quaternion.clone();
drag.advance(f.earth, f.camera, 0.25);
expect(f.earth.quaternion.angleTo(rotation)).toBeCloseTo(0, 7);
});
test("repeated horizontal small-globe drags and inertia preserve the existing geographic axis", () => {
const f = scene(1, 1440, 900), drag = createScreenSpaceDrag();
for (const pitch of [0.4, 2.6, -2.7]) {
f.earth.rotation.set(pitch, 18 * Math.PI + 1.8, 0.35);
const north = new THREE.Vector3(0, 1, 0).applyQuaternion(f.earth.quaternion);
for (let step = 0; step < 600; step++) {
drag.move(step % 80 < 40 ? 0.035 : -0.035, 0);
drag.advance(f.earth, f.camera, 0.18);
expect(new THREE.Vector3(0, 1, 0).applyQuaternion(f.earth.quaternion).distanceTo(north)).toBeLessThan(1e-10);
}
drag.stop();
}
});
test("vertical small-globe input deliberately changes tilt", () => {
const f = scene(1, 1440, 900), drag = createScreenSpaceDrag();
const north = new THREE.Vector3(0, 1, 0).applyQuaternion(f.earth.quaternion);
drag.move(0, 0.3); drag.advance(f.earth, f.camera, 1);
expect(new THREE.Vector3(0, 1, 0).applyQuaternion(f.earth.quaternion).distanceTo(north)).toBeGreaterThan(0.1);
});
for (const [width, height] of [[1440, 900], [390, 844]]) for (const zoom of [0.5, 1, 3, 5]) {
test(`${zoom * 100}% at ${width}px: dragging outside the globe keeps rotating in the pointer direction`, () => {
const f = scene(zoom, width, height), drag = createScreenSpaceDrag();
const factor = globeDragRotationFactor(zoom, f.camera, f.viewport, CONFIG);
let previous = f.earth.quaternion.clone();
for (let x = width / 2; x < width * 1.2; x += 10) {
drag.move(10 * factor, 0); drag.advance(f.earth, f.camera, 0.18);
const north = new THREE.Vector3(0, 1, 0).applyQuaternion(previous);
const delta = f.earth.quaternion.clone().multiply(previous.clone().invert());
expect(new THREE.Vector3(delta.x, delta.y, delta.z).dot(north)).toBeGreaterThan(0);
previous.copy(f.earth.quaternion);
}
});
}
test("normal zoom keeps legacy sensitivity while extreme zoom limits magnification", () => {
const normal = scene(1, 1440, 900), large = scene(5, 1440, 900);
expect(globeDragRotationFactor(1, normal.camera, normal.viewport, CONFIG)).toBe(CONFIG.dragRotationFactorBase);
expect(globeDragRotationFactor(5, large.camera, large.viewport, CONFIG)).toBeLessThan(CONFIG.dragRotationFactorBase * CONFIG.dragRotationScaleMin);
});
test("wheel zoom accepts projected markers and legs but not HUD scroll content", async()=>{
const {isGlobeWheelTarget}=await import("./globe-navigation.js");
const canvas={};
expect(isGlobeWheelTarget(canvas,canvas)).toBe(true);
expect(isGlobeWheelTarget({closest:selector=>selector===".map-clusters"?{}:null},canvas)).toBe(true);
expect(isGlobeWheelTarget({closest:()=>null},canvas)).toBe(false);
});