Skip to content

Instantly share code, notes, and snippets.

@elkraneo
Created May 21, 2019 17:20
Show Gist options
  • Save elkraneo/4b9d327338d43e1956a5351d15e14117 to your computer and use it in GitHub Desktop.
Save elkraneo/4b9d327338d43e1956a5351d15e14117 to your computer and use it in GitHub Desktop.
Author @kynd - 2016 Title: Distance Field Shapes http://www.kynd.info
float circle(vec2 p, float radius) {
return length(p) - radius;
}
float rect(vec2 p, vec2 size) {
vec2 d = abs(p) - size;
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}
float roundRect(vec2 p, vec2 size, float radius) {
vec2 d = abs(p) - size;
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0)) - radius;
}
float ring(vec2 p, float radius, float width) {
return abs(length(p) - radius * 0.5) - width;
}
float hexagon(vec2 p, float radius) {
vec2 q = abs(p);
return max(abs(q.y), q.x * 0.866025 + q.y * 0.5) - radius;
}
float triangle(vec2 p, float size) {
vec2 q = abs(p);
return max(q.x * 0.866025 + p.y * 0.5, -p.y * 0.5) - size * 0.5;
}
float ellipse(vec2 p, vec2 r, float s) {
return (length(p / r) - s);
}
float capsule(vec2 p, vec2 a, vec2 b, float r) {
vec2 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0 );
return length(pa - ba * h) - r;
}
//http://thndl.com/square-shaped-shaders.html
float polygon(vec2 p, int vertices, float size) {
float a = atan(p.x, p.y) + 0.2;
float b = TWO_PI / float(vertices);
return cos(floor(0.5 + a / b) * b - a) * length(p) - size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment