Thursday, 26 June 2014

Custom GLSL shaders in BGE

Has it been over a year since the last post!? Whoops.

As you may or may not be aware the blender game engine has support for custom GLSL shaders. These allow greater potential variation than the material panel by giving an artist access to how the game renders at a lower level.

I've been having a go at writing these recently:

First off we have a standard Phong shader, This uses Lambert diffuse and a stretched Phong for specular highlights. There's a version with clipped alpha support as well.

Next up cube mapping! For non-planar reflections, cube maps have pretty much become the standard in real-time applications. Blender's support of them is unfortunately a bit limited, I was unable to get variable roughness with cube maps as there is no support for choosing the mip level with them. I was also unable to swap them around with bge.texture, making changing the reflections based on the location in a level difficult to achieve.

Finally we have something a little more interesting, normalized Phong! This is a simple tweak to the Phong shader that provides quite a pleasing result. it takes principles seen in PBR (physically based rendering) systems and applies them to a simpler model.

Essentially what this shader is doing is using 1 texture to control specular intensity and gloss values, but it does so in a physically correct manner. this makes for small, bright highlights on shinier objects and more matte, dimmer specular on rougher objects.

All the shaders you see above are gamma corrected so they should look fine on most monitors, I may modify them to accept external values for the gamma. It would be trivial then to make a gamma slider option, that'd be all professional and whatnot.
Unfortunately they don't have shadow map support and only seem to recognize up to 8 lights, that said future versions of blender have been pegged to be getting some updates in the shadows area (hopefully), not sure about the light limit.

===================== File Download ========================

Hopefully someone will find them useful!

There will be another post up next week continuing on from this one.