Search Unity

Question How to prevent culled sprite backface from casting shadows

Discussion in 'Shaders' started by theforgot3n1, Oct 15, 2022.

  1. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    208
    I want to prevent the culled backface in this example from casting shadows. I am using the UberSpriteShaders (https://github.com/traggett/UnitySpriteShaders).

    remove shadow from backface.png

    Here it shows the shadow "through the back". I want the back never to cast any shadows. The next image shows the backface culling.

    culled but still showing shadows.png

    Is there a way to solve this? It would be huge for our game.
     
  2. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    208
    I've investigated regular meshes and it is the default behaviour for them. But not sprites it seems. Is it possible to replicate this effect for sprites?
     
    Last edited: Oct 15, 2022
  3. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    It sounds like the shader hasn't been created with a custom ShadowCaster pass that takes in the _Cull variable. You will most likely have to edit the shader a bit to do that.

    But before that, you can also try clicking the 3 dots at the top right of the inspector and go to Debug mode. Then you'll see hidden settings of the Sprite Renderer that normally people don't need to change, like the "Cast Shadows" value. Try setting it to On, as it may be currently set to Two-Sided.
     
  4. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    208
    Thanks for the response. I have tried both Two-sided and On, it seems to be the shader that isn't doing what you're saying. I'm quite the noob with shaders so it will take me a while to do the modification, but it makes sense.
     
  5. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    If you get stuck, feel free to link the shader code if you can.
     
    theforgot3n1 likes this.
  6. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    208

    This was actually surprisingly easy. All it took is a simple change in

    Name "ShadowCaster"
    Tags { "LightMode"="ShadowCaster" }
    ...
    Cull Off

    To changing the last line to `Cull [_Cull]`
     
    Invertex likes this.