precision mediump float;

varying vec2 textureCoordinate;

uniform sampler2D inputImageTexture;

uniform vec2 iResolution;
uniform float iTime;

#define PI 3.14159265359

vec2 rotate(vec2 uv, vec2 centerP, float rotation, float ratio) {
    vec2 ori = vec2(uv.x - centerP.x, (uv.y - centerP.y) * ratio);
    return vec2(ori.x * cos(rotation) - ori.y * sin(rotation), (ori.x * sin(rotation) + ori.y * cos(rotation)) / ratio) + centerP;
}

vec2 scale(vec2 uv, vec2 centerP, vec2 scalePro, float ratio) {
    vec2 ori = vec2(uv.x - centerP.x, (uv.y - centerP.y) * ratio);
    return vec2(ori.x, ori.y/ratio)/scalePro + centerP;
}

vec2 transform(vec2 uv, float params[5], float ratio) {
    vec2 pos = uv;
    pos -= (vec2(params[2], -0.5) + vec2(-0.5, params[3]));
    pos = scale(pos, vec2(0.5), vec2(params[0], params[1]), ratio);
    pos = rotate(pos, vec2(0.5), -params[4]/180.0*PI, ratio);
    return pos;
}

int inside(vec2 uv, vec4 edge) {
    return (uv.x >= edge.x && uv.x <= (edge.x + edge.z) && uv.y >= edge.y && uv.y <= (edge.y + edge.w)) ? 1 : 0;
}

void main(void) {
    vec2 uv = textureCoordinate;

    float period = 1.0;
    float halfPer = period / 2.0;
    float time = mod(iTime, period);
    float scalePro;
    if (time < halfPer*3.0/5.0) {
        scalePro = mix(1.0, 1.044, time*(5.0/3.0)/halfPer);
    } else if (time < halfPer - 0.3) {
        float t = halfPer*2.0/5.0-0.3;
        float d = (1.044-1.0)/(1.11-1.0);
        float a = pow(d, 0.2)*t/(1.0-pow(d, 0.2));
        scalePro = mix(1.0, 1.11, pow((time - halfPer*3.0/5.0 + a)/(t + a), 5.0));
    } else if (time < halfPer) {
        scalePro = mix(1.11, 1.116, 1.0-pow(1.0-(time-halfPer+0.3)/0.3, 5.0));
    } else {
        scalePro = mix(1.116, 1.0, 1.0-pow(1.0-(time-halfPer)/(halfPer), 3.0));
    }

    if (inside(uv, vec4(0.0, 0.0, 1.0, 1.0)) == 1) {
        vec2 st = scale(uv, vec2(0.5), vec2(scalePro, scalePro), iResolution.y/iResolution.x);

        gl_FragColor = texture2D(inputImageTexture, st);
    }
}