Reference Shader Parameter in code

Godot Version

4.2.1

Question

I’m making a Pause Menu for a 3D game, I added a ColorRect to the menu and added a blur shader under “Inspector-CanvasItem-Material” and I have the code for the opening and closing of the Pause Menu in the Main Scene.

I want to make the blur fade in when you pause, and I do have a blur parameter in the shader. How do I access it in the code?

get_shader_parameter and set_shader_parameter

If I change this variable
@onready var Blur = $PauseMenu/ColorRect.material.get_shader_parameter('blur_amount')
It doesn’t give me any errors anymore, but it doesn’t seem to work. Thank you btw

$PauseMenu/ColorRect.material.set_shader_parameter('blur_amount',10.0)
for this, use set

It gives me this error:
Only identifier, attribute access, and subscription access can be used as assignment target.
Maybe it is because the shader is assigned to the canvasItem and not the colorrect itself, but I’m not sure because changing ColorRect to CanvasItem does the same

colorrect or any Control inherits type class node has canvasItem being as its ancestor, so in theory, colorrect should have its own canvasitem’s properties

make it like this

@onready var color_rect_node = $PauseMenu/ColorRect
@export blur_shader_material:ShaderMaterial

assign the blur shader material at inspector dock
then

to put the blur shader via code, you just need to

color_rect_node.material=blur_shader_material

this doesnt need to change the blur_amount uniform property of the shader, so set the default blur amount to where it blurs

when unpause, you can just set the material to null, so the blur gone

But if you really want to make the blur kind of animation from blur to not blur and from not blur to blur, then you need to access it via set_shader_parameter

1 Like

I’m getting an error that there is an unexpected “Identifier” in the class body, at the @export line. I’m sorry that I’m asking so much from you but thanks

is that from the newest snippet or the one you have right now by assigning the blur right away as variable of @onready?

In this line:

@export blur_shader_material:ShaderMaterial

sorry i forgot the var after @export
it should be @export var blur_shader_material:ShaderMaterial

Ok thank you it works!

1 Like

Godot is so flaky. Trying to change the is_shaking parameter of my TextureButton .

This works:

@onready var texture_button = $TextureButton

func _on_start_pressed():
    texture_button.material.set_shader_parameter('is_shaking',true)

While this does not:

@onready var texture_button = $TextureButton
func _on_start_pressed():
    var btn_mat = texture_button.material
    var is_shaking = btn_mat.get_shader_parameter("is_shaking")
    btn_mat.set_shader_parameter("is_shaking", !is_shaking)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.