# three-mesh-bvh

```js
import { ... } from 'three-mesh-bvh';
```

### Classes

- [BVH](/tools/docs/three-mesh-bvh/BVH.md): Abstract base class for BVH implementations. Provides core tree traversal and spatial query methods. Subclasses implement primitive-specific logic by overriding `writePrimitiveBounds` and related internal methods.
- [BVHHelper](/tools/docs/three-mesh-bvh/BVHHelper.md): A `THREE.Group` that visualizes a BVH as wireframe bounding boxes or solid face overlays. Attach it as a sibling of the mesh in the scene graph and call `update()` whenever the mesh's BVH or world transform changes.
- [ExtendedTriangle](/tools/docs/three-mesh-bvh/ExtendedTriangle.md): An extended version of three.js' Triangle class. A variety of derivative values are cached on the object to accelerate the intersection functions. `.needsUpdate` must be set to true when modifying the triangle parameters.
- [FloatVertexAttributeTexture](/tools/docs/three-mesh-bvh/FloatVertexAttributeTexture.md): A VertexAttributeTexture that forces the float texture type.
- [GenerateMeshBVHWorker](/tools/docs/three-mesh-bvh/GenerateMeshBVHWorker.md): Helper class for generating a MeshBVH for a given geometry in asynchronously in a worker. The geometry position and index buffer attribute `ArrayBuffers` are transferred to the Worker while the BVH is being generated meaning the geometry will be unavailable to use while the BVH is being processed unless `SharedArrayBuffers` are used. They will be automatically replaced when the MeshBVH is finished generating.
- [GeometryBVH](/tools/docs/three-mesh-bvh/GeometryBVH.md): Abstract base class for geometry-backed BVH implementations. Handles geometry indexing, indirect mode, and bounding box initialization. Subclasses implement primitive-specific bounds computation and raycasting via `writePrimitiveBounds` and `raycastObject3D`.
- [IntVertexAttributeTexture](/tools/docs/three-mesh-bvh/IntVertexAttributeTexture.md): A VertexAttributeTexture that forces the signed integer texture type.
- [LineBVH](/tools/docs/three-mesh-bvh/LineBVH.md): BVH for `THREE.Line` geometries. Like `LineLoopBVH` but excludes the final closing segment so the open line is accurately represented.
- [LineLoopBVH](/tools/docs/three-mesh-bvh/LineLoopBVH.md): BVH for `THREE.LineLoop` geometries. Forces indirect mode since the loop structure requires that the index buffer remain unmodified.
- [LineSegmentsBVH](/tools/docs/three-mesh-bvh/LineSegmentsBVH.md): BVH for `THREE.LineSegments` geometries. Each BVH primitive represents one line segment (two consecutive vertices).
- [MeshBVH](/tools/docs/three-mesh-bvh/MeshBVH.md): The MeshBVH generation process modifies the geometry's index bufferAttribute in place to save memory. The BVH construction will use the geometry's boundingBox if it exists or set it if it does not. The BVH will no longer work correctly if the index buffer is modified.
- [MeshBVHUniformStruct](/tools/docs/three-mesh-bvh/MeshBVHUniformStruct.md): A shader uniform object corresponding to the `BVH` shader struct defined in shaderStructs. The object contains four textures containing information about the BVH and geometry so it can be queried in a shader using the bvh intersection functions defined in shaderFunctions. This object is intended to be used as a shader uniform and read in the shader as a `BVH` struct.
- [ObjectBVH](/tools/docs/three-mesh-bvh/ObjectBVH.md): BVH built from a scene hierarchy rather than a single geometry. Each leaf holds one Object3D (or one instance of an InstancedMesh/BatchedMesh), enabling accelerated raycasting and spatial queries across many objects at once.
- [OrientedBox](/tools/docs/three-mesh-bvh/OrientedBox.md): An oriented version of three.js' Box3 class. A variety of derivative values are cached on the object to accelerate the intersection functions. `.needsUpdate` must be set to true when modifying the box parameters.
- [ParallelMeshBVHWorker](/tools/docs/three-mesh-bvh/ParallelMeshBVHWorker.md): A drop-in replacement for `GenerateMeshBVHWorker` that distributes BVH construction across multiple Web Workers in parallel for faster builds on large geometry. Requires `SharedArrayBuffer` support (cross-origin isolated context). Falls back to a single-threaded `GenerateMeshBVHWorker` automatically if `SharedArrayBuffer` is unavailable.
- [PointsBVH](/tools/docs/three-mesh-bvh/PointsBVH.md): BVH for `THREE.Points` geometries. Each BVH primitive represents a single point.
- [SkinnedMeshBVH](/tools/docs/three-mesh-bvh/SkinnedMeshBVH.md): BVH for `SkinnedMesh` objects. Computes primitive bounds using `SkinnedMesh.getVertexPosition` so the tree reflects the current posed state of the mesh. Call `refit()` after updating the skeleton to keep bounds accurate.
- [StaticGeometryGenerator](/tools/docs/three-mesh-bvh/StaticGeometryGenerator.md): A utility class for taking a set of SkinnedMeshes or morph target geometry and baking it into a single, static geometry that a BVH can be generated for.
- [UIntVertexAttributeTexture](/tools/docs/three-mesh-bvh/UIntVertexAttributeTexture.md): A VertexAttributeTexture that forces the unsigned integer texture type.
- [VertexAttributeTexture](/tools/docs/three-mesh-bvh/VertexAttributeTexture.md): Float, Uint, and Int VertexAttributeTexture implementations are designed to simplify the efficient packing of a three.js BufferAttribute into a texture. An instance can be treated as a texture and when passing as a uniform to a shader they should be used as a `sampler2d`, `usampler2d`, and `isampler2d` when using the Float, Uint, and Int texture types respectively.

