Custom GLSL Materials with Shadows

Hi, HG1 wrote shaders to access shadow maps and create custom GLSL materials with shadows. To make things easier, we made a patch: to get lights shadows data from Blender and now we can make our material shaders with shadows settings taken from Blender.

Here is the template we wrote:

UPDATE by HG1: 20/01/2016

  1. Fixed the bias value.
  2. Removed unneeded code for it.
  3. Removed unneeded python code to get program number.

UPDATE by youle with the help of Kupoman 26/01/2016:

  1. Support of several lamps.
    (I have to change active texture number to make it always < 32 and vec4 shadows[10];
    for (int i = 0; i < numlights; i++))

UPDATE by youle with the help of Kupoman 18/03/2016:

  1. Changed passing uniform to the shader method (using struct Light instead of variables array)

UPDATE by HG1 december 2016:

Fix issues with some hardwares: CustomGLSLMaterialsWithShadowsHG1Update.blend (157 KB)

Hope some of you will enjoy it :slight_smile:

Perfect! Very nice thing.
I never have thought that it comes into blender in the near future.

Now if one can just be able to see the custom GLSL shaders in the viewport (and perhaps even while modeling) without having to start the game first.

That type of ‘guessing game’ workflow belongs in the 1990’s, not the 2010’s.

@Maujoe: Thanks!
@Ace Dragon: There are many tools on the net to test your GLSL materials in realtime. Why a game engine couldn’t offer such features to advanced users? It’s useful if you want more control on what you’re doing, and it extends the creation potential of the engine. It doesn’t prevent devs to do other features beside:
And what’s the problem with that? It’s not because you won’t use this feature that no one will use it… I don’t understand what’s the problem.

@Ace dragon -> Please for the love of god… stop with the negative stuff when we get a sorely needed new feature.

These incremental steps are how we add up to working new PBR materials :smiley:

Those links point either to a patch written in C or a setup made of nodes and not code.

Anyway, I think it’s worth noting that I myself have attempted to get custom GLSL together with video textures working in one or more projects, and the whole trial-and-error guessing game where you have to start the engine to see the effect is a pain. Considering that, I also recall Moguri once starting work on a patch that would make those custom shaders fully viewable during game editing (but was later abandoned and forgotten about).

I’m not saying this patch is a bad thing (it’s good for those who use custom shaders a lot), but please don’t be suggesting that one is being negative and a nag for the mere suggestion that the workflow is not optimal.

(I say the devs not only work for advanced users: The first link is a patch that implement world textures in viewport. The second link is a video I made after I implemented CubeMap reflections in viewport (blender sources code).
Whatever we do, you’re still complaining about bge, I hopped these links could reassure you that the bge users can excpect changements in BGE. Cubemap reflections is the first step to realtime reflections/refraction in the viewport and in the game engine)

To test your glsl material, you can write it here: for example.

And when you say “GLSL is deprecated”, It exists only since gpu support it. It’s the programmation langage of the future. Blender does the same as unity:

And I don’t know for unreal but if you can’t code in GLSL on unreal engine… You can’t control what your GPU does… Unless it uses directX, I don’t know…

Thank you Youle! :smiley:

Awesome work youle, don’t let the BGE grinch deter you :smiley:

@BluePrintRandom and Thatimster: Thanks! Actually, as I mention in the video comments, the great part of the work has beeen done by Blender4web team (who implemented world textures (cubemap world textures)). I used their work to have material cubemap reflections in the viewport. I’m so happy that it works :slight_smile:

since you have cube maps as a default material setting, how hard would interpolating between light probes be ? For pre rendered environment maps?

@BluePrintRandom: Multiples light sources is already supported (sorry if I misanderstood the question). You can set transparency, specular, and the material reflect scene lights etc… And it has excellent performances on my computer (compared to GLSL shader written in Blender with only 1 light source or without lighting)

I am talking about mapping environment maps for all points in a grid in a level (like unity light probes)

as you walk, it blends these maps as your reflection map, so the reflection is correct in the whole level (or at least close)

another option would be for the player to generate a realtime cubemap (like hg1) and all objects around him get that cube map but with the player rendered on top of it somehow per instance…

hard to explain but here I go

Player generates cube map (everything around him is on it)

if you apply this to a object in front of the player, it is pretty accurate, but does not have the player included,
so maybe each object near the player that uses the reflection renders the player on a layer top of the cube map.

(so the reflection has everything in it and is failry accurate, without rendering a cube map for each object)

I have a question about the original topic. Is it possible to use this with multiple shadowmaps but without the need to edit the shader code for every single shadowmap(add uniforms for every shadowmap and so on) because this is very annoying if you use the shader in different scenes with different light setups and you have to edit the shader every time again.

I tried it with struct and a for loop but this seems not accepted by GLSL so there is no other way than edit the shader for every shadowmap like blender it does?

@Maujoe: I need to test…

EDIT: With bgl api we can pass some arrays but I’m stuck because GL_UNSIGNED_INT is needed to pass shadowBindId to the shader and it’s not in the bgl API.
And in some functions like: glUniformMatrix4fv(glGetUniformLocation(object[“prog”], ‘m_c2l’), 1, True, Buffer(GL_FLOAT, [4, 4], shadowMatrix)), the “1” is GLsizei count, and I don’t know how to pass 2 matrix or more whereas it works for floats and ints… So maybe Kupoman, Moguri, or HG1 have a solution…

I think he thinks you feed the shader lights as needed, instead of 1 time for all lamps.

ie changing lamps used in shader as you get within range.

@Maujoe: (MultiLamps)

Oh thank you but I get the same error that I get when I use struct:

Blender Game Engine Started
---- GLSL Program ----
Fragment info
0(95) : error C5208: Sampler needs to be a uniform (global or parameter to main)
, need to inline function or resolve conditional expression
0(95) : error C5208: Sampler needs to be a uniform (global or parameter to main)
, need to inline function or resolve conditional expression

---- GLSL program failed to link ----
Blender Game Engine Finished

Does it work for you?
Maybe it’s only my Graphic chip (Nvidia geforce Gt 540M) but for me it’s looks more like a GLSL limitation.

Maujoe: Hehe yes I works for me (GTX970)… I don’t know. So there is an error at this line: shadows[i] = mainShadows(shadowMap[i], ShadowCoord[i], shadowColor[i], shadowmaptype[i], shadowactive[i], shadowbias[i], bleedbias[i]); with shadowmap (sampler2DShadow)
I don’t know… I’ll try to look at your error on the net. I don’t understand because shadowMap[i] is an uniform.

EDIT: This: helps? Or could you try with other drivers for your graphic card?

Today I install another driver but I have the same problem.
When it works for you I don’t know why it doesn’t work on my computer because the shader should be okay…
Later I also will test it on a second computer.

Someone else test the file or can say something about the error that I get (above)?