2D Shadow Mapping Project

Hello Everyone,

after looking at the trailer for  Cryptic Sea’s : “Hitlers Must Die!”,

http://crypticsea.blogspot.com/2009/05/hitlers-must-die.html

I noticed the absolutely fantastic 2d dynamic shadow mapping,  since this is a feature I would love to add to a project of my own at some point in the future, I set out working on it. My own implementation is unlikely to use the fantastic “blocky” artistic style used in “Hitlers Must Die”, so I decided to try to create basic soft shadows.

I intend to write a full article on this subject at a later date, when I am happy and have a full sample to show off.

I will also create a simple library and release it here for people to play with.

My implementation basically works by iterating over all of the points of every polygon in the scene, since the polygon is created with a set winding order, the direction of the edge is predictable. The shadow mapping its self is quite simple, the geometry is created by extruding the edges that face away from the light source. The facing direction can be computed by calculating the inner(dot) product between the edge normal and the position of the light source.

The length of the shadow is the next issue, after looking around for a while I found “Salt’s” fantastic article on TigSource:

http://forums.tigsource.com/index.php?topic=8803.0

He proposed that the projected point ” p’ ” is equal to the vector between the position of the light ” L” and the position of the edge point ” P ” + the position of the point P.

Once the shadow volume has been drawn, I am left with quite a convincing hard edge shadowed effect.

This is great, but real world shadows are not hard edged, and this is an issue I wanted to take care of, since I might wish to have a more realistic look to the game.

I managed to find a fantastic article by “Orangy Tang” on Gamedev.Net entitled  Dynamic 2d Soft shadows, his approach was to generate “wings” to represent the penumbra of the outer edges of the shadow.

http://www.gamedev.net/reference/programming/features/2dsoftshadow/page4.asp

I liked this system, but it seemed overinvolved for what I wanted, while “Orangy Tang’s” shadows are acurate mathematically, I felt that they didnt need to be, and the additional steps have a cost association.

My alternative, was very simple,  I added shader support to Haaf’s Game Engine (HGE), then drew the shadows seperately to a  render target, generating a shadow map.

I can then apply a pixel shader to the “shadow map”, and do a number of screen space special effects.In this instance I chose to use a very simple box blur filter, this essentailly “smudges” the shadow map, giving soft edges.

This isnt mathematically accurate but it is simple and robust, and it generates fantastic looking results. Another advantage is that the degree of the blur and the alpha blend are both very simple variables,  making it possible to create a range of effects, to suit the artistis direction of the game being made.

Thanks for reading -Anthony Littlewood


Comments are closed.