KX_PolygonMaterial(PyObjectPlus)

base class — PyObjectPlus

class bge.types.KX_PolygonMaterial(PyObjectPlus)

This is the interface to materials in the game engine.

Materials define the render state to be applied to mesh objects.

Warning

Some of the methods/variables are CObjects. If you mix these up, you will crash blender.

from bge import logic

vertex_shader = """

void main(void)
{
   // original vertex position, no changes
   gl_Position = ftransform();
   // coordinate of the 1st texture channel
   gl_TexCoord[0] = gl_MultiTexCoord0;
   // coordinate of the 2nd texture channel
   gl_TexCoord[1] = gl_MultiTexCoord1;
}
"""

fragment_shader ="""

uniform sampler2D color_0;
uniform sampler2D color_1;
uniform float factor;

void main(void)
{
   vec4 color_0 = texture2D(color_0, gl_TexCoord[0].st);
   vec4 color_1 = texture2D(color_1, gl_TexCoord[1].st);
   gl_FragColor = mix(color_0, color_1, factor);
}
"""

object = logic.getCurrentController().owner
object = cont.owner
for mesh in object.meshes:
    for material in mesh.materials:
        shader = material.getShader()
        if shader != None:
            if not shader.isValid():
                shader.setSource(vertex_shader, fragment_shader, True)

            # get the first texture channel of the material
            shader.setSampler('color_0', 0)
            # get the second texture channel of the material
            shader.setSampler('color_1', 1)
            # pass another uniform to the shader
            shader.setUniform1f('factor', 0.3)
texture

Texture name.

Type:string (read-only)
gl_texture

OpenGL texture handle (eg for glBindTexture(GL_TEXTURE_2D, gl_texture).

Type:integer (read-only)
material

Material name.

Type:string (read-only)
tface

Texture face properties.

Type:CObject (read-only)
tile

Texture is tiling.

Type:boolean
tilexrep

Number of tile repetitions in x direction.

Type:integer
tileyrep

Number of tile repetitions in y direction.

Type:integer
drawingmode

Drawing mode for the material. - 2 (drawingmode & 4) Textured - 4 (drawingmode & 16) Light - 14 (drawingmode & 16384) 3d Polygon Text.

Type:bitfield
transparent

This material is transparent. All meshes with this material will be rendered after non transparent meshes from back to front.

Type:boolean
zsort

Transparent polygons in meshes with this material will be sorted back to front before rendering. Non-Transparent polygons will be sorted front to back before rendering.

Type:boolean
diffuse

The diffuse color of the material. black = [0.0, 0.0, 0.0] white = [1.0, 1.0, 1.0].

Type:list [r, g, b]
specular

The specular color of the material. black = [0.0, 0.0, 0.0] white = [1.0, 1.0, 1.0].

Type:list [r, g, b]
shininess

The shininess (specular exponent) of the material. 0.0 <= shininess <= 128.0.

Type:float
specularity

The amount of specular of the material. 0.0 <= specularity <= 1.0.

Type:float
updateTexture(tface, rasty)

Updates a realtime animation.

Parameters:
  • tface (CObject) – Texture face (eg mat.tface)
  • rasty (CObject) – Rasterizer
setTexture(tface)

Sets texture render state.

Parameters:tface (CObject) – Texture face
mat.setTexture(mat.tface)
activate(rasty, cachingInfo)

Sets material parameters for this object for rendering.

Material Parameters set:

  1. Texture
  2. Backface culling
  3. Line drawing
  4. Specular Colour
  5. Shininess
  6. Diffuse Colour
  7. Polygon Offset.
Parameters:
  • rasty (CObject) – Rasterizer instance.
  • cachingInfo (CObject) – Material cache instance.
setCustomMaterial(material)

Sets the material state setup object.

Using this method, you can extend or completely replace the gameengine material to do your own advanced multipass effects.

Use this method to register your material class. Instead of the normal material, your class’s activate method will be called just before rendering the mesh. This should setup the texture, material, and any other state you would like. It should return True to render the mesh, or False if you are finished. You should clean up any state Blender does not set before returning False.

Activate Method Definition:

def activate(self, rasty, cachingInfo, material):
Parameters:material (instance) – The material object.
class PyMaterial:
  def __init__(self):
    self.pass_no = -1

  def activate(self, rasty, cachingInfo, material):
    # Activate the material here.
    #
    # The activate method will be called until it returns False.
    # Every time the activate method returns True the mesh will
    # be rendered.
    #
    # rasty is a CObject for passing to material.updateTexture()
    #       and material.activate()
    # cachingInfo is a CObject for passing to material.activate()
    # material is the KX_PolygonMaterial instance this material
    #          was added to

    # default material properties:
    self.pass_no += 1
    if self.pass_no == 0:
      material.activate(rasty, cachingInfo)
      # Return True to do this pass
      return True

    # clean up and return False to finish.
    self.pass_no = -1
    return False

# Create a new Python Material and pass it to the renderer.
mat.setCustomMaterial(PyMaterial())

Previous topic

KX_PolyProxy(SCA_IObject)

Next topic

KX_RadarSensor(KX_NearSensor)