shader_type canvas_item;

uniform vec4 outline_color : source_color = vec4(1.0, 1.0, 1.0, 1.0);
uniform float outline_width : hint_range(0.0, 10.0) = 1.0;
uniform float outline_alpha : hint_range(0.0, 1.0) = 1.0;

void fragment() {
    vec2 size = TEXTURE_PIXEL_SIZE * outline_width;
    vec4 sprite_color = texture(TEXTURE, UV);
    float outline = 0.0;

    // Left
    outline += texture(TEXTURE, UV + vec2(-size.x, 0)).a;
    // Right
    outline += texture(TEXTURE, UV + vec2(size.x, 0)).a;
    // Top
    outline += texture(TEXTURE, UV + vec2(0, -size.y)).a;
    // Top-left
    outline += texture(TEXTURE, UV + vec2(-size.x, -size.y)).a;
    // Top-right
    outline += texture(TEXTURE, UV + vec2(size.x, -size.y)).a;

    outline = min(outline, 1.0);
    vec4 final_color = mix(outline_color, sprite_color, sprite_color.a);
    COLOR = vec4(final_color.rgb, max(sprite_color.a, outline * outline_color.a * outline_alpha));
}