Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Recieve but not cast shadows

Discussion in 'Shaders' started by lewdoo, Sep 7, 2018.

  1. lewdoo

    lewdoo

    Joined:
    Sep 7, 2018
    Posts:
    9
    Hey everyone! Been playing with shaders for quite some time now and have started dabbling into lighting, but I have been unable to implement a vertex frag shader, which receives shadows and does not cast them...

    Is it possible?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    Yes. ish.

    The best solution is to not do it in the shader. Each renderer component has an option to disable casting and receiving shadows separately.

    It is relatively easy to make a shader that casts shadows and doesn't receive them, just don't sample the shadow in the shader, but much harder to do the opposite. The main issue is that the way shadows work in Unity, at least for the main directional light on PC and consoles, is that Unity renders the scene's camera depth texture, then casts shadows onto that depth texture rather than using the actual geometry. The camera depth texture uses the same shadow caster pass that are used to render the shadow maps, and there's no clear way to differentiate between when the shadow caster pass is being used to render the camera depth, or the lightmaps. The easiest test I found was to test to see if the current projection matrix was perspective or orthographic as the camera depth texture is always a perspective matrix, and the main directional light is always orthographic, but this also disables shadows for spot lights and point lights which both use a perspective matrix. Luckily you can test if it's a point or spot light, so a combination of testing the matrix and #ifdef tests can get you close.

    https://forum.unity.com/threads/dif...dow-caster-and-shadow-receiver-in-5-2.362653/
     
  3. lewdoo

    lewdoo

    Joined:
    Sep 7, 2018
    Posts:
    9
    Thanks for the reply, I never even noticed that option, it didn't work straight away, but im going to play around a bit. Thanks again!