PDA

View Full Version : Dynamic 2D Shadows



savage
30-10-2006, 04:08 PM
Hi all,
Just wondering if anyone has any experience, tips or links to creating dynamic shadows in 2D. ie Given a sprite, I want to create either a soft shadow or shadow that I can adaptively blend, to give the illusion of a soft shadow.

cronodragon
30-10-2006, 04:22 PM
What about using filters? Edge detection to make it black, rotation and shrink and, then blending... everything is here:

http://www.gamedev.net/reference/programming/features/imageproc/default.asp

Traveler
30-10-2006, 08:30 PM
To enhance the 3d element in Village Defense I used a lot of shadows. To create this effect I created 2 renders of each frame of an animationcycle. The first render is the characters, but without it's shadows. In the render only the shadows are visible. Then, in photoshop using the alpha channels I joing them together again.

Below you can see a screenshot of all frames (best viewed in FireFox as Internet Explorer kills the png transparency effect)

http://www.gameprogrammer.net/pics/outside/transsprite.png

There is another technique available where you use real 3d objects to calculate shadows and then render only the shadows, but it is quite difficult, and only performs well in certain cases. Iirc there's an article about it on gamedev.

savage
30-10-2006, 09:44 PM
Hi crono,
Thanks for the link looks like it might be quite usefull. I'm not sure if just doing the black, shrink and blend will be too expensive.

Hi Traveller,
I unfortunately don't have the luxury of of rendering the shadows as I did not create the initial isometric models/renderings. FYI I'm using the Siege of Avalon sprites. At the time the engine used a 3rd Party DLL to create the shadows dynamically, and I am trying to do away with the use of said DLL.

Either way it is something that would be implemented further down the line. But I wanted to gather the information before hand. I also thought others on here might find it interesting as well.

jdarling
30-10-2006, 09:58 PM
It doesn't matter if you created the sprites or not :). If you have them available in a renderable format, then you can edit them (granted may take a nasty hack of the engine to render to a clipping plane to create a sprite sheet).

As for run time generated shadows; its very expensive to do it all of the time. Best best is to creat them on the fly when the sprite is loaded. Store them off for re-use later on, and then re-use when needed.

I've been playing with stretch - skew - blur combinations and the results are quite nice. Graphics32 lets you do all of the operations quite quicly. Once I have rendered them I store them off to a png with alpha and everything is ready for the next engine run. Of course you don't have to store them local, you could generate them every time the game runs.

Now, if you want them to bend up buildings, fall down holes, and do other 3Dish effects, then look out brother the road ahead just got rocky. I hate to point off site, but take a look at Malahedra on GameDev.net in the Isometric Land group. EDI has done quite a bit of work with shaders and 2D graphics to get nice looking shadows that "react" with the environment.

WILL
30-10-2006, 10:11 PM
There is the possibility to pre-rendering them during the 'level/ map load' cycle. Basically calculate the width & height required to make a copy of the character sprites.

Then do a bit of processing and copy the alpha chanel and convert each non-transparent pixel as a white pixel onto the new texture. Continue through all the sprite frames until you have made a complete shilohette copy of the sprite's frames. You can then take these and use Traveler's trick to get them to show as a shadow (considering that you are using your GPU this costs you peanuts to scale & add to screen) you may then draw your sprites with the added benifit of shadows. :D

Or... to reduce the need to have to scale each time you want to draw a 'shadowed' sprite.. you can scale them all to size when drawing them to the new texture during the processing. This will also have the added benift of reducing the amount of extra texture space need from double down to about one and a half.

Then it'll cost you nothing more than a measily Add function each character on screen.

jdarling
30-10-2006, 10:30 PM
Thats what I ment :) (WILL's comment that is). BTW: You can't use rotate to generate a shadow. You have to use stretch. For example a typical shadow transform looks something like:

Invert Mask - IE: Make a copy then fill all but mask with a color (not black)
Scale: 2/3
Transform Top, Left: 2/3 * Width
Apply Aplha: 30-50%
Render Shadow
Render Sprite