Cannot change CanvasItem shader uniform more than once

Godot Version

Godot 4.2

Question

I have an AnimatedSprite2D with a ShaderMaterial applied. I want to apply an effect to invert the sprite’s colors for a time and then return to normal after a signal is emitted. Using the shader below, I am able to start the inverse effect using a uniform, but if I try to set it back the colors never change.
My Shader:

shader_type canvas_item;
uniform bool applied = false;
void fragment() {
	vec4 color = texture(TEXTURE, UV);
	if (applied) {
		COLOR = vec4(1.0 - color.rgb, color.a);
	} else {
		COLOR = texture(TEXTURE, UV);
	}
}

The code I use to enable/disable the shader:

if invert_colors:
	shader_material.set_shader_parameter("applied", true)

func on_revert_colors() -> void:
	print("got signal to turn off color inversion")
	shader_material.set_shader_parameter("applied", false)

What am I doing wrong?

Can you post more of the script, maybe invert_colors isn’t being set?

Actually, it turns out the code is working. The sprite I was applying this to was stationary, but when I updated its position after changing the shader parameter a second time, I finally saw the right colors.

I tried the same shader with a Sprite2D and it worked correctly. It’s only with an AnimatedSprite2D that I don’t see the color alternating until I move it around the screen.