# GenerateMeshBVHWorker

class in `three-mesh-bvh`, extends `WorkerBase`

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

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._

Example: Building a BVH for half a million triangles off the main thread

```js
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 );
```

## Properties

### .running: boolean

Flag indicating whether or not a BVH is already being generated in the worker.

## Methods

### .generate

```js
.generate( geometry: BufferGeometry, options?: Object ): Promise<MeshBVH>
```

Generates a `MeshBVH` instance for the given geometry with the given options in a WebWorker.
Returns a Promise that resolves with the generated `MeshBVH`. Throws if already running.

- `geometry`, `BufferGeometry`
- `options`, `Object`, optional: Same options accepted by the `MeshBVH` constructor.
  - `onProgress`, `function`, optional: Callback invoked with a `[0, 1]`
  progress value as the BVH is built.

### .dispose

```js
.dispose(  )
```

Terminates the worker.
