Search Unity

AlphaTest Questions

Discussion in 'Shaders' started by Phantomx, Sep 10, 2014.

  1. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Hey,
    I am doing some shaders using alphaTest and I must say I ran into some weird stuff.

    1- Shader with no prepass in deferred rendering path
    The object will have it's cutout parts rendered in black if the shader has a property _Color (even if it's not used). Note: Same effect if the _Cutoff property is named something else.

    The Shader:
    Code (CSharp):
    1. Shader "Custom/AlphaTest" {
    2.     Properties {
    3.         _Color ("MainColor",Color) = (0,0,0,0)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _DisolveTex("Disolve Texture", 2D) = "white" {}
    6.         _Cutoff("Disolve Power",Range(0,1)) = 0
    7.        
    8.     }
    9.     SubShader {
    10.         Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
    11.         LOD 200
    12.        
    13.         CGPROGRAM
    14.         #pragma surface surf Lambert exclude_path:prepass alphatest:Zero
    15.  
    16.         sampler2D _MainTex;
    17.         sampler2D _DisolveTex;
    18.         float _Cutoff;
    19.  
    20.         struct Input {
    21.             float2 uv_MainTex;
    22.         };
    23.  
    24.         void surf (Input IN, inout SurfaceOutput o) {
    25.             fixed3 tex = tex2D (_MainTex, IN.uv_MainTex);
    26.             fixed  alpha = tex2D (_DisolveTex, IN.uv_MainTex).r;
    27.            
    28.             o.Albedo = tex;
    29.             o.Alpha = alpha - _Cutoff;
    30.         }
    31.         ENDCG
    32.     }
    33.     FallBack "Diffuse"
    34. }
    35.  
    The look (a bit hard to see but the black part is not the space background, it's just black):
    1.jpg

    2- Same Shader, no _Color no prepass

    the shader doesn't write to the zbuffer as soon as the _Cutoff property is not equal to 0. so post effects like DOF will break. Adding a _Color property will break alpha test but it will write in Z again.

    2.jpg


    Why isn't there any documentation on this and why do these variable have ( or not ) to be there with that exact name?!