Voxelizing 3D models in a shader?

I’ve created a model voxelizer in Threejs. I’m using the Threejs raycaster
const raycaster = new THREE.Raycaster();
Since voxelizing is quite CPU-intensive, I’d like to do it in a shader at GPU side. Does anyone have experience with raycasting in shaders? Or better yet, has anyone voxelized a model using a shader?

Voxels give me precise knowledge of the position and volume element of a voxel. Why is this desirable? Voxels allow for very precise buoyancy calculations, but also much more accurate collision detection in a shader.

There’s a voxelization demo in three-mesh-bvh that uses box queries to check geometry intersection which should be more precise than a raycaster. Demo here:

Using a BVH helps accelerate the query on the CPU quite a bit but if you’d still like to run this on the GPU then there is already code for packing and uploading the BVH and related geometry to the GPU as a texture so it can be queried in a WebGL shader. Then you can write the result of a voxel out to a pixel and read the texture back to the CPU to get the data. The closest thing to a demo in this regard is the GPU-generated SDF demo:

The only caveat is that currently only raycasts and distance cast functions are implemented via shader. If you’d like the use the more precise box queries via shader then these would have to be added.

7 Likes

I’ll take a closer look at that, thanks gkjohnson.
That looks very good