#version 330 core

uniform vec3 iResolution;
uniform float iTime;
uniform sampler2D iChannel0;

out vec4 FragColor;

// Radial blur shader (main pass)
const float SAMPLES = 24.;

float hash( vec2 p ){ return fract(sin(dot(p, vec2(41, 289)))*45758.5453); }

vec3 lOff(){
    vec2 u = sin(vec2(1.57, 0) - iTime/2.);
    mat2 a = mat2(u, -u.y, u.x);
    vec3 l = normalize(vec3(1.5, 1., -0.5));
    l.xz = a * l.xz;
    l.xy = a * l.xy;
    return l;
}

void mainImage( out vec4 fragColor, in vec2 fragCoord ){
    vec2 uv = fragCoord.xy / iResolution.xy;

    float decay = 0.9;
    float density = 0.4;
    float weight = 0.1;

    vec3 l = lOff();
    vec2 tuv =  uv - .5 - l.xy*.45;
    vec2 dTuv = tuv*density/SAMPLES;

    vec4 col = texture(iChannel0, uv.xy)*0.25;
    uv += dTuv*(hash(uv.xy + fract(iTime))*2. - 1.);

    for(float i=0.; i < SAMPLES; i++){
        uv -= dTuv;
        col += texture(iChannel0, uv) * weight;
        weight *= decay;
    }

    col *= (1. - dot(tuv, tuv)*.75);
    fragColor = sqrt(smoothstep(0., 1., col));
}

void main()
{
    mainImage(FragColor, gl_FragCoord.xy);
}
