precision highp float;

uniform float time;
uniform sampler2D u_tex;
uniform vec2 resolution;

float d = sin(time * 5.0) * 0.5 + 1.5; // kernel offset

float lookup(vec2 p, float dx, float dy)
{
    vec2 uv = (p.xy + vec2(dx * d, dy * d)) / resolution.xy;
    uv.y = 1.0 - uv.y; // vertical flip
    vec4 c = texture2D(u_tex, uv.xy);

    // return as luma
    return 0.2126 * c.r + 0.7152 * c.g + 0.0722 * c.b;
}

void main(void)
{
    vec2 p = gl_FragCoord.xy;

    // simple sobel edge detection
    float gx = 0.0;
    gx += -1.0 * lookup(p, -1.0, -1.0);
    gx += -2.0 * lookup(p, -1.0,  0.0);
    gx += -1.0 * lookup(p, -1.0,  1.0);
    gx +=  1.0 * lookup(p,  1.0, -1.0);
    gx +=  2.0 * lookup(p,  1.0,  0.0);
    gx +=  1.0 * lookup(p,  1.0,  1.0);

    float gy = 0.0;
    gy += -1.0 * lookup(p, -1.0, -1.0);
    gy += -2.0 * lookup(p,  0.0, -1.0);
    gy += -1.0 * lookup(p,  1.0, -1.0);
    gy +=  1.0 * lookup(p, -1.0,  1.0);
    gy +=  2.0 * lookup(p,  0.0,  1.0);
    gy +=  1.0 * lookup(p,  1.0,  1.0);

    // hack: use g^2 to conceal noise
    float g = gx*gx + gy*gy;
    float g2 = g * (sin(time) / 2.0 + 0.5);

    vec2 uv = p / resolution.xy;
    uv.y = 1.0 - uv.y; // vertical flip
    vec4 col = texture2D(u_tex, uv);
    col += vec4(0.0, g, g2, 1.0);
    
    gl_FragColor = col;
}