+1 vote

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

GDScript:

func _ready():
    get_material().set_shader_param("theta",randf()*3.1415)
    pass

shader:

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

void vertex() {
    if(sin(TIME+theta)>0.0){
        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.

in Engine by (18 points)

2 Answers

+3 votes
Best answer

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)
by (27,812 points)
selected by

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

This is a great work around. Thank you.

+1 vote

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 ()

by (2,048 points)
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 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 webmaster@godotengine.org with your username.