#define SPEED 0.3
#define SCALAR 1.0
#define FREQ 1.2
#define SEED 3.398218

// https://en.wikipedia.org/wiki/Collatz_conjecture
float collatz(float v) { return int(v) % 2 != 0 ? ((3. * v) + 1.) : (v / 2.); }

//https://en.wikipedia.org/wiki/Pairing_function
float cantor(float k1, float k2) { return ((k1 + k2) * (k1 + k2 + 1.0)) / 2.0 + k2; }
vec2 decant(float c) {
  float w = floor((sqrt(c * 8. + 1.) - 1.) / 2.);
  float y = float(c - (w * (w + 1.)) / 2.);
  return vec2(float(w - y), y);
}
float idx(vec2 v, float r) { return v.x * r + v.y; }
float idxcant(vec2 v, float r) { return fract(collatz(idx(decant(idx(v, r)), r))); }
float rand0(vec2 uv, float r, float seed) {
    vec2 cant = decant(sin(seed*3.14*2.0)*(1.0+idxcant(uv, r)));
    return fract(dot(uv, vec2(cos(cant.x), sin(cant.y))));
}
vec3 shade(in vec2 fc) {
   float ripple = rand0(fc, SCALAR, SEED);
   float ripple0 = rand0((fc*ripple)+(vec2(0.0, 1.0)*SCALAR*1.01*ripple), SCALAR, SEED*ripple);
   float ripple1 = rand0((fc*ripple0)+(vec2(1.0, 0.0)*SCALAR*1.001*ripple), SCALAR, SEED*ripple);
   float ripple2 = rand0((vec2(ripple0, ripple1)*fc)*0.5, SCALAR*0.5, ripple*ripple0*ripple1);
   vec3 blue = mix(vec3(0.0, 0.4, 1.0), vec3(0.0, 0.5, 1.0), smoothstep(0.0, 1.0, pow(ripple0, 2.6)));
   blue = mix(blue, vec3(0.0, 0.6, 0.9), smoothstep(0.0, 1.0, pow(ripple1, 2.6)));
   blue = mix(blue, vec3(0.8, 0.8, 0.9), 0.8*smoothstep(0.0, 1.0, ripple2*1.6));   
   return max(ripple, max(ripple0, ripple1))*blue;
}

void mainImage( out vec4 fragColor, in vec2 fc )
{
    float T = iTime*SPEED;
    vec2 move = iMouse.z > 0.001 ? iMouse.xy : vec2(cos(T), sin(T))*3.14;
    fragColor = vec4(shade((fc+move)*((fc/iResolution.xy) + 0.5)*FREQ),1.0);
}
