[[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 vec4 col;
uniform mediump float alpha;
uniform bool draw_outline;

in mediump vec2 uv;
out mediump vec4 frag;

void main()
{
    mediump float stroke = 1.0 - texture(tex, uv).r;
    if (draw_outline)
    {
        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 < 9) ? 0.75 : 0.0;
        frag = vec4(col.rgb, edge * (1.0 - float(zero_count) / 9.0));
    }
    else
    {
        frag = vec4(col.rgb, stroke * alpha);
    }
}
