@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
35 lines (34 loc) • 916 B
JavaScript
;
import { Vector3, BackSide } from "three";
import { createRaycaster } from "../../../../RaycastHelper";
export const DIRS = [
new Vector3(1, 0, 0),
new Vector3(-1, 0, 0),
new Vector3(0, 1, 0),
new Vector3(0, -1, 0),
new Vector3(0, 0, 1),
new Vector3(0, 0, -1)
];
const _raycaster = createRaycaster();
export function isPositionInsideMesh(pos, mesh, minDist) {
let numIn = 0;
const bvh = mesh.geometry.boundsTree;
_raycaster.ray.origin.copy(pos);
for (const dir of DIRS) {
_raycaster.ray.direction.copy(dir);
const hit = bvh.raycastFirst(_raycaster.ray, BackSide);
if (hit) {
const { normal, distance } = hit;
if (normal) {
normal.applyMatrix4(mesh.matrixWorld);
if (normal.dot(dir) <= 0) {
numIn++;
}
if (minDist > 0 && distance < minDist) {
return false;
}
}
}
}
return numIn > 4;
}