I want to try adding shadows to a 3D XNA game. I've set up some testing environment with one light and one camera. However, I'm running into artifacts with my approach.

I'm using the shader code from http://pastebin.com/zXi0hmsU to render the final result and http://pastebin.com/rY4Gcj9N to create the shadow maps. The code is pretty straight forward and involves production of the depthmap from the light point of view, then projecting it into the camera space and checking for occlusions.

However, from most angles I am getting pretty ugly results

The scene consists of some simple cubes (constructed by hand, facing outside, backface is culled), one light (shadow map shown in top left corner - looks okay) and one camera.

My RenderTarget for the shadow maps is initialized by

int shadowMapSize = 512;
DepthFormat.Depth24Stencil8);
GraphicsDevice.Clear(Color.White);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;


Afterwards, the CreateShadowMap effect is applied, followed by the LambertWithShadows effect. Finally, the shadow map is drawn using a SpriteBatch.

Rest of the code consists of simply providing the correct values to the shaders. If necessary, I can provide it at well.

This is typical problem called shadow map acne. It is caused because of the projected pixel of the shadow map(depth) is bigger than pixel on the screen (eq. cannot represent depth correctly).

There are plenty of solutions to this. First most easy is bias (i see it in your code, so that is not the problem). And second easy solution is shadow map in bigger resolution, it is really more than typical to have shadowmaps in resolution 2048x2048 on current hw. (EDIT: thats not true, i've wrote that when i was working on movie rendering. In game development, you want to keep you shadow map as small as possible)

Some resources to study more advanced techniques.

edit:

• I have some more materials on this topic in work, if you are interested i will share it too. – Notabene Mar 31 '11 at 19:16
• The main problem with big resolutions is that in the end game I have about 3-4 lights which are undirected => I will have to use cubic shadow maps there, which results in 24 shadowmaps. If you have additional materials, I would really appretiate your effort of sharing it with me :-) – Etan Mar 31 '11 at 21:46
• I am interested at the least if that matters :) – James Mar 31 '11 at 21:48
• @Etan. Yeah omni directional lights are real pain if talkin about shadows. (i know something about it :) gamedev.stackexchange.com/questions/6203/…). Try to use dual paraboloid maps. Or if you like cubemaps, try to save some space by frustum-camera_frustum intersection culling for every face/render. You propably won't need shadow for all directions all the time (especially in exteriors) – Notabene Mar 31 '11 at 22:28
• @Etan @James I added some more stuff for you. Both is worth reading if you are interested on this topic. – Notabene Apr 2 '11 at 19:37