Faster Ray Traced Terrain Shadow Maps

Monkey Forums/Monkey Code/Faster Ray Traced Terrain Shadow Maps

Raph(Posted 2013) [#1]
I found this fast enough to use in real-time in Monkey in maps up to 256x256. I extracted it from a larger codebase, and I think I removed the stuff specific to my code. If not, it should be easy to spot. :)




GW_(Posted 2013) [#2]
Very cool!


Sammy(Posted 2013) [#3]
Looks great, any chance of some demo code, to show how to use it?


Raph(Posted 2013) [#4]
Well, you need a fair amount of code to use it -- some way to render the map, some way to generate the map, etc.

How I am using it is more or less like this (mostly pseudocode)



When I go to render it, I am actually only letting this shadowmap contribute 10% of the overall light, because it's a binary value -- either it's shadowed or it's not. So I blend in two other light levels. One is purely based on the elevation of the tile. The other is based on the gap between the highest and lowest corners of the tile.

Here it is with only the gap between highest and lowest -- basically, this is relief lighting. I am just using SetColor here to darken the quad.



Then if you add in the output of IntersectMap, you can see it does self-shadowing.



I am doing soft shadowing by overlaying four shadow blends onto each tile, one from each corner of the tile. Same idea as vertex colors, but DrawTextPoly doesn't have those. :) I query the light levels of the neighboring tiles, and pick a light level for that corner. Then I set the alpha for that blend, and repeat for each of the four corners. As you can see, my shadow blend tiles are not perfect. That's actually been the hardest part of this! You can see a sort of "grid effect" on the big shadowed hillside.



The last touch is a little trick that helps pop hills off of backgrounds with similar light levels -- an up to 10% penalty on light level based on elevation. This way ridges pop off, though the price is a slight darkening of the overall scene.



I call UpdateLights() whenever the sun moves, which I currently have on some hotkeys...

I am probably going to rework this so that instead of just drawing a black shadow texture with alpha, I'll draw a white one, and SetColor it to the right light level. Then I can add in a vertex color map into the mix without incurring any additional draw calls.


muddy_shoes(Posted 2013) [#5]
Pretty nifty. Mojo can be persuaded to take a vertex colour array if you're willing to get your hands dirty by the way.


Raph(Posted 2013) [#6]
Yeah, I was pretty sure it could be extended for that. Given that Mark was going to do his 'Mojo2din3d' thing, I wasn't sure whether to bother. And really, I am past the overkill point on this, considering it's primarily intended for a background for a bird to fly over. ;)

Still having issues with alpha in the HTML5 target, btw. Have been trying varied params in TexturePacker and so far have only managed to make it worse. :P


muddy_shoes(Posted 2013) [#7]
Extrude with a value of 1 seems to do the trick for me but I can't be sure the few tiled textures would show the same issue you're having. WebGL is just a convenient test platform rather than a serious target for me at the moment so I've also only looked at output on Chrome on my laptop. There could be variation across browsers and drivers, I suppose.

I also use my own atlas code that could vary in behaviour from what you're using. I should maybe get around to organising that and releasing it as a module sometime. It would give me an excuse to ask the TexturePacker guy for a free license as my upgrades expired a while back.


Raph(Posted 2013) [#8]
Good point about the different browsers -- haven't thought to try in more than Firefox yet.


Sammy(Posted 2013) [#9]
Many thanks for the extra information and pics Raph.