home/ atoms/ sdf-from-implicit-equation

An SDF can be derived from any implicit curve equation by setting the LHS equal to d

Any curve defined by an implicit equation of the form f(x,y)=0 can be converted to a signed distance function: set the left-hand side equal to d and use d as the SDF return value. The zero-set of the equation becomes the shape boundary (d=0), positive values are outside, negative inside. The result may not be a true Euclidean distance function (Lipschitz constant may not equal 1), but it works as a rendering SDF when used with step or smoothstep thresholding. This technique is used to create heart, ellipse, parabola, and other mathematically-defined SDFs. The signed quality (inside vs outside) is preserved so long as the equation is consistent about its sign convention.

Examples

Heart curve: (x² + y² - 1)³ - x²y³ = 0 → d = (xx + yy - 1)^3 - xxyyy Render: if (d <= 0.0) col = red;

Assessment

Take the equation of an ellipse (x/a)² + (y/b)² = 1 and convert it to an SDF. Test it in Shadertoy. Is the resulting function a true distance function? How would you tell?

“We simply set the left-hand side (LHS) of the equation equal to the distance, `d`.”
corpus · shadertoy-tutorial-nathan-vaughn-inspirnathan · chunk 3