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

  • #1 written by yodaime
    about 1 year ago

    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)

  • #2 written by Pnikosis
    about 1 year ago

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

  • #3 written by bubbapops
    about 1 year ago

    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!

  • #4 written by Lawrie
    about 1 year ago

    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.

  • #5 written by Chase
    about 1 year ago

    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.

  • #6 written by Brandon
    about 11 months ago

    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.

  • #7 written by DarkPrisma
    about 10 months ago

    Hi,

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

  • #8 written by Twitchy
    about 5 months ago

    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)