Topic was automatically imported from the old Question2Answer platform.
Asked By
Flying_cupcake
I have a sprite and i am going to want to apply a shader to it when my mouse hovers over it. I have some working code for the shader, but I am stuck when it comes to turning it on and off at will. Is there some bit of code that allows me to target the shader in some sort of choice to have it ON or OFF as needed.
You can influence shaders code from GDScript by set_param() method
It will influence any shader variable introduced as uniform
So You can create uniform boolean variable in shader and calculate effect with it.
For example :
I apologize that I’m reviving a dead thread, but for anyone whos doing anything similar to me, I want to offer a solution as this did not do what I needed it to. Also, if I’m not mistaken this also just turns your sprite invisible too no? Or at least it did for me.
Really all you seemingly need to do is:
var entity_here: Sprite2D
# whenever you want to turn it off ENTIRELY
func turn_off_shader() -> void:
entity_here.material = null
I haven’t seen an error pop up as a result of doing this, but there may be some issue with it, though I’d think by default your material is kept as null for optimization if you don’t add one yourself. No need to add anything to your shader, though if you want to re-enable it, you’ll need to have a reference to the original material you used.