Shader: setSampler

setSampler(name, index)
  • Sets the sampler. 
     
  • This defines which texture will be used and how.

name:

  • Type: string
  • name is defined in the openGL code.
index:
  • Type: integer
  • index is the position of the texture in Blender's Texture tab.
  • 0 = First position
  • 1 = Second position
  • Etc.
Note:
  • Shaders work with Viewport Shading GLSL and Viewport Shading Multitexture.
     
  • Shaders don't work with Viewport Shading Singletexture.
Sample Code

############################# set the sampler

############## main program

def main():

# import bge
import bge

# get the current controller
cont = bge.logic.getCurrentController()

# get object that owns this controller
obj = cont.owner
 
# Get a list of the mesh
meshList = obj.meshes

# Get the first mesh on the object
mesh = meshList[0]
 
# only one material on mesh
mat = mesh.materials[0]

# Using Blender GLSL Materials or Blender Multitexture Materials?
if hasattr( mat, 'getMaterialIndex') == True:
 
# get shader envelope
shader = mat.getShader()

# did it get the shader envelope?
if shader != None:
 
# set the source and use it
shader.setSource(VertexProgram,FragmentProgram, True)

# "color" is a texture called by the Fragment Program just loaded
# 0 is the 1st Blender texture channel
shader.setSampler("color", 0)

####### end of main program

VertexProgram =  """

void main()
{

// OpenGL glsl code
gl_TexCoord[0] = gl_MultiTexCoord0; 
gl_Position = ftransform();


}
"""

FragmentProgram = """

// texture passed from main part of program
uniform sampler2D color;

void main()

{

// Get texture from color map
vec3 texture   = (texture2D(color, gl_TexCoord[0].st).xyz);         

// Use it
gl_FragColor = vec4(texture, 1.0);


}
"""

############  Run Program

main()