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

@gkjohnson Cheers! Looking forward very much to the next release.
I couldn’t work out a way to reduce the “intensity” of the shadows, so I ended up boosting the ambient light when rendering. Must admit I still couldn’t work out quite how it worked, but was blown away by the results. Shame we don’t have AI noise reduction in the browser yet, but I suppose it’s only a matter of time :slight_smile:

1 Like

couldn’t work out a way to reduce the “intensity” of the shadows

The throughput *= 1.0 / PI; is what attenuates the intensity after a light bounce for the Lambertian model. You can change it to fit what you want stylistically but I believe 1 / PI is what is physically correct and the reason shadows and corners get dark after multiple bounces.

Shame we don’t have AI noise reduction in the browser yet, but I suppose it’s only a matter of time

There’s probably some kind of noise reduction that can be done without AI but that’s beyond the scope of the small demo, though. Possible a future project.

It certainly looks correct :slight_smile:
It’s obviously the lighting pass which it missing.
I’ll try the cpu version, which I can see includes lighting.
I just couldn’t get the workers to function from the demo.

1 Like

Version 0.5.1 of three-mesh-bvh has been released! As Peter found early the newest release includes some helpers and shader functions for raycasting against the BVH in a shader which can be used for things like path tracing, runtime lightmap generation, and other gpgpu applications! The release also includes new typescript definition files.

Here’s the latest demo for path tracing on the GPU which renders a simple Lambertian material. Perhaps at some point I’ll create a more robust gpu path tracing project:

For those interested the BVH and associated geometry vertex buffer attributes are packed into the textures so they can be sampled in the fragment shader. The shader coder for performing a raycast query is pretty simple, as well. Check out the example code for a full demonstration of how set the shader up! Looking forward to seeing what people make!


Looks awesome. Whats the best way to use this to create a cross section through terrain like:

I found this example but by the looks the lines will come out in a random order and I will have to reorder them which could be tricky: three-mesh-bvh/edgeIntersect.js at fbb6b43452ad0a02d494b629673bc9e09e4e211f · gkjohnson/three-mesh-bvh · GitHub

It’s best to come up with an algorithm for achieving the effect you’re looking for without the BVH first. With or without the BVH the results for your chart will have to be sorted based on the domain of the overhead lines you’ve drawn for the cross section chart. Once that’s been figured out you can use the BVH to accelerate the generation of the cross section.

The “clipped edges” or “selection” examples should provide a good start for how to the BVH and “shapecast” function to accelerate the intersections.


@gjix Here is the SO answer, evolved from another one, where contours made from sorted line segments.

1 Like

Thanks. SO answer has this:

function getNearestPointIndex(point, points){
  let index = 0;
  for (let i = 0; i < points.length; i++){
    let p = points[i];
    if (p.checked == false && p.equals(point, tolerance)){ 
      index = i;
  return index;

which would be very slow for a large number of points but Im guessing I could use BVH raycast to point cloud example to find the coincident points to greatly speed this up but it means creating a new point cloud with each point becoming a face. Is there a better way?

I’ve put together a separate repo with a demo Lambertian GPU Path Tracer using three-mesh-bvh that includes support for basic material properties, textures, an environment map, tiled renderer, and mroe. Check out the project page here!

And live demo here:

“Interior Scene” model by Allay Design

Neko Stop Diorama model by Art by Kidd

Perseverance Rover model by NASA / JPL-Caltech

Sculpture scans model by Threedscans


I had to add a demo with Lego models using the LDrawLoader, as well :grin:

All LDraw Lego models are from this LDraw Model Repository. Enjoy!


WOW, amazing, how did you render it like this!!!

Thanks! It’s using a technique called “raytracing” or “path tracing”. There are some resources at the bottom of the README in the three-gpu-pathtracing repo.


@gkjohnson I want to thank you for this great lib.

I try to use three-mesh-bvh to solve my problem.

I have a mesh A ( plan + noise ) and multiple meshes colliding with Mesh A.

I want to distort Mesh A where it collides with the other meshes.

Currently, I created a custom shader that uses the BoundingBox of the meshes to distort the mesh A as shown in the screenshot.

my question is, Can I use three-mesh-bvh instead of the BoundingBox to distort Mesh A precisely?


Yes you can use the “shapecast” function to determine which triangles are hit, save the indices of the triangles, and then adjust the vertices accordingly. The sculpting example does something similar if you’d like to take a look there. Just keep in mind that the BVH must be rebuilt or adjusted after the vertices have been moved so the bounds include the shifted vertices.

Here’s the function in the sculpting example that finds and adjusts the vertices.

1 Like

Thanks @gkjohnson for your help.

The three-gpu-pathtracer project built on three-mesh-bvh has just had it’s initial release! It now supports a new slew of material properties including transmission, specular, metalness, and more! There’s still a lot more progress to be made. I made a separate thread here with more project details -



v0.5.10 of three-mesh-bvh has just been released! This version adds a utility called StaticGeometryGenerator for baking and merging skinned and morph target meshes into static geometry so a BVH can be built. Pathtraced skinned geometry coming soon? :eyes:

Demo here: Skinned Mesh BVH Demo



I’ve just added a new demo that demonstrates how to generate projected, triangle-clipped geometry edges onto a plane to produce a line geometry - accelerated using the BVH! Each potential edge is clipped by the triangles above it to produce the visible portion that would be seen from above. This could be useful for generating 2d, line vectorized versions of geometry for things like mechanical drawings or object footprints for floor plans. Using the BVH here speeds up the search for triangles by multiple orders of magnitude to keep things updating quickly!

Here’s the demo!

And of course some screenshots:


Unbelievable! I’m speechless. :drooling_face:

That is so dope!