# How can I convert 3D mesh to SDF?

Previously, I found an useful article that explained how to convert 3d mesh to SDF easily.

I want our artists to be able to easily convert from polygonal data to SDFs with little-to-no effort.

http://colourmath.com/2018/development/signed-distance-fields-in-unity/

this article is what I want but unfortunately there wasn't simple Implemention.

there is algorithm in this article:

foreach (pixel in texture)
foreach (triangle in mesh)
get pixel center as mesh bounds position
get closest point on triangle
calculate distance from pixel position to triangle
keep the smallest distance
perform a raycast along arbitrary vector against triangle
if(raycast intersects triangle)
increment an intersection counter
if(number of intersections is even)
pixel value is positive // outside mesh
else
pixel value is negative // inside mesh


I like to Implement this article but I can't understand above algorithm and I don't know how can I convert 3D mesh to SDF data and then sotre this data to 3D RenderTexture?

• Is there a particular step in the algorithm above that you're stuck on? Or a particular concept you need elaboration about? – DMGregory Oct 26 '18 at 23:48
• @DMGregory My problem is that I don't know where to start Implement above algorithm.I'm confused – Seyed Morteza Kamali Oct 27 '18 at 8:09
• Okay, so it says for each pixel in texture — have you got an outer triple for loop iterating over each cell in the volume grid? Next it says for each triangle, have you got an inner for loop iterating over the triangles? – DMGregory Oct 27 '18 at 8:23