three-mesh-bvh
Version:
A BVH implementation to speed up raycasting against three.js meshes.
52 lines (32 loc) • 850 B
JavaScript
// Returns a Float32Array representing the bounds data for box.
export function boxToArray( bx ) {
const arr = new Float32Array( 6 );
arr[ 0 ] = bx.min.x;
arr[ 1 ] = bx.min.y;
arr[ 2 ] = bx.min.z;
arr[ 3 ] = bx.max.x;
arr[ 4 ] = bx.max.y;
arr[ 5 ] = bx.max.z;
return arr;
}
export function arrayToBox( arr, target ) {
target.min.x = arr[ 0 ];
target.min.y = arr[ 1 ];
target.min.z = arr[ 2 ];
target.max.x = arr[ 3 ];
target.max.y = arr[ 4 ];
target.max.z = arr[ 5 ];
return target;
}
export function getLongestEdgeIndex( bounds ) {
let splitDimIdx = - 1;
let splitDist = - Infinity;
for ( let i = 0; i < 3; i ++ ) {
const dist = bounds[ i + 3 ] - bounds[ i ];
if ( dist > splitDist ) {
splitDist = dist;
splitDimIdx = i;
}
}
return splitDimIdx;
}