docsby gkjohnson
Ray tracing a mesh's BVH in a compute shader
On this page

Methods

new BVHComputeData(
	// Scene objects to include. A single item or array of Object3D, BufferGeometry, or GeometryBVH instances are all accepted and wrapped automatically in a BVH.
	objects: Object3D | BufferGeometry | GeometryBVH | Array,
	options?: {
		// Type map for the interleaved per-vertex attribute buffer. Keys are geometry attribute names; values are `'f32'`, `'vec2f'`, `'vec3f'` or `'vec4f'`, or one of the packed types: `'oct16'` stores a direction and the sign of w in one word (normals, tangents) and always unpacks to a `vec4f` unit vector with w = ±1, `'2x16float'` stores two half floats in one word (uvs) and unpacks to a `vec2f`, and `'4x16float'` stores four half floats in two words (colors) and unpacks to a `vec4f`.
		attributes: Record<string, string> = { position: 'vec4f' },

		// When true, a MeshBVH is automatically built for any object that does not already have `geometry.boundsTree` set, using the `SAH` split strategy for faster GPU traversal.
		autogenerateBvh: boolean = true
	}
)
getRootObject(): Object3D
getShapecastFn(
	options: {
		// Function name. Defaults to a random identifier.
		name?: string,

		// TSL struct or definition describing the query shape.
		shapeStruct: StructTypeNode,

		// TSL struct for the accumulated result, or null.
		resultStruct?: StructTypeNode | null,

		// function node that runs before the bvh traversal - useful for resetting or initializing necessary module variables.
		prefixFn?: function | null,

		// function node controlling left/right child traversal order.
		boundsOrderFn?: function | null,

		// function node testing the shape against a BVH node's bounds.
		intersectsBoundsFn: function,

		// function node testing a leaf triangle range, called with the shape, the range's triangle offset and count, the "indexOffset" and "attributesOffset" of the geometry being traversed to pass to `fns.getTriangleIndices` and `fns.getVertexPosition`, and the result. Returns `NOT_INTERSECTED` (0), `INTERSECTED` (1), or `STOP_TRAVERSAL` (3, a hit that ends the entire traversal after running `transformResultFn` and `resetShapeFn`). Boolean returns remain supported: false misses and true hits without stopping.
		intersectRangeFn: function,

		// function node that transforms the shape into object local space. Returns whether to traverse the object. When returning false the shape and any per-object state must be left untouched, since `resetShapeFn` is not called for a skipped object.
		transformShapeFn?: function | null,

		// function node that transforms a hit result back to world space.
		transformResultFn?: function | null,

		// function node called after each BLAS traversal to reset any per-object state set by `transformShapeFn`.
		resetShapeFn?: function | null
	}
): function

update

async
async update(): Promise<void>
updateTransforms(): void
writeObjectRecord(
	// The object, its instance index and group, and the index of its record, as in `ObjectInfo`.
	info: Object,

	// Matrix pre-multiplied onto the object's world matrix (usually the inverse TLAS root matrix).
	premultiplyMatrix: Matrix4,

	// Index of the record to write into.
	writeOffset: number,

	// Destination buffer.
	targetBuffer: ArrayBuffer
): void
getBVH(
	// The object to generate a BVH for.
	object: Object3D,

	// Instance index (relevant for InstancedMesh / BatchedMesh).
	instanceId: number,

	// Populated with the object's geometry range. Every field must be written.
	rangeTarget: Object
): MeshBVH | SkinnedMeshBVH | null | Promise<(MeshBVH|SkinnedMeshBVH|null)>
getObjectInfo(
	// The mesh.
	object: Object3D,

	// Instance index (relevant for InstancedMesh / BatchedMesh).
	instanceId: number = 0,

	// Index of the BVH root, one per geometry group and each packed as its own object.
	group: number = 0
): ObjectInfo | null
getAllObjectInfo(): Array<ObjectInfo>
getDefaultAttributeValue(
	// Attribute name (e.g. `'position'`, `'normal'`).
	key: string,

	// Receives the default value.
	target: Vector4
): Vector4
dispose(): void