mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
feat(globe): implement the engine on Navara
src/globe/navara builds the GlobeEngine on a Navara 0.2.1 ThreeView. The engine keeps the camera pose in src/geo values and pushes it to Navara's setCamera in degrees (Cesium's heading/pitch/roll convention, S4); whatever Navara moves itself (user input, its flyTo arc) is read back from the drawn camera each frame. Projection, picks against the ellipsoid and the view rectangle are geo math on that pose, with Navara's fov kept on Cesium's 60° across the wider side. - flights: Navara flyTo in ms with mapped easings, cancelFlight via an empty setCamera, user input cancels a flight as in Cesium - events: moveStart/moveEnd (500 ms still)/changed from the pose - render: forceUpdate and animation (render on demand, S5) - surface: S2 CPU ray over 3D-tile meshes, Navara terrain fallback - maps: OSM/Esri raster, Re:Earth or ion quantized-mesh terrain, Google 3D tiles with a browser key and Google's logo - input: DOM pointer events with ScreenSpaceEventHandler semantics - the three private Navara members used live in internals.js, pinned to 0.2.1 by a test index.js builds the product view (MSAA 4, native pixel ratio, preserved drawing buffer, effects=minimal) for a dynamic import. The contract QA script gains --engine=navara on a dev-only harness page: 46/46 contract and 4/4 Navara checks; Cesium stays 53/53.
This commit is contained in:
1 parent
b6ac0e9c99
commit
90c8e7763a
19 files changed
+3311
No files matched your search
@@ -0,0 +1,132 @@
|
||||
// Screen input for the Navara engine, from DOM pointer events on Navara's
|
||||
// canvas, with Cesium's ScreenSpaceEventHandler semantics: left button only,
|
||||
// a click is a down and up within 5 px, positions are CSS px from the
|
||||
// canvas's top-left corner, and a listener with a modifier fires only while
|
||||
// that key is held (shift over ctrl over alt), one without only while none is.
|
||||
//
|
||||
// Camera inputs map to Navara's controller flags (view.camera.options, S4).
|
||||
// Navara 0.2.1 lets a flyTo run on under user input, where Cesium cancels it;
|
||||
// `onCameraInput` reports user camera input so the engine can do the same.
|
||||
|
||||
import { INPUT_TYPES, MODIFIERS } from '../contract.js';
|
||||
|
||||
const CLICK_PIXEL_TOLERANCE = 5; // Cesium's _clickPixelTolerance
|
||||
const SCREEN_EVENTS = ['pointerdown', 'pointerup', 'pointermove', 'dblclick'];
|
||||
const CAMERA_EVENTS = ['pointerdown', 'wheel', 'touchstart'];
|
||||
|
||||
function modifierOf(event) {
|
||||
if (event.shiftKey) return 'shift';
|
||||
if (event.ctrlKey) return 'ctrl';
|
||||
if (event.altKey) return 'alt';
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{ canvas: HTMLCanvasElement, camera: { options: object } }} view
|
||||
* @param {{ onCameraInput?: (type: string) => void }} [options]
|
||||
* @returns {{ input: object, destroy: () => void }}
|
||||
*/
|
||||
export function createNavaraInput(view, { onCameraInput = () => {} } = {}) {
|
||||
const { canvas } = view;
|
||||
const listeners = new Map(INPUT_TYPES.map((type) => [type, new Set()]));
|
||||
let attached = false;
|
||||
let destroyed = false;
|
||||
let cameraInputs = true;
|
||||
let downAt = null;
|
||||
let last = null;
|
||||
|
||||
const positionOf = (event) => {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
return { x: event.clientX - rect.left, y: event.clientY - rect.top };
|
||||
};
|
||||
const emit = (type, event, payload) => {
|
||||
const modifier = modifierOf(event);
|
||||
for (const entry of [...listeners.get(type)])
|
||||
if (entry.modifier === modifier && listeners.get(type).has(entry))
|
||||
entry.listener(payload);
|
||||
};
|
||||
|
||||
const onScreenEvent = (event) => {
|
||||
const position = positionOf(event);
|
||||
switch (event.type) {
|
||||
case 'pointerdown':
|
||||
if (event.button !== 0) break;
|
||||
downAt = position;
|
||||
emit('down', event, { position });
|
||||
break;
|
||||
case 'pointerup': {
|
||||
if (event.button !== 0) break;
|
||||
emit('up', event, { position });
|
||||
const start = downAt;
|
||||
downAt = null;
|
||||
if (
|
||||
start &&
|
||||
Math.hypot(position.x - start.x, position.y - start.y) <
|
||||
CLICK_PIXEL_TOLERANCE
|
||||
)
|
||||
emit('click', event, { position });
|
||||
break;
|
||||
}
|
||||
case 'pointermove':
|
||||
emit('move', event, {
|
||||
startPosition: last ?? position,
|
||||
endPosition: position,
|
||||
});
|
||||
break;
|
||||
case 'dblclick':
|
||||
emit('doubleClick', event, { position });
|
||||
break;
|
||||
}
|
||||
last = position;
|
||||
};
|
||||
const onCameraEvent = (event) => {
|
||||
if (cameraInputs) onCameraInput(event.type);
|
||||
};
|
||||
for (const type of CAMERA_EVENTS)
|
||||
canvas.addEventListener(type, onCameraEvent, {
|
||||
capture: true,
|
||||
passive: true,
|
||||
});
|
||||
|
||||
const input = {
|
||||
on(type, listener, { modifier } = {}) {
|
||||
if (!INPUT_TYPES.includes(type))
|
||||
throw new TypeError(`Unknown input type: ${type}`);
|
||||
if (modifier !== undefined && !MODIFIERS.includes(modifier))
|
||||
throw new TypeError(`Unknown input modifier: ${modifier}`);
|
||||
if (destroyed) return () => {};
|
||||
if (!attached) {
|
||||
attached = true;
|
||||
for (const name of SCREEN_EVENTS)
|
||||
canvas.addEventListener(name, onScreenEvent);
|
||||
}
|
||||
const entry = { listener, modifier };
|
||||
listeners.get(type).add(entry);
|
||||
return () => {
|
||||
listeners.get(type).delete(entry);
|
||||
};
|
||||
},
|
||||
setCameraInputs(enabled) {
|
||||
cameraInputs = Boolean(enabled);
|
||||
view.camera.options = {
|
||||
enableSpin: cameraInputs,
|
||||
enableZoom: cameraInputs,
|
||||
enableTilt: cameraInputs,
|
||||
};
|
||||
},
|
||||
};
|
||||
return {
|
||||
input,
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
destroyed = true;
|
||||
for (const type of CAMERA_EVENTS)
|
||||
canvas.removeEventListener(type, onCameraEvent, { capture: true });
|
||||
if (attached)
|
||||
for (const name of SCREEN_EVENTS)
|
||||
canvas.removeEventListener(name, onScreenEvent);
|
||||
attached = false;
|
||||
for (const set of listeners.values()) set.clear();
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user