31 lines
1.2 KiB
JavaScript
31 lines
1.2 KiB
JavaScript
import * as THREE from "three";
|
|
|
|
const center = new THREE.Vector3();
|
|
const axis = new THREE.Vector3();
|
|
const point = new THREE.Vector3();
|
|
const view = new THREE.Vector3();
|
|
|
|
// A cable may wrap across the limb while its center is behind the globe.
|
|
// Keep such geometry in the broad phase, then reject individual hidden hits.
|
|
export function intersectVisibleCables(raycaster, cables, earth, camera, radius) {
|
|
if (!earth || !camera) return [];
|
|
center.setFromMatrixPosition(earth.matrixWorld);
|
|
axis.copy(camera.position).sub(center);
|
|
const distance = axis.length();
|
|
if (!distance) return [];
|
|
axis.divideScalar(distance);
|
|
const horizon = radius * radius / distance;
|
|
const candidates = cables.filter(cable => {
|
|
if (!cable.visible) return false;
|
|
const bounds = cable.geometry?.boundingSphere;
|
|
if (!bounds) return true;
|
|
point.copy(bounds.center).applyMatrix4(cable.matrixWorld).sub(center);
|
|
return point.dot(axis) + bounds.radius * cable.matrixWorld.getMaxScaleOnAxis() >= horizon;
|
|
});
|
|
return raycaster.intersectObjects(candidates).filter(hit => {
|
|
point.copy(hit.point).sub(center).normalize();
|
|
view.copy(camera.position).sub(hit.point).normalize();
|
|
return point.dot(view) >= -0.001;
|
|
});
|
|
}
|