Godot 4.2
I have a shader that changes the hue of an AnimatedSprite2D. Here is the shader:
shader_type canvas_item;
uniform vec4 color : source_color;
void fragment(){
vec4 pixelColor = texture(TEXTURE, UV);
float brightness = (0.299 * pixelColor.r + 0.587 * pixelColor.g + 0.114 * pixelColor.b);
vec4 greyScale = vec4(brightness, brightness, brightness, pixelColor.a);
COLOR = greyScale * color;
I would like to be able to change the color through code, so I've been looking into how to use set_shader_parameter. Just as a test, I've set up this code
func _on_button_pressed():
material.set_shader_parameter("color", [Vector4(1.0, 0.3, 0.5, 1.0)])
However, once I click the button, the AnimatedSprite2D either disappears or becomes invisible.
I am new to shaders, so there may be a step I am missing!
Shader parameters in Godot 4 need to be set using the correct data type. In your code, the color parameter has a vec4. If this is the case, then it needs to pass it as a color, not a vector4.
Simply change Vector4 to Color for the shader parameter and reference it.