Why isn't my Sprite2D updating?

Godot Version

4.4

The Issue

I’m pretty new to godot, but I’ve made a simple shader to change the colors of a sprite to other colors. I can confirm it works, as by changing the parameters while in the editor will have effects in runtime, however, setting the parameters in code via set_instance_shader_parameter() doesn’t seem to update the sprite? It does change the parameter, but doesn’t look like it changes the sprite.
The shader code:

shader_type canvas_item;

uniform vec4 color1 : source_color;
uniform vec4 color2 : source_color;
uniform vec4 color1map: source_color;
uniform vec4 color2map: source_color;

void fragment() {
	// Called for every pixel the material is visible on.
	if (COLOR == color1) {
		COLOR = color1map;
	}
	else if (COLOR == color2) {
		COLOR = color2map;
	}
}
1 Like

According to the docs,

For a shader uniform to be assignable on a per-instance basis, it must be defined with instance uniform … rather than uniform … in the shader code.

however

Per-instance uniforms are only available in spatial (3D) shaders.

and you are using a canvas item shader. So I don’t think you can change the colors of sprites individually like that.

you need to add instance before instance uniforms.

instance uniform vec4 color1 : source_color;

instance uniforms for 2D were added in 4.4, unfortunately no one talked about it. it’s such a cool and useful feature.

1 Like

oh dang really? guess they didn’t update the docs then :skull:

the docs are locked behind the github commit system.
they haven’t even updated AnimationTree, it says nothing about the useful expressions and it has pictures from godot 3.
but I saw a PR that will update it, hopefully soon.
2D instance shaders on the other hand, who know?

1 Like

oh, interesting
yeah changing color1map and color2map to instance uniform fixed it

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.