combine glsl shader with blender materials

Dear community,

I need help with materials.
I’m using the envmap-glsl-shader for reflections:

import GameLogic as g
objlist = g.getCurrentScene().getObjectList()
g.setLogicTicRate(60.0)
 
# -------------------------------------
 
obj = objlist['OBSphere']
#obj = objlist['OBCube.002']
 
camera = objlist['OBCamera']
cam2world = camera.getCameraToWorld()

MaterialIndexList = [0] # material index
#GlobalAmbient = [0.39,0.35,0.32,1]
#AmbF = 0.5
# -------------------------------------
 
 
VertexShader = """
 
attribute float time;
varying vec3 dir;
uniform mat4 cam2world;
 
void main()
{
    gl_Position = ftransform();
    
    vec3 normal = normalize(gl_NormalMatrix * gl_Normal);  
    vec3 view = (gl_ModelViewMatrix * gl_Vertex).xyz;  

    dir =  mat3(cam2world) * reflect(view, normal);
}
 
"""
 
 
 
FragmentShader = """
 
uniform samplerCube CubeMap;
varying vec3 dir;
 
void main (void)
{

    vec3 envColor = vec3(textureCube(CubeMap, dir));
    
    vec3 baseColor = vec3(0.0,0.0,0.);

    envColor =         mix(envColor, baseColor, 0.0);
    
    gl_FragColor = vec4(envColor, 1.0);
    
}
 
"""
 
 
 
def MainLoop ():
 
    mesh_index = 0
    mesh = obj.getMesh(mesh_index)
 
    while mesh != None:
 
        for mat in mesh.materials:
               
            # regular TexFace materials do NOT have this function
            if not hasattr(mat, "getMaterialIndex"):
                return
               
            mat_index = mat.getMaterialIndex()
 
            # find an index            
            found = 0
            for i in range(len(MaterialIndexList)):
                if mat_index == MaterialIndexList[i]:
                    found=1
                    break
            if not found: continue
 
            shader = mat.getShader()
            if shader != None:
                if not shader.isValid():
                    shader.setSource(VertexShader, FragmentShader,1)
                   
                shader.setSampler('CubeMap',0)
                #shader.setUniform4f('eta', 0.67, 0.65, 0.63, 0.0)
                shader.setUniformMatrix4('cam2world',cam2world)                
        mesh_index += 1
        mesh = obj.getMesh(mesh_index)
        
 
# -------------------------------------
 
MainLoop()
 
#------------------------------------------------------------------------------

As a result, it overwrites all material settings of my blender materials,
Isn’t it possible to define a mix value, how much the materials will be mixed?

By the way: has anyone experience with dynamical envmaps for the game engine?
and: how is it possible to assign the shader to a cube-mesh?

thanks a lot,

greetings

Hi,

When you use shaders (whether HLSL or GLSL) they completely override the standard, fixed-function, materials.

It’s one or the other I’m afraid.

For example, if you have a GLSL shader that changes the texel colours of a texure applied to a mesh object, and you need the material to have Phong specularity, then you have to code it into the shader. This is also true for things like alpha-transparency.

You can’t do a 50:50 mix.

Hope this helps.

Journeyman

that’s why I was suggesting how awesome it would be if we could get the generated code for the blender GLSL material, made in the material editor so we could for instance combine it with our own custom glsl code… would be super awesome!!