#define PI 3.141592
#define SIN(x) (.5*sin(x)+.5)
#define hue(v) (.6+.6*cos(2.*PI*v + vec3(0, -2.*PI/3., 2.*PI/3.)))

float t;


mat2 rot(float a) {
    return mat2(cos(a), sin(a), -sin(a), cos(a));
}


// noise function from https://www.shadertoy.com/view/MdyfWz
float r(in vec2 p)
{
    return fract(cos(p.x*42.98 + p.y*43.23) * 1127.53);
}

// using noise functions from: https://www.shadertoy.com/view/XtXXD8
float n(in vec2 p)
{
    vec2 fn = floor(p);
    vec2 sn = smoothstep(vec2(0), vec2(1), fract(p));
    
    float h1 = mix(r(fn), r(fn + vec2(1,0)), sn.x);
    float h2 = mix(r(fn + vec2(0,1)), r(fn + vec2(1)), sn.x);
    return mix(h1 ,h2, sn.y);
}

float noise(in vec2 p)
{
    return n(p/32.) * 0.58 +
           n(p/16.) * 0.2  +
           n(p/8.)  * 0.1  +
           n(p/4.)  * 0.05 +
           n(p/2.)  * 0.02 +
           n(p)     * 0.0125;
}

vec2 pixelate(vec2 uv, float gran) {
    return vec2(gran*floor(uv/gran));
}


float radial(vec2 uv, float o) {
    //return SIN(length(2.*uv)-t*2.);
    
    //return smoothstep(ts+.1, ts-.1, n);
    float ts = SIN(length(.5*uv)-t*2.4+o);
    float n = noise(uv*70.);
    return smoothstep(ts+.5, ts-.5, n);
}

vec3 gradient1(vec2 uv) {

    return hue(.4*iTime+2.*radial(uv, 0.3)) - mix(vec3(0.933,1.000,0.000), vec3(0.702,0.000,0.071), radial(uv*4., 0.));
}

vec3 gradient2(vec2 uv) {
    return  hue(0.2*iTime+radial(uv, 0.9)) + mix(vec3(0.000,0.047,0.678), vec3(0.110,0.000,0.502), radial(uv*4., .5));
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = (fragCoord - 0.5*iResolution.xy)/iResolution.y;
    
    vec2 uvo = uv;
    uv = pixelate(uv, .008);

    t= iTime;
    vec3 col = vec3(0);
    
    
    float r = 2.*length(uv*1.);
    float n = noise(uv*rot(t*.1)*200.);
    
    float ts = SIN(4.5*length(uv)+t*4.);
    
    col = mix(gradient1(uvo), gradient2(uvo), smoothstep(ts+.2, ts-.2, n));
   
    fragColor = vec4(col, 1);
}
