What happens behind a single mouse click
When you click an object in a 3D viewer, a process called "Raycasting" runs. A virtual ray is shot from the mouse position into the scene, and the system checks which object it hits.
The limitation of the default approach
The default approach checks every triangle in a mesh, one by one. 100,000 triangles means 100,000 intersection tests. Every time the mouse moves.
What is BVH?
BVH (Bounding Volume Hierarchy) is a tree data structure designed for efficient spatial queries in 3D space.
How it works
A 3D model is made up of tens or hundreds of thousands of triangles. To find which triangle a ray hits, you'd normally have to check every single one.
BVH wraps these triangles in bounding boxes (AABBs — Axis-Aligned Bounding Boxes) and groups them hierarchically.
[Entire Model] ← Root bounding box
/ \
[Left Half] [Right Half] ← Space split in two
/ \ / \
[△△△] [△△△] [△△△] [△△△] ← Leaf nodes (actual triangles)
If the ray doesn't hit the root box → done, skip the entire model. If the ray only hits the left half → skip the entire right half. At each step, the search space is cut in half.
Split strategy: SAH (Surface Area Heuristic)
The performance of a BVH tree depends on where the space is divided. Voxel3D uses the SAH (Surface Area Heuristic) strategy.
SAH is based on the principle that "the smaller a child node's surface area, the lower the probability a ray will hit it." It evaluates all possible split positions and picks the one that minimizes the overall traversal cost.
Cost = left surface area / parent surface area × left triangle count
+ right surface area / parent surface area × right triangle count
It takes longer to build, but produces the fastest tree for ray traversal. This makes it ideal for 3D viewers where real-time interaction matters.
Other split strategies
| Strategy | Method | Build speed | Query performance |
|---|---|---|---|
| SAH | Optimal split based on surface area | Slow | Best |
| CENTER | Split at triangle centroid | Fast | Average |
| AVERAGE | Split at mean position | Fast | Average |
Where BVH is used
BVH is a fundamental algorithm used across the 3D industry.
- Game engines (Unreal, Unity): Collision detection, occlusion culling
- Ray tracing renderers (NVIDIA RTX): GPU-accelerated BVH for real-time ray tracing
- Physics simulations: Fast collision candidate pair search
- 3D viewers (Voxel3D): Raycasting, cross-section computation, distance measurement
Solving it with BVH
Voxel3D applies this BVH to Raycasting. It divides space into a tree structure, skipping entire regions the ray doesn't pass through.
Default: 100K triangles → 100K tests (O(n))
BVH: 100K triangles → ~17 tests (O(log n))
How it's implemented
- Built on three-mesh-bvh, wrapped in a BVHManager for control
- All mesh raycasting is accelerated automatically, no code changes needed
- Meshes over 500K triangles are auto-filtered for memory protection
- Helper objects like Grid and Gizmo are excluded from BVH generation
Features that run on top of BVH
The real value of BVH goes beyond faster clicks.
Selection (object picking)
BVH tree traversal narrows down candidate triangles, and stops as soon as the first intersection is found. Even on large models, mouse clicks respond instantly.
Clipping Edge (cross-section outlines)
All scene models are merged into a single geometry, and a unified BVH is built. This BVH computes plane-triangle intersections in real time to visualize cross-section outlines.
Measurement (distance calculation)
BVH-based geometric analysis calculates precise distances between two meshes.
BVH (foundation)
├── Selection - firstHitOnly + BVH acceleration
├── Clipping Edge - merged BVH + shapecast
└── Measurement - BVH-based geometric analysis
Without BVH, none of these features could work in real time on large models. Every bit of responsiveness you feel in Voxel3D starts with BVH.