diff --git a/basic/distance_field.glsl b/basic/distance_field.glsl new file mode 100644 index 0000000..f189c09 --- /dev/null +++ b/basic/distance_field.glsl @@ -0,0 +1,21 @@ +#ifdef GL_ES +precision mediump float; +#endif + +uniform vec2 u_resolution; + +void main(){ + vec2 st = gl_FragCoord.xy/u_resolution.xy; + st.x *= u_resolution.x/u_resolution.y; + vec3 color = vec3(0.0); + float d = 0.0; + + // Remap the space to -1. to 1. + st = st *2.-1.; + + // Make the distance field + d = length(st)+length(min(st,0.))+length(max(st,0.)); + + // Visualize the distance field + gl_FragColor = vec4(vec3(fract(d*10.0)),1.0); +} diff --git a/utilities/shapes.glsl b/utilities/shapes.glsl index 039c132..dd4d343 100644 --- a/utilities/shapes.glsl +++ b/utilities/shapes.glsl @@ -4,3 +4,19 @@ vec3 circle(in vec2 _st, in float _radius, vec3 color){ _radius+(_radius*0.02), dot(dist,dist)*4.0); } + +// Inspired by (but modified) +// http://thndl.com/square-shaped-shaders.html +#define TAU 6.28318530718 +vec3 polygon(int n, float size, vec2 pos, vec3 color, float rot) { + float e = 0.01; + vec2 st = gl_FragCoord.xy/u_resolution.xy; + st.x *= u_resolution.x/u_resolution.y; + st = (st-pos)*2.; + float a = atan(st.x,st.y)+rot; + float r = TAU/float(n); + + float d = cos(floor(.5+a/r)*r-a)*length(st); + + return color-smoothstep(size-e,size+e,d); +}