Converting raster shape/blob into displacement map

I am a beginner in digital image processing and computer graphics. I would like to program a similar behavior than the Shadermap 3 normal editor (displacement layer more specifically).

As shown in this Shadermap 3 demonstration video, the displacement brush allows the user to paint a displacement map (grayscale)

The brush strokes produce shapes with a smooth value transitioning from the middle of the shape to its edge, in this case giving the shape a slightly curved profile. As it is also shown, the shape's profile can be fine-tuned with the brush curve editor.

What combination of filters and/or algorithms could Shadermap 3 possibly use to convert a pixel blob or shape to displacement map with a given width and curve?

One way I can think of is to make a "signed distance transform" of the image where there is information for each pixel about how far the pixel is to the closest surface of the shape. Since it's signed, youll be able to know if the pixel is inside or outside he shape, and by how much.

Using this knowledge, you could easily make a new image, where the pixel is black if the distance is greater than -10, else it is white. That would make the shape shrink by 10 pixels.

You can use smoothstep or other curves to transition between black and white over a number of pixels, to soften it anti alias the edges.

You can make the distance transform by brute force, but another method would be to make a voronoi diagram first and then turn that into a distance transform.

There is lots of info out there on both voronoi diagrams as well as (signed) distance transforms but these two links will probably be helpful to get you started:

http://blog.demofox.org/2016/02/29/fast-voronoi-diagrams-and-distance-dield-textures-on-the-gpu-with-the-jump-flooding-algorithm/

http://blog.demofox.org/2016/03/02/actually-making-signed-distance-field-textures-with-jfa/

• You might also want to read up on signed distance fields in general by the way. Seems relevant as you might possibly be able to skip the distance transform step and just draw sdf's Commented Mar 14, 2016 at 3:12
• Thanks, it seems to work quite well. Using OpenCV, applying a distance transform, thresholding the image and finally normalizing it back to 0 to 255 values seems to give quite a good result. Some jagged lines are really visible though. I tried a Gaussian blur which kind of works but I would really like to know if there is a better solution. Commented Mar 15, 2016 at 2:54
• There is a better solution. The topic kind of warrants it's own question but here's a short answer. First step is to make it fade from white to black over a specific distance. Like if 10 was the distance that went from black to white, you could make it fade between 9 and 11 where you use the distance value to figure out how light or dark to make the pixel based on distance. Next, you take that pixel shade, which should be between 0 and 1, and put it through the smoothstep function. That will turn it from a linear fade to something a lot more appealing. Adjust fade distance to taste (: Commented Mar 15, 2016 at 3:10
• Here is the details of the smoothstep function: en.m.wikipedia.org/wiki/Smoothstep for a better or more detailed answer you might ask how to do anti aliasing when rendering using a distance field. Commented Mar 15, 2016 at 3:12
• oh sorry, to address more towards your original question. I believe the curve just says how to turn the linear distance into a pixel shade. like if your width was 10 pixels, and you were 5 pixels away (either on the positive or negative side), that it would take that to mean you were at 50% distance, and do a lookup on the curve at 50% to see how white or black to make the output pixel. Commented Mar 15, 2016 at 3:47