0 votes

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);
    }
}
Godot version 3.5 rc3
in Engine by (23 points)
edited by

1 Answer

+1 vote
Best answer

There seems to be a typo, could that be why it's not working?

set_shader_param("greyscale", true)

vs

uniform bool grayscale = false;

by (860 points)
selected by

OMG! I spent hours on that. Thats literally all it was. UK / US spelling strikes again.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.