How do I change the texture of a 2D sprite in shader script

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

:bust_in_silhouette: Reply From: kidscancode

Since it looks like you want to keep the original sprite’s alpha, just sample the rgb pixels from your texture:

shader_type canvas_item;

uniform sampler2D new_texture;

void fragment() {
	COLOR.rgb = texture(new_texture, UV).rgb;
}

Drop your new texture in the shader parameter in the Inspector.

Thanks for reply.

I must be missing something, because here’s my result:

I just want to have the happy face shape/silhouette with the striped image.

ville | 2023-06-13 21:14

Sorry, that’s because it works slightly differently between 3.x and 4.0.

In 3.x, you also need to specifically set the alpha. Add this to use the sprite’s alpha value:

COLOR.a = texture(TEXTURE, UV).a;

kidscancode | 2023-06-13 21:26

Thank you! Thad did the trick!

ville | 2023-06-13 21:27