Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | JayFi |
I am adding a shader to the Material option of an Image, and also of a TextureButton to allow making the image or button greyscale when needed. (Godot 3.5) The “Greyscale” radio box appears in the editor, and I can turn the shader on and off manually and it works fine.
Greyscale shader
How do I activate/deactivate the shader correctly in code? This doesn’t seem to work. I thought it was at times, but now it is not. I am not sure what I am doing wrong.
$Main/Center/.../TextureButton.get_material().set_shader_param("greyscale",true)
$Main/Center/.../Image.get_material().set_shader_param("greyscale",true)
In case it matters, here’s the code I am using, it was from another post here I believe:
shader_type canvas_item;
uniform bool grayscale = false;
void fragment() {
COLOR = texture(TEXTURE, UV);
if (grayscale) {
float avg = (COLOR.r + COLOR.g + COLOR.b) / 3.0;
COLOR.rgb = vec3(avg);
}
}