Home

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

  1. Drag the MeshToSDF prefab into your scene.
  2. Either set a mesh for the Mesh field or set a SkinnedMeshRenderer for the Skinned Mesh Renderer field
  3. 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
  4. Outputs:
    1. VFX graph output - set the Vfx Output field to a VFX graph and the Vfx Property to an exposed Texture3D parameter of the VFX graph
    2. Material output - same as vfx graph output, but with a material. There's a Slice Texture 3D material in the Editor 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.
    3. 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 the offset or scale or sdfResolution fields in a build, you also have to set meshToSdfComponent.fieldsChanged = true

How it works

  1. Convert the triangle mesh into voxels
  2. 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.
  3. 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.
  4. 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
  5. 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.
  6. Subtract some constant from the unsigned distance field to thicken the surface a little bit.

Limitations & Improvements to make