### Functions

- [acceleratedRaycast](/tools/docs/three-mesh-bvh/functions.md): An accelerated raycast function with the same signature as `THREE.Mesh.raycast`. Uses the BVH for raycasting if it's available otherwise it falls back to the built-in approach. The results of the function are designed to be identical to the results of the conventional `THREE.Mesh.raycast` results.
- [computeBatchedBoundsTree](/tools/docs/three-mesh-bvh/functions.md): Equivalent of `computeBoundsTree` for `BatchedMesh`. Creates the `BatchedMesh.boundsTrees` array if it does not exist. If `index` is `-1` BVHs for all available geometries are generated and the full array is returned; otherwise only the BVH at that geometry index is generated and returned.
- [computeBoundsTree](/tools/docs/three-mesh-bvh/functions.md): A pre-made BufferGeometry extension function that builds a new BVH, assigns it to `boundsTree` for BufferGeometry, and applies the new index buffer to the geometry. Comparable to `computeBoundingBox` and `computeBoundingSphere`.
- [disposeBatchedBoundsTree](/tools/docs/three-mesh-bvh/functions.md): Equivalent of `disposeBoundsTree` for `BatchedMesh`. Sets entries in `BatchedMesh.boundsTrees` to `null`. If `index` is `-1` all BVHs are disposed; otherwise only the BVH at that geometry index is disposed.
- [disposeBoundsTree](/tools/docs/three-mesh-bvh/functions.md): A BufferGeometry extension function that disposes of the BVH.
- [estimateMemoryInBytes](/tools/docs/three-mesh-bvh/functions.md): Roughly estimates the amount of memory in bytes used by a BVH by walking its object graph and summing typed-array byte lengths and primitive sizes.
- [getBVHExtremes](/tools/docs/three-mesh-bvh/functions.md): Measures the min and max extremes of the BVH tree structure, including node depth, leaf primitive count, split axis distribution, and a surface-area heuristic score. Returns one entry per root group in the BVH.
- [getJSONStructure](/tools/docs/three-mesh-bvh/functions.md): Returns a plain-object tree that mirrors the BVH hierarchy, useful for inspecting or serialising the structure for debugging. Each node has a `bounds` (`Box3`) and either `{ count, offset }` (leaf) or `{ left, right }` (internal) fields.
- [getTriangleHitPointInfo](/tools/docs/three-mesh-bvh/functions.md): Computes hit-point information for a point on a triangle within a `BufferGeometry`. Returns the face vertex indices, face normal, material index, UV coordinates, and barycentric coordinates. Useful for retrieving detailed hit data after a call to `MeshBVH.closestPointToPoint` or `MeshBVH.closestPointToGeometry`.
- [validateBounds](/tools/docs/three-mesh-bvh/functions.md): Validates that every node's bounding box fully contains its children and, for leaf nodes, fully contains all of its primitives. Uses `console.assert` to log failures and returns `false` if any check fails.

