How to modify shader's uniforms from a script?

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

Hi, friends!

Checked the official documentation, but couldn’t find any info.

Is it possible?

Happy New Year!!!

:bust_in_silhouette: Reply From: Zylann

Shaders don’t do anything on their own, they must be set on a material.
In 2D, there is CanvasItemMaterial. Then you can set the material on your 2D object.
And I think you can set the uniforms by doing this (for example):

sprite.get_material().set_shader_param("uniform_name", Color(1,1,1))


Mike Gringauz | 2016-12-30 16:23

:bust_in_silhouette: Reply From: jarlowrey

In 3.0


uniform float my_value;

GD Script

self.material.set_shader_param("my_value",Color(0.26, 1, .9, 1))

If you set a parent material, make sure the child has updated with the new material/shader. Otherwise will encounter null errors

Well, you are setting vec3 (Color(x,y,z)) to float (uniform float my_value)…

splite | 2018-08-24 16:13