Skip to content

Introduction

To speed up raycasting and frustum culling, a spatial indexing data structure can be created to contain the bounding boxes of all instances.

This works very well if the instances are mostly static (updating a BVH can be expensive) and scattered in world space.

Setting a margin makes BVH updating faster, but may make raycasting and frustum culling slightly slower.

myBatchedMesh.computeBVH(renderer.coordinateSystem, { margin: 0 }); // margin is optional

It’s necessary to manually update the BVH after its creation with the following methods:

myBatchedMesh.bvh.insert(instanceId);
myBatchedMesh.bvh.insertRange(instanceIdsArray);
myBatchedMesh.bvh.move(instanceId);
myBatchedMesh.bvh.delete(instanceId);
myBatchedMesh.bvh.clear();

Per-instance uniforms (WebGLRenderer only)

Section titled “Per-instance uniforms (WebGLRenderer only)”

Assign unique shader uniforms to each instance, working with every materials.

myBatchedMesh.initUniformsPerInstance({ vertex: { noise: 'float' }, fragment: { metalness: 'float', roughness: 'float', emissive: 'vec3' } });
myBatchedMesh.setUniformAt(index, 'noise', 0.5);
myBatchedMesh.setUniformAt(index, 'emissive', new Color('red'));

Improve rendering performance by dynamically adjusting the detail level of instances based on their distance from the camera.

Use simplified geometries for distant objects to optimize resources.

Currently, only LODs that share the same geometry vertex array can be added. This will improve in the future.

const geometryId = batchedMesh.addGeometry(geometry, -1, reservedIndexCount);
batchedMesh.addGeometryLOD(geometryId, geometryLOD1, distanceLOD1);
batchedMesh.addGeometryLOD(geometryId, geometryLOD2, distanceLOD2);
batchedMesh.addGeometryLOD(geometryId, geometryLOD3, distanceLOD3);