Awesome
MeshToSDF
Convert a mesh to an signed distance field for the VFX Graph in realtime.
See the MeshToSDF/Demo.unity
scene to see how to use.
How to use
- Drag the MeshToSDF prefab into your scene.
- Either set a mesh for the
Mesh
field or set aSkinnedMeshRenderer
for theSkinned Mesh Renderer
field - Enter play-mode and set the offset and scale such that the mesh is placed within the SDF where you want it to be. Copy these values into edit-mode
- Outputs:
- VFX graph output - set the
Vfx Output
field to a VFX graph and theVfx Property
to an exposed Texture3D parameter of the VFX graph - Material output - same as vfx graph output, but with a material. There's a
Slice Texture 3D
material in theEditor
folder that can be used to debug the SDF. Put it on a plane and put it in to the Material output property to see a slice of the SDF. - Script output - the SDF is available on the
outputRenderTexture
field of the component. The distance is stored in a RGBAFloat texture, in the RGB channels. Note that if you update theoffset
orscale
orsdfResolution
fields in a build, you also have to setmeshToSdfComponent.fieldsChanged = true
- VFX graph output - set the
How it works
- Convert the triangle mesh into voxels
- There are many "correct" ways to do this, for instance by iterating over the voxels that each triangle might intersect with and testing if it does intersect with any of them.
- But it's faster to just sample a bunch of quasi-randomly distributed points on each triangle and marking them as filled in the voxels texture, hoping we sample enough to get a good surface. I use the R_2 sequence for this.
- This would also be possible with a geometery or tesselation shader to split the triangles to be below the voxel resolution followed by marking the location of each vertex in the voxel texture as filled
- Flood fill the voxel texture using Jump Flood Assignment. This creates a voroni diagram with each voxel acting as a seed, AKA an unsigned distance field of the voxels.
- Subtract some constant from the unsigned distance field to thicken the surface a little bit.
Limitations & Improvements to make
- Currently SDFs are hollow, however the VFX treats all SDFs as hollow anyway.
- The same sample count is used for all triangles, but smaller triangles can get away with fewer samples.
- Should benchmark to see if a dynamic length loop that samples & writes to fewer locations is faster than fixed length, unrolled by the compiler loop that samples and writes to too many.
- Since we know the vertex normals, we could write this into the voxels and have the vfx graph use the flood-filled SDF normals, rather than recomputing them per particle per timestep. However, this would require reimplementing the Conform To SDF block in the VFX graph to sample the sdf gradient, rather than computing it with a 3-tap approximation.
- Instead of each thread writing
numSamples
times into the voxel array, spawnnumTriangles * numSamples
threads and each thread writes 1 sample into the array. Maybe this is faster? - Try the geometry shader technique. It used to be annoying to do this, but apparently is easier in HDRP.
- There's no need for the dependency on the VFX graph except for the demo scene.