0 votes

I would love to know how to access/change a visual shader material's uniform parameters from within a GD script.
Scenario: I create a visual shader material for a given mesh, and I want to tweak the material from my GD script for a given game event.

I assume I go to

$sphere.mesh.surface_get_material(0).  #....something goes here

... and after that I have no idea how to access the "shader_param/AlphaKnob" or whatever other uniform variable I've put in there. I can easily change in in the editor, I just can't figure out how to access it from code (script).

Can someone give me the syntax, or better, an example of how I would get at this uniform variable?

Thanks in advance :)

enter image description here

Specific example: I have a visual shader with a ScalarUniform connected to the Alpha output. I named it "AlphaKnob". AlphaKnob is now exposed in the editor, and I can change it in the editor and that works great.
Now I want to change "AlphaKnob" from within a GDscript.
The "float over" text says it's called "shader_param/AlphaKnob".
What am I typing into my GDscript to change the value of AlphaKnob to 0.5, or anything else ?

in Engine by (328 points)
edited by

1 Answer

+2 votes
Best answer

Basically there's two ways to do it:

by (218 points)
selected by

So for the record, that worked. Here's the commands:

so if the texture is on the NODE, you would use:

$sphere.get_surface_material(0).set("shader_param/AlphaKnob", 0.5)

and if the texture is on the MESH, (like it is when it comes over from Blender via gLTF2), you would use:

$sphere.mesh.surface_get_material(0).set("shader_param/AlphaKnob", 0.5)

Also note that the commands are different depending on whether the texture is on the mesh or on the node. The "0" in both cases refers to the material in that slot, so if you have materials 0,1,2 etc., you're referencing the correct one (but if you only have one material, it's 0.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.