Shader-Based Dynamic 2D smooth shadows

This sample shows a method of drawing soft dynamic shadows in 2D using shaders. A detailed explanation of the technique can be seen here.

The algorithm used ensures that any object of any shape can cast shadows, and the complexity of the shadow casters does not affect the performance of the algorithm.

The sample shows a simple scene with some objects rendered in it (one of which is an animation), and two light casting shadows.

The red light can be moved with the arrow keys, and both light can be moved with a controller’s thumbsticks.

2dShadowMaps 2010-07-10 22-05-38-41

The source code: shadows2D.zip

  • Pingback: Catalin’s XNA Experiments » My technique for the shader-based dynamic 2D shadows

  • yodaime

    Hello Catalin !

    Which version of XNA framework did you used ? I think it’s version 4.

    Is it possible to convert this project into 3.1 version ? I tried but some method have seriously changed between 3.1 and 4 (like RenderTarget2D)

  • Pnikosis

    I’m having a really hard time trying to conert it to 3.1 too. Does anyone have a hint or an idea?

  • http://Website bubbapops

    The problem im getting is is not needing Framework 4.0 but rather this is a Windows Phone App so your gonna need the “Microsoft Visual Studio 2010 Express for Windows Phone” application, but from there the problems multiply for me I wish had the time to sort out all the issues but looks like this was created with special build requirements. If you have time Catalin it would be nice to see your example..hope this helps!

  • http://Website Lawrie

    I too had trouble loading it. In the end I made a new project file and simply added all the files into that and it worked fine. Amazing lateral thinking Catalin.

  • http://Website Chase

    Hello Catalin,

    I was attempting to implement your technique within my own project using my own map, where I came across an issue such that it isn’t displaying the shadows being cast. It displays the overlay with a spotlight in the proper light location, but that is it. Do I have to create a separate shadow mapped image that contains transparency and black pixels for the shadows to be displayed properly, where that is called within the DrawCasters function?

    Thanks for any assistance.

  • http://Website Brandon

    Thanks for the example! Looks good, but how would I go about getting my objects to receive shadows as well? For example, the ‘XNA’ should be visible but the cat at the top should be completely hidden from the Red light.

  • http://Website DarkPrisma

    Hi,

    is it possible to use a texture as light instead of the round light ball? A light beam from a flashlight for example.

  • http://Website Twitchy

    Great tutorial! Am going to use this in my game.

    Just wondering whether anyone could help me out if I were to be doing a Matric transformation on the sprite batch? (2D Camera)

  • http://Website Spectre


    Twitchy:

    Just wondering whether anyone could help me out if I were to be doing a Matric transformation on the sprite batch? (2D Camera)

    I need the same thing, did you ever figure it out? I know its been awhile.

  • Pingback: Lights « Code from the Wellspring of Incompetence

  • http://www.roughplace.co.uk Yakyb

    hi,
    i have been trying to convert this project to MONOGAME but i’m having issues with the shaders, would it be possible to do a followup on this but as a port for monogame

  • http://www.funhazard.com Fun Hazard

    Catalim Zima’s shadow system is simply genial.

    I loved this system so much that I wanted to make a Reach Profile compatible version, along with some improvements:

    - Many quality settings to best tune your lights.
    - Improved shadow calculation performance (rendering is about +15% faster than Part One and by tuning the lights quality settings you can go +250% faster than the original code without noticeable differences).
    - Different draw methods and effects can be assigned to each light.

    It can be downloaded here:

    http://www.funhazard.com/xna-resources.html