Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Can I ignore light cookie per object?

Discussion in 'Shaders' started by richGlumberland, Jul 14, 2021.

  1. richGlumberland

    richGlumberland

    Joined:
    Jan 25, 2021
    Posts:
    3
    Hi, I'm making a animated cloud shadow effect by applying an animated render texture to the main directional light as a cookie. However, I have cloud meshes in the scene and they are also effected. Is there a way to make a shader ignore cookies? One solution that I would like to avoid is creating a new directional light without a cookie for these objects. I'm using the build in standard render pipeline.

    Thanks for any help!

    upload_2021-7-14_17-51-29.png
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,907
    Yes. But you’ll need to write a custom shader to do so. They key is the keyword
    DIRECTIONAL_COOKIE
    . That gets enabled on a shader when the directional light is using a cookie. To avoid the cookie being used, you need to either disable that keyword prior to
    #include AutoLight.cginc
    showing up in the shader. For a fully custom shader you can add
    #undef DIRECTIONAL_COOKIE
    before including the AutoLight.cginc, but you can’t do that for a Surface Shader, which is a lot easier to slap together quickly and match the built in shaders. So instead you can do this:
    Code (CSharp):
    1. #if defined(LIGHT_ATTENUATION)
    2. #undef LIGHT_ATTENUATION
    3. #define LIGHT_ATTENUATION(a) SHADOW_ATTENUATION(a)
    4. #endif
    If you add that near the start of the default Surface Shader that’s created when creating a new shader in the project view, it should be unaffected by the light cookie on a directional light.
     
  3. richGlumberland

    richGlumberland

    Joined:
    Jan 25, 2021
    Posts:
    3
    Thanks for the reply! What you're saying makes sense but it didn't seem to work. I made a new standard shader to test. Does this look right?

    Code (CSharp):
    1. Shader "Custom/testStandardShader"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.  
    15.         CGPROGRAM
    16.         #if defined(LIGHT_ATTENUATION)
    17.         #undef LIGHT_ATTENUATION
    18.         #define LIGHT_ATTENUATION(a) SHADOW_ATTENUATION(a)
    19.         #endif
    20.  
    21.         // Physically based Standard lighting model, and enable shadows on all light types
    22.         #pragma surface surf Standard fullforwardshadows
    23.  
    24.         // Use shader model 3.0 target, to get nicer looking lighting
    25.         #pragma target 3.0
    26.  
    27.         sampler2D _MainTex;
    28.  
    29.         struct Input
    30.         {
    31.             float2 uv_MainTex;
    32.         };
    33.  
    34.         half _Glossiness;
    35.         half _Metallic;
    36.         fixed4 _Color;
    37.  
    38.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    39.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    40.         // #pragma instancing_options assumeuniformscaling
    41.         UNITY_INSTANCING_BUFFER_START(Props)
    42.             // put more per-instance properties here
    43.         UNITY_INSTANCING_BUFFER_END(Props)
    44.  
    45.         void surf (Input IN, inout SurfaceOutputStandard o)
    46.         {
    47.             // Albedo comes from a texture tinted by color
    48.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    49.             o.Albedo = c.rgb;
    50.             // Metallic and smoothness come from slider variables
    51.             o.Metallic = _Metallic;
    52.             o.Smoothness = _Glossiness;
    53.             o.Alpha = c.a;
    54.         }
    55.         ENDCG
    56.     }
    57.     FallBack "Diffuse"
    58. }
    59.  
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,907
    Whoops! Replacing the wrong macro.

    Code (csharp):
    1. #if defined(DIRECTIONAL_COOKIE) && defined(UNITY_LIGHT_ATTENUATION)
    2. #undef UNITY_LIGHT_ATTENUATION
    3. #define UNITY_LIGHT_ATTENUATION(destName, input, worldPos) fixed destName = UNITY_SHADOW_ATTENUATION(input, worldPos);
    4. #endif
     
  5. richGlumberland

    richGlumberland

    Joined:
    Jan 25, 2021
    Posts:
    3
    Perfect, works like a charm! Thanks for the help!
     
unityunity