Monday, 17 June 2013

Monday Shadow Success

Weekend Power

It took a long 8 hour stint on Saturday and most of Monday but I finally managed to crack the cascade shadow mapping trick. Phew!  It has been a while since I coded some deep 3D or shaders, and last week it was very much a case of cut and pasting my way to victory.

Alas when things start to go wrong, or shadows render in strange and unpredictable ways, you have nowhere to turn when you base your techniques on someone else's idea.  I resolved to sit down and learn every single field, call, transform and math step and really understand what shadow mapping. This approach worked, and mid-day Saturday I began to see how the shadow coordinates where calculated to produce the desired results.

Blue Screen Of Despair

It did not help the situation when close to the end of Saturday, my new machine did a massive BSOD and completely erased one of the header files I was working on. All gone. No back-ups that I could find and despair for me. Rewriting it would have taken another half a day, and then in my darkest hour I remembered that I had set-up a network backup system to grab the contents of the drive and copy it each night at 7PM. Amazingly I had a complete version from Friday night and was able to put it back in and continue working.

Monday 6PM

As of now, what I have you can see below. The clever stuff is invisible of course, but the shadow you see is actually generated from four depth render targets and seamlessly stitched together using orthographic projections and some clever 3D math.

In order to be able to read the depth buffer for this technique, I discovered a cool hack you can make on DX9 hardware to transfer the depth buffer contents over to a regular texture that can be passed to a shader. Using the same trick, it might be possible to do some really nice effects such as depth-sensitive smoke and particle effects to avoid those sharp polygons cutting into the scene. I will leave those for another day, but now I have access to the depth buffer from a single pass with no loss of performance, it's another tool in my Reloaded toolbox for the future.

The battle is far from over as the current prototype does not use PCF (percentage closer filtering), cascade blending (to make the cascade transitions invisible) and I still need to check out the depth ranges I am using to ensure I get the highest quality depth information from each cascade.

Signing Off

Sorry if you where expecting a video, but I am going to wait for some new artwork from Mark before putting something on YouTube as I think it will show off the shadow technique better with brighter more interesting objects.  I have just made a massive backup of my files so far (twice) in a bout of healthy paranoia and I am going to switch to a few emails that need my attention. I was hoping to get the shadow stuff cracked today, and my mission was a success. The lesson learned over the last few days is that as good as cut and paste is, there is no substitute for a piece of paper, a pen and a brain that wants to learn how to do the 'working out'.  Well done brain!


  1. Looks really good and nice to see you got it working Lee.
    But are there some artefacts on the edges of the objects? Like little white seems? Might also be image quality or my eyes, just wondering.
    And you should consider to smooth out the shadows, so they don't have such a hard edge, but still great progress.

  2. Hi Lee, well done to you on this progress next step. Now, just take a few minutes to rewrite the part of your brain that thinks it knows what where and were mean!

    "I began to see how the shadow coordinates where calculated" - should be were.

    "Sorry if you where expecting a video" - should again be were!



  3. I'm certainly looking forward to FPS Creater: Reloded.

  4. The sharp edges will be solved when I use PCF and the white edge artefacts are the result of the orthographic ranges being a little out for that scene. Once this system is in the real engine I can tweak the values from the shader :)

  5. Hi its my first post, I loved FPSC but this looks amazing its what i wanted x10 was gonna be and then some.

    the only thing that i want its my number one on my wish list and that's half grid segments.

    is there any chance we can increase the grid and have this?
    just being able to put windows half a square closer or further away will do amazing things for realism.

    1. @Michael:

      I agree with you in that half-width segments would make it look heaps better (if you knew how to use them effectively), but the way the engine is designed internally means that is virtually impossible. HOWEVER, it WOULD be possible to create custom segments that act as half-segments; heck, it's possible in current FPSC, but you gotta know what you're doing.