PDA

View Full Version : Casting shadows from 2D sprites in a top-down perspective



NeomerArcana
09-10-2017, 06:14 AM
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.

EDIT:
For example, all of these are cool https://www.reddit.com/r/gamedev/comments/238z3e/any_good_resources_for_glsl_shader_techniques/ but I want the added ability to raise and lower the lights "vertically".

Dark Photon
09-10-2017, 08:35 PM
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)?

NeomerArcana
09-10-2017, 09:49 PM
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.

GClements
09-11-2017, 05:00 AM
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.