extendsWorkerBase
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.
NOTE It's best to reuse a single instance of this class to avoid the overhead of instantiating a new Worker.
import { AmbientLight, DirectionalLight, Mesh, MeshStandardMaterial, TorusKnotGeometry } from 'three';
import { BVHHelper } from 'three-mesh-bvh';
import { GenerateMeshBVHWorker } from 'three-mesh-bvh/worker';
// scene, camera and renderer are initialized here
const light = new DirectionalLight( 0xffffff, 3 );
light.position.set( 1, 1, 1 );
scene.add( light, new AmbientLight( 0xb0bec5, 1.5 ) );
const geometry = new TorusKnotGeometry( 1, 0.3, 500, 500 );
const worker = new GenerateMeshBVHWorker();
geometry.boundsTree = await worker.generate( geometry );
const mesh = new Mesh( geometry, new MeshStandardMaterial( { color: 0x4db6ac, roughness: 0.75 } ) );
const helper = new BVHHelper( mesh, 10 );
helper.traverse( object => object.frustumCulled = false );
scene.add( mesh, helper );On this page
Properties
running: boolean
Flag indicating whether or not a BVH is already being generated in the worker.
Methods
generate(
geometry: BufferGeometry,
// Same options accepted by the `MeshBVH` constructor.
options?: {
// Callback invoked with a `[0, 1]` progress value as the BVH is built.
onProgress?: function
}
): Promise<MeshBVH>dispose(): voidTerminates the worker.