Blender can now assign OpenGL SL pixel and vertex shaders for use in the Game Engine. More robust notes and documentation can be found in the Game Engine documentation.

 

 

Shaders can be applied to unique materials with python. For each material on an object there, is access to a single shader for that material. Most GLSL functions are accessible, with a few exceptions. Attribute locations are not available.

 

For instance, the following declaration will not work:

//--

attribute vec3 variable;

//--

 

In the fragment shader, the only available samplers are

// --

uniform sampler2D;

uniform samplerCube;

//--

The sampler2D refers to an Image type, and a samplerCube refers to an EnvMap.

 

To access uniform variables in your program you need to supply the name/value of the variable. shader.setUniform[if][v](name,value)

 

A vec3 would be

shader.setUniform3f(name,f,f,f)

or

shader.setUniformfv(name, [f,f,f] )

 

Samplers are handled a little differently. They are accessed by index (0,2). The index references the order the texture is supplied in the material panel. If there is no material, 0 can always be used as a valid sampler index.

 

shader.setSampler(sampler_name, texture_index)

 

Template.

#------------------------------------------------------------------------------

import GameLogic

objects = GameLogic.getCurrentScene().getObjectList()

 

# -------------------------------------

ShaderObjects = [] # add objects

MaterialIndexList = [0] # material index

# -------------------------------------

 

VertexShader = """

void main(){

gl_Position = ftransform();

}

"""

 

FragmentShader = """

void main(){

gl_FragColor = vec4(1.0, 0.5, 0.0, 1.0);

}

"""

 

def MainLoop ():

for obj in ShaderObjects:

mesh_index = 0

mesh = obj.getMesh(mesh_index)

while mesh != None:

for mat in mesh.materials:

if not hasattr(mat, "getMaterialIndex"):

return

mat_index = mat.getMaterialIndex()

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)

# set shader variables

# --

 

mesh_index += 1

mesh = obj.getMesh(mesh_index)

# -------------------------------------

MainLoop()

#------------------------------------------------------------------------------

 

 

-------------------------------------------------------------------------------

If an error occurs material.getShader() function will return None, and the shader for the material will not be used.