uniform mat4 u_Color;
varying vec2 v_TexCoord;
varying vec2 v_TexCoord2;
uniform sampler2D u_Tex0;
uniform float u_Time;
uniform float u_spriteSize;

float outline_thickness_blur = 1.2; // tu zmieniasz grubosc
float outline_thickness = 0.15;
vec3 outline_colour = vec3(0.0, 0.5, 0.1); // tu kolor
float outline_threshold_blur = .8;
float outline_threshold = .5;

void main()
{
	vec4 pixelOrig = texture2D(u_Tex0, v_TexCoord);
	float pixelOrigGray = dot(pixelOrig.rgb, vec3(0.299, 0.587, 0.114));
	vec4 outlineBlur;
	vec4 outline;
    int j;
    int i;
	
	if (pixelOrig.a <= outline_threshold_blur) {
        ivec2 size = textureSize(u_Tex0, 0);

        float uv_x = v_TexCoord.x * size.x;
        float uv_y = v_TexCoord.y * size.y;

        float sum = 0.0;
		float scaledThicknessBlur = u_spriteSize * outline_thickness_blur / 32.0;
        for (int n = 0; n < 9; ++n) {
            uv_y = (v_TexCoord.y * size.y) + (scaledThicknessBlur * float(n - 4.5));
            float h_sum = 0.0;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x - (4.0 * scaledThicknessBlur), uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x - (3.0 * scaledThicknessBlur), uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x - (2.0 * scaledThicknessBlur), uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x - scaledThicknessBlur, uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x, uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x + scaledThicknessBlur, uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x + (2.0 * scaledThicknessBlur), uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x + (3.0 * scaledThicknessBlur), uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x + (4.0 * scaledThicknessBlur), uv_y), 0).a;
            sum += h_sum / 9.0;
        }

        outlineBlur = vec4(outline_colour, (sum / 9.0) * outline_threshold_blur);
    }
	
	if (pixelOrig.a <= outline_threshold) {
        ivec2 size = textureSize(u_Tex0, 0);

        float uv_x = v_TexCoord.x * size.x;
        float uv_y = v_TexCoord.y * size.y;

        float sum = 0.0;
		float scaledThicknessOutline = u_spriteSize * outline_thickness / 32.0;
        for (int n = 0; n < 9; ++n) {
            uv_y = (v_TexCoord.y * size.y) + (scaledThicknessOutline * float(n - 4.5));
            float h_sum = 0.0;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x - (4.0 * scaledThicknessOutline), uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x - (3.0 * scaledThicknessOutline), uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x - (2.0 * scaledThicknessOutline), uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x - scaledThicknessOutline, uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x, uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x + scaledThicknessOutline, uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x + (2.0 * scaledThicknessOutline), uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x + (3.0 * scaledThicknessOutline), uv_y), 0).a;
            h_sum += texelFetch(u_Tex0, ivec2(uv_x + (4.0 * scaledThicknessOutline), uv_y), 0).a;
            sum += h_sum / 9.0;
        }

        if (sum / 9.0 >= 0.0001) {
            float gray = dot(pixelOrig.rgb, vec3(0.299, 0.587, 0.114));
            outline = mix(vec4(gray, gray, gray, pixelOrig.a), vec4(outline_colour, 1), sum);
        }
    }
	
	vec4 result = max(outline, outlineBlur);
	if(result.a < 0.01) discard;
	
	gl_FragColor = result;
}
