three-mesh-bvh
Version:
A BVH implementation to speed up raycasting against three.js meshes.
84 lines (60 loc) • 2.13 kB
JavaScript
export const common_functions = /* glsl */`
// A stack of uint32 indices can can store the indices for
// a perfectly balanced tree with a depth up to 31. Lower stack
// depth gets higher performance.
//
// However not all trees are balanced. Best value to set this to
// is the trees max depth.
// Utilities
uvec4 uTexelFetch1D( usampler2D tex, uint index ) {
uint width = uint( textureSize( tex, 0 ).x );
uvec2 uv;
uv.x = index % width;
uv.y = index / width;
return texelFetch( tex, ivec2( uv ), 0 );
}
ivec4 iTexelFetch1D( isampler2D tex, uint index ) {
uint width = uint( textureSize( tex, 0 ).x );
uvec2 uv;
uv.x = index % width;
uv.y = index / width;
return texelFetch( tex, ivec2( uv ), 0 );
}
vec4 texelFetch1D( sampler2D tex, uint index ) {
uint width = uint( textureSize( tex, 0 ).x );
uvec2 uv;
uv.x = index % width;
uv.y = index / width;
return texelFetch( tex, ivec2( uv ), 0 );
}
vec4 textureSampleBarycoord( sampler2D tex, vec3 barycoord, uvec3 faceIndices ) {
return
barycoord.x * texelFetch1D( tex, faceIndices.x ) +
barycoord.y * texelFetch1D( tex, faceIndices.y ) +
barycoord.z * texelFetch1D( tex, faceIndices.z );
}
void ndcToCameraRay(
vec2 coord, mat4 cameraWorld, mat4 invProjectionMatrix,
out vec3 rayOrigin, out vec3 rayDirection
) {
// get camera look direction and near plane for camera clipping
vec4 lookDirection = cameraWorld * vec4( 0.0, 0.0, - 1.0, 0.0 );
vec4 nearVector = invProjectionMatrix * vec4( 0.0, 0.0, - 1.0, 1.0 );
float near = abs( nearVector.z / nearVector.w );
// get the camera direction and position from camera matrices
vec4 origin = cameraWorld * vec4( 0.0, 0.0, 0.0, 1.0 );
vec4 direction = invProjectionMatrix * vec4( coord, 0.5, 1.0 );
direction /= direction.w;
direction = cameraWorld * direction - origin;
// slide the origin along the ray until it sits at the near clip plane position
origin.xyz += direction.xyz * near / dot( direction, lookDirection );
rayOrigin = origin.xyz;
rayDirection = direction.xyz;
}
`;