Materials edited in game?

Can materials be edited in game? Like say a flat material becomes glossy?

Hello! It’s quite easy, you must get the KX_BlenderMaterial, which have the properties of an object’s material.
See the attached blend file for a working example. The code used in the blend is the following:

import bge

def main(cont):
    
    """ Set the object's game properties to see the realtime changing of material attributes. """
    
    # Objects
    own = cont.owner
    
    # Sensors
    always = cont.sensors[0]
    
    # Attributes
    mesh = own.meshes[0] # First mesh of the list (usually the current)
    material = mesh.materials[0] # First material index (change if you want)
    
    # Colors
    red = [1.0, 0.0, 0.0]
    green = [0.0, 1.0, 0.0]
    blue = [0.0, 0.0, 1.0]
    white = [1.0, 1.0, 1.0]
    black = [0.0, 0.0, 0.0]
    
    if always.positive:
        
        # Set properties to material at runtime
        material.alpha = own['alpha']
        material.hardness = own['hardness']
        material.emit = own['emit']
        material.diffuseColor = blue # Change to other color
        material.specularColor = red # Change to other color
        material.diffuseIntensity = own['diffuseIntensity']
        material.specularIntensity = own['specularIntensity']

I hope it helps you. :wink:

NOTE: I wasn’t able to change the material specularIntensity and emit in my Blender 2.78. It’s supposed to work the same as the other attributes, so I don’t know if it is a bug or what.

Attachments

setMaterialProps.blend (116 KB)

Cool, I’ll play around with it. Never thought about it before til I was playing a game and notice materials going from flat to glossy.

Setting smooth mode for a mesh is trickier.

I think you must average the normals across faces.