Godot Alpha Sorting Effect

If anyone is curious, I think I have the solution it was a lot simpler than I expected (though it may be inflexible for now). You can set up a simple scene that displays some sprites and order them to your liking. And simply apply this shader I made to them.

shader_type canvas_item;

uniform sampler2D screen_texture : hint_screen_texture;
uniform float alpha_255 : hint_range(0, 255, 1) = 255.0;

void fragment() {
    vec4 screen_col = texture(screen_texture, SCREEN_UV);
    vec4 sprite_col = texture(TEXTURE, UV);
    
    float alpha = alpha_255 / 255.0;

    // Skip fragment if sprite's alpha is nearly zero (empty-space)
    if (abs(sprite_col.a) < 0.001) {
        discard;
    }

    // Blend visible parts only
    COLOR = mix(screen_col, sprite_col, alpha);
}

You can see in this image the transparent “Godots” (or whatever you call the little guy) do not blend with one another despite being transparent indicated by the red line behind them all showing that they are still technically see through.

When you use hint_screen_texture, each sprite samples a snapshot of the screen before it is drawn. So if multiple sprites overlap, and they all use this effect, they end up sampling the same unmodified background, not each other. At least that is what I believe is going on still kind new to this.

So, thanks, pennyloafers for suggesting the documentation (despite me objecting to it, ironic) I guess I just needed some direction. :slight_smile:

4 Likes