/
distfield2d.n
59 lines (48 loc) · 1.34 KB
/
distfield2d.n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import math
define union(d1, d2){
return min(d1, d2)
}
define intersect(d1, d2){
return max(d1,d2)
}
define subtract(d1, d2){
return max(-d1,d2)
}
define circle(p, x, y, radius){
return distance(p,vec2(x,y)) - radius
}
define box(p, left, top, width, height){
vec2 xy(left, top)
vec2 hwh(.5*width, .5*height)
var d = abs(p - xy-hwh) - hwh
return min(max(d.x,d.y),0.) + length(max(d,0.))
}
define roundbox(p, left, top, width, height, radius){
vec2 rad2(radius,radius)
vec2 hwh(.5*width, .5*height)
vec2 xy(left, top)
return length(max(abs(p - xy - hwh) - (hwh - 2.rad2), 0.)) - 2. * radius
}
define line(p, left, top, right, bottom, radius){
vec2 a(left, top)
vec2 b(right, bottom)
var pa = p - a, ba = b - a
float h = clamp(dot(pa,ba)/dot(ba,ba), 0., 1.)
return length(pa - ba * h) - radius
}
define drawField(coord, field, color){
float edge = length(vec2(length(dFdx(coord)), length(dFdy(coord)))) * SQRT_1_2
return vec4(color, smoothstep(-edge, edge, field))
}
define smoothpoly(float a,float b,float k){
float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 )
return mix( b, a, h ) - k*h*(1.0-h)
}
define smoothpow( float a, float b, float k ){
a = pow( a, k ); b = pow( b, k )
return pow( (a*b)/(a+b), 1.0/k )
}
define smoothexp( float a, float b, float k ){
float res = exp( -k*a ) + exp( -k*b )
return -log( res )/k
}