I'm trying to create a custom water shader but I can't figure out how to pass uniforms from GDScript. Has anyone done this before?
I've tried using ShaderMaterial but the documentation is sparse. Any help would be appreciated!
Building on what ModeratorMax said, for water shaders specifically you'll want to use TIME built-in and combine it with noise textures. Here's a complete water shader:
shader_type spatial;
uniform float wave_speed : hint_range(0.0, 5.0) = 1.0;
uniform sampler2D noise_tex;
void fragment() {
vec2 uv = UV + TIME wave_speed 0.01;
float noise = texture(noise_tex, uv).r;
ALBEDO = vec3(0.0, 0.3 + noise * 0.2, 0.8);
ALPHA = 0.8;
}
This gives you a basic animated water effect. You can extend it with reflection and refraction.
You need to use set_shader_parameter() on the ShaderMaterial. Here's a quick example:
var mat = ShaderMaterial.new()
mat.shader = preload("res://shaders/water.gdshader")
mat.set_shader_parameter("wave_speed", 2.0)This is exactly what I needed! The noise texture approach works perfectly. Thanks @CodeCrafter!
For anyone else looking at this, I'd also recommend checking out the Grai Engine shader tutorial series on the docs site. It covers advanced techniques like screen-space reflections.