Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Toon/Lit Shader with cutout transparency

Discussion in 'Shaders' started by Wojzax, Sep 6, 2019.

  1. Wojzax

    Wojzax

    Joined:
    Jul 22, 2014
    Posts:
    34
    I've got this shader Toon/Lit from Standard Assets. It goes like this:
    Code (CSharp):
    1. Shader "Toon/Lit" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (0.5,0.5,0.5,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
    6.     }
    7.  
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 200
    11.        
    12. CGPROGRAM
    13. #pragma surface surf ToonRamp
    14.  
    15. sampler2D _Ramp;
    16.  
    17. // custom lighting function that uses a texture ramp based
    18. // on angle between light direction and normal
    19. #pragma lighting ToonRamp exclude_path:prepass
    20. inline half4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
    21. {
    22.     #ifndef USING_DIRECTIONAL_LIGHT
    23.     lightDir = normalize(lightDir);
    24.     #endif
    25.    
    26.     half d = dot (s.Normal, lightDir)*0.5 + 0.5;
    27.     half3 ramp = tex2D (_Ramp, float2(d,d)).rgb;
    28.    
    29.     half4 c;
    30.     c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);  
    31.     return c;
    32. }
    33.  
    34.  
    35. sampler2D _MainTex;
    36. float4 _Color;
    37.  
    38. struct Input {
    39.     float2 uv_MainTex : TEXCOORD0;
    40. };
    41.  
    42. void surf (Input IN, inout SurfaceOutput o) {
    43.     half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    44.     o.Albedo = c.rgb;
    45.     o.Alpha = c.a;
    46.    
    47. }
    48. ENDCG
    49.  
    50.     }
    51.  
    52.     Fallback "Diffuse"
    53. }
    I want to make it so it can show cut-out transparency on textures
    To achieve this I've added
    Code (CSharp):
    1. c.a=s.Alpha;
    2.     clip(c.a - 0.5f);
    after
    Code (CSharp):
    1. return c;
    The cut-out is working now, but the cutted parts of textures does'nt allow shadows behind it to show up (image below)

    Is there a way to make it right?
     
  2. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    Change your surface shader declaration to the following;

    Code (CSharp):
    1. #pragma surface surf ToonRamp addshadow
    The 'addshadow' parameter generates a new shadowcaster pass to match your shader, fixing the depth issues that are masking the shadows here.