three-mesh-bvh
Version:
A BVH implementation to speed up raycasting against three.js meshes.
58 lines (33 loc) • 1.15 kB
JavaScript
import { intersectTri } from './ThreeRayIntersectUtilities.js';
export function intersectTris( geo, side, ray, offset, count, intersections ) {
for ( let i = offset, end = offset + count; i < end; i ++ ) {
intersectTri( geo, side, ray, i, intersections );
}
}
export function intersectClosestTri( geo, side, ray, offset, count ) {
let dist = Infinity;
let res = null;
for ( let i = offset, end = offset + count; i < end; i ++ ) {
const intersection = intersectTri( geo, side, ray, i );
if ( intersection && intersection.distance < dist ) {
res = intersection;
dist = intersection.distance;
}
}
return res;
}
// converts the given BVH raycast intersection to align with the three.js raycast
// structure (include object, world space distance and point).
export function convertRaycastIntersect( hit, object, raycaster ) {
if ( hit === null ) {
return null;
}
hit.point.applyMatrix4( object.matrixWorld );
hit.distance = hit.point.distanceTo( raycaster.ray.origin );
hit.object = object;
if ( hit.distance < raycaster.near || hit.distance > raycaster.far ) {
return null;
} else {
return hit;
}
}