Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Casting shadows from 2D sprites in a top-down perspective

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member Newbie
    Join Date
    Sep 2014

    Casting shadows from 2D sprites in a top-down perspective

    I'm trying to workout how I can accomplish this:

    I have a top-down perspective. Everything drawn is just 2D sprites. However; one of the sprites color-channels, or maybe it's in a different sprite image, is that sprites "depth" information. Meaning, each pixel on the sprite, what is the z-value of that sprite, or how high is it from the ground.

    I thought it would be cool to cast shadows. But unlike the normal methods I've seen where shadows are essentially endless, what I want is to show the sprites shadow as though the sun where "above" it. Because it's a top-down perspective (this is a 2D game), the shadows being cast can't be endless; they'd usually be short.

    I would also like to be able to show sunrise and sunset, i.e. long shadows, and I'd like to be able progress the "sun" through the sky during the game. And I'd even like dynamic shadows; so an explosion casts the sprites shadows away.

    I can render the sprites into a framebuffer and get myself the diffuse information, depth information (the z-component really); but how can I combine this to get the shadows? For each frag, do I just loop through all my lights (or single "sun"), find the direction to it and step towards it until I hit a piece of depth that generates a shadow, or if I'm further than the shadow casting length just discard? I'm a bit lost as to what to do.

    For example, all of these are cool but I want the added ability to raise and lower the lights "vertically".
    Last edited by NeomerArcana; 09-10-2017 at 06:25 AM.

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    What about: render your clouds into a monochrome "shadow texture" and just project (texture) that onto the terrain (e.g. dim the specular and diffuse lighting wherever there's a cloud shadow)?

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2014
    Thanks for your response. I must not have explained very well. I'm not attempting to cast cloud shadows down onto the world, but cast shadows of the objects in the world.

    I.e. a tank is driving around, it should cast a little shadow off to the side. I can't just use the sprite and extrude it, because a tank isn't a rectangle from the side. Likewise, i can't use a simple "shadow sprite" either because the sun could be at any angle.

    Sort of like, 3D shadows without having any 3d objects.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Quote Originally Posted by NeomerArcana View Post
    3D shadows without having any 3d objects.
    In general, this is impossible.

    Using a height map will only work if that height map completely describes the shape of the object, which isn't true for most 3D objects (any vertical ray which intersects the object must intersect its surface in precisely two places, one of which must lie on the ground plane). Essentially, the only shapes which can be described that way are those which could be formed by a jelly mould.

    And even if all of your objects could be described by a height map, that form isn't particularly convenient for shadow casting. You'd probably end up converting it to a triangle mesh. Either that, or you'd have to perform manual raycasts in the fragment shader, which are relatively expensive.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts