@three.ez/batched-mesh-extensions
Version:
Utility extension methods for BatchedMesh
155 lines • 6.21 kB
JavaScript
import { box3ToArray, BVH, HybridBuilder, vec3ToArray, WebGLCoordinateSystem, WebGPUCoordinateSystem } from 'bvh.js';
import { Box3, Matrix4 } from 'three';
// TODO implement getBBoxFromBSphere (add property to geometryInfo)
// TODO implement frustumCullingLOD?
/**
* Class to manage BVH (Bounding Volume Hierarchy) for `BatchedMesh`.
* Provides methods for managing bounding volumes, frustum culling, raycasting, and bounding box computation.
*/
export class BatchedMeshBVH {
/**
* @param target The target `BatchedMesh`.
* @param margin The margin applied for bounding box calculations (default is 0).
* @param accurateCulling Flag to enable accurate frustum culling without considering margin (default is true).
*/
constructor(target, coordinateSystem, margin = 0, accurateCulling = true) {
/**
* A map that stores the BVH nodes for each instance.
*/
this.nodesMap = new Map();
this._origin = new Float32Array(3);
this._dir = new Float32Array(3);
this._cameraPos = new Float32Array(3);
this._boxArray = new Float32Array(6);
this.target = target;
this.accurateCulling = accurateCulling;
this._margin = margin;
this.bvh = new BVH(new HybridBuilder(), coordinateSystem === 2000 ? WebGLCoordinateSystem : WebGPUCoordinateSystem); // TODO fix in BVH.js
}
/**
* Builds the BVH from the target mesh's instances using a top-down construction method.
* This approach is more efficient and accurate compared to incremental methods, which add one instance at a time.
*/
create() {
const count = this.target.instanceCount;
const instancesArrayCount = this.target._instanceInfo.length; // TODO this may change.. don't like it too much
const instancesInfo = this.target._instanceInfo;
const boxes = new Array(count); // test if single array and recreation inside node creation is faster due to memory location
const objects = new Uint32Array(count);
let index = 0;
this.clear();
for (let i = 0; i < instancesArrayCount; i++) {
if (!instancesInfo[i].active)
continue;
boxes[index] = this.getBox(i, new Float32Array(6));
objects[index] = i;
index++;
}
this.bvh.createFromArray(objects, boxes, (node) => {
this.nodesMap.set(node.object, node);
}, this._margin);
}
/**
* Inserts an instance into the BVH.
* @param id The id of the instance to insert.
*/
insert(id) {
const node = this.bvh.insert(id, this.getBox(id, new Float32Array(6)), this._margin);
this.nodesMap.set(id, node);
}
/**
* Inserts a range of instances into the BVH.
* @param ids An array of ids to insert.
*/
insertRange(ids) {
const count = ids.length;
const boxes = new Array(count);
for (let i = 0; i < count; i++) {
boxes[i] = this.getBox(ids[i], new Float32Array(6));
}
this.bvh.insertRange(ids, boxes, this._margin, (node) => {
this.nodesMap.set(node.object, node);
});
}
/**
* Moves an instance within the BVH.
* @param id The id of the instance to move.
*/
move(id) {
const node = this.nodesMap.get(id);
if (!node)
return;
this.getBox(id, node.box); // this also updates box
this.bvh.move(node, this._margin);
}
/**
* Deletes an instance from the BVH.
* @param id The id of the instance to delete.
*/
delete(id) {
const node = this.nodesMap.get(id);
if (!node)
return;
this.bvh.delete(node);
this.nodesMap.delete(id);
}
/**
* Clears the BVH.
*/
clear() {
this.bvh.clear();
this.nodesMap.clear();
}
/**
* Performs frustum culling to determine which instances are visible based on the provided projection matrix.
* @param projScreenMatrix The projection screen matrix for frustum culling.
* @param onFrustumIntersection Callback function invoked when an instance intersects the frustum.
*/
frustumCulling(projScreenMatrix, onFrustumIntersection) {
if (this._margin > 0 && this.accurateCulling) {
this.bvh.frustumCulling(projScreenMatrix.elements, (node, frustum, mask) => {
if (frustum.isIntersectedMargin(node.box, mask, this._margin)) {
onFrustumIntersection(node);
}
});
}
else {
this.bvh.frustumCulling(projScreenMatrix.elements, onFrustumIntersection);
}
}
/**
* Performs raycasting to check if a ray intersects any instances.
* @param raycaster The raycaster used for raycasting.
* @param onIntersection Callback function invoked when a ray intersects an instance.
*/
raycast(raycaster, onIntersection) {
const ray = raycaster.ray;
const origin = this._origin;
const dir = this._dir;
vec3ToArray(ray.origin, origin);
vec3ToArray(ray.direction, dir);
// TODO should we add margin check? maybe is not worth it
this.bvh.rayIntersections(dir, origin, onIntersection, raycaster.near, raycaster.far);
}
/**
* Checks if a given box intersects with any instance bounding box.
* @param target The target bounding box.
* @param onIntersection Callback function invoked when an intersection occurs.
* @returns `True` if there is an intersection, otherwise `false`.
*/
intersectBox(target, onIntersection) {
const array = this._boxArray;
box3ToArray(target, array);
return this.bvh.intersectsBox(array, onIntersection);
}
getBox(id, array) {
const target = this.target;
const geometryId = target._instanceInfo[id].geometryIndex;
target.getBoundingBoxAt(geometryId, _box3).applyMatrix4(target.getMatrixAt(id, _matrix));
box3ToArray(_box3, array);
return array;
}
}
const _box3 = new Box3();
const _matrix = new Matrix4();
//# sourceMappingURL=BatchedMeshBVH.js.map