varying highp vec2 textureCoordinate;
#define iChannel0 inputImageTexture
#define texture(a,b) texture2D(a,fract(b))
#define fragColor gl_FragColor
uniform sampler2D inputImageTexture;

uniform highp float iTime;

#define GlitchAmount 1.0
#define time iTime
#define PI 3.14159265

highp vec4 posterize(highp vec4 color, highp float numColors)
{
    return floor(color * numColors - 0.5) / numColors;
}

highp vec2 quantize(highp vec2 v, highp float steps)
{
    return floor(v * steps) / steps;
}

highp float dist(highp vec2 a, highp vec2 b)
{
    return sqrt(pow(b.x - a.x, 2.0) + pow(b.y - a.y, 2.0));
}

void main() {

    highp vec2 uv = textureCoordinate;
    highp float amount = pow(GlitchAmount, 2.0);
    highp vec2 pixel = 1.0 / vec2(16.0,9.0) / 120.0;
    highp vec4 color = texture2D(iChannel0, uv);

    //     if (textureCoordinate.x < 0.0)
    //     {
    //         gl_FragColor = color;
    //         return;
    //     }
    highp float t = mod(mod(iTime, amount * 100.0 * (amount - 0.5)) * 109.0, 1.0);
    highp vec4 postColor = posterize(color, 16.0);
    highp vec4 a = posterize(texture2D(iChannel0, quantize(uv, 64.0 * t) + pixel * (postColor.rb - vec2(.5)) * 100.0), 5.0).rbga;
    highp vec4 b = posterize(texture2D(iChannel0, quantize(uv, 32.0 - t) + pixel * (postColor.rg - vec2(.5)) * 1000.0), 4.0).gbra;
    highp vec4 c = posterize(texture2D(iChannel0, quantize(uv, 16.0 + t) + pixel * (postColor.rg - vec2(.5)) * 20.0), 16.0).bgra;
    gl_FragColor = mix(
    texture2D(iChannel0,
    uv + amount * (quantize((a * t - b + c - (t + t / 2.0) / 10.0).rg, 16.0) - vec2(.5)) * pixel * 100.0),
    (a + b + c) / 3.0,
    (0.5 - (dot(color, postColor) - 1.5)) * amount);

}