Mittwoch, 20. März 2013

[BGE + GLSL] GLSL Shader Repository Addon

The Blender Game Engine can make use of shader written in GLSL. In the standard version you can easily use vertex and fragment shader for your objects and fragment shader for post-processing (also known as 2D Filter).



Simple way to add 2D Filter
Adding post-processing effects is very simple. In the logic brick section you'll find an Actuator called "Filter 2D" where you set up the filter you want to use. You can choose from pre-built filters or one of you own from a text file.

To just GLSL shader on your objects you need to do a bit more:
Use GLSL shader on object
To use a GLSL shader on your object you need to write a bit of python code.
My code does it in a very simple way. I assume that my object is just one mesh. Therefore I put the code only on the first mesh. A mesh can have different materials. Because I don't care I put my code on all attached materials. Currently I can only set the vertex and fragment shader code. That can come from a string defined somewhere or loaded into a string from some file. Because I like to keep code separate I have put the shader code into own files and load those into strings at the beginning of the function. You can also push some variables, also known as uniforms, to the shader. In my case the are a texture, some matrices and some floats. (Read more about those here: http://www.blender.org/documentation/blender_python_api_2_66a_release/bge.types.BL_Shader.html?highlight=shader#bge.types.BL_Shader)

Recent development in the harmony branch (https://svn.blender.org/svnroot/bf-blender/branches/ge_harmony/ ) is making using shader much easier, and also brings support for geometry shader.
Improvements inside the harmony branch
You don't need python code anymore. Just add a custom shader, choose its type, its source and you are ready to go.


A big problem remains:
Where do I get all those fancy shader from or do I have to code them all by myself?

Yes. And No.

For most things you don't need a custom shader. There are built-in post-processing shader and for materials and objects you can use the material nodes most of the time.
But there are cases where you need custom code. If you are lucky someone already posted the answer to your question on some forum, probably on http://blenderartists.org/ . Have fun searching…

There was a discussion about integrating more shader into Blender:
http://blenderartists.org/forum/showthread.php?283250-Shader-Trunk-Integration

And because I had time and an idea I made a little addon:
GLSL Shader Repository

Basically there is a repository at https://bitbucket.org/Urfoex/bge-shader/
It has some shader sorted in folders.
The addon "GLSLShaderRepository.py" can be downloaded from the top directory. (Click on the file in the source view: https://bitbucket.org/Urfoex/bge-shader/src and select RAW from top right. Then save it e.g. via File → "Save page as …" or by pushing Ctrl + S ) Inside Blender go to
File → User Preferences… → Addons → Install from File…
and select the fresh downloaded file GLSLShaderRepository.py
You should get something that looks like this:
Addon loaded and activated
 You don't need to change a thing. If you disable "Use ZIPped Version" the addon will try to download the files using mercurial. Now you'll find a new option under File → Import:


Import shader from repository
That will download all shader from the repository and modify some files. Because of the modification you need to restart Blender now. Just then the new options will appear. They will be in the editor under Templates:
Vertex shader
Fragment shader
Post Processing shader

Click on one of the shader and it gets loaded as an internal file.
So currently the post-processing shader are easiest to use with the Filter 2D Actuator.

Most of those shader I found on http://blenderartists.org/
I haven't tried them so they might not work. Please post issues (best on the repository site) so they can be fixed.

It would be very nice if you could also give me more shader to put in the repository. There is also a reference folder in the repository containing lots of shader code that would need some revision to make it compatible with BGE.


Now have fun with shader!