How should I organize a complex visual shader with reused values?

Godot Version

4.4.1

Question

What is the best way to organize a visual shader in the node/graph editor that has a whole lot of values that need to get used in many ways? I have a shader that does some UV parallaxing, and the final output of the parallax section is making a lot of spaghetti:

Ideally, I’d just set a variable instead of connecting everything visually, but I’m not sure the right way to do that in the visual shader editor. Should I be using a Varying to keep track of things even though they’d both be set and got within the same fragment shader? Is there a way to use something like a ParameterRef node, but to retrieve a value calculated elsewhere in the fragment shader? Should I somehow be using Expression nodes to get and set values via Godot’s shading language?

I have only just started playing with shaders (and they are amazing) but I did it like this.

In the shader you set a variable like this:

uniform vec2 camera_offset;
uniform float camera_zoom;

Then you can update these whenever you like like this:

# I set this one up in the ready function for example
BACKGROUND.material.set_shader_parameter("camera_zoom", CAMERA.zoom.x)

# I call this in the _process function
func _process(_delta: float) -> void:
    BACKGROUND.material.set_shader_parameter("camera_offset", CAMERA.global_position)

Where BACKGROUND is a ColorRect that I have applied the shader material to.

Hope that helps.

I’d love to be able to do that. My question is how to set variables in the visual shader editor depicted in my screenshot.

Oh, I thought you wanted wanted to do this in code instead of using the visual editor. Sorry. I was sure you said something like that in your original post. Anyway, my bad, I have no idea about the visual editor, sorry.

Darn. I was delighted to help with a shader question as well. :frowning:
They are just so amazing! It has been a real eye opener for me.

1 Like