Skip to content

1) How to build a BVH

Andrea Gargaro edited this page Sep 3, 2024 · 1 revision

How to build a BVH

Create BVH object

Instantiate a BVH using the only builder currently available: HybridBuilder

import { BVH, HybridBuilder, WebGLCoordinateSystem } from 'bvh.js';

const highPrecision = false; // 'false': float32array / 'true': float64array
const coordinateSystem = WebGLCoordinateSystem; // or WebGPUCoordinateSystem

const bvh = new BVH( new HybridBuilder( highPrecision ), coordinateSystem );

Build a BVH

BVH nodes are created using an object and its AABB.

It can be built with two methods:

createFromArray

This method builds a BVH in a top down way, faster and with higher quality than using insert or insertRange.

const objects = [ obj1, obj2 ];
const boxes = [ box1, box2 ]; // box1 and box2 are 'float32array' or 'float64array';

bvh.createFromArray( objects, boxes );

⚠️ The construction is always performed from scratch. ⚠️

insert or insertRange

This method inserts nodes one at a time incrementally, slower and with lower quality than createFromArray, but it doesn't need to rebuild BVH from scratch, so it's perfect for dynamic objects.

bvh.insertRange( [ obj1, obj2 ], [ box1, box2 ] );

bvh.insert( object, box );

Hybrid approach

TODO

Clone this wiki locally