1

I wish to control the transparency of a model during runtime.

The model's material is connected to a shader graph. After changing the Surface Type of the shader graph to Transparent, an Alpha attribute became available to control the transparency of the model (shown in the image at the bottom).

enter image description here

However, I do not know how to access this Alpha attribute in the script.

I have tried OBJ.GetComponent<Renderer>().material.SetColor() and it did not work, since Material 'MATERIAL_NAME' with Shader 'SHADER_NAME' doesn't have a color property '_Color'.

Is there a way I can access this Alpha attribute outside of the shader graph in a script?

Amarth Gûl
  • 1,040
  • 2
  • 14
  • 33

3 Answers3

1

To set the alpha in your material, you have to provide a variable that is passed in the output. At this moment, just a constant 1 is passed instead.

You have basically two options:

  1. A separate scalar parameter just for setting the alpha
  2. Utilizing a scalar component of a vector parameter (like the main color)

As you can see, the Base Color uses a vector3 (indicated by the 3 behind the name), so you could just use the source of that (if it has actually 4 components, that is RGBA) and pass the fourth one (the A) into alpha.

You have to show more of the graph, if you want more details, but the most basic thing you could do is:

Add a scalar variable "alpha" to your material. Pass the value to the output "Alpha(1)". You can access "alpha" as a scalar parameter in your script with SetFloat("alpha", yourAlphaValue).

Max Play
  • 3,717
  • 1
  • 22
  • 39
  • Could you illustrate a bit more on how to add a variable to a material and how to pass the value of a material to a shader graph? Especially since the material's `Surface Inputs` are all gone once a shader graph is selected as the shader. – Amarth Gûl Apr 17 '23 at 20:08
  • Let me refer you to the [documentation](https://docs.unity3d.com/Packages/com.unity.shadergraph@16.0/manual/Blackboard.html) for this question. I hope it helps you. – Max Play Apr 18 '23 at 08:12
  • This documentation turned out to be very helpful, thank you – Amarth Gûl Apr 18 '23 at 14:33
1

In order to create publicly available variables in your shader graph, you are going to want to open the Blackboard window and create a new Float type variable. Use the Graph Inspector window to mark your variable as exposed and give it a custom reference if desired. Once exposed, this variable can be changed from the material window or from a C# script using the reference name.

You can drag this variable out of the Blackboard window and connect it to the alpha in order to control that property.

Unity graph inspector

Kit MacAllister
  • 234
  • 1
  • 8
0

You can change the alpha by passing as a 4th parameter when setting the color. Like this:

void ChangeColor(float alpha){
    var currentColor = OBJ.GetComponent<Renderer>().material.GetColor("SHADER_VARIABLE");

    OBJ.GetComponent<Renderer>().material.SetColor("SHADER_VARIABLE", new Color(currentColor.r, currentColor.g, currentColor.b, alpha));
}

EDIT 1

public Material myMat;

void ChangeColor(float alpha){
    var currentColor = OBJ.GetComponent<Renderer>().material.GetColor("SHADER_VARIABLE");

    myMat.color = new Color(currentColor.r, currentColor.g, currentColor.b, alpha);

    OBJ.GetComponent<Renderer>().material = myMat;
}
Raphael Frei
  • 361
  • 1
  • 10
  • This was precisely what I did and it did not work. – Amarth Gûl Apr 17 '23 at 19:45
  • What version you're using? Instead of trying to access direct into the material, try creating a temp material and later on setting to the object. – Raphael Frei Apr 17 '23 at 19:47
  • 2021.3.6f1. And unfortunately, this is not likely to work. The goal is to change the opacity gradually in real-time and I don't think creating hundreds of materials to apply them one by one is a good idea, let alone replicating the special effects of the shader graph already in use – Amarth Gûl Apr 17 '23 at 19:51
  • The temp material is only on the code... I've assigned an edit to the code, see if that works – Raphael Frei Apr 17 '23 at 19:52