#version 330 core
out vec4 FragColor;
uniform vec3 iResolution;
uniform float iTime;
uniform float uAlpha;
uniform sampler2D iChannel0;
uniform float uStartDelay;

#define PI 3.14159265

// Translate
mat4 Translate(float Tx, float Ty, float Tz)  
{ 
    return mat4(1., 0., 0., 0., 
                0., 1., 0., 0., 
                0., 0., 1., 0., 
                Tx, Ty, Tz, 1.); 
}

// Rotate the image in the XY-plane
mat4 RotateZ(float angle) 
{ 
    float c = cos(angle); 
    float s = sin(angle); 
    
    return mat4(c,  s, 0., 0,
                -s, c,  0., 0,
                0., 0., 1., 0,
                0., 0., 0., 1 ); 
}

// Scale the image
mat4 Scale(float Sx, float Sy, float Sz) 
{ 
    return mat4(Sx, 0., 0., 0,
                0., Sy, 0., 0,
                0., 0., Sz, 0,
                0., 0., 0., 1 ); 
}

in vec2 uv;

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

    // allow parametrized start delay (uStartDelay) in the same timebase used originally (iTime*0.5)
    float t = iTime * 0.5;
    if (t > uStartDelay) 
    {    
        float local = t - uStartDelay;
        float s = 1.0 + sin(local) * sin(local) * 10.0; // Scaling
        uvLocal = (Scale(s, s, s) * vec4(uvLocal, 0.0, 1.0)).xy;
        uvLocal = (RotateZ(local * 2.0) * vec4(uvLocal, 0.0, 1.0)).xy;
    }
    fragColor = texture(iChannel0, mod(uvLocal, 1.0));
}

void main(){ mainImage(FragColor, gl_FragCoord.xy); FragColor.a *= uAlpha; }
