How do I add multiple textures to a shader?
Setting up multiple textures is the same as setting up a single texture.
See this first as we will be adding to the example.
Add a second texture in the material panel, from the previous example
Only uv coordinates will be used, so the vertex shader will be identical to the previous example
void main() {
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
The fragment shader needs another uniform sampler for the second texture.
uniform sampler2D texture0;
uniform sampler2D texture1;
void main()
{
}
With a second texture you can now add some variations to the final output of the texture. You can multiply the two together, or you can add the two together, its up to you.
for this example we will mix them with the following formula
a+b-0.5
the fragment shader will look like:
uniform sampler2D texture0;
uniform sampler2D texture1;
void main()
{
vec4 texa = texture2D(texture0, gl_TexCoord[0].st);
vec4 texb = texture2D(texture1, gl_TexCoord[0].st);
gl_FragColor = texa+texb-0.5;
}
Another uniform was added to the shader so it needs to be added to the material loop.
shader = mat.getShader() if shader != None: if not shader.isValid(): shader.setSource(VertexShader, FragmentShader,1) first_texture_index = 0 shader.setSampler('texture0', first_texture_index) second_texture_index = 1 shader.setSampler('texture1', second_texture_index)
And the final script
##----------------------------------------------------------------------------- ## Shader Texture (howto) ##----------------------------------------------------------------------------- import GameLogic ObjectList = GameLogic.getCurrentScene().getObjectList() # ------------------------------------- ShaderObjects = [ObjectList['OBCube']] MaterialIndexList = [0] # ------------------------------------- # ------------------------------------- VertexShader = """ void main(){ gl_TexCoord[0] = gl_MultiTexCoord0; gl_Position = ftransform(); } """ FragmentShader = """ uniform sampler2D texture0; uniform sampler2D texture1; void main() { vec4 texa = texture2D(texture0, gl_TexCoord[0].st); vec4 texb = texture2D(texture1, gl_TexCoord[0].st); gl_FragColor = texa+texb-0.5; } """ def MainLoop (): # for each object supplied for obj in ShaderObjects: mesh_index = 0 mesh = obj.getMesh(mesh_index) while mesh != None: # for each material in the mesh for mat in mesh.materials: # sanity check.. # is the use blender materials option checked? if not hasattr(mat, "getMaterialIndex"): return # 0->15 avaiable mat_index = mat.getMaterialIndex() # find an index from the list 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) first_texture_index = 0 shader.setSampler('texture0', first_texture_index) second_texture_index = 1 shader.setSampler('texture1', second_texture_index) mesh_index += 1 mesh = obj.getMesh(mesh_index) ## call it MainLoop()