Files
gods-eye-view/src/globe/cesium/picker.js
T
German Khokhlov 2213be6566 Draw movers and lines on Cesium and require them of every engine
Layers are to draw through the globe contract on both engines, so Cesium
gets back the movers and lines removed as unused:

- engine.movers draws sprite sets as a BillboardCollection (atlas cells
  cropped once per set), point sets as a PointPrimitiveCollection and
  model sets as one Model per instance. depthTest, disableWithinM, order,
  scaleByDistance, colorBlend, unlit and minimumPixelSize follow the
  contract; a model's matrix is written in place, so instances no longer
  share one scratch matrix.
- engine.lines draws each line as a polyline Primitive with width in CSS
  px, geodesic arcs, dashes, the tactical and arrow materials,
  depthFailAlpha and modelMatrix; a rebuild keeps its show, matrix,
  colour and place in order.
- The picker returns the pickId a layer wrote last, skips sets that are
  not pickable and instances behind the horizon, and falls through to
  what lies under them. Surface samples can exclude mover sets again.

movers and lines move from the optional capabilities into the required
contract shape, and the contract suite always runs their checks.
2026-11-07 01:24:59 +04:00

88 lines
3.2 KiB
JavaScript

// GlobePicker on a Cesium scene. Pick results keep Cesium's `id`,
// `primitive` and `content` (3D tile content, read by surface-pick checks);
// the untouched Cesium result rides along as `object` for Cesium-only callers.
// A pick of an engine mover or line (`identify`) names its owner and
// instance and returns the pickId the layer gave it as `id`, as on Navara.
import { toCesium2, toGeo3 } from './convert.js';
/**
* @param {Cesium.Viewer} viewer
* @param {{
* identify?: (picked: object) => ({ owner: string, instance?: number, id: any, pickable?: boolean, visible?: boolean } | null),
* }} [options]
* `identify` names the mover set or line (and instance) a pick belongs to,
* with its pickId; `pickable: false` (a set made without pickable) and
* `visible: false` (an instance behind the horizon, which Cesium's GPU
* pick sees through the globe when drawn without a depth test) drop it.
* @returns {{ picker: object, destroy: () => void }}
*/
export function createCesiumPicker(viewer, { identify = () => null } = {}) {
const normalise = (picked) => {
if (picked == null) return undefined;
// The follow camera's proxy entity (follow.js) is not an object.
if (picked.id?.gevFollowProxy) return undefined;
const known = identify(picked);
if (known && (known.pickable === false || known.visible === false))
return undefined;
return {
id: known ? known.id : picked.id,
primitive: picked.primitive,
content: picked.content,
object: picked,
...(known
? {
owner: known.owner,
...(known.instance !== undefined
? { instance: known.instance }
: {}),
}
: {}),
};
};
const { scene } = viewer;
const drill = (screen, limit, width, height) =>
scene
.drillPick(toCesium2(screen), limit, width, height)
.map(normalise)
.filter(Boolean);
const picker = {
pick(screen, width, height) {
const top = scene.pick(toCesium2(screen), width, height);
if (top == null) return undefined;
const picked = normalise(top);
if (picked) return picked;
// The top result is filtered (the follow proxy, an unpickable or
// hidden mover): what lies under it, the first with an id (an
// object), else the first kept (map tiles).
const under = drill(screen, undefined, width, height);
return under.find((entry) => entry.id !== undefined) ?? under[0];
},
drillPick(screen, limit, width, height) {
return drill(screen, limit, width, height);
},
pickPosition(screen) {
if (!scene.pickPositionSupported) return undefined;
try {
// A new Cesium.Cartesian2 per call: Cesium caches by its toString().
return toGeo3(scene.pickPosition(toCesium2(screen)));
} catch {
return undefined;
}
},
get pickPositionSupported() {
return Boolean(scene.pickPositionSupported);
},
pickGlobe(screen) {
if (!scene.globe) return undefined;
try {
const ray = viewer.camera.getPickRay(toCesium2(screen));
return ray ? toGeo3(scene.globe.pick(ray, scene)) : undefined;
} catch {
return undefined;
}
},
};
return { picker, destroy() {} };
}