Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2D shadows strategy

Discussion in '2D' started by aelbers, Jul 11, 2017.

  1. aelbers

    aelbers

    Joined:
    Sep 16, 2014
    Posts:
    7
    Hello everyone,

    I'm currently experimenting with creating 2d shadows on a map which has different heights.
    heights_and_shadows.PNG
    Since it is top down, orthogonal, I figured, that I should add shadows to the plateaus to indicate varying heights.
    In the image, the heights are marked as 0, 1, 2, infinite. For a height difference of 1, I add a small shadow (a), for everything above, I add a large shadow (b).

    But I also want other shadows in the scene, but they should not multiply with the shadows of the plateaus. I achieved that by using the stencil buffer, marking every pixel which is occupied by the shadow and ignoring those, when drawing overlaying shadows.
    merged_shadows_via_stencil_buffer.PNG

    Since I want other gameobjects to cast shadows too, they should merge with the shadows on the plateau they're on. But that also means, that they should clip to the plateaus they're on.
    shadows_clipped_to_plateaus.PNG

    I don't know, how to achieve this yet. I tried using the stencil method, writing a heightmap as I draw the various floors, but I did not succeed.

    Moving away from the stencil buffer, I thought, that I could also use shadow maps to be drawn on each plateau. But I don't know, how I could to achieve that.
    shadow_and_light_maps_large.png shadow_and_light_maps.PNG


    So I ask you - what would a general approach to create fake 2d shadows look like?

    Best regards

    aelbers