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.

2d Sprite Shadows

Discussion in 'Shaders' started by gdg, Jan 13, 2017.

  1. gdg

    gdg

    Joined:
    Jul 8, 2013
    Posts:
    18
    Hi guys,
    I have a day night cycle with a moving sun and I want to cast shadows from sprites like the attached image. The ground, which can receive shadows, is also a sprite. I was thinking that a way to do this would be to use a shader for the sprites with two passes, the first pass to draw the object normally and the second pass would move the vertices and draw the shadow with transparent gray. Is this a possible way? I can get the pixel shader to output a gray color but I am also not sure of the vertex shader code required to do this.


    reference.png
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yeah I did that in the other brothers mobile retro title. I tried a couple of approaches but just in the end decided to go with DrawMeshNow, passing in the current mesh of the sprite because then, the uvs and mesh matches the caster. All the shader needs to do is flip it and draw it black, respecting alpha * transparency of shadow.

    I'm not really going to dig around in ancient project files so maybe someone will come along and add more. Just pointing out your idea is feasible and proven.

    TLDR: it's draw it again flipped along Y with suitable shader, you can't obviously do it in the same shader because that's not how it works.
     
  3. gdg

    gdg

    Joined:
    Jul 8, 2013
    Posts:
    18
    Thanks for the reply. It seems that with DrawMeshNow I would not need to use a shader with two passes as it does that in the script. Hoping to get some help with the distortion of the shadow based on sun angle.
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    You cannot use a shader with two passes. Shaders don't work beyond the mesh geometry, and your sprite doesn't have space in the mesh to draw the shadow. If you are using a vertex shader on second pass it could work. Do you have source?

    Seems to me you'd be much better off just using Unity's shadows. If your sprites are hard-edge, just use cutout and it'll work.

    The extrusion in vertex shader would probably just require passing a light position as a Vector4 type.
     
  5. gdg

    gdg

    Joined:
    Jul 8, 2013
    Posts:
    18
    Using actual directional light was simple and gives good result, I think i will go with that