
[vert.glsl]

#version 130

in vec2 in_position;

uniform vec2 u_screen_res;
uniform float u_step;

out vec2 pass_pos;
out vec2 pass_p[8];

void main()
{
    //JFA ALGO
    pass_pos = ((vec2(1.0) + in_position) * 0.5);

    float k = u_step;

    vec2 p2 = pass_pos;
    pass_p[0] = p2 + vec2(-k, -k);
    pass_p[1] = p2 + vec2( 0, -k);
    pass_p[2] = p2 + vec2( k, -k);
    pass_p[3] = p2 + vec2(-k,  0);
    pass_p[4] = p2 + vec2( k,  0);
    pass_p[5] = p2 + vec2(-k,  k);
    pass_p[6] = p2 + vec2( 0,  k);
    pass_p[7] = p2 + vec2( k,  k);

    gl_Position = vec4(in_position, 0.0, 1.0);
}

[frag.glsl]

#version 130

#if defined GL_ES
precision highp float;
#endif

in vec2 pass_pos;
in vec2 pass_p[8];

uniform sampler2D u_texture;

void main(void)
{
    vec4 src_color = texture2D(u_texture, pass_pos);
    vec4 neigh_color;

    for (int i = 0; i < 8; ++i)
    {
        neigh_color = texture2D(u_texture, pass_p[i]);

        if (neigh_color.z > 0.0 && src_color.z == 0.0)
            src_color = neigh_color;
        else if (neigh_color.z > 0.0 && src_color.z > 0.0 &&
                 length(neigh_color.xy - pass_pos) < length(src_color.xy - pass_pos))
            src_color = neigh_color;

    }

    gl_FragColor = src_color;
}

