precision mediump float;

uniform sampler2D	texture_unit;
varying	vec2		texture_uv;		

uniform	vec2 	texel_size;									// set automatically by gfx engine
uniform	float	outline_size;// = 2;							// pixels, set by code
uniform	vec4	outline_color;// = float4(1.f, 0.f, 0.f, 1.f);	// r g b a, set by code
uniform	float	alpha_mul;// = 3;								// affects the overall thickness and opacity of blur. can set to a high number to produce a solid outline instead.

void main() {
    //this will be our RGBA sum
    float sum = 0.0;

    float hstep = texel_size.x * outline_size;
    float vstep = 0.0;

    //apply blurring, using a 9-tap filter with predefined gaussian weights

    sum += texture2D(texture_unit, vec2(texture_uv.x - 4.0*hstep, texture_uv.y - 4.0*vstep)).a * 0.0162162162;
    sum += texture2D(texture_unit, vec2(texture_uv.x - 3.0*hstep, texture_uv.y - 3.0*vstep)).a * 0.0540540541;
    sum += texture2D(texture_unit, vec2(texture_uv.x - 2.0*hstep, texture_uv.y - 2.0*vstep)).a * 0.1216216216;
    sum += texture2D(texture_unit, vec2(texture_uv.x - 1.0*hstep, texture_uv.y - 1.0*vstep)).a * 0.1945945946;

    sum += texture2D(texture_unit, vec2(texture_uv.x, texture_uv.y)).a * 0.2270270270;

    sum += texture2D(texture_unit, vec2(texture_uv.x + 1.0*hstep, texture_uv.y + 1.0*vstep)).a * 0.1945945946;
    sum += texture2D(texture_unit, vec2(texture_uv.x + 2.0*hstep, texture_uv.y + 2.0*vstep)).a * 0.1216216216;
    sum += texture2D(texture_unit, vec2(texture_uv.x + 3.0*hstep, texture_uv.y + 3.0*vstep)).a * 0.0540540541;
    sum += texture2D(texture_unit, vec2(texture_uv.x + 4.0*hstep, texture_uv.y + 4.0*vstep)).a * 0.0162162162;

    //discard alpha for our simple demo, multiply by vertex color and return
    gl_FragColor = vec4(outline_color.rgb, sum*alpha_mul);
}