DataTexture3D Generation (three-mesh-bvh)

Hi community!

Next experiment with the awesome Three-mesh-bvh plugin from @gkjohnson :beers:

This time I used it as a generator of DataTexture3D, based on the spatial data of a mesh.
And it’s quite fast :fire: :metal:

Picture:

“Model” - just the transparent mesh of the dragon
“Points” - visualisation of texture data, where value is 1
“3d grid” - the using of the texture to show only those parts of lines of 3d grid, that are cross 3d texture with the value of 1, otherwise “discard”.

Demo: https://3nkp6.csb.app

Source code: nostalgic-sun-3nkp6 - CodeSandbox

6 Likes

Looks very cool, I have no idea what’s going on though :smiley:

Mind explaining a bit?

2 Likes

@Usnul

(sigh) I would like to have your ability to explain things :sweat_smile:

  1. Having a mesh, build a Box3 object from it.

  2. Having that box and using Raycaster, in the triple loop (x, y, z), cast rays, where ray’s origin is the current position inside the box in accordance to values of the loop (x, y, z), and ray’s direction is a random normalized vector.
    2.1. Check the result of raycast against the mesh. The same way, described here: Sand Nefertiti (three-mesh-bvh) (first hit + dot-product of raycaster’s ray and mesh’s normal). If we’re inside of the mesh, push 1 into data array, otherwise push 0.

  3. Having filled data array, create DataTexture3D object.

  4. Having that 3D texture, do/create cool/awesome stuff :slight_smile: I used this texture with 3d grid of lines, discarding the parts, that is not in the model. I modified the example, added one more uniform to cancel the discarding. When you set noDiscard to true, you’ll see full cubic grid of magenta. Set it to false, and lines will remain only inside of the dragon.

2 Likes

Ha, sounds pretty cool! Might do some SDF (signed distance field) generation with a little more effort, and that can be used for particle effects and other cools stuff. Or perhaps just ray-march the 3d texture for… reasons? I’m not really all that aware of raymarching applications.

In a nutshell, you wrote a mesh voxelizer, right?

1 Like

Sort of that :slight_smile:

The first thing that comes in my head is this example: three.js examples

1 Like

Demo with volume, based on the official example "volume / cloud": https://bywyt.csb.app/

2 Likes

This is great! I’ve been wanting to make something like an SDF texture with the bvh at some point using the the closestPointToPoint function to find the distance to the mesh surface but I haven’t found the time. Like @usnul mentioned you could do particle physics effects or other GPU-based animations. I bet you could find something fun to do with it!

1 Like

That’s exactly my thought on the evolving of the things :beers:

1 Like