Search Unity

Shader Alpha Cutout

Discussion in 'Shaders' started by unitynewb, Aug 5, 2017.

  1. unitynewb

    unitynewb

    Joined:
    Feb 22, 2009
    Posts:
    243
    My app currently uses multiple shaders based on users selection. I wanted to add a feature to each shader that will cutout parts of the object based on an alpha image.

    In my shader below, you'll notice I have a separate pass in the end of the cutout, this is basically adding the image on top of the model over the existing color, I want to have it cutout instead of actually drawing the image over the material. I would even settle with having the reverse effect where the alpha portion would be where it shows the original material.

    Code (CSharp):
    1.  
    2. Shader "Opaque Reflective"
    3. {
    4.     Properties
    5.     {
    6.         _BaseColor("Base Color", Color) = (1.0, 1.0, 1.0, 1.0)
    7.         _DetailColor("Detail Color", Color) = (1.0, 1.0, 1.0, 1.0)
    8.         _DetailMap("Detail Map", 2D) = "white" {}
    9.         _DetailMapDepthBias("Detail Map Depth Bias", Float) = 1.0
    10.         _DiffuseColor("Diffuse Color", Color) = (1.0, 1.0, 1.0, 1.0)
    11.         _DiffuseMap("Diffuse Map", 2D) = "white" {}
    12.         _Cutout("Cutout", 2D) = "" {}
    13.         _NormalMap("Normal Map", 2D) = "bump" {}
    14.         _ReflectionColor("Reflection Color", Color) = (1.0, 1.0, 1.0, 1.0)
    15.         _ReflectionMap("Reflection Map", Cube) = "" {}
    16.         _ReflectionStrength("Reflection Strength", Range(0.0, 1.0)) = 0.5
    17.     }
    18.  
    19.     SubShader
    20.     {
    21.         Tags
    22.         {
    23.             "Queue" = "Geometry"
    24.             "RenderType" = "Opaque"
    25.         }
    26.  
    27.         Blend Off
    28.         Cull Back
    29.         ZWrite On
    30.  
    31.         CGPROGRAM
    32.         #pragma target 3.0
    33.         #pragma surface SurfaceMain Lambert vertex:VertexMain
    34.  
    35.         float4 _BaseColor;
    36.         float4 _DetailColor;
    37.         sampler2D _DetailMap;
    38.         float _DetailMapDepthBias;
    39.         float4 _DiffuseColor;
    40.         sampler2D _DiffuseMap;
    41.         sampler2D _Cutout;
    42.         sampler2D _NormalMap;
    43.         float4 _ReflectionColor;
    44.         samplerCUBE _ReflectionMap;
    45.         float _ReflectionStrength;
    46.  
    47.         struct Input
    48.         {
    49.             float depth;
    50.             float2 uv_DetailMap;
    51.             float2 uv_DiffuseMap;
    52.             float2 uv_NormalMap;
    53.             float3 worldRefl;
    54.             INTERNAL_DATA
    55.         };
    56.  
    57.         void SurfaceMain(Input input, inout SurfaceOutput output)
    58.         {
    59.             output.Normal = UnpackNormal(tex2D(_NormalMap, input.uv_NormalMap));
    60.  
    61.             float3 reflectionColor = texCUBE(_ReflectionMap, WorldReflectionVector(input, output.Normal)).rgb * _ReflectionColor.rgb;
    62.             float4 diffuseColor = tex2D(_DiffuseMap, input.uv_DiffuseMap) * _DiffuseColor;
    63.             float3 finalColor = lerp(lerp(_BaseColor.rgb, diffuseColor.rgb, diffuseColor.a), reflectionColor, _ReflectionStrength);
    64.  
    65.             float3 detailMask = tex2D(_DetailMap, input.uv_DetailMap).rgb;
    66.             float3 detailColor = lerp(_DetailColor.rgb, finalColor, detailMask);
    67.             finalColor = lerp(detailColor, finalColor, saturate(input.depth * _DetailMapDepthBias));
    68.  
    69.             output.Albedo = finalColor;
    70.             output.Alpha = _BaseColor.a;
    71.         }
    72.  
    73.         void VertexMain(inout appdata_full input, out Input output)
    74.         {
    75.             UNITY_INITIALIZE_OUTPUT(Input, output);
    76.             output.depth = UnityObjectToClipPos(input.vertex).z;
    77.         }
    78.         ENDCG
    79.          
    80.             Pass{
    81.             Name "Cutout"
    82.             AlphaTest Greater 0.9
    83.             SetTexture[_Cutout]{
    84.  
    85.         }
    86.         }
    87.     }
    88.  
    89.     FallBack "VertexLit"
    90. }
    91.  
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    You don't need another pass for this. Just set your alpha texture and clip directly in the first pass.
     
  3. unitynewb

    unitynewb

    Joined:
    Feb 22, 2009
    Posts:
    243
    The problem is I am new to shaders and wouldn't even know where to start. I have added a pass before to add a decal, but this is different.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
  5. unitynewb

    unitynewb

    Joined:
    Feb 22, 2009
    Posts:
    243
    Last edited: Aug 6, 2017
  6. gwelkind

    gwelkind

    Joined:
    Sep 16, 2015
    Posts:
    66
    How can I do this with URP?
     
  7. lgarczyn

    lgarczyn

    Joined:
    Nov 23, 2014
    Posts:
    68
    Blend One Zero
    AlphaToMask On
     
    diegosainz3d likes this.