[Godot 3.1] How do you assign a shader param at instantiation?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sapporovhs

I would like to assign random values to theta in order to stagger sinusoidal motion randomly at instantiation:


func _ready():


shader_type canvas_item;
uniform float theta : hint_range(0.0,3.1415);

void vertex() {
		VERTEX.x += 20.0*sin((TIME+theta)*2.0);

but the nodes are instantiated all with the same value. Can anyone help with this?

Thank you in advance.

:bust_in_silhouette: Reply From: Zylann

Your nodes all use the same ShaderMaterial. So when you modify one, you actually modify the same all the time, so all nodes end up the same.

If you want your nodes to use the same shader but have different parameters, you need to make them unique:

material = material.duplicate()
material.set_shader_param("theta", randf() * PI)

If I had forgotten, I think that checking in the inspector resource> Local to Scene is the same.

estebanmolca | 2020-01-19 02:56

This is a great work around. Thank you.

sapporovhs | 2020-01-19 20:41

:bust_in_silhouette: Reply From: estebanmolca

It is good that it is so, it is by depuration. To have a new random value each time use this function in _ready (): randomize ()