### Constants

- [AVERAGE](/tools/docs/three-mesh-bvh/constants.md): Option for splitting each BVH node at the average point along the longest axis for all triangle centroids in the bounds.
- [bvh_distance_functions](/tools/docs/three-mesh-bvh/constants.md): Set of shader functions used for interacting with the packed BVH in a shader and sampling VertexAttributeTextures. Provides distance query functions. See [src/webgl/glsl](https://github.com/gkjohnson/three-mesh-bvh/tree/master/src/webgl/glsl) for full implementations and declarations.
- [bvh_ray_functions](/tools/docs/three-mesh-bvh/constants.md): Set of shader functions used for interacting with the packed BVH in a shader and sampling VertexAttributeTextures. Provides ray intersection functions. See [src/webgl/glsl](https://github.com/gkjohnson/three-mesh-bvh/tree/master/src/webgl/glsl) for full implementations and declarations.
- [bvh_struct_definitions](/tools/docs/three-mesh-bvh/constants.md): Set of shader structs and defined constants used for interacting with the packed BVH in a shader. See [src/webgl/glsl/bvh_struct_definitions.glsl.js](https://github.com/gkjohnson/three-mesh-bvh/blob/master/src/webgl/glsl/bvh_struct_definitions.glsl.js) for full implementations and declarations.
- [CENTER](/tools/docs/three-mesh-bvh/constants.md): Option for splitting each BVH node down the center of the longest axis of the bounds.
- [common_functions](/tools/docs/three-mesh-bvh/constants.md): Set of shader functions used for interacting with the packed BVH in a shader and sampling VertexAttributeTextures. Provides common utility functions including `texelFetch1D`. See [src/webgl/glsl](https://github.com/gkjohnson/three-mesh-bvh/tree/master/src/webgl/glsl) for full implementations and declarations.
- [CONTAINED](/tools/docs/three-mesh-bvh/constants.md): Indicate the shape entirely contains the given bounding box.
- [INTERSECTED](/tools/docs/three-mesh-bvh/constants.md): Indicates the shape did intersect the given bounding box.
- [NOT_INTERSECTED](/tools/docs/three-mesh-bvh/constants.md): Indicates the shape did not intersect the given bounding box.
- [SAH](/tools/docs/three-mesh-bvh/constants.md): Option to use a Surface Area Heuristic to split the bounds more optimally. This SAH implementation tests 32 discrete splits in each node along each axis to determine which split is the lowest cost.

### Types

- [BVHExtremes](/tools/docs/three-mesh-bvh/functions.md)
- [HitPointInfo](/tools/docs/three-mesh-bvh/HitPointInfo.md)
- [HitTriangleInfo](/tools/docs/three-mesh-bvh/HitTriangleInfo.md)
- [SerializedBVH](/tools/docs/three-mesh-bvh/SerializedBVH.md): Plain-object representation of a `MeshBVH` produced by `MeshBVH.serialize` and consumed by `MeshBVH.deserialize`. Suitable for transfer across WebWorker boundaries or storage, with optional buffer sharing via `SharedArrayBuffer`.
