#version 330 core

in vec2 vUV;
out vec4 FragColor;

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

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

    vec2 light = vec2(
        0.4 * (1.2 + sin(iTime)),
        0.4 * (1.2 + cos(iTime))
    );

    vec2 toLight = uv - light;
    float dist = length(toLight);
    vec2 dir = normalize(toLight);

    vec4 col = texture(iChannel0, uv);

    vec2 offsetUV = uv - dir * (1.2 / 500.0);
    vec4 ref = texture(iChannel0, offsetUV);

    float lum = dot(col.rgb, vec3(0.333));
    float refLum = dot(ref.rgb, vec3(0.333));

    float bump = smoothstep(0.1, 0.25, dist)
               * step(lum, refLum)
               * clamp((refLum - lum) * 16.0, 0.0, 1.0);

    float lightAmount = 1.6 - bump;
    float falloff = max(1.0 - dist / 0.7, 0.0);

    fragColor = vec4(col.rgb * lightAmount * falloff, col.a);
}

void main()
{
    vec2 fragCoord = vUV * iResolution.xy;
    vec4 outCol;
    mainImage(outCol, fragCoord);
    outCol.a *= uAlpha;
    FragColor = outCol;
}
