Pages

Friday, April 2, 2010

Crepuscular Rays/God Rays

Have you ever witness a sunset/sunrise and saw rays of light emitting from the sun through gaps in clouds or between buildings? If you have, lucky you! If not, i hope to show you through the use of computer graphics, the beauty of this natural phenomenon known as crepuscular rays.

Crepuscular rays a.k.a god rays, to put it simply, occurs when light from the sun is occluded by various mediums (clouds, buildings, small atmospheric particles) and scattered across the atmosphere. To illustrate these words, below is a screenshot showing crepuscular rays running real-time in MageDx.

Crepuscular rays running real-time in MageDx Tech Demo

My implementation is loosely based around the one described here with a few tweaks mostly pertaining to quality and performance. If you have implemented this technique before, you would almost immediately notice one shortcoming, that is the huge amount of samples (100 samples and above) you would have to take in order to generate moderately smooth crepuscular rays. This means you are limited to using pixel shader 3.0 and above for this technique, and even then, you hit the instruction count limit pretty fast as you increase the number of samples. Luckily in this case, there is an alternative way of doing the sampling that is described in one of the crytek presentation slides found here. In short, instead of brute force sampling, we sample using an iterative process. In my case, i iterate over 3 passes with each pass taking 8 samples. The result is comparable to the brute force method quality wise, is way cheaper and fits well within the instruction count limit of pixel shader 2.0. Also described in the slide is a cheap way to generate the occlusion mask provided that you have scene depth stored.

Screenshots in this case does not do justice to how
crepuscular rays looks like in real-time. As such, i have included 3 videos showcasing crepuscular rays in different environments and light settings. Have fun watching!