Search Unity

Toggle shading using shadow attenuation's value?

Discussion in 'Shaders' started by LezCT, Jun 25, 2019.

  1. LezCT

    LezCT

    Joined:
    Nov 15, 2014
    Posts:
    55
    Hi, I'm looking for a way to toggle the shading by checking if the shadow attenuation covers the majority of the object(not normals) just like in this GIF. I tried IF statements and even rounding off it. but i knew it'll only make the shadow more sharp instead of toggling between 0 and 1.

     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Due to the way Unity's directional shadows work, this is a non-trivial task.

    There's no way within a single shader to get any kind of approximation of a character's in-shadow coverage. You only know if the specific pixel you're currently rendering is in shadow or not (and then, you don't know if that shadow is from the world or the same character self-shadowing). Additionally, due to the way Unity's main directional shadows work on desktop and console, you can't sample the shadow from an arbitrary position either (like some point above the player's head, or a few points around the character).

    It's also useful to know that's now how it's being done in the above example either. The character is fully in the light long before she becomes "lit" by the sun. This is because the way to do this isn't by doing any of the work in the shader. Instead the world has prebaked probes or low detail volumes that denote where shadows are. No idea if the game is generating that data automatically, or if it's hand placed. But either way it'd be difficult to replicate in Unity since that kind of system doesn't exist.

    If you put in a ton of ambient probes into the scene, you could potentially check if the probe was above some brightness threshold in the direction towards the sun.

    Alternatively you could write your own c# side system to trace from the character's position towards the sun for some arbitrarily far distance and see if you hit anything and toggle a value on the material based on if you do or not. This would probably be the cheapest and quickest to implement.
     
    LezCT likes this.