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.
KingGD
February 2, 2025, 4:31pm
2
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:
KingGD
February 2, 2025, 5:12pm
4
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
KingGD
February 2, 2025, 5:45pm
6
Here it is:
func _ready():
fog.material.set("shader_paramater/density", 0.5)
1 Like
1 more question, can I set a variable for the shader material?
something like fogcolor="shader_parameter/color"
KingGD
February 2, 2025, 6:50pm
9
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