Setting a shader parameter on a noise in gdscript - how?

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

Hi All. I have a simple shader where that uses Sampler2d with a noise texture.

In gdscript, I want to animate the offset parameter on the noise. I’m using this code:

func _physics_process(delta):
	time += delta
	self.material.set_shader_param("noise_img/noise/offset:x", time)

It prints correctly - the x offset parameter seems to be going up as expected. However, the noise is completely static. When I slide the x offset parameter in the editor, the noise is moving fine. The above code is not doing anything in runtime though, except print the correct value.

Interestingly, if use this:


to print the param, it remains static (unaffected by the set_shader_param).
It looks like it’s not really assigning the value to the param after all?

BTW, not sure it matters but the shader itself is a fog() shader in Godot 4.

:bust_in_silhouette: Reply From: Inces

I doubt shader will update information about change of sampler source like this. It is better to code offset manually inside shader as another uniform. Then You can add this uniformed offset to UV.x of your sampled noise. If You didn’t understand this paste code of your shader and I will help You adapt it

Thanks. I ended up doing what you suggested: I enabled repeat on the sampler2d and went uv.x*TIME*0.1 on the texture in the shader code

Macryc | 2022-04-30 09:48