I wanna use shader parameter in gdscript

Godot Version

4.2.1

Question

I have this fog shader that I got from internet. now I want to change some of its parameters from code based on the movement of the character. how would I do that? I tried using @onready and it didnt work exactly and the game crashed.


now I know that im using the wrong commands. please help.

Here is the basic structure on how to do it:

node.material.set("shader_paramater/density", 0.5)

Or,

node.material.set_shader_parameter("density", value) #Proper way

I think you need to replace node with fog as of your codes but the above is just the structure.

You can read the shader docs of Godot for more information.

1 Like

ok I did this:

@onready var fog=$CanvasLayer/ColorRect3
@onready var density=fog.material.set("shader_paramater/density", 0.5)

but gives me an error:
image

Not inside @onready, place it in a function.
Do you want something else?

im sorry, but can you write the code? i really dont understand how to do it

Here it is:

func _ready():
    fog.material.set("shader_paramater/density", 0.5)
1 Like

ohhhh… ok I get it

1 Like

1 more question, can I set a variable for the shader material?
something like fogcolor="shader_parameter/color"

If you want to get that value only, then you can do like this:

var fog_color = node.material.get("shader_parameter/fog_color")

Or if you can to automatically set the shader parameter by changing the variable then you can use setget method.

1 Like