Screenspace Outline Shader 2D Sprites

Godot Version

4.3

Question

Hi All. Trying to get a shader to show a outline on all sprites in screenspace. meaning when 2 sprites also overlaps it outlines both as one.

Really battling and have no idea how to setup the tree for this. Trying with canvas container and color rect but nothing is working.

shader_type canvas_item;

uniform sampler2D screen_texture : hint_screen_texture;
uniform float outline_thickness : hint_range(1.0, 10.0) = 1.0;
uniform vec4 outline_color : source_color = vec4(1.0, 0.0, 0.0, 1.0);
uniform float alpha_threshold : hint_range(0.0, 0.5) = 0.01;

void fragment() {
    vec2 texel_size = 1.0 / vec2(textureSize(screen_texture, 0));
    vec2 uv = SCREEN_UV;

    // Sample the center pixel
    vec4 center = texture(screen_texture, uv);

    // Neighbor offsets
    vec2 offset_x = vec2(texel_size.x * outline_thickness, 0.0);
    vec2 offset_y = vec2(0.0, texel_size.y * outline_thickness);

    // Sample neighbors' alpha
    float left  = texture(screen_texture, uv - offset_x).a;
    float right = texture(screen_texture, uv + offset_x).a;
    float up    = texture(screen_texture, uv - offset_y).a;
    float down  = texture(screen_texture, uv + offset_y).a;

    // Is this transparent pixel next to an opaque one?
    float has_neighbor = step(alpha_threshold, max(max(left, right), max(up, down)));

    // Final color
    if (center.a <= alpha_threshold && has_neighbor > 0.0) {
        // Draw outline
        COLOR = outline_color;
    } else {
        // Keep original screen pixel
        COLOR = center;
    }
}

What is not working exactly? Is shader not detecting edges between opaque and transparent pixels? Or are you unsure on how to set up the scene?