Skip to content

Instantly share code, notes, and snippets.

@mpersano
Last active March 3, 2016 22:15
Show Gist options
  • Select an option

  • Save mpersano/101c5f45125d9aac5a5a to your computer and use it in GitHub Desktop.

Select an option

Save mpersano/101c5f45125d9aac5a5a to your computer and use it in GitHub Desktop.
#version 300 es
precision highp float;
uniform vec2 resolution;
uniform float time;
const float PI = 3.14159265358979323844;
const float TRANSITION = .75;
out vec4 color;
//
// r*cos(a) = R + t*(R*cos(b) - R)
// r*sin(a) = t*R*sin(b)
//
// t = (r*sin(a))/(R*sin(b))
//
// r*cos(a) = R + (r*sin(a))/(R*sin(b))*(R*cos(b) - R)
// r*cos(a) = R + (r*sin(a)/sin(b))*(cos(b) - 1)
// r*(cos(a) - (sin(a)/sin(b))*(cos(b) - 1)) = R
float inside_polygon(vec2 pos, vec2 center, float r, float n, float s)
{
float theta = 2.*PI/n;
vec2 d = pos - center;
float a = mod(mod(atan(d.y, d.x) + s, 2.*PI), theta);
float l = length(d);
float m = r*cos(.5*theta)/cos(a - .5*theta); // r/(cos(a) - (sin(a)/sin(theta))*(cos(theta) - 1.));
const float border = .001;
return smoothstep(m + border, m - border, l);
}
float wobble(vec2 pos)
{
vec2 d = pos - vec2(.5, .5);
float a = (1.5 + (atan(d.y, d.x) + PI/2.)/(2.*PI));
float t = time/TRANSITION;
return min(t*a*a, 1.);
}
float inside_triangle(vec2 pos, vec2 center, float r, float s)
{
return inside_polygon(pos, center, wobble(center)*r, 3., s);
}
float inside_triangles(vec2 pos, float r)
{
const float da = 2.*PI/6.;
float a = 0.;
float v = 0.;
for (int i = 0; i < 6; i++) {
float c = cos(a);
float s = sin(a);
vec2 d = vec2(c, s);
vec2 n = vec2(-s, c);
vec2 o0 = vec2(.5, .5) + (2./3.)*sqrt(3.)*d*r;
vec2 o1 = vec2(.5, .5) + (5./6.)*sqrt(3.)*d*r;
float r_triangle = 1.*r/sqrt(3.);
v += inside_triangle(pos, o0, r_triangle, a) +
inside_triangle(pos, o1 - n*.5*r, r_triangle, a + PI) +
inside_triangle(pos, o1 + n*.5*r, r_triangle, a + PI);
a += da;
}
return v;
}
void main()
{
vec2 pos = gl_FragCoord.xy/resolution;
float r0 = .125;
float v;
if (time < TRANSITION) {
v = (inside_triangles(pos, r0) + inside_polygon(pos, vec2(.5, .5), r0, 6., PI/6.));
} else {
float r1 = (1.115 + sqrt(3.)/2.)*r0;
float t = (time - TRANSITION)/(1. - TRANSITION);
float r = mix(r1, r0, -t*(t - 2.));
v = inside_polygon(pos, vec2(.5, .5), r, 6., PI/6.);
}
color = mix(vec4(.5, 1., 1., 1.), vec4(.25, .25, .25, 1.), v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment