Add texture color on top of white background

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Eric Ellingson

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.

:bust_in_silhouette: Reply From: estebanmolca

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  );

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) )


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

Eric Ellingson | 2019-12-16 05:33

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 …

estebanmolca | 2019-12-16 06:26