Search Unity

Receive shadows, but don't cast any

Discussion in 'Shaders' started by SunnySunshine, May 26, 2016.

  1. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    976
    I need a shader that receives shadows, but don't cast any itself. Basically mimic the behavior you get when you disable shadow casting on a mesh renderer, but on a shader level instead.

    Been trying to write this shader, but so far I either get both or no shadows at all.

    Any help would be greatly appreciated.
     
  2. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    What type of shader are you writing? Surface?
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    To do this from the shader only you'll need to detect if your shadowcaster pass is rendering during the shadow pass or the depth pass:
    http://forum.unity3d.com/threads/di...dow-caster-and-shadow-receiver-in-5-2.362653/

    The main directional light shadows work by casting against the camera's depth texture, which is rendered with the same shadowcaster pass as shadows, at least as of Unity 5.

    You basically want to add this line to your shadowcaster fragment shader.

    if (unity_LightShadowBias.z != 0.0) discard;

    If you're using a surface shader you can add addshadow to the #pragma surface line and then add this to your surface function.

    #ifdef UNITY_PASS_SHADOWCASTER
    if (unity_LightShadowBias.z != 0.0) discard;
    #endif
     
    SunnySunshine likes this.
  4. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    976
    Doesn't really matter, as long as I can get this effect.

    Wow, this was pretty much exactly what I was looking for and it's working great. Thank you so much for sharing!
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    As an additional note that "hack" is also what Unity uses to differentiate the two cases. It happens to fail if your light doesn't have a normal bias (that's what that value is). That thread was me attempting to find another value that is consistently different between the passes that wouldn't fail in the case of normal bias being disabled.