Hello everybody, I tried the first shader tutorial and it did not work properly. It seems to be because I was using a sprite sheet instead of a regular sprite. The UV seems to cover the entire sprite sheet instead of the current frame. Is that expected? How can I deal with this problem and complete the tutorial with my sprite sheet? Thank you, ATN

@atn said: It seems to be because I was using a sprite sheet instead of a regular sprite. The UV seems to cover the entire sprite sheet instead of the current frame. Is that expected?

Yes that is to be expected, you'll have to scale(multiply) and translate(add) values to get what you want from the shader.

To get the scale right you would multiply UV x and UV y coordinates by a 1/number-of-frames per coordinate axis. For the translation you would add positive or negative values to UV x and y coordinates to get the location of the correct frame.

Ok thank you. I will try and post some results here for anybody looking at the same thing to find it. ATN

Okay, whatever I do I can't make it work. I attached some images that show my problem. It actually does not seem to be related to the sprite sheet, right?

I'm assuming there's already an alpha channel in your image? And you want to make just the opaque pixels semi-transparent? Then you'll have to take your textures alpha channel and multiply it with the scalar and output the result.

make line 12:

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

Awesome thank you ! It works

shader_type canvas_item;

uniform float frame_x = 0.0;
uniform float frame_y = 0.0;
uniform float h_frames = 5.0;
uniform float v_frames = 7.0;

void fragment() {
	COLOR = texture(TEXTURE,UV);
	float UV_X = (UV.x * h_frames - frame_x);
	float UV_Y = (UV.y * v_frames - frame_y);

	COLOR.a = texture(TEXTURE, UV).a * UV_X;
}

To avoid any nasty surprises you'll probably want to clamp the UV_X on the alpha assignment on line 13.

I'll do it thank you :)

2 years later