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 resultslike in this screenshot

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;
RenderTarget2D shadowMap = new RenderTarget2D(GraphicsDevice, shadowMapSize,
    shadowMapSize, true, GraphicsDevice.PresentationParameters.BackBufferFormat,
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.


1 Answer 1


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).

enter image description here

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.


  • \$\begingroup\$ I have some more materials on this topic in work, if you are interested i will share it too. \$\endgroup\$
    – Notabene
    Commented Mar 31, 2011 at 19:16
  • \$\begingroup\$ 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 :-) \$\endgroup\$
    – Etan
    Commented Mar 31, 2011 at 21:46
  • \$\begingroup\$ I am interested at the least if that matters :) \$\endgroup\$
    – James
    Commented Mar 31, 2011 at 21:48
  • 1
    \$\begingroup\$ @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) \$\endgroup\$
    – Notabene
    Commented Mar 31, 2011 at 22:28
  • 1
    \$\begingroup\$ @Etan @James I added some more stuff for you. Both is worth reading if you are interested on this topic. \$\endgroup\$
    – Notabene
    Commented Apr 2, 2011 at 19:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .