Light shaft shader
This shader adds a flare-like effect for the sun which also casts light shafts. I learned about the method from this tutorial.
The way this shader works is like this. First, it detects which pixels are sky - this should be done using the z-buffer but because that is unavalible in Sauerbraten right now I instead detect sky using brightness. Second step is to apply radial blur originating from the sun's location. This is the most hardware intensive step. The third and final step is to blend the blurred sky over the original scene.
An important part of this effect is also a cubescript loop which calculates the sun's position on the screen and passes it to the shader. The calculation requires transforming the sun's angles to a vector, rotating the vector according to camera angles and projecting it to the screen. Huge thanks to my brother for explaining the math and making this part work for me.
Copy lightshaft.cfg to your game directory and then add "exec lightshaft.cfg" into your autoexec (you can do this in-game).
When ingame use the command /lstoggle Q which will toggle light shafts on and off.
Q is the optional quality of the radial blur (100 is the best quality). Valid range is 10 to 100, and by default it is set to 50. You can also add "lstoggle" into your autoexec to have lightshafts on all the time.
Do not use addpostfx to start the shader - it requies a cubescript loop to update the sun's position.
- lightshafts are only rendered near sun, thus creating a flare
- lightshaft center is no longer calculated from sunlight direction, instead it is set to the sun's (or moon's) position in each skybox
- Q in lstoggle Q now controls the quality in percentage instead of number of passes for blur. This means that setting the quality to 50 in new lightshafts results in better quality than 50 in old lightshafts
These changes cause lightshafts to work with nearly every daytime map and even with some night maps. They also add a glow flare-like effect to the sun. Plus only rendering lightshafts near sun improves performance.
- Optimized! Mainly "if" removals (GPUs don't like ifs)
- Sun is now slightly colored
- lstoggle has a second parameter which controls the maximum number of checked pixels (like in the first version of lightshafts)
- lightshafts are now turned off on skyboxes without sun
- lightshafts will use sunlight as the sun's position on custom skyboxes
- added comments to the shader code
(script not available for preview because quadropolis doesn't like greater-than and less-than signs)