Small pre-pre-beta shadow mapping lirary

I’m releasing a small library for shadow mapping in XNA. This is the beginning of a library that will allow using shadow maps in XNA without writing a new shader, or setting up your models. Just using some calls to a library class.

I also included a small example of using the library with 1 object. The main class of the library is ShadowMapRenderer, and it implements PCF shadow mapping, with screen-space bluring.

Remember, this is a very early version, I will probably change the API very much until a real beta version. I just thought I’d put it out to help people who want to have quick shadows in their 3D games.

You can download it here: ShadowMapRenderer

Have Fun!

Categorized: HLSL, XNA
  • Jan Willem

    Sounds very interesting. I’ll take a look at it when I’m back at home. First time I comment here, but I’ve used your samples more then once in the past, and they are excellent! Thanks for that! Anyway, I think I’m not going to use this library but I will certainly learn from it :). Is it alright if I just use the code instead of the complete library?

    Keep up the good work!

  • CatalinZima

    Sure, go ahead and use whatever you want.

  • Mahdi Khodadadi Fard

    Hey Catalin, this library sounds exellent. when the final version will be out ?

  • Catalin Zima

    It’s going to take a while. Right now, my time is almost fully dedicated to my exams.
    This library will be a slower-growing one, I won’t rush it, because I want to make it a good one.
    Whenever I’ll make significant progress, I’ll blog about it.

  • Nick

    You are my best friend. :p I’m just starting a 3D game and was not looking forward to doing the shadows. Hopefully I can toss this in tonight and see how it works out for me.

  • Jeff

    I like how well you have minimized the intrusiveness of this component. Any way we can get the shader sources? I could only find the .xnb files in the archive.

    Thanks for sharing!

  • Pingback: » XNA Shadow Mapping Library

  • Catalin Zima

    Nick: try it for a start, but don’t get too used to the API. As I’ve said, I’ll change the API to support moving and animated objects. If you have any questions, drop me an email

    Jeff: I’ll try to make it even less intrusive, but that’s work in progress.

    I’ll give out the shader code once I’m satisfied by it 🙂

  • Andreas Alexandrou

    Hey Catalin, i need some advice creating shadows for moving/animated meshes (characters etc….). Could you give some advices?


  • Catalin Zima

    What I’m going to do in the next version of the library is expose a way to update the world matrix of an object in the list kept by the ShadowMapRenderer.
    I’m still working on the best way to do this, but if you want a quick and dirty solution, you can make the list of casters public, and remember the index of each added object.

    If you’re not afraid of a little garbage (until the next verison comes out), simply modify the library code, clear the list of shadow casters at the end of each frame, and add the casters with their correct world matrices each frame.

  • Roonda

    Thankyou Catalin, that is truly excellent. Can I put forward submission for future design changes? I’d love to use your library in a WinForms tool, which means the fact that you’re using Game class is unfortunate. You only use the Game to get the GraphicsDevice and to pass the IServiceProvider to the ContentManager. However, if the constructor takes an IServiceProvider instead of a Game, you can get the GraphicsDevice via IGraphicsDeviceService, as well as pass it to the ContentManager. I have made these minor changes myself, but I’d prefer to use a clean version of your DLL. Can’t wait for your updates. Oh and good luck in your exams.

  • Catalin Zima

    That’s great, Roonda.
    Request noted, and will be included in the next version. Thanks for the indications.

    I’m almost done with my exams (one more to go, at the and of the week) and then I’ll get back to coding 😉

  • Andreas Alexandrou

    Hello Gatalin again.

    Could you give me some pointers how to make your library work in XNA 1.0. Am trying for the past few hours with no luck.

    I have make a project in XNA 1.0 and i had no luck converting it to 2.0.

  • Roonda

    Andreas: I doubt it is possible to get the shadow mapping library working in an XNA 1.0 game, as it was built against XNA 2.0. You should bite the bullet and move to 2.0 or 3ctp.

    Catalin: Is it possible to have translucent shadows support in your library? I gave it a try by turning the sprite drawing blend mode on and drawing with a colour with 50% alpha but with no success. Maybe the alpha value needs to be rendered into the shadow texture.

    One other thing, that I’m sure you’re aware of, is the outline of the self shadowed objects. I guess it is the feathered shadow edges causing the outline to be visible, and I have no idea if there is something that can be done about this, but I hope so 😉

    Also, for others using this library, if you need it to support moving objects, just do this:
    1) Iterate through all the Model’s ModelMeshes and all the ModelMesh’s ModelMeshParts
    2) Create your own ShadowCasterObject for each ModelMeshPart. Store these in a list that is related to the parent ModelMesh (maybe via a Dictionary).
    3) Use the version of AddShadowCaster() that takes a ShadowCasterObject instead of the one that takes a model.
    4) Each frame, update all of your ShadowCasterObject’s World matrices based on their parent ModelMesh’s World matrix.

  • Catalin Zima

    I was going to say that for translucent shadow you should just set the ShadowColor property to a higher value 0.7+, but then I realized that the property isn’t exposed in the version uploaded.

    I saw the outline, but I haven’t yet been able to remove it yet. I’m looking into it.

    That’s what I’m going to do in the library itself for moving objects. Unfortunately, I’m stuck on deciding how to handle skinned model animations. I have to choose between supportinh shadows for skinned model animations, and leaving the user flexibility. If I try to support skinned models, I’ll need to force used to use a certain kind of skinned models shader, and that’s not an option I’m willing to take. I think I’ll just ignore support for skinned models.

  • Catalin Zima

    as for the next release.. I’m currently working on the fur tutorial, and after that, I’m going to update the shadow library sometime during July and August.

  • Roonda

    Yep, integrating it with existing skinning is definitely the difficult bit. Maybe you could somehow allow custom Effects, but impose requirements like variable names or semantics, texture formats. That would be really cool. But no pressure, I’m delighted with what you’ve done already, and all that’s in the pipeline 🙂

  • Lesha VH

    Could u please include shaders code for further improvement? thanks!

  • Catalin Zima
  • http://tex2dproj Enzi

    Hi Catalin!

    Nice work, but I have one question.
    Is the tex2dproj function really working on ATI Cards? I heard its a Nvidia function only.


  • Catalin Zima

    tex2Dproj works on ATI also, as far as I know (maybe someone can confirm?).

    The one you’re referring to is tex2Dlod, which is related to vertex textures (see my tutorial on vertex textures), and doesn’t work on ATI cards.

  • Zuk

    Hey Catalin, do you mind if I use this for the DreamBuildPlay competition coming up in August? I’m out of time to create my own shading since I have no experience with HLSL. 🙁

  • Catalin Zima

    Sure, no problem. You can use it with no restrictions.
    But I won’t be working on any improvements and extensions at least until DBP is over.

  • Zuk


  • Lima

    Hi, I’ve downloaded the sample, but the assembly library seems to be missing. The project references the file ShadowMapRender.dll which does not exist.
    I’m having some trouble with shadow maps, and maybe you could help me. Do you have any contact email I can use? Thanks.

  • Catalin Zima

    There’s nothing missing. The library is provided as a XNA Game Library Project (the ShadowMapRenderer project), with the source code included.

  • Lima

    Sorry. You’re right. I unzipped the file to my projects root directory and the project got lost in the middle of the other projects. Usually zip files have a root folder with subfolders.

  • Lima

    Catalin, unfortunately my video card does not support the Single Surface Format (24 bits red channel if I’m not mistaken).
    Is there any alternative format I can use?

  • Catalin Zima

    You could try and use Color, but the precision will be very low, and the results will not look too good.

  • Lima

    Hum… That could explain the problems I’m having.
    But there must be another way to do shadow maps without the Single surface format. I’ll investigate this. thx

  • Robert

    Awesome work, I surely have learned a lot from your lib, thank you and keep it up!

  • ktg

    Any progress using thiswith skinned messhed?

  • Catalin Zima

    unfortunately no

  • Tim

    Hi Catalin,

    Nice work. There are a couple of things I was hoping you could help me understand.

    1. What’s the purpose of the “RenderShadows” function and the MatTexture?

    It looks like this solution draws each model 3 times. Once in RenderShadowMap, once in RenderShadows, and once to truely render the model.

    I understand that RenderShadowMap draws each object in order to create the shadow, but I’m not sure what the “RenderShadows” function does.

    2. In my world, 1 XNA unit = 1 meter. As the scale of the model becomes smaller, the the detail of the shadow becomes much worse. I’m assuming that it’s because we’re using less area of the shadow map. So, I reduced the orthographicSize to 10 and that helps. However, the self shadowing on the models doesn’t look right. (Just shrink the model to 1/10th the size and you’ll see what I mean).

    Can you help me understand the relationship between the size of the model, the shadow map, and the rendered shadow itself?

    Thanks for your time and great work!!

  • http://Website Sun

    Catalin you r very very very goooddd 😀 Thank for everything

  • Takako Amesquieto

    Thank you for your time and efforts to have put these things together on this blog. John and i also very much appreciated your knowledge through your articles with certain things. I’m sure that you have a variety of demands on your timetable so the fact that you took just as much time like you did to guide people like us via this article is actually highly appreciated.

  • Pingback: mass effect 3 beta