Search Unity

Alpha no longer affecting transparency after Unity 5 upgrade

Discussion in 'Shaders' started by Wolfos, Mar 25, 2015.

  1. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    735
    We have a shader that bases alpha off distance from the center (so basically you get a radial gradient), and it used to work before but now it seems alpha no longer affects transparency at all. When multiplying albedo by alpha, the parts that should be transparent simply show up as black.

    Here is the shader source:
    Code (ShaderLab):
    1. Shader "MaskedTexture"
    2. {
    3.   Properties {
    4.         _MainTex ("Main Texture", 2D) = "white" {}
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _Distort("Distort", vector) = (0.5, 0.5, 1.0, 1.0)
    7.         _OuterRadius ("Outer Radius", float) = 0.5
    8.         _InnerRadius ("Inner Radius", float) = -0.5
    9.         _Hardness("Hardness", float) = 1.0
    10.     }
    11.     SubShader {
    12.         Tags { "RenderType"="Transparent" "Queue"="Transparent" "AllowProjectors"="False" }
    13.         blend SrcAlpha OneMinusSrcAlpha
    14.         CGPROGRAM
    15.         #pragma surface surf NoLighting
    16.         fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
    17.         {
    18.             return fixed4(s.Albedo, s.Alpha);
    19.         }
    20.         sampler2D _MainTex;
    21.         struct Input
    22.         {
    23.             float2 uv_MainTex;
    24.         };
    25.         float4 _Color, _Distort;
    26.         float _OuterRadius, _InnerRadius, _Hardness;
    27.         void surf (Input IN, inout SurfaceOutput o)
    28.         {
    29.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    30.             float x = length((_Distort.xy - IN.uv_MainTex.xy) * _Distort.zw);
    31.             float rc = (_OuterRadius + _InnerRadius) * 0.5f; // "central" radius
    32.             float rd = _OuterRadius - rc; // distance from "central" radius to edge radii
    33.            
    34.  
    35.             float circleTest = saturate(abs(x - rc) / rd);
    36.             o.Alpha = (1.0f - pow(circleTest, _Hardness)) * _Color.a * c.a;
    37.             o.Albedo = _Color.rgb * c.rgb * o.Alpha;
    38.         }
    39.         ENDCG
    40.     }
    41.     FallBack "Diffuse"
    42. }
     
  2. UnityGuillaume

    UnityGuillaume

    Unity Technologies

    Joined:
    Mar 16, 2015
    Posts:
    121
    Surface shader generate multiple shaders. And when a surface shader don't specify the "alpha" keyword in its pragma, I think it don't write any alpha out.

    If I get the doc correctly, you need to add the keepalpha tag in the pragma , see doc here : http://docs.unity3d.com/Manual/SL-SurfaceShaders.html

    Sidenote : you seems to need a simple alpha blending, so removing the BLEND operation and adding alpha:fade in your pragma should also do the trick, Unity will take care of generating shader with the BLEND operation and with ZWrite disabled etc.
     
  3. Proxi

    Proxi

    Joined:
    Jan 15, 2015
    Posts:
    5
    Replacing line 15 with:

    Code (ShaderLab):
    1. #pragma surface surf NoLighting keepalpha
    worked for us, thanks!
     
    comworm likes this.