[[vertex]]

uniform mat4 mvp;

in vec4 pos;
in vec2 uvs;
out vec2 uv;

void main()
{
    uv = uvs;
    gl_Position = mvp * vec4(pos.xyz, 1.0);
}

[[fragment]]

uniform sampler2D tex;
uniform mediump vec2 pattern_offset;

in mediump vec2 uv;
out mediump vec4 frag;

void main()
{
    int zero_count = 0;
    if (textureOffset(tex, uv, ivec2(-1, -1)).r > 0.99) zero_count++;
    if (textureOffset(tex, uv, ivec2(-1,  0)).r > 0.99) zero_count++;
    if (textureOffset(tex, uv, ivec2(-1, +1)).r > 0.99) zero_count++;
    if (textureOffset(tex, uv, ivec2( 0, -1)).r > 0.99) zero_count++;
    if (textureOffset(tex, uv, ivec2( 0,  0)).r > 0.99) zero_count++;
    if (textureOffset(tex, uv, ivec2( 0, +1)).r > 0.99) zero_count++;
    if (textureOffset(tex, uv, ivec2(+1, -1)).r > 0.99) zero_count++;
    if (textureOffset(tex, uv, ivec2(+1,  0)).r > 0.99) zero_count++;
    if (textureOffset(tex, uv, ivec2(+1, +1)).r > 0.99) zero_count++;
    mediump float edge = (zero_count > 1 && zero_count < 8) ? 1.0 : 0.0;

    const mediump vec4 c1 = vec4(1.0, 1.0, 1.0, 1.0);
    const mediump vec4 c2 = vec4(0.9, 0.9, 0.9, 1.0);
    mediump vec2 c = floor(fract((uv + pattern_offset) * 50.0) * 2.0);
    mediump float alpha = mix(c.x, 1.0 - c.x, c.y);

    frag = vec4(vec3(edge * alpha), edge);
}
