gkjohnson / three-mesh-bvh

A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes.
https://gkjohnson.github.io/three-mesh-bvh/example/bundle/raycast.html
MIT License
2.5k stars 259 forks source link

Add utility for generating geometry from a vertex shader #420

Open gkjohnson opened 2 years ago

gkjohnson commented 2 years ago

See https://twitter.com/edankwan/status/1518294689400725507

chasedavis commented 1 year ago

I was trying to implement Bvh via drei's abstractions and realized that it may not work with a vertex shader driving transformations on the mesh I'm trying to compute bounds for. After some snooping I saw that this issue might be tracking it already. Can I ask if that is the case or if maybe I am going about it wrong?

https://user-images.githubusercontent.com/10068755/216519777-04568bdc-6297-488e-a9c7-aa9a2f842bce.mp4

Basically in my r3f scene I'm shaping it like this demo one:

  useHelper(ref, MeshBVHVisualizer)
...
      <Bvh>
        <Rays>
          <mesh raycast={() => null} ref={ref}>
            <planeGeometry />
            <CustomShaderMaterial />          <---- driving the mesh deformation, scale, and rotation in vertex shader here
          </mesh>
        </Rays>
      </Bvh>

This project is looking really amazing, so hats off @gkjohnson! Is there another approach I can take where three-mesh-bvh is compatible with custom vertex shader in the scene?

gkjohnson commented 1 year ago

Is there another approach I can take where three-mesh-bvh is compatible with custom vertex shader in the scene?

Unfortunately no - the BVH is built is and queried on the CPU so any vertex transformation dynamically generated on the GPU are inaccessible on the CPU unless its been written to a buffer and transferred back which is what this issue suggests. The only other alternative is to perform all your vertex transformations on the CPU, instead.

If you'd like to work on a class that enables this as a contribution I'd be happy to provide guidance.

This project is looking really amazing, so hats off @gkjohnson!

Thank you!

CodyJasonBennett commented 3 months ago

This is very easy with and possibly the optimal use-case for transform feedback, which captures the primitive assembly of the vertex shader. Can either stay on the GPU like I do here, or be readback onto the CPU. Needs a patch to three to access GPU buffers https://github.com/mrdoob/three.js/pull/26777, or use of GLBufferAttribute.

https://github.com/CodyJasonBennett/gpu-culling