UNPKG

three-mesh-bvh

Version:

A BVH implementation to speed up raycasting against three.js meshes.

53 lines (33 loc) 1.66 kB
import { intersectRay } from '../utils/intersectUtils.js'; import { IS_LEAF, OFFSET, COUNT, LEFT_NODE, RIGHT_NODE } from '../utils/nodeBufferUtils.js'; import { BufferStack } from '../utils/BufferStack.js'; import '../utils/iterationUtils.generated.js'; import { intersectTris_indirect } from '../utils/iterationUtils_indirect.generated.js'; /******************************************************/ /* This file is generated from "raycast.template.js". */ /******************************************************/ function raycast_indirect( bvh, root, materialOrSide, ray, intersects, near, far ) { BufferStack.setBuffer( bvh._roots[ root ] ); _raycast( 0, bvh, materialOrSide, ray, intersects, near, far ); BufferStack.clearBuffer(); } function _raycast( nodeIndex32, bvh, materialOrSide, ray, intersects, near, far ) { const { float32Array, uint16Array, uint32Array } = BufferStack; const nodeIndex16 = nodeIndex32 * 2; const isLeaf = IS_LEAF( nodeIndex16, uint16Array ); if ( isLeaf ) { const offset = OFFSET( nodeIndex32, uint32Array ); const count = COUNT( nodeIndex16, uint16Array ); intersectTris_indirect( bvh, materialOrSide, ray, offset, count, intersects, near, far ); } else { const leftIndex = LEFT_NODE( nodeIndex32 ); if ( intersectRay( leftIndex, float32Array, ray, near, far ) ) { _raycast( leftIndex, bvh, materialOrSide, ray, intersects, near, far ); } const rightIndex = RIGHT_NODE( nodeIndex32, uint32Array ); if ( intersectRay( rightIndex, float32Array, ray, near, far ) ) { _raycast( rightIndex, bvh, materialOrSide, ray, intersects, near, far ); } } } export { raycast_indirect };