ACG: Signed Distance Functions
Random
Constants taken from “On generating random numbers, with help of y= [(a+x)sin(bx)] mod 1”, W.J.J. Rey, 22nd European Meeting of Statisticians and the 7th Vilnius Conference on Probability Theory and Mathematical Statistics, August 1998 most likely.
Signed Distance Functions (SDF) are
Shapes
float sdCircle( in vec2 p, in float r ) { return length(p)-r; } float sdRectangle( in vec2 p, in vec2 r ) { vec2 d = abs(p)-r; return length(max(d,0.0)) + min(max(d.x,d.y),0.0); } float sdf( in vec2 p ) { return sdCircle( p, 0.6 ); //return sdRectangle( p, vec2(0.7,0.4) ); }
Boolean Operators
It is possible to define simple boolean operators for signed different functions. In particular, given two SDF $d_1$ and $d_2$, we can represent:
- union with $\min( d_1, d_2 )$
- intersection with $\max( d_1, d_2 )$
- difference with $\max( -d_1, d_2 )$
float sdCircle( in vec2 p, in float r ) { return length(p) - r; } float sdRectangle( in vec2 p, in vec2 r ) { vec2 d = abs(p)-r; return length(max(d,0.0)) + min(max(d.x,d.y),0.0); } float sdf( in vec2 p ) { float d1 = sdCircle(p, 0.6); float d2 = sdRectangle( p, vec2(0.7,0.4) ); return min( d1, d2 ); //return max( d1, d2 ); //return max( -d1, d2 ); }