Last active
August 29, 2015 14:17
-
-
Save m1el/92da555708d3c04c2f39 to your computer and use it in GitHub Desktop.
playing with sdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float circle(vec2 point, vec2 center, float r) | |
{ | |
return distance(point, center) - r; | |
} | |
float hollowCircle(vec2 point, vec2 center, float r1, float r2) | |
{ | |
float med = (r1+r2)/2.; | |
return abs(distance(point, center) - med) - r2 + med; | |
} | |
float rect(vec2 point, vec2 center, vec2 size) | |
{ | |
point = point - center; | |
point.x = abs(point.x); | |
point.y = abs(point.y); | |
if (point.x > size.x && point.y > size.y) { | |
return distance(size, point); | |
} | |
if (point.x > size.x) { | |
return point.x - size.x; | |
} | |
if (point.y > size.y) { | |
return point.y - size.y; | |
} | |
return max(point.x - size.x, point.y - size.y); | |
} | |
float sqr(float x) { return x*x; } | |
float det(vec2 a, vec2 b) { return a.x*b.y-b.x*a.y; } | |
float halfPlane(vec2 point, vec2 p0, vec2 p1) | |
{ | |
if (distance(p0, p1) < 0.001) { | |
// not a line | |
return distance(p0, point); | |
} | |
return det(point - p0, point - p1) / distance(p0, p1); | |
} | |
float thing(vec2 point, vec2 p0, vec2 p1, float k) | |
{ | |
float d = 0.01 - (exp(-distance(point, p0)/10.) + exp(-distance(point, p1)/10.)); | |
if (d > 0.005) { | |
return min(distance(point, p0), distance(point, p1)); | |
} | |
return d; | |
return (-sqr(distance(point, p0)/k)); | |
} | |
void mainImage( out vec4 fragColor, in vec2 fragCoord ) | |
{ | |
vec2 center = iResolution.xy / 2.; | |
vec2 h = vec2(iResolution.x / 20., 0); | |
float dist; | |
// dist = hollowCircle(fragCoord.xy, center, 100., 200.); | |
// dist = rect(fragCoord.xy, center, vec2(200., 100.)); | |
// dist = (circle(fragCoord.xy, center - h, 250.) | |
// + circle(fragCoord.xy, center + h, 250.)) / 2.; | |
// dist = halfPlane(fragCoord.xy, center, iMouse.xy); | |
vec2 xy = mix(fragCoord.xy, center, 0.); | |
dist = thing(xy, center - h, center + h, 10.); | |
if (dist > 0.) { | |
fragColor = vec4(mod(dist/50.,1.),0.,0.,1.); | |
} else { | |
dist = -dist; | |
fragColor = vec4(1.-mod(dist/50.,1.),1.-mod(dist/50.,1.),0.,1.); | |
} | |
// vec2 uv = fragCoord.xy / iResolution.xy; | |
// fragColor = vec4(uv,0.5+0.5*sin(iGlobalTime),1.0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float det(vec2 a, vec2 b) { return a.x*b.y-b.x*a.y; } | |
float approx_distance(vec2 point, vec2 mouse, vec2 p0, vec2 p1) { | |
vec2 dir = mouse - p0; | |
if (length(dir) < 0.001) { | |
// YOU LIED TO ME, THIS IS NOT A LINE | |
return distance(point, p0); | |
} | |
return abs(det(point - p0, dir) / length(dir)); | |
} | |
vec4 getColor( in vec2 fragCoord) | |
{ | |
vec2 xy = fragCoord.xy; | |
vec2 p0 = vec2(0.25, .5) * iResolution.xy; | |
vec2 p1 = vec2(.75, .5) * iResolution.xy; | |
vec2 mouse = iMouse.xy; | |
float d = approx_distance(xy, mouse, p0, p1); | |
float thickness = 1.0; | |
if(d < thickness) { | |
return vec4(d/50.,1.,1.,1.); | |
} else { | |
// Anti-alias the edge. | |
float a = 1.0 - smoothstep(d, thickness, thickness+1.0); | |
return vec4(mod(d/50.,1.0),a,a,1.); | |
} | |
} | |
// anti-aliasing | |
void mainImage( out vec4 fragColor, in vec2 fragCoord ) | |
{ | |
vec4 sum; | |
sum = getColor( fragCoord + vec2(-0.25,-0.25)); | |
sum = sum + getColor( fragCoord + vec2(-0.25,0.25)); | |
sum = sum + getColor( fragCoord + vec2(0.25,-0.25)); | |
sum = sum + getColor( fragCoord + vec2(0.25,0.25)); | |
fragColor = sum / 4.0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment