Access and change the values of a gradient texture via code

Godot Version



Hello everyone,
I have a Sprite 3d with a gradient texture 2D applied.
I would like to change the gradient value via script, in particular the “fill_to” value and the “raw_data offsets” of specific colors inside the gradient in order to move each of them individually. Is it possible? And if so, how can I achieve that?
Sorry for the noob question, and thank you in advance for your time and help.

You can access the ShaderMaterial via GDScript and then you will use the set_shader_parameter ( StringName param, Variant value ) method which will probably return a GradientTexture1D. You assign it to a new variable and there you can use the available methods to change the gradient. After changing it you assign it back to the ShaderMaterial by using the set_shader_parameter ( StringName param, Variant value ) method

Thank you for helping me!
I’m sure the process you suggest is correct, my problem is that every time I look at the documentation I struggle to understand the correct syntax to be used.
I grabbed my sprite 3d with this:

@onready var _screen = $screenHolder/screen

where “screen” is the name of the sprite 3d with the gradient2dtexture applied.
Then I say:

_screen.set_shader_parameter(GradientTexture2D(this is what I want to change), fill_to,(this is the parameter I want to change) Vector2(0.7,0.7)(I use a vector2 because fill_to expect a vector2))

But it gives me an error:

Identifier “fill_to” is not declared in the current scope.

I always struggle with this. I think I get the idea, but I always find it difficult to write it properly without an example.
Here is the clean code used:

_screen.set_shader_parameter(GradientTexture2D, fill_to, Vector2(1,1))

I’m sorry to bother you with this simple problem, thank you again for your time and patience.

Ah gotcha. Well, you try to search up the Node Type you are working with in the docs and then search what methods you have available. So for instance the Sprite3D is inheriting from GeometryInstance3D which has the methods get_instance_shader_parameter ( StringName name ) and set_instance_shader_parameter ( StringName name, Variant value ). So you best use those then as the ones I suggested before aren’t available.
From there on you go ahead with the rest from above.
And no problem. Happy to help :smile:

Ok, now it’s better, and, to be fair, godot suggested me " get_instance_shader_parameter " and I refused stupidly…
Now, using this one, it offers me “GradientTexture2D” as string name, but ther is no Fill_to option as variant, even if Fill_to is indicated as a property in the documentation:

What am I supposed to put as “variant”?
Playing around, I also noted that I can type :
witch seems to be what I’m looking for, but how do I grab the GradientTexture2D?
It also say:
Cannot call non-static function “set_fill_to” on the class “GradientTexture2D” directly. Make an instance instead.
I know godot is trying to help me, but I’m too noob to understand…
How can I create an instance?
Thank you again!

The get_instance_shader_parameter should return the GradientTexture2D so you assign it to a variable. From there you can access the gradient part of the GradientTexture2D

Ok, thank you, I’ll try and let you know. Till then, thank you again!

Ok, I tried with:

var gradient

gradient = _screen.get_instance_shader_parameter(GradientTexture2D)

but it say: Invalid type in function ‘get_instance_shader_parameter’ in base ‘Sprite3D (’. Cannot convert argument 1 from Object to StringName.

It is surely again my fault, but I really can’t figure it out…

get_instance_shader_parameter(GradientTexture2D) is wrong. The method get_instance_shader_parameter is taking a String in the name of your shader parameter as an input to read it. So:

var gradient_texture = _screen.get_instance_shader_parameter("shader_parameter_name")

where shader_parameter_name is the name of your texture uniform you set in the shader.