Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
three-mesh-bvh
Advanced tools
The three-mesh-bvh package is a utility for creating and using Bounding Volume Hierarchies (BVH) to accelerate raycasting, intersection testing, and spatial queries on three.js meshes. It is particularly useful for improving performance in complex scenes with many objects or detailed geometries.
Raycasting
This feature allows you to perform efficient raycasting on a mesh by using a BVH. The code sample demonstrates how to create a BVH for a mesh and override the raycast method to use the accelerated raycasting provided by three-mesh-bvh.
const { MeshBVH, acceleratedRaycast } = require('three-mesh-bvh');
const { Mesh, Raycaster, BoxGeometry, MeshBasicMaterial } = require('three');
// Create a mesh
const geometry = new BoxGeometry(1, 1, 1);
const material = new MeshBasicMaterial({ color: 0x00ff00 });
const mesh = new Mesh(geometry, material);
// Create BVH
mesh.geometry.boundsTree = new MeshBVH(mesh.geometry);
// Override raycast method
mesh.raycast = acceleratedRaycast;
// Perform raycasting
const raycaster = new Raycaster();
raycaster.setFromCamera({ x: 0, y: 0 }, camera);
const intersects = raycaster.intersectObject(mesh);
console.log(intersects);
Intersection Testing
This feature allows you to test for intersections between two geometries using BVH. The code sample demonstrates how to create a BVH for one mesh and check if it intersects with another mesh.
const { MeshBVH, acceleratedRaycast } = require('three-mesh-bvh');
const { Mesh, BoxGeometry, MeshBasicMaterial, Vector3 } = require('three');
// Create two meshes
const geometry1 = new BoxGeometry(1, 1, 1);
const material1 = new MeshBasicMaterial({ color: 0x00ff00 });
const mesh1 = new Mesh(geometry1, material1);
const geometry2 = new BoxGeometry(1, 1, 1);
const material2 = new MeshBasicMaterial({ color: 0xff0000 });
const mesh2 = new Mesh(geometry2, material2);
mesh2.position.set(0.5, 0.5, 0.5);
// Create BVH for the first mesh
mesh1.geometry.boundsTree = new MeshBVH(mesh1.geometry);
// Check for intersection
const intersects = mesh1.geometry.boundsTree.intersectsGeometry(mesh2.geometry, mesh2.matrixWorld);
console.log(intersects);
Spatial Queries
This feature allows you to perform spatial queries, such as checking if a sphere intersects with a mesh, using BVH. The code sample demonstrates how to create a BVH for a mesh and perform a spatial query with a sphere.
const { MeshBVH } = require('three-mesh-bvh');
const { Mesh, BoxGeometry, MeshBasicMaterial, Sphere } = require('three');
// Create a mesh
const geometry = new BoxGeometry(1, 1, 1);
const material = new MeshBasicMaterial({ color: 0x00ff00 });
const mesh = new Mesh(geometry, material);
// Create BVH
mesh.geometry.boundsTree = new MeshBVH(mesh.geometry);
// Define a sphere for spatial query
const sphere = new Sphere(new Vector3(0, 0, 0), 1.5);
// Perform spatial query
const intersects = mesh.geometry.boundsTree.intersectsSphere(sphere);
console.log(intersects);
The three package is the core library for three.js, which provides a wide range of functionalities for 3D graphics rendering. While it includes basic raycasting and intersection testing, it does not provide the optimized BVH structures that three-mesh-bvh offers for performance improvements.
ammo.js is a physics engine that can be used with three.js for collision detection and physics simulations. It provides broad-phase and narrow-phase collision detection but is more complex and heavier compared to the lightweight and specialized BVH implementation in three-mesh-bvh.
cannon-es is a lightweight 3D physics engine for JavaScript. It provides collision detection and physics simulations, similar to ammo.js, but does not specifically focus on BVH for raycasting and spatial queries like three-mesh-bvh.
A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes.
Casting 500 rays against an 80,000 polygon model at 60fps!
Point cloud interesection demo
Using pre-made functions
// Import via ES6 modules
import * as THREE from 'three';
import { computeBoundsTree, disposeBoundsTree, acceleratedRaycast } from 'three-mesh-bvh';
// Or UMD
const { computeBoundsTree, disposeBoundsTree, acceleratedRaycast } = window.MeshBVHLib;
// Add the extension functions
THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;
THREE.Mesh.prototype.raycast = acceleratedRaycast;
// Generate geometry and associated BVH
const geom = new THREE.TorusKnotBufferGeometry( 10, 3, 400, 100 );
const mesh = new THREE.Mesh( geom, material );
geom.computeBoundsTree();
Or manually building the BVH
// Import via ES6 modules
import * as THREE from 'three';
import { MeshBVH, acceleratedRaycast } from 'three-mesh-bvh';
// Or UMD
const { MeshBVH, acceleratedRaycast } = window.MeshBVHLib;
// Add the raycast function. Assumes the BVH is available on
// the `boundsTree` variable
THREE.Mesh.prototype.raycast = acceleratedRaycast;
// ...
// Generate the BVH and use the newly generated index
geom.boundsTree = new MeshBVH( geom );
And then raycasting
// Setting "firstHitOnly" to true means the Mesh.raycast function will use the
// bvh "raycastFirst" function to return a result more quickly.
const raycaster = new THREE.Raycaster();
raycaster.firstHitOnly = true;
raycaster.intersectObjects( [ mesh ] );
import * as THREE from 'three';
import { MeshBVH, acceleratedRaycast } from 'three-mesh-bvh';
let mesh, geometry;
const invMat = new THREE.Matrix4();
// instantiate the geometry
// ...
const bvh = new MeshBVH( geometry );
invMat.copy( mesh.matrixWorld ).invert();
// raycasting
// ensure the ray is in the local space of the geometry being cast against
raycaster.ray.applyMatrix4( invMat );
const hit = bvh.raycastFirst( mesh, raycaster, raycaster.ray );
// spherecasting
// ensure the sphere is in the lcoal space of hte geometry being cast against
sphere.applyMatrix4( invMat );
const intersects = bvh.intersectsSphere( mesh, sphere );
const geometry = new KnotBufferGeometry( 1, 0.5, 40, 10 );
const bvh = new MeshBVH( geometry );
const serialized = MeshBVH.serialize( bvh, geometry );
// ...
const deserializedBVH = MeshBVH.deserialize( serialized, geometry );
geometry.boundsTree = deserializedBVH;
NOTE WebWorker syntax is inconsistently supported across bundlers and sometimes not supported at all so the GenereateMeshBVHWorker class is not exported from the package root. If needed the code from src/worker
can be copied and modified to accomodate a particular build process.
import { GenerateMeshBVHWorker } from 'three-mesh-bvh/src/workers/GenerateMeshBVHWorker.js';
// ...
const geometry = new KnotBufferGeometry( 1, 0.5, 40, 10 );
const worker = new GenerateMeshBVHWorker();
worker.generate( geometry ).then( bvh => {
geometry.boundsTree = bvh;
} );
Option for splitting each BVH node down the center of the longest axis of the bounds.
This is the fastest construction option but will yield a less optimal hierarchy.
Option for splitting each BVH node at the average point along the longest axis for all triangle centroids in the bounds.
Option to use a Surface Area Heuristic to split the bounds optimally.
This is the slowest construction option.
Indicates the shape did not intersect the given bounding box.
Indicates the shape did intersect the given bounding box.
Indicate the shape entirely contains the given bounding box.
The MeshBVH generation process modifies the geometry's index bufferAttribute in place to save memory. The BVH construction will use the geometry's boundingBox if it exists or set it if it does not. The BVH will no longer work correctly if the index buffer is modified.
static serialize( bvh : MeshBVH, geometry : BufferGeometry, copyIndexBuffer = true : Boolean ) : SerializedBVH
Generates a representation of the complete bounds tree and the geometry index buffer which can be used to recreate a bounds tree using the deserialize function. The serialize
and deserialize
functions can be used to generate a MeshBVH asynchronously in a background web worker to prevent the main thread from stuttering.
bvh
is the MeshBVH to be serialized and geometry
is the bufferGeometry used to generate and raycast against using the bvh
.
If copyIndexBuffer
is true then a copy of the geometry.index.array
is made which is slower but useful is the geometry index is intended to be modified.
static deserialize( data : SerializedBVH, geometry : BufferGeometry, setIndex = true : Boolean ) : MeshBVH
Returns a new MeshBVH instance from the serialized data. geometry
is the geometry used to generate the original bvh data
was derived from. If setIndex
is true then the buffer for the geometry.index
attribute is set from the serialized data attribute or created if an index does not exist.
NOTE: In order for the bounds tree to be used for casts the geometry index attribute must be replaced by the data in the SeralizedMeshBVH object.
NOTE: The returned MeshBVH is a fully generated, buffer packed BVH instance to improve memory footprint and uses the same buffers passed in on the data.root
property.
constructor( geometry : BufferGeometry, options : Object )
Constructs the bounds tree for the given geometry and produces a new index attribute buffer. A reference to the passed geometry is retained. The available options are
{
// Which split strategy to use when constructing the BVH.
strategy: CENTER,
// The maximum depth to allow the tree to build to.
// Setting this to a smaller trades raycast speed for better construction
// time and less memory allocation.
maxDepth: 40,
// The number of triangles to aim for in a leaf node.
maxLeafTris: 10,
// If true then the bounding box for the geometry is set once the BVH
// has been constructed.
setBoundingBox: true,
// Print out warnings encountered during tree construction.
verbose: true,
}
NOTE: The geometry's index attribute array is modified in order to build the bounds tree. If the geometry has no index then one is added.
raycast( mesh : Mesh, raycaster : Raycaster, ray : Ray, intersects : Array ) : Array<RaycastHit>
Adds all raycast triangle hits in unsorted order to the intersects
array. It is expected that ray
is in the frame of the mesh being raycast against and that the geometry on mesh
is the same as the one used to generate the bvh.
raycastFirst( mesh : Mesh, raycaster : Raycaster, ray : Ray ) : RaycastHit
Returns the first raycast hit in the model. This is typically much faster than returning all hits.
intersectsSphere( mesh : Mesh, sphere : Sphere ) : Boolean
Returns whether or not the mesh instersects the given sphere.
intersectsBox( mesh : Mesh, box : Box3, boxToBvh : Matrix4 ) : Boolean
Returns whether or not the mesh intersects the given box.
The boxToBvh
parameter is the transform of the box in the meshs frame.
intersectsGeometry( mesh : Mesh, geometry : BufferGeometry, geometryToBvh : Matrix4 ) : Boolean
Returns whether or not the mesh intersects the given geometry.
The geometryToBvh
parameter is the transform of the geometry in the mesh's frame.
Performance improves considerably if the provided geometry also has a boundsTree
.
closestPointToPoint(
mesh : Mesh,
point : Vector3,
target : Vector3,
minThreshold : Number = 0,
maxThreshold : Number = Infinity
) : Number
Returns the closest distance from the point to the mesh and puts the closest point on the mesh in target
.
If a point is found that is closer than minThreshold
then the function will return that result early. Any triangles or points outside of maxThreshold
are ignored.
closestPointToGeometry(
mesh : Mesh,
geometry : BufferGeometry,
geometryToBvh : Matrix4,
target1 : Vector3 = null,
target2 : Vector3 = null,
minThreshold : Number = 0,
maxThreshold : Number = Infinity
) : Number
Returns the closest distance from the geometry to the mesh and puts the closest point on the mesh in target1
and the closest point on the other geometry in target2
in the frame of the BVH.
The geometryToBvh
parameter is the transform of the geometry in the mesh's frame.
If a point is found that is closer than minThreshold
then the function will return that result early. Any triangles or points outside of maxThreshold
are ignored.
shapecast(
mesh : Mesh,
callbacks : {
traverseBoundsOrder : (
box: Box3
) => Number = null,
intersectsBounds : (
box : Box3,
isLeaf : Boolean,
score : Number | undefined,
depth : Number,
nodeIndex : Number
) => NOT_INTERSECTED | INTERSECTED | CONTAINED,
intersectsRange : (
triangleOffset : Number,
triangleCount : Number
contained : Boolean,
depth : Number,
nodeIndex : Number
) => Boolean = null,
intersectsTriangle : (
triangle : Triangle,
triangleIndex : Number,
contained : Boolean,
depth : Number
) => Boolean = null,
}
) : Boolean
A generalized cast function that can be used to implement intersection logic for custom shapes. This is used internally for intersectsBox and intersectsSphere. The function returns as soon as a triangle has been reported as intersected and returns true
if a triangle has been intersected. The bounds are traversed in depth first order calling traverseBoundsOrder
, intersectsBoundsFunc
, intersectsRange
, and intersectsTriangle for each node and using the results to determine traversal depth. The depth
value passed to callbacks indicates the depth of the bounds the provided box or bounds belongs to unless the triangles are indicated to be CONTAINED
, in which case depth is the depth of the parent bounds that were contained. It can be used to precompute, cache, and then read information about a parent bound to improve performance while traversing.
mesh
is the is the object this BVH is representing.
callbacks
is a list of callback functions (defined below) used for traversing the tree. All functions except for intersectsBounds
are optional.
traverseBoundsOrder
takes the axis aligned bounding box representing an internal node local to the bvh and returns a score (often distance) used to determine whether the left or right node should be traversed first. The shape with the lowest score is traversed first.
intersectsBounds
takes the axis aligned bounding box representing an internal node local to the bvh, whether or not the node is a leaf, and the score calculated by orderNodesFunc
, the node depth, and the node index (for use with the refit function) and returns a constant indicating whether or not the bounds is intersected or contained meaning traversal should continue. If CONTAINED
is returned then and optimization is triggered allowing the range and / or triangle intersection callbacks to be run immediately rather than traversing the rest of the child bounds.
intersectsRange
takes a triangle offset and count representing the number of triangles to be iterated over. 1 triangle from this range represents 3 values in the geometry's index buffer. If this function returns true then traversal is stopped and intersectsTriangle
is not called if provided.
intersectsTriangle
takes a triangle and the index buffer indices used by the triangle from the geometry and returns whether or not the triangle has been intersected with. If the triangle is reported to be intersected the traversal ends and the shapecast
function completes. If multiple triangles need to be collected or intersected return false here and push results onto an array. contained
is set to true
if one of the parent bounds was marked as entirely contained in the intersectsBoundsFunc
function.
refit(
traversedNodeIndices : Array<Number> | Set<Number> = null,
endNodeIndices : Array<Number> | Set<Number> = null
) : void
Refit the node bounds to the current triangle positions. This is quicker than regenerating a new BVH but will not be optimal after significant changes to the vertices. traversedNodeIndices
is a set of node indices (provided by the shapecast function) that need to be refit including all internal nodes. endNodeIndices
is the set of nodes that traversal ended at and that triangles need to be updated for. If neither index set is provided then the whole BVH is updated which is significantly slower than surgically updating the nodes that need to be updated.
Here's how to get the set of indices that need to be refit:
const traversedNodeIndices = new Set();
const endNodeIndices = new Set();
bvh.shapecast(
mesh,
{
intersectsBounds: ( box, isLeaf, score, depth, nodeIndex ) => {
if ( /* intersects shape */ ) {
traversedNodeIndices.add( nodeIndex );
return INTERSECTED;
}
return NOT_INTERSECTED;
},
intersectsRange: ( offset, count, contained, depth, nodeIndex ) => {
// collect triangles to update
endNodeIndices.add( nodeIndex );
}
}
);
// update the positions of the triangle vertices
bvh.refit( traversedNodeIndices, endNodeIndices );
getBoundingBox( target : Box3 ) : Box3
Get the bounding box of the geometry computed from the root node bounds of the BVH. Significantly faster than BufferGeometry.computeBoundingBox
.
roots : Array< ArrayBuffer >
index : TypedArray
Displays a view of the bounds tree up to the given depth of the tree.
Note: The visualizer is expected to be a sibling of the mesh being visualized.
depth : Number
The depth to traverse and visualize the tree to.
constructor( mesh: THREE.Mesh, depth = 10 : Number )
Instantiates the helper with a depth and mesh to visualize.
update() : void
Updates the display of the bounds tree in the case that the bounds tree has changed or the depth parameter has changed.
firstHitOnly = false : Boolean
The the Raycaster
member firstHitOnly
is set to true then the .acceleratedRaycast function will call the .raycastFirst function to retrieve hits which is generally faster.
computeBoundsTree( options : Object ) : void
A pre-made BufferGeometry extension function that builds a new BVH, assigns it to boundsTree
, and applies the new index buffer to the geometry. Comparable to computeBoundingBox
and computeBoundingSphere
.
THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
disposeBoundsTree() : void
A BufferGeometry extension function that disposes of the BVH.
THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;
acceleratedRaycast( ... )
An accelerated raycast function with the same signature as THREE.Mesh.raycast
. Uses the BVH for raycasting if it's available otherwise it falls back to the built-in approach.
If the raycaster object being used has a property firstHitOnly
set to true
, then the raycasting will terminate as soon as it finds the closest intersection to the ray's origin and return only that intersection. This is typically several times faster than searching for all intersections.
THREE.Mesh.prototype.raycast = acceleratedRaycast;
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.
running : Boolean;
Flag indicating whether or not a BVH is already being generated in the worker.
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. This function will throw an error if it is already running.
terminate() : Boolean;
Terminates the worker.
estimateMemoryInBytes( bvh : MeshBVH ) : Number
Roughly estimates the amount of memory in bytes a BVH is using.
getBVHExtremes( bvh : MeshBVH ) : Array< Object >
Measures the min and max extremes of the tree including node depth, leaf triangle count, and number of splits on different axes to show how well a tree is structured. Returns an array of extremes for each group root for the bvh. The objects are structured like so:
{
depth: { min: Number, max: Number },
tris: { min: Number, max: Number },
splits: [ Number, Number, Number ]
}
List of functions stored in the src/workers/
and are not exported via index.js because they require extra effort to integrate with some build processes. UMD variants of these functions are not provided.
generateAsync( geometry : BufferGeometry, options : Object ) : Promise<MeshBVH>
Generates a BVH for the given geometry in a WebWorker so it can be created asynchronously. A Promise is returned that resolves with the generated BVH. During the generation the geometry.attributes.position
array and geometry.index
array (if it exists) are transferred to the worker so the geometry will not be usable until the BVH generation is complete and the arrays are transferred back.
BufferGeometry
, but an index will
be produced and retained as a side effect of the construction.BufferGeometry.center()
before creating the BVH if the geometry is sufficiently large or off center.[0.4.0] - 2021-06-11
MeshBVH.refit
function to refit the bounds to modified vertices.setBoundingBox
MeshBVH construction option.MeshBVH.getBoundingBox
function.intersectsRange
callback option to MeshBVH.shapecast
.src/worker/generateAsync.js
function. Use GenerateMeshBVHWorker
instead.type: module
in package.json to enable use of es6 modules in node.sideEffects: false
to package.json.MeshBVH.shapecast
to take an object of callback functions instead of a list of function arguments and the triangle intersection callback has been changed to take a single triangle index. See README for new API. Calls using the old function will log a warning.MeshBVHVisualizer
not using the new geometry BVH if one was generated.MeshBVHVisualizer
not using the new mesh if it was set.intersectsTriangleFunc
to MeshBVH.shapecast
could throw an error.FAQs
A BVH implementation to speed up raycasting against three.js meshes.
The npm package three-mesh-bvh receives a total of 222,822 weekly downloads. As such, three-mesh-bvh popularity was classified as popular.
We found that three-mesh-bvh demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.