Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Transparency with Standard Surface shader

Discussion in 'Shaders' started by NemoKrad, Mar 29, 2016.

  1. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    I am trying to give my surface shader transparency, when I create a surface shader from scratch, I get the following code created by Unity:-

    Code (CSharp):
    1. Shader "Custom/Test" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 200
    11.        
    12.         CGPROGRAM
    13.         // Physically based Standard lighting model, and enable shadows on all light types
    14.         #pragma surface surf Standard fullforwardshadows
    15.  
    16.         // Use shader model 3.0 target, to get nicer looking lighting
    17.         #pragma target 3.0
    18.  
    19.         sampler2D _MainTex;
    20.  
    21.         struct Input {
    22.             float2 uv_MainTex;
    23.         };
    24.  
    25.         half _Glossiness;
    26.         half _Metallic;
    27.         fixed4 _Color;
    28.  
    29.         void surf (Input IN, inout SurfaceOutputStandard o) {
    30.             // Albedo comes from a texture tinted by color
    31.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    32.             o.Albedo = c.rgb;
    33.             // Metallic and smoothness come from slider variables
    34.             o.Metallic = _Metallic;
    35.             o.Smoothness = _Glossiness;
    36.             o.Alpha = c.a;
    37.         }
    38.         ENDCG
    39.     }
    40.     FallBack "Diffuse"
    41. }
    Which is great and it all renders fine, but if I alter the o.Alpha it makes no difference to the render, so I add "alpha" to the #pragma like this

    Code (CSharp):
    1. Shader "Custom/Test" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 200
    11.        
    12.         CGPROGRAM
    13.         // Physically based Standard lighting model, and enable shadows on all light types
    14.         #pragma surface surf Standard fullforwardshadows alpha
    15.  
    16.         // Use shader model 3.0 target, to get nicer looking lighting
    17.         #pragma target 3.0
    18.  
    19.         sampler2D _MainTex;
    20.  
    21.         struct Input {
    22.             float2 uv_MainTex;
    23.         };
    24.  
    25.         half _Glossiness;
    26.         half _Metallic;
    27.         fixed4 _Color;
    28.  
    29.         void surf (Input IN, inout SurfaceOutputStandard o) {
    30.             // Albedo comes from a texture tinted by color
    31.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    32.             o.Albedo = c.rgb;
    33.             // Metallic and smoothness come from slider variables
    34.             o.Metallic = _Metallic;
    35.             o.Smoothness = _Glossiness;
    36.             o.Alpha = c.a;
    37.         }
    38.         ENDCG
    39.     }
    40.     FallBack "Diffuse"
    41. }
    And that then allows me to alter the alpha and I can make the render transparent, however, if I do this using forward render, it does not render at all even with an alpha of 1, switch to deferred and it does, but the texture showing through is the clear color for the camera not the skybox behind, and nothing in the scene behind, just the clear color. If I switch back to forward and set the camera clear flags to Solid color, it works.

    I am testing this with 5.3.2

    To me this behavior does not seem right...
     
    Climber-fx likes this.
  2. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    Same in 5.3.4f1 too :(
     
  3. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    313
    change Tags { "RenderType"="Opaque" } to Tags {"Queue" = "Transparent" "RenderType"="Transparent" }
     
    AAAAAAAAAE and Climber-fx like this.
  4. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    Thanks for the reply, I am sure I have tried that, will give it ago again in 5.3.41f now
     
  5. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    Yep, makes no difference at all :(
     
    iperov likes this.
  6. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    313
    works for me, but depending on the blend mode you want you can also use alpha:fade instead of just "alpha" in your #pragma declaration.

    also would use ZWrite Off for transparent stuff
     
    McMayhem likes this.
  7. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    What version of unity are you using and how I your camera set up?
     
  8. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    313
    unity 5.3.3, basic camera setup, like when you create a new scene.
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
  10. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    @NemoKrad, please post your current shader code to have a look at it.
     
  11. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    @Ippokratis it's the second shader in the opening post.

    Just had a friend try it with alpha:fade and he says it's resolved this issue! o_O

    @bgolus Ill check that out thanks, sorry if I am posting about a well known issue (to others) but you know what this forum is like for searching lol
     
  12. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    @bgolus Made no difference switching the shaders in the material either :(
     
  13. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    o_O Switching the lighting to Lambert, gives even more odd effects, it's like the whole alpha blending with surface shaders is totally broken...
     
  14. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    This vertex fragment shader works perfectly. looks like the surface shader model is busted :(

    Code (CSharp):
    1. Shader "Hidden/VertFragTest"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Color("Color", Color) = (1,1,1,1)
    7.     }
    8.     SubShader
    9.     {
    10.         Tags{ "Queue" = "AlphaTest" "RenderType" = "TransparentCutout" "IgnoreProjector" = "True" }
    11.  
    12.         Blend SrcAlpha OneMinusSrcAlpha
    13.  
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.            
    20.             #include "UnityCG.cginc"
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float2 uv : TEXCOORD0;
    31.                 float4 vertex : SV_POSITION;
    32.             };
    33.  
    34.             v2f vert (appdata v)
    35.             {
    36.                 v2f o;
    37.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    38.                 o.uv = v.uv;
    39.                 return o;
    40.             }
    41.            
    42.             sampler2D _MainTex;
    43.             fixed4 _Color;
    44.  
    45.             fixed4 frag (v2f i) : SV_Target
    46.             {
    47.                 fixed4 col = tex2D(_MainTex, i.uv) * _Color;
    48.                
    49.                 return col;
    50.             }
    51.             ENDCG
    52.         }
    53.     }
    54. }
    Try and replicate that in a surface shader and it just does not work correctly.
     
  15. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    Oh, I lie, that also when alpha is 0 and camera is set to clear sky box, shows the camera clear color rather than what's behind it in the scene..... :( Looks OK in the editor again, but not in the game window :(
     
  16. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    Ah, interesting, scene objects get shown through it, but not the sky box.... Both in forward render and deferred.
     
  17. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    This is now just totally bizarre, placed a #pragma debug in another shader (same project), and now, all my broken alpha shaders are working.... this really does not bode well :(

    I hope in 5.4 stability is there...
     
  18. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    That shader above doesn't have zwrite turned off and isn't using the transparent queue but is using blending. The result is what you're seeing is expected behavior. You need to do those two things as suggested by the thread already and then do the shader switch (the bug is changing queues on a shader after it's been set on a material doesn't update the material properly because of a change Unity made that enforces the initial queue rather than just always using the one in the shader).
     
    NemoKrad likes this.
  19. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    @NemoKrad : Surface shader that works.

    Code (CSharp):
    1. Shader "Custom/Transparent"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags {"Queue" = "Transparent" "RenderType"="Transparent" }
    13.         LOD 200
    14.    
    15.         CGPROGRAM
    16.  
    17.         #pragma surface surf Standard fullforwardshadows alpha:fade
    18.         #pragma target 3.0
    19.  
    20.         sampler2D _MainTex;
    21.  
    22.         struct Input {
    23.             float2 uv_MainTex;
    24.         };
    25.  
    26.         half _Glossiness;
    27.         half _Metallic;
    28.         fixed4 _Color;
    29.  
    30.         void surf (Input IN, inout SurfaceOutputStandard o)
    31.         {
    32.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    33.             o.Albedo = c.rgb;
    34.             o.Metallic = _Metallic;
    35.             o.Smoothness = _Glossiness;
    36.             o.Alpha = c.a;
    37.         }
    38.         ENDCG
    39.     }
    40.     FallBack "Standard"
    41. }
    42.  
    Results :
    transparentSurfaceShader.png

    Let me know if you have any issues.
     
  20. NemoKrad

    NemoKrad

    Joined:
    Jan 16, 2014
    Posts:
    632
    @Ippokratis & @bgolus

    Thanks guys, but as I said, the same shader I have at the top is working now, I agree should have the Que and Type set, think I did, set them back to the original post and they still work now.

    It looks like Unity is not taking the changes into account. Bottom line is I have it working now, thanks for your time and input, shame the pipeline is so buggy at the moment, hope 5.4 sorts all this out, looking forward to some stability :)
     
  21. babyspinach

    babyspinach

    Joined:
    Mar 2, 2017
    Posts:
    1
    you'r shader can receive shadow?
     
  22. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,866
    Answer for new readers
    Only Cutout shaders can receives shadows. Transparent and Fade mode shaders does not receives shadows

    Cutout : Tree,grass ...
    Transparent : Glass
    Fade : other ...
     
  23. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    80
    :D سلام رئیس
    you should use alpha

    Code (CSharp):
    1. Shader "Custom/Transparent"
    2.     {
    3.         Properties
    4.         {
    5.             _Color ("Color", Color) = (1,1,1,1)
    6.             _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.             _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.             _Metallic ("Metallic", Range(0,1)) = 0.0
    9.         }
    10.         SubShader
    11.         {
    12.              Tags {"RenderType"="Transparent" "Queue"="Transparent"}
    13.             LOD 200
    14.              Pass {
    15.                  ColorMask 0
    16.              }
    17.              // Render normally
    18.      
    19.                  ZWrite Off
    20.                  Blend SrcAlpha OneMinusSrcAlpha
    21.                  ColorMask RGB
    22.  
    23.  
    24.  
    25.  
    26.    
    27.             CGPROGRAM
    28.  
    29.             #pragma surface surf Standard fullforwardshadows alpha:fade
    30.             #pragma target 3.0
    31.  
    32.  
    33.  
    34.             sampler2D _MainTex;
    35.  
    36.             struct Input {
    37.                 float2 uv_MainTex;
    38.             };
    39.  
    40.             half _Glossiness;
    41.             half _Metallic;
    42.             fixed4 _Color;
    43.  
    44.             void surf (Input IN, inout SurfaceOutputStandard o)
    45.             {
    46.                 fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    47.                 o.Albedo = c.rgb;
    48.                 o.Metallic = _Metallic;
    49.                 o.Smoothness = _Glossiness;
    50.                 o.Alpha = c.a;
    51.             }
    52.             ENDCG
    53.         }
    54.         FallBack "Standard"
    55.     }
     
    Last edited: Jul 25, 2017
  24. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    972
    fading.jpg
    That seems to be true in realtime rendering.
    But in baked does not as you can see on this example.
     
    Last edited: Jul 31, 2017
  25. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    291
    Had the same problems in 2017.1.
    Reason shader always alpha explicitly set. So something like:

    void surf (Input IN, inout SurfaceOutputStandard o)
    {
    o.Albedo = _Color;
    }

    will not work. This works::

    void surf (Input IN, inout SurfaceOutputStandard o)
    {

    //fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    //o.Albedo = c.rgb;
    o.Albedo = _Color;

    //o.Metallic = _Metallic;
    //o.Smoothness = _Glossiness;
    //o.Alpha = c.a;
    o.Alpha = _Color.a;

    }