Search Unity

Transparent Cutout Shader, just render shadow

Discussion in 'Shaders' started by Airship, Apr 29, 2013.

  1. Airship

    Airship

    Joined:
    Sep 10, 2011
    Posts:
    254
    Is it possible to turn off the visibility for the cutout shader so just the shadows render while still using the alpha cutout in the shadows?

    Code (csharp):
    1. Shader "Transparent/Cutout/Diffuse" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    6. }
    7.  
    8. SubShader {
    9.     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    10.     LOD 200
    11.    
    12. CGPROGRAM
    13. #pragma surface surf Lambert alphatest:_Cutoff
    14.  
    15. sampler2D _MainTex;
    16. fixed4 _Color;
    17.  
    18. struct Input {
    19.     float2 uv_MainTex;
    20. };
    21.  
    22. void surf (Input IN, inout SurfaceOutput o) {
    23.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    24.     o.Albedo = c.rgb;
    25.     o.Alpha = c.a;
    26. }
    27. ENDCG
    28. }
    29.  
    30. Fallback "Transparent/Cutout/VertexLit"
    31. }
    32.  
     
  2. Jessespike

    Jessespike

    Joined:
    Jul 9, 2012
    Posts:
    43
  3. andererandre

    andererandre

    Joined:
    Dec 17, 2010
    Posts:
    680
    What I ususally do is rendering transparent objects twice, once with a cutout shader that drops shadows and once with a blended shader that does neither drop nor receive shadows. This of course only works if your cutout shader cutoff value is low enough as otherwise your blended shader will be overwritten by the cutout shader, causing ugly cutout artifacts. This technique is also quite useful for trees as that way you can write to the depth buffer with the cutout shader while maintaining smooth edges using the blended shader.
     
  4. cician

    cician

    Joined:
    Dec 10, 2012
    Posts:
    233
    Try this :cool:

    Code (csharp):
    1. Shader "Custom/CutoutShadowOnly" {
    2.     Properties {
    3.         _MainTex ("Alpha (A)", 2D) = "white" {}
    4.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    5.     }
    6.    
    7.     SubShader {
    8.         Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    9.  
    10.         Pass {
    11.             Name "Caster"
    12.             Tags { "LightMode" = "ShadowCaster" }
    13.             Offset 1, 1
    14.            
    15.             Fog {Mode Off}
    16.             ZWrite On ZTest LEqual Cull Off
    17.    
    18.             CGPROGRAM
    19.                 #pragma vertex vert
    20.                 #pragma fragment frag
    21.                 #pragma multi_compile_shadowcaster
    22.                 #pragma fragmentoption ARB_precision_hint_fastest
    23.                 #include "UnityCG.cginc"
    24.                
    25.                 struct v2f {
    26.                     V2F_SHADOW_CASTER;
    27.                     float2  uv : TEXCOORD1;
    28.                 };
    29.                
    30.                 uniform float4 _MainTex_ST;
    31.                
    32.                 v2f vert( appdata_base v ) {
    33.                     v2f o;
    34.                     TRANSFER_SHADOW_CASTER(o)
    35.                     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    36.                     return o;
    37.                 }
    38.                
    39.                 uniform sampler2D _MainTex;
    40.                 uniform fixed _Cutoff;
    41.                
    42.                 float4 frag( v2f i ) : COLOR {
    43.                     fixed alpha = tex2D( _MainTex, i.uv ).a;
    44.                     // or if you want to use grayscale texture:
    45.                     // fixed alpha = tex2D( _MainTex, i.uv ).r;
    46.                     clip( alpha - _Cutoff );
    47.                    
    48.                     SHADOW_CASTER_FRAGMENT(i)
    49.                 }
    50.             ENDCG
    51.         }
    52.     }
    53.     Fallback Off
    54. }
    55.  
     
  5. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,646
    @AirShip.
    Hi,

    Seem you have a solution. For those who are no idea that what you're talking about can you show a picture. Before and after. Please?
     
  6. Airship

    Airship

    Joined:
    Sep 10, 2011
    Posts:
    254
    Thanks cician, that's perfect!
     
  7. rooster

    rooster

    Joined:
    May 22, 2009
    Posts:
    13
    Could you explain a bit about how to use this shader? I've placed it on the ground object where the shadow would be cast if I used a diffuse shader. This doesn't seem to work. I just see a transparent material and the alpha cutoff doesn't do anything.