Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

SpriteRenderer real-time drop shadows - RenderTexture?

Discussion in '2D' started by Industrion, May 12, 2015.

  1. Industrion

    Industrion

    Joined:
    Nov 21, 2013
    Posts:
    41
    Hi,

    I'm looking to render or 'paint' drop-shadow style semi-transparent copies of 2d sprites in my game (using 2D mode in unity, with SpriteRenderers, not 3D quads). I want them to be blended correctly and not create any strange overlap, so I imagined I may need to use a blend mode or pixel buffer, but I'm not sure of the best approach, or how to execute it.

    I imagined I could probably use a RenderTexture, with a shader that tints everything black then uses transparency.

    I'm looking for people's thoughts on this approach before I attempt it.

    The game will have objects in the sky as well as a lot more objects on the ground, so I thought about using two separate 'layers', so the shadows coming from objects in the sky would lie on top of the ground objects, and shadows of ground objects would go between themselves and the floor of the game world.

    Note: Many of these sprites will move and/or be animated, so I can't really bake these shadows.

    I'm thinking that the fallback to painting these repeatedly is to manage duplicates of every object in the game that casts a shadow, putting them at a lower depth/Z position, and setting their transform and animation frame number to the same as their counterpart.

    Thanks!
     
  2. DalerHakimov

    DalerHakimov

    Joined:
    Mar 14, 2014
    Posts:
    302
    TomasJ likes this.
  3. Industrion

    Industrion

    Joined:
    Nov 21, 2013
    Posts:
    41
    Thanks, I did actually look at it before but I think I kept looking because I wasn't sure how easy it'd be to scale based on a given parameter (I know how to add a parameter, it's just adding the vertex shader I might struggle with). I think it's something I'll need, to correctly represent how far objects are from the ground level.

    Another issue is that the offset is 'in local space', so the shadow doesn't stay at the same angle when an object is rotated. I think that'd mean that every object being rotated would have to continually, and uniquely, set the offsetX and offsetY input parameters on the shader.
     
  4. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    You can always just place a second game object underneath with the same sprite and a fixed color shader.
     
  5. Industrion

    Industrion

    Joined:
    Nov 21, 2013
    Posts:
    41
    @TomasJ I considered doing that but because I'll have objects with a hierarchy (often with animation clips) that is more complex than just a sprite renderer, there could be quite a bit of work to make these match up in terms of transform and other information, with their original counterpart GameObjects. I also have a parallax scrolling system that bunches game entities into layers, rather than using Z for depth directly, so I think that complicates things further.
     
  6. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    Fair enough :)

    But in that case the script in bitbucket won't do you much good either because it won't work on overlapping sprites. You will see each sprite cast a separate shadow.
    However if you render your entire character of multiple sprites to a texture, then it will work because your renderables will be essentially flattened.
     
  7. Industrion

    Industrion

    Joined:
    Nov 21, 2013
    Posts:
    41
    Yeah that's what I'm going to try. Or maybe I can put a shader on all of the shadow objects that take a max alpha value and prevent their frag color values from exceeding that alpha value after additive blend with other shadows - or something like that.
     
  8. Slapworth

    Slapworth

    Joined:
    Aug 11, 2013
    Posts:
    19
    I'm looking for this shader as well, and am a total shader noob. Even the word "frag" is over my head at the moment. :)

    In my case, I also have a multi-sprite character that I hope will cast a single shadow. I also have a number of environment objects that can cast separate shadows, but are rotated in arbitrary directions. The solution I'm looking for would process all X/Y offsets on world (or screen) orientations, as if lit from a common source regardless of rotation.

    Is this sort of thing practical with shaders? Could the shadows sort at a greater depth than the objects that "cast" them? Managing depth or draw order seems like a possible way to manage shadow behavior.

    Thanks - Slapworth