Three-mesh-bvh: A plugin for fast geometry raycasting and spatial queries!

I appreciate the interest but if you need help or have questions on how to use something please make a dedicated help thread or a Github issue with appropriate context.

3 Likes

Three-mesh-bvh v0.7.5 has just been released which focuses primarily on even more raycast performance improvements thanks to @agargaro! Specifically you should see up to 15% or more perf improvement with from the removal of an array → box conversion. And when setting the raycaster near / far fields the BVH traversal now ignores bounds outside that range which speeds things up even further. I massively appreciate the help on the project.

You can try the adjusted near / far field behavior in the raycast demo.

10 Likes

The next version of three-mesh-bvh has just been released with some more great changes from @agargaro! The MeshBVH class now allows for specifying sub ranges of the geometry which means they can be used for generating a new BVH for individual sub-geometries in a BatchedMesh geometry.

Release notes here.

BatchedMesh Raycasting

BatchedMesh raycasting demo here

And new functions for initializing BatchedMesh geometry bounds trees to accelerate raycasting:

8 Likes

I’m blown away by this plugin. Its enabled my project to actually be feasible. Is an update to work with Three.js 0.17+ a possibility in the future? Either way, thanks so much!!

1 Like

Is there something that’s not working? If so then please make an issue at the repo and we can see if we can get it fixed.

Ignore that! Works perfectly! super stoked.

1 Like

@Ayush_Ranjan_Sharma - this is not a help thread. Please make a new thread or a submit an issue at the repo.

Version 0.9.2 of the project has been released! This version brings some performance improvements for tree construction and, more excitingly, new support for queries using WebGPU compute shaders by importing from three-mesh-bvh/webgpu! This is the first step towards functionality like compute shader-based pathtracing, tree construction, and hopefully a lot more! Using WebGPU compute shaders alone brings a meaningful performance improvement over WebGL.

So thanks to @Attila_Schroeder and user “TheBlek” on Github you can now perform raycasting and distance queries with WebGPU! See the changelog here for more details.

You can see the compute shader ray tracing demo here. And an SDF generation (distance query) demo here.

9 Likes

This is a game changer.

3 Likes

The latest v0.9.5 release brings support for new geometry types, including Lines, LineLoops, LineSegments, and Points. All new BVH types support the same underlying “build” settings in addition to accelerated raycast and shapecast function support. It took quite a bit of refactoring but it’s been on my list of features to support for awhile. Now all your line and point queries can be fast, as well :rocket:

The Line intersection demo can be seen here and point cloud one here.

Hopefully some more custom BVH types coming soon :sparkles:

Point cloud & Line geometry BVHs with 2 million points and 1 million lines respectively
5 Likes

That link no longer works, is there an update?

These examples seem to not work for me (macOS 26 Safari):

Those ones are working very nicely! Is there some WebGPU feature used in the other ones Safari is not supporting yet perhaps?

That link no longer works, is there an update?

It’s moved here: three-edge-projection

These examples seem to not work for me (macOS 26 Safari):

There are some non-standard WebGPU shader behavior used that are not working in Safari and Firefox at the moment, which needs to be fixed.

1 Like

Already at version 0.9.7! The last two versions bring a couple new features for the new Line and Points BVHs; including “refit” and “bvhcast” support. But primarily I’ve worked on some new examples demonstrating how the new BVH abstractions can be used to write custom BVH classes for other geometries or objects. The examples below show new BVH classes for SkinnedMeshes and Scenes with BatchedMeshes, InstancedMeshes, and other renderable objects but you can imagine support for ray-marched SDF shapes & Gaussian Splats, as well.

The new abstractions will be changing a bit as development continues but these new additions should bring a lot of new capabilities to the project moving forward.

Skinned Mesh BVH

There was already a SkinnedMesh example in the project but it previously required converting the SkinnedMesh geometry to a static Mesh in order to construct a MeshBVH. With a dedicated SkinnedMeshBVH the geometry can be used directly, resulting in an over 2x performance improvement for BVH generation and refitting. It will work for SkinnedMesh geometry as well as Meshes with morph target geometry. See the demo here.

SkinnedMesh with BVH being refit every frame.

Scene BVH

One thing I’ve wanted to add for while is a “Scene BVH” that allows for constructing a scene-wide acceleration structure that can easily be used for raycasting and spatial queries for all items in the scene, including Batched and InstancedMesh instances. Due to the nature of the BVH construction the hierarchy structure must be unchanging (though you can refit if something moves), but now building a scene or sub-hierarchy acceleration structure can be as easy as writing new StaticSceneBVH( object ).

I’ve been experimenting in a prototype repository for now so you can check out the demos at the link, but I will probably plan to move them into the main project soon.

Scene Raycast

This is a scene constructed from Meshes, BatchedMesh instances, and InstancedMesh instances. Each mesh and instance are included in the same BVH, accelerating raycasting by over 10x.

Character Physics

A scene BVH can help split up a scene to accelerate collision detection & physics. Previously models would have to be merged into a single geometry to gain any benefits from MeshBVH but now a higher-level scene-wide BVH can be used as a first pass before using granular BVHs for triangle testing.

Frustum Culling

This example shows a BatchedMesh BVH being used for frustum culling of 500,000 instances, bringing the framerate from 30fps to 120fps.
6 Likes

Anyone, stop this man! (or maybe not) :laughing:

Can’t wait to test scene raycast. So this mean we don’t need “acrobatic” usage of BufferGeometryUtils.mergeGeometries for a full scene ? Frustrum culling also really complement it well.
Thanks again for your amazing work!

3 Likes

Jure Triglav has recently shared his work using WebGPU compute, three.js, and three-mesh-bvh to implement surfel-based GI. I’ll share some screenshots below but the article is worth a read.

7 Likes

Semver is so beautiful :heart_eyes:

This blog has some of the best visualizations ever. Thanks for sharing this.