Search Unity

Cutout shadows are only culled through the shader properties

Discussion in 'Shaders' started by ThomasCreate, Apr 24, 2015.

  1. ThomasCreate

    ThomasCreate

    Joined:
    Feb 23, 2015
    Posts:
    81
    I want to create a "wipe" effect, such as is often done by using a black and white gradient texture and changing the cutoff value of the cutout shader to create the desired animation. However, this only works on very basic UV maps; as soon as you have a real world scenario, you're not going to have a correct wipe using this method.

    I'm still very new to shaders, but I adapted a custom cutout shader that I happened across to have a "Height" property, which can be used to create a cut off based on the world position of the pixels. (if the object starts at y = 0, but for my specific case this is always true)

    Code (CSharp):
    1.  
    2.  Shader "Animations/VerticalFadeInCutout"
    3.  {
    4.     Properties
    5.     {
    6.         _Color ("Main Color", Color) = (1,1,1,1)
    7.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    8.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    9.         _Height("Height", Float) = 1
    10.     }
    11.      
    12.     SubShader
    13.     {
    14.         Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    15.         LOD 200
    16.         Cull Off
    17.            
    18.         CGPROGRAM
    19.         #pragma surface surf Lambert alphatest:_Cutoff
    20.          
    21.         sampler2D _MainTex;
    22.         fixed4 _Color;
    23.         float _Height;
    24.          
    25.         struct Input
    26.         {
    27.             float2 uv_MainTex;
    28.             float3 worldPos;
    29.         };
    30.          
    31.         void surf (Input IN, inout SurfaceOutput o)
    32.         {
    33.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    34.             o.Albedo = c.rgb;
    35.             o.Alpha = (_Height - IN.worldPos.y) - (_Height / 2) + 0.5;
    36.         }
    37.         ENDCG
    38.     }
    39.      
    40.     Fallback "Transparent/Cutout/Diffuse"
    41.  }
    Turning the object transparent works just fine this way, however the shadow is still being cast, both on the transparent bits of the mesh itself and on the geometry behind it. If I change the color in the inspector directly though, the shadows do get removed. I reckon I'm doing something wrong here, so I'd really appreciate a hand in this matter.

    Thanks!
     

    Attached Files:

  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    You'll need to add your own shadow caster and receiver passes whose clip value matches your modifications.

    The built-in shadow casters don't know you're hiding part of the mesh.
     
    ThomasCreate likes this.
  3. ThomasCreate

    ThomasCreate

    Joined:
    Feb 23, 2015
    Posts:
    81
    Oh, that's it, thanks! Using this post from Aras it was really easy.