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

Alpha Cutout with Custom Lighting

Discussion in 'Shaders' started by Carl-Gran, Jul 20, 2016.

  1. Carl-Gran

    Carl-Gran

    Joined:
    Jul 13, 2015
    Posts:
    12
    Hey all!

    Im sitting and trying to write a custom lighting (toonish shader)... Ran into a problem when I try to have alpha cutout combined with my own custom lighting model though:


    Code (CSharp):
    1. #pragma surface surf ToonLighting alphatest:_Cutoff
    2.  
    3. vs
    4.  
    5. #pragma surface surf Lambert alphatest:_Cutoff
    As you can see, the alpha cutout works just fine with Lambert lighting but not with my custom lighting. Here's the custom ToonLighting.cginc that I've made:

    Code (CSharp):
    1. #ifndef TOON_LIGHTING
    2. #define TOON_LIGHTING
    3.  
    4. #include "UnityLightingCommon.cginc"
    5. #include "UnityGlobalIllumination.cginc"
    6. #include "Lighting.cginc"
    7.  
    8. sampler2D         _LightRamp;
    9.  
    10. inline fixed4 UnityToonRampLight (SurfaceOutput s, UnityLight light)
    11. {
    12.     fixed diff = max (0, dot (s.Normal, light.dir));  
    13.     fixed4 c;
    14.     c.rgb = s.Albedo * light.color * diff;
    15.     c.a = s.Alpha;
    16.     return c;
    17. }
    18.  
    19. inline half4 LightingToonRamp(SurfaceOutput s, half3 lightDir, half atten)
    20. {
    21.     #ifndef USING_DIRECTIONAL_LIGHT
    22.     lightDir = normalize(lightDir);
    23.     #endif
    24.    
    25.     half d = dot (s.Normal, lightDir)*0.5 + 0.5;
    26.     half3 ramp = tex2D (_LightRamp, float2(d,d)).rgb;
    27.    
    28.     half4 c;
    29.     c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);
    30.     c.a = s.Alpha;
    31.     return c;
    32. }
    33.  
    34. inline fixed4 LightingToonRamp_PrePass(SurfaceOutput s, half4 light)
    35. {
    36.     fixed4 c;
    37.     c.rgb = s.Albedo * light.rgb;
    38.     c.a = s.Alpha;
    39.     return c;
    40. }
    41.  
    42. #endif
    Any ideas how to fix this? :)

    Cheers
     
  2. Carl-Gran

    Carl-Gran

    Joined:
    Jul 13, 2015
    Posts:
    12