Search Unity

Master shader opacity

Discussion in 'Shaders' started by tinning, May 27, 2015.

  1. tinning

    tinning

    Joined:
    May 18, 2015
    Posts:
    4
    Hi all, I have a bunch of objects I want to fade in when they are spawned. They all have different materials, or multiple materials etc. Is there a way of adding some kind of overall opacity to each object? Or does it have to be done at the shader level?

    If its the shader level, is there a way of adding a master opacity? One that would multiple everything by its value?

    For example, I have this shader that is already using alpha to blend between two textures... is it possible to add a master opacity to this?
    Code (CSharp):
    1.  Shader "Custom/AlphaBlendTransition" {
    2. Properties {
    3.      _Blend ("Blend", Range (0, 1) ) = 0.0
    4.      _BaseTexture ("Base Texture", 2D) = "" {}
    5.      _OverlayTexture ("Texture 2 with alpha", 2D) = "" {}
    6. }
    7. SubShader {
    8.          Pass {
    9.              SetTexture[_BaseTexture]
    10.              SetTexture[_OverlayTexture] {
    11.                  ConstantColor (0,0,0, [_Blend])
    12.                  combine texture Lerp(constant) previous
    13.              }
    14.          }
    15.      }
    16. }
    Thanks!
     
  2. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    I'm not entirely sure how to do it with SetTexture, but for a typical surface shader, it is fairly simple.

    Taking the first example in Unity's surface shader page ( http://docs.unity3d.com/460/Documentation/Manual/SL-SurfaceShaderExamples.html )

    Code (csharp):
    1.  
    2. Shader "Example/Diffuse Simple" {
    3.     SubShader
    4.     {
    5.         Tags { "RenderType" = "Opaque" }
    6.         CGPROGRAM
    7.         #pragma surface surf Lambert
    8.         struct Input
    9.         {
    10.             float4 color : COLOR;
    11.         };
    12.         void surf (Input IN, inout SurfaceOutput o)
    13.         {
    14.             o.Albedo = 1;
    15.         }
    16.         ENDCG
    17.     }
    18.     Fallback "Diffuse"
    19. }
    20.  
    You can add a variable into the shader, anywhere in the CGPROGRAM block, as an example:

    Code (csharp):
    1.  
    2. Shader "Example/Diffuse Simple" {
    3.     SubShader
    4.     {
    5.         Tags { "RenderType" = "Opaque" }
    6.         CGPROGRAM
    7.         #pragma surface surf Lambert
    8.  
    9.         float _Opacity; //add the variable into here!
    10.  
    11.         struct Input
    12.         {
    13.             float4 color : COLOR;
    14.         };
    15.         void surf (Input IN, inout SurfaceOutput o)
    16.         {
    17.             o.Albedo = 1;
    18.         }
    19.         ENDCG
    20.     }
    21.     Fallback "Diffuse"
    22. }
    23.  
    And then you can very easily set that variable from script with

    Code (csharp):
    1.  
    2. Shader.SetGlobalFloat("_Opacity", value);
    3.  
    You can also use Material.SetFloat on a per material basis, or you can assign textures, vectors, and more if you go through the API.
     
  3. tinning

    tinning

    Joined:
    May 18, 2015
    Posts:
    4
    Thanks for the reply. I understand how to add the float, I'm just stuck with making it affect the shader. How would I then apply that opacity value to the overall shader? Does it need another pass, so it affects everything before it? I've been looking at other shaders to try and splice two together, but no luck, just standard colour or textures with alpha channels.
    Cheers
     
  4. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    Code (CSharp):
    1. Shader "Example/Diffuse Simple" {
    2.     SubShader
    3.     {
    4.         Tags { "RenderType" = "Opaque" }
    5.         CGPROGRAM
    6.         #pragma surface surf Lambert
    7.         float _Opacity; //add the variable into here!
    8.         struct Input
    9.         {
    10.             float4 color : COLOR;
    11.         };
    12.         void surf (Input IN, inout SurfaceOutput o)
    13.         {
    14.             o.Albedo =  _Opacity;
    15.         }
    16.         ENDCG
    17.     }
    18.     Fallback "Diffuse"
    19. }
    If you actually have a texture just multiply the normally output alpha with the global opacity.
    Your shader example uses fixed function stuff, and is kinda irritating/hard to modify...