Search Unity

Alpha cut pass and Fade pass on Surface shader

Discussion in 'Shaders' started by Harrison_Hough, Feb 16, 2017.

  1. Harrison_Hough

    Harrison_Hough

    Joined:
    Dec 3, 2015
    Posts:
    43
    Hey all!

    I am trying to make a multipass Surface shader. The first pass to be done with Alpha Cut transparency and the second pass to be done with Fade blend mode. The reason for this is because the Fade pass alone gives all sorts of Z-sorting issues and the Alpha cut on its own has horrible antialiasing. I am trying not to resort to duplicate meshes (with different shaders/materials) and hoping to do it in a multi pass shader instead.

    Firstly I cant figured out what tags or #pragmas I need for Alpha Cut transparency. I found some info here at https://docs.unity3d.com/Manual/SL-SurfaceShaders.html. which said "alphatest:VariableName - Enable alpha cutout transparency. Cutoff value is in a float variable with VariableName." Which is SUPER helpful. I tried adding "alphatest:cut" pragma but it doesnt seem to work.

    Secondly is it even possible (with surface shaders) to have multiple passes with each pass having a different rendertype/ rendermode / blendingmode?

    Anyways here's what I'm currently working with.
    Any help would be great!

    Code (CSharp):
    1.  
    2. Shader "Custom/TwoPass" {
    3.     Properties {
    4.         _Color ("Color", Color) = (1,1,1,1)
    5.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    7.         _Metallic ("Metallic", Range(0,1)) = 0.0
    8.         _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
    9.     }
    10.     SubShader {
    11.            
    12.  
    13.         Tags { "Queue"="Transparent" "IgnoreProjector" = "True"  "RenderType"="TransparentCutout" }
    14.             ZWrite off
    15.         //Blend SrcAlpha One
    16.         LOD 200
    17.        
    18.  
    19.            
    20.             //first pass alpha cut
    21.         CGPROGRAM
    22.         // Physically based Standard lighting model, and enable shadows on all light types
    23.         #pragma surface surf Standard fullforwardshadows alpha
    24.  
    25.         // Use shader model 3.0 target, to get nicer looking lighting
    26.         #pragma target 3.0
    27.  
    28.         sampler2D _MainTex;
    29.  
    30.         struct Input {
    31.             float2 uv_MainTex;
    32.         };
    33.  
    34.         half _Glossiness;
    35.         half _Metallic;
    36.         fixed4 _Color;
    37.         float _Cutoff;
    38.  
    39.         void surf (Input IN, inout SurfaceOutputStandard o) {
    40.             // Albedo comes from a texture tinted by color
    41.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    42.             float ca = tex2D(_MainTex, IN.uv_MainTex).a;
    43.             o.Albedo = c.rgb;
    44.             // Metallic and smoothness come from slider variables
    45.             o.Metallic = _Metallic;
    46.             o.Smoothness = _Glossiness;
    47.             if (ca > _Cutoff)
    48.                 o.Alpha = c.a;
    49.             else
    50.                 o.Alpha = 0;
    51.             //o.Alpha = c.a;
    52.             //o.Alpha = tex2D(_MainTex, IN.uv_MainTex).a;
    53.         }
    54.         ENDCG
    55.  
    56.            
    57.            
    58.             //first pass second pass alpha fade/blend
    59.             CGPROGRAM
    60.             // Physically based Standard lighting model, and enable shadows on all light types
    61. #pragma surface surf Standard fullforwardshadows alpha
    62.  
    63.             // Use shader model 3.0 target, to get nicer looking lighting
    64. #pragma target 3.0
    65.  
    66.             sampler2D _MainTex;
    67.  
    68.         struct Input {
    69.             float2 uv_MainTex;
    70.         };
    71.  
    72.         half _Glossiness;
    73.         half _Metallic;
    74.         fixed4 _Color;
    75.  
    76.         void surf(Input IN, inout SurfaceOutputStandard o) {
    77.             // Albedo comes from a texture tinted by color
    78.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    79.             o.Albedo = c.rgb;
    80.             // Metallic and smoothness come from slider variables
    81.             o.Metallic = _Metallic;
    82.             o.Smoothness = _Glossiness;
    83.             o.Alpha = c.a;
    84.             //o.Alpha = tex2D(_MainTex, IN.uv_MainTex).a;
    85.         }
    86.         ENDCG
    87.        
    88.     }
    89.     //FallBack "Diffuse"
    90. }
    91.  
    92.  
     
    Last edited: Feb 17, 2017