#version 330 core
in vec2 vTex;
out vec4 FragColor;

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

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    float transitionTime = max(0.0, uTransitionTime);

    vec2 fragSize = iResolution.xy;
    vec2 fragUV = fragCoord / fragSize;

    if (iTime < uStartDelay)
    {
        fragColor = texture(iChannel0, fragUV);
        return;
    }

    vec2 uv = fragCoord / iResolution.xy;
    vec3 col = texture(iChannel0, uv).rgb;
    col = vec3(max(col.r, max(col.g, col.b)));
    col = 1.0 - col;
    col = min(vec3(1.0), col * 1.25);
    col = pow(col, vec3(1.00 / 0.90));

    float anim = 0.85 + 0.15 * sin(iTime * 2.0);
    vec3 processed = col * anim;

    vec3 orig = texture(iChannel0, uv).rgb;

    float t = (iTime - uStartDelay) / transitionTime;
    float blend = 1.0 / (1.0 + exp(-t * 6.0));
    blend *= clamp(uAlpha, 0.0, 1.0);

    vec3 outcol = mix(orig, processed, clamp(blend, 0.0, 1.0));

    fragColor = vec4(outcol, 1.0);
}

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