#ifdef GL_ES
precision mediump float;
#endif

// moving_gummy_2015 by natade
// Tokyo Demo Fest 2015

#define PI2 6.28318530718

uniform float time;
uniform vec2 resolution;

// rand code by http://glslsandbox.com/e#23104.1
float rand( const float x, const float y, const float seed ){
       return fract( sin( x + y * 0.192837465 ) * 1928374.0 * cos(seed) );
}

vec4 getRandomPoint(const float t) {
       float u = float(int(t)) * 0.001;
       return vec4(
               rand(0.144, 0.15, u + 0.2) * 2.0,
               rand(0.37, 0.17, u) - ((fract(t * 0.1) - 0.5)  * 2.0),
               fract(t),
               u
       );
}

vec3 getColor(vec2 point, const float t) {
       vec4 grp = getRandomPoint(t);
       vec2 center = grp.xy;
       float dist = length(point - center);
       float radius = 0.05;
       float is_inside = step(dist, radius);
       float r = rand(1.0, 4.0, grp.w);
       float g = rand(2.0, 5.0, grp.w);
       float b = rand(3.0, 6.0, grp.w);
       float a = 0.5 - cos(grp.z * PI2) * 0.5;
       return vec3(r, g, b) * is_inside * a;
}

void main( void ) {
       vec2 p = (gl_FragCoord.xy / resolution.xy);
       p.x *= resolution.x / resolution.y;
       vec3 color = vec3(0.0, 0.0, 0.0);
       if(abs(p.y - 0.5) < 0.4) {
               color = vec3(1.0, 0.9, 0.8);
               for(int i = 0;i < 100;i++) {
                       vec2 zurashi = vec2(p.x + sin(p.y * 50.0) * 0.02, p.y);
                       color -= getColor(zurashi, time * 0.01 * float(i) + float(i));
               }
               color += rand(p.x * 1234.0, p.y * 23.0, time) * 0.1;
               color *= 1.0 - pow(fract(time), 30.0);
       }
       gl_FragColor = vec4(color.xyz, 1.0);
}