0 votes

I have a number of sprites where the color is somewhat transparent. They were originally created on top of a white background, and I would like to keep the appearance they have with the white background, and I also don't want to have anything show through either. I'm looking to create a shader that will effectively take any non-transparent pixel, draw a fully white, opaque pixel, and then draw the original pixel color on top.

My knowledge of shaders is pretty limited, and I don't really know where to start.

in Engine by (1,605 points)

1 Answer

+1 vote
Best answer

If I didn't get it wrong, it's something like this: (I use google for translate to spanish)

shader_type canvas_item;
uniform float white_mix=0.55; //amount of white that is added to the sprite

void fragment(){
    vec4 tex=texture(TEXTURE,UV); //sprite texture
    vec3 white_color= vec3(1.0); //pure white, without alpha;;
    vec3 sprite_color= tex.rgb; //sprite color, without alpha;

    if(tex.a > 0.1) //if it is not transparent..
        COLOR=vec4(  mix(white_color,sprite_color, white_mix), 1.0  );
    else
        COLOR.a=0.0;    

}

Only you have to mix the amount of white manually. the mix around 0.5 should work.

another more precise way:

shader_type canvas_item ;

void fragment(){

    vec4 tex=texture(TEXTURE,UV); //sprite texture
    float w=1.0; //white
    vec3 color=vec3(tex.r * tex.a + ((w-tex.a) ),
                    tex.g * tex.a + ((w-tex.a) ), 
                    tex.b * tex.a + ((w-tex.a) )
                    );

    if(tex.a>0.0)
        COLOR=vec4(color,1.0);
    else
        COLOR.a=0.0;


}    
by (2,058 points)
selected by

Thank you! They both work, but I think the second one is closest to what I'm looking for.

You're welcome, I also learn along the way ... the second one uses the premultiply alpha formula. I discovered it by brute force xd..If you want you can say that it would be missing so that it is exactly what you are looking for ...

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.