Search Unity

Make shader not receive own shadows

Discussion in 'Shaders' started by VictorKs, Apr 6, 2019.

  1. VictorKs

    VictorKs

    Joined:
    Jun 2, 2013
    Posts:
    242
    I have a model which flickers on distance because of self shadowing is it possible to create a shader which doesn't receive self shadows? I'm using deferred rendering.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    No (but kind of yes).

    The way Unity’s shadow mapping works, there is no way to exclude the shadows from a specific object from affecting another specific object as the shadow map has no understanding of individual objects after it’s been generated. Similarly deferred similarly has no knowledge of individual objects once the gbuffers have been generated.

    A hack work around would be to have two identical lights and two sets of shadowing geometry with different layers. Have one light have all objects cast shadows, and most of the scene receives that light. The second light would have all objects except your non-self shadowing object shadow casting, but only that one object receives that light.
    Pros: Works as described.
    Cons: A ton of work to setup and maintain. Also super expensive as you’re rendering out a second shadow map with all of the objects in the scene.

    Next hacky work around would be to use a custom shadow caster for that object to push the shadow away from the light by some custom amount. Basically a custom per-object bias. Push it far enough and it shouldn’t receive any shadows from itself, but any thing it casts shadows on also needs to be relatively far away.
    Pros: Works*, and is easier to setup.
    Cons: Only works well in very limited scenarios, and requires manual tweaking.

    A more complicated option would be to disable shadow casting from your renderer component entirely, and then inject it’s shadow manually using command buffers. You’d have to generate a custom shadow map and shadow projection of just your one object, and then generate a mask so you only add the shadow on things that aren’t your object.
    Pros: Works very well, and potentially produces much nicer shadows from your object. Once setup, should require no or very little manual tweaking.
    Cons: Requires a decent bit of graphics programming know how. Only really works on the main directional light, as you can trivial inject stuff into the screen space shadow texture it uses. All other light types would require custom deferred shading shaders.
     
  3. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    bgolus, every time I read your post I learn something new. Thank you for taking the time to make quality posts like this.