Set a uniform variable with godot C#

DeepLearningDeepLearning Posts: 9Member

Hello there,

I am trying to to set a uniform variable to my canvas_item shader in godot. I want to set the uniform variable from code and not from the inspector. I dont find anywhere a method to do this. I have tried to use the function "GetMaterial()" of the sprite to with the shader is set but it doesnt give anything....

Has someone already done that ?

Thanks a lot. Regards.

Tags :


  • TwistedTwiglegTwistedTwigleg Posts: 2,458Admin

    I do not have very much experience with C# in Godot, but it looks like you can access/change uniform variables within a shader in C# using the SetShaderParam function, at least that is what I gather from the website.
    (Side note: Welcome to the forums)

  • DeepLearningDeepLearning Posts: 9Member

    Thanks for your reply . SetShaderParam is a method of the ShaderMaterial Class. How could I access this class by code ?

    I would like to do something like :

    ShaderMaterial shaderMaterial = GetShader(); (this line doesnt work)

  • TwistedTwiglegTwistedTwigleg Posts: 2,458Admin
    edited December 2019

    I think you need to get the material and then cast it to a ShaderMaterial class. Something like this might work (untested):

    Material node_material = ReferenceToNodeHere.GetMaterial();
    if (node_material is ShaderMaterial)
        ShaderMaterial node_shader_material = (ShaderMaterial)node_material;
        node_shader_material.SetShaderParam("param name", 10)
  • DeepLearningDeepLearning Posts: 9Member

    Finally I just to write this in the script attached to the sprite :

    (Material as ShaderMaterial).SetShaderParam("mousePosition", GetViewport().GetMousePosition());

    Thanks all.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file