Transparency with Standard Surface shader

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:-

Shader "Custom/Test" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        void surf (Input IN, inout SurfaceOutputStandard o) {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

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

Shader "Custom/Test" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows alpha

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        void surf (Input IN, inout SurfaceOutputStandard o) {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

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...

1 Like

Same in 5.3.4f1 too :(

change Tags { "RenderType"="Opaque" } to Tags {"Queue" = "Transparent" "RenderType"="Transparent" }

2 Likes


Thanks for the reply, I am sure I have tried that, will give it ago again in 5.3.41f now


Yep, makes no difference at all :(

1 Like

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

1 Like

What version of unity are you using and how I your camera set up?

unity 5.3.3, basic camera setup, like when you create a new scene.

This is going to sound odd, but try changing your material to use a different shader (doesn't matter which one) and back to this one.

If that works then this thread talks about the problem you were experiencing.
http://forum.unity3d.com/threads/5-3p1-queue-tag-buggy.379544/#post-2464781

Please vote on this issue so this can finally get fixed and we can stop having several posts a week caused by it.
https://issuetracker.unity3d.com/issues/materials-render-queue-value-is-not-updated-until-shader-is-changed-and-reverted

@NemoKrad , please post your current shader code to have a look at it.

@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

@bgolus Made no difference switching the shaders in the material either :(

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...

This vertex fragment shader works perfectly. looks like the surface shader model is busted :(

Shader "Hidden/VertFragTest"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color("Color", Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags{ "Queue" = "AlphaTest" "RenderType" = "TransparentCutout" "IgnoreProjector" = "True" }

        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            fixed4 _Color;

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv) * _Color;

                return col;
            }
            ENDCG
        }
    }
}

Try and replicate that in a surface shader and it just does not work correctly.

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 :(

Ah, interesting, scene objects get shown through it, but not the sky box.... Both in forward render and deferred.

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...

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).

1 Like

@NemoKrad : Surface shader that works.

Shader "Custom/Transparent"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags {"Queue" = "Transparent" "RenderType"="Transparent" }
        LOD 200

        CGPROGRAM

        #pragma surface surf Standard fullforwardshadows alpha:fade
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Standard"
}

Results :
2575732--179815--transparentSurfaceShader.png

Let me know if you have any issues.

15 Likes

@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 :)