Shader without loosing self background color?

80 Views Asked by At

Is it possible to apply a shader while preserving the self color?

so far this is what I've come up with

shader_type canvas_item;

void fragment(){
    vec4 self_color = texture(TEXTURE, UV);
    vec4 back_color = texture(SCREEN_TEXTURE, SCREEN_UV);
    
    if(self_color.a==0.0)
        COLOR=back_color;
    else // invert color
        COLOR = mix(self_color,vec4(1.0 - back_color.rgb, back_color.a),0.5);
}

but as you can see (on left) it's faded and merges with the editor's gray background
as compared to the original image (on right)

enter image description here

This is what I'm trying to achieve:

enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

The only thing that comes to mind is to tell the shader what the clear color is and use that for reference. Something like the following (I'm not sure if this is the desired effect, but hopefully you can tweak to what you want):

shader_type canvas_item;
uniform vec4 clear_color:hint_color;

void fragment(){
    vec4 self_color = texture(TEXTURE, UV);
    vec4 back_color = texture(SCREEN_TEXTURE, SCREEN_UV);

    COLOR = mix
    (
        self_color,
        mix(self_color, back_color, 0.5),
        distance(back_color, clear_color)
    );
}

Here I'm mixing colors based on how far are the background is from the clear color distance(back_color, clear_color)※. When they match, you should get self_color, and as they differ the mix would approach mix(self_color, back_color, 0.5) (tweak that to your needs).

※: You can try other metrics, for example dot(back_color - clear_color, back_color - clear_color) or dot(abs(back_color - clear_color), vec4(1.0)).