• 2D
  • Shader vs Animation?

Hi, I recently discovered shaders and it was a revelation to me. I am currently working on a 2d pixel game and my character should blink after it was hit by an enemy.

Originally I wanted to realize this from an animation player. just alternate the opacity of the sprite but now I figured out that I can do the same by using shader code like

shader_type canvas_item;

void fragment(){
	COLOR = texture(TEXTURE, UV);
	if(COLOR.a > 0.0){
		COLOR.a = abs(sin(TIME*10.0));
	}
}

Now I wonder what the better solution could be. Does the shader solution have any advantage over the animation player solution?

Thanks Bernd

Shaders run on the GPU, whereas AnimationPlayers run on the CPU. This can have performance implications when using huge numbers of instances (thousands or more), but it won't have any noticeable impact when using just a few dozen instances.

That said, AnimationPlayer is generally easier to control from code. This is probably what you should favor in most situations :)