Me and my shadow...

Published February 15, 2015
Advertisement
distance2.jpg

So I finally got to working on the shadow mapping and making it a bit more sophisticated. Previously I was only casting shadows directly down from above and only casting onto surfaces which were fairly flat, which was nice and simple, but we can do better, so started working on doing it properly.

I have used the normal offset shadow approach rather than biasing for getting rid of the shadow acne, having seen that et1337 had great success with this in Lemma, It does actually work very well for my case as well. Shadow acne is now gone completely and it all looks pretty rock solid.

I literally just had to change my shadow pixel shader like this:[code=:0]vector pos = mul(input.vpos, worldviewproj);// tovector pos = mul(input.vpos + vector(input.normal * 0.4f, 0), worldviewproj);
input.normal is already transformed to world space in the vertex shader. 0.4f is just a fudge factor that can be tweaked to suit.

distance.jpg

Coloured the shadows red in the shader here to make them easier to spot at a distance.

Next up, I added some per-pixel distance fog to the main pixel shader for rendering the level geometry and started playing around trying to get a good general matrix for the directional light casting. After much tweaking and fiddling, I have a matrix for the light that now draws shadows correctly on anything that is in the view frustrum and within the fog distance range.

I haven't really done this "properly", I understand you can transform the view frustrum corners into light space and do a fitting calculation like that, but I've found that if I just take a point in the middle of the view frustrum then offset a distance from this point with the light direction, that gives me an acceptable result.

Everything is working off a single value I set CPU side for the farClip. In the screenshots above we have a far clip of 200, and the fog starts at around 170 and the shadow quality is still acceptable. I can make the far clip larger, but the shadow quality degrades the further I make it, so all about finding a happy balance for each level here.

Casting shadows onto walls is sure a neat feature though and really adds to the game. I think the shadow quality is going to be less of an issue when proper detailed levels with normal and diffuse mapping are in place. Just very obvious at the moment as being projected onto flat colour surfaces. We'll see. Can always revisit the shadow matrix generation in the future, since there are a range of ways to improve this.

So if I now start to divide levels up into sectors, I can only render the sectors who would fall within the fog distance, and in theory can have very large levels now without performance suffering.

Thanks for reading.
Previous Entry It Lives!
Next Entry Thought about PIMPL
5 likes 4 comments

Comments

unbird

It's quite peculiar that with all that articles and threads about shadow mapping one doesn't see this approach more often. I also learned about it thanks to et1337's blog post. Works even reasonably well if you offset in the vertex shader already.

Edit: [url="http://www.gamedev.net/topic/665740-cant-fix-shadow-acne-with-bias/?view=findpost&p=5210839"]There you are[/url] smile.png

February 15, 2015 10:05 AM
Aardvajk

Yes, its strange. I know some people have reported that it doesn't work for their cases but I can't personally see a) how it would fail or b) why anyone ever preferred using any other kind of biasing. This just seems like a common sense approach to me.

Perhaps I'll encounter more problems as I add more sophisticated geometry to the mix.

Good thought re adding in the VS rather than the PS, will have to give that a try.

February 15, 2015 10:27 AM
unbird
Do you happen to have links about the downside of this approach ?
February 15, 2015 12:41 PM
Aardvajk
Will see if I can dig up what I saw. Was just a comment in a piece giving an overview of various dynamic shadow techniques I think.
February 15, 2015 02:47 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement