Activating a shader only when needed from GDScript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HatEm
:warning: Old Version Published before Godot 3 was released.

I’m new to shading in general and I want to know how to activate a shader when needed using GDScript, for example on taking damage, entering some kind of poison spell or anything like that

:bust_in_silhouette: Reply From: Zylann

Shaders go inside a Material resource, and Materials are assigned to visual nodes so their appearance can change. Then you can change the material on the fly.
Godot has both a 2D and 3D engine, so the way to do that will slightly differ between the two.

In 2D:

sprite.set_material(new_material)

In 3D:

meshinstance.set_material_override(new_material)

Note that for the use you describe, you are not forced to use a shader to change the appearance of your node, especially in 2D (you can change its color or texture with modulate or texture properties).

:bust_in_silhouette: Reply From: eons

You can have an always active shader with some uniform that acts as trigger (to use with an if) for specific stuff or uniforms that null the shader effect.

Or toggle visibility if using stuff like TextureFrame nodes for shaders.

I’m not sure if these ways affects performance in some way (for simple fragments must be harmless).


But as Zylann said, you have other options too, like modulate, lights, overlapping sprites with different blending…