Files
planet/frontend/public/earth/js/interactive-picking.js
T
linkong b3d2d9e9f1
ci / backend (push) Canceled after 0s
ci / frontend (push) Canceled after 0s
ci / delivery (push) Canceled after 0s
release: bump version to 0.81.0
2026-11-30 19:39:29 +08:00

130 lines
4.2 KiB
JavaScript

import { isSurfaceAnchorVisible } from "./surface-marker-occlusion.js";
// Shared screen-pick ordering for surface icon layers.
// Smaller screen distance wins; the priority only resolves a true overlap.
export const INTERACTIVE_PICK_PRIORITY = Object.freeze({
earth_interactable: 50,
compute_center: 40,
bgp: 30,
bgp_collector: 20,
vessel: 10,
});
export function pickNearestInteractiveHit(groups = []) {
const candidates = [];
groups.forEach(({ type, hits = [] }) => {
hits.forEach((hit) => {
if (!hit?.object || hit.cluster) return;
candidates.push({
type,
hit,
object: hit.object,
distancePxSq: Number.isFinite(hit.distancePxSq) ? hit.distancePxSq : Infinity,
priority: INTERACTIVE_PICK_PRIORITY[type] || 0,
});
});
});
candidates.sort((left, right) =>
left.distancePxSq - right.distancePxSq || right.priority - left.priority,
);
return candidates[0] || null;
}
export function pickNearestPointCandidate(candidates = []) {
return candidates
.filter(candidate => candidate?.object && Number.isFinite(candidate.distancePxSq))
.sort((left, right) => left.distancePxSq - right.distancePxSq)[0] || null;
}
/**
* Project local-space satellites into screen space with inline matrix math so
* off-target points reject early without per-satellite allocations or occlusion
* math, then run ray-sphere Earth occlusion only on hits inside radiusPx.
*/
export function pickVisibleSatellitesInScreenRadius({
positions,
count = positions?.length || 0,
localToClipElements,
cameraLocal,
occlusionRadius,
rect,
targetX,
targetY,
radiusPx,
isCategoryVisible,
} = {}) {
if (
!Array.isArray(positions) ||
count <= 0 ||
!localToClipElements ||
!cameraLocal ||
!rect ||
rect.width <= 0 ||
rect.height <= 0 ||
radiusPx <= 0
) {
return [];
}
const m0 = localToClipElements[0], m1 = localToClipElements[1], m2 = localToClipElements[2], m3 = localToClipElements[3];
const m4 = localToClipElements[4], m5 = localToClipElements[5], m6 = localToClipElements[6], m7 = localToClipElements[7];
const m8 = localToClipElements[8], m9 = localToClipElements[9], m10 = localToClipElements[10], m11 = localToClipElements[11];
const m12 = localToClipElements[12], m13 = localToClipElements[13], m14 = localToClipElements[14], m15 = localToClipElements[15];
const halfWidth = rect.width * 0.5;
const halfHeight = rect.height * 0.5;
const cursorNdcX = ((targetX - rect.left) / rect.width) * 2 - 1;
const cursorNdcY = 1 - ((targetY - rect.top) / rect.height) * 2;
const ndcRadiusX = (radiusPx / rect.width) * 2;
const ndcRadiusY = (radiusPx / rect.height) * 2;
const minNdcX = cursorNdcX - ndcRadiusX;
const maxNdcX = cursorNdcX + ndcRadiusX;
const minNdcY = cursorNdcY - ndcRadiusY;
const maxNdcY = cursorNdcY + ndcRadiusY;
const radiusSq = radiusPx * radiusPx;
const limit = Math.min(count, positions.length);
const hits = [];
for (let index = 0; index < limit; index += 1) {
if (isCategoryVisible && !isCategoryVisible(index)) continue;
const position = positions[index]?.current;
if (!position) continue;
const px = position.x;
const py = position.y;
const pz = position.z;
if (px === 0 && py === 0 && pz === 0) continue;
const w = m3 * px + m7 * py + m11 * pz + m15;
if (w <= 0) continue;
const invW = 1 / w;
const ndcX = (m0 * px + m4 * py + m8 * pz + m12) * invW;
if (ndcX < minNdcX || ndcX > maxNdcX) continue;
const ndcY = (m1 * px + m5 * py + m9 * pz + m13) * invW;
if (ndcY < minNdcY || ndcY > maxNdcY) continue;
const ndcZ = (m2 * px + m6 * py + m10 * pz + m14) * invW;
if (ndcZ < -1 || ndcZ > 1) continue;
const dx = (ndcX - cursorNdcX) * halfWidth;
const dy = (cursorNdcY - ndcY) * halfHeight;
const distanceSq = dx * dx + dy * dy;
if (distanceSq > radiusSq) continue;
if (!isSurfaceAnchorVisible(position, cameraLocal, occlusionRadius)) continue;
hits.push({
index,
distanceSq,
ndcZ,
screenX: rect.left + (ndcX * 0.5 + 0.5) * rect.width,
screenY: rect.top + (-ndcY * 0.5 + 0.5) * rect.height,
});
}
hits.sort((a, b) => a.distanceSq - b.distanceSq || a.ndcZ - b.ndcZ);
return hits;
}