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 time = mod(iTime, period);
    time = time - floor(time * 4.0/period/3.0) * period;
    time = 1.0 - abs(time * 4.0/period - 1.0);
    float xOffset = time * 30.0/iResolution.x;
    float yOffset = time * 19.0/iResolution.y;

    if (inside(uv, vec4(0.0, 0.0, 1.0, 1.0)) == 1) {
        vec2 st = vec2(uv.x - xOffset, uv.y - yOffset);
        st = vec2(abs(mod(st.x + 1.0, 2.0) - 1.0), abs(mod(st.y + 1.0, 2.0) - 1.0));

        gl_FragColor = texture2D(inputImageTexture, st);
    }
}