LOD system on game engine

I’m in the process of developing a LOD system in python for Blender Game Engine. But I’m stuck on the alpha of the objects.

Here is the deal:

I need an object in the game, that fades in, and fades out, as the camera gets near and far away from the object. Using alpha of course.

I know this can be done easily with IPO. But I need to be in python code only, because it will get all the objects in the scene. And please, no GLSL. I really need to use Texture Faces or Multitexture.

Someone told me it could be done by using this:

import bpy, bge;

cont = bge.logic.getCurrentController()
obj = cont.owner
mesh = obj.meshes[0]

obj.color = [.5, .5, .5, 0.1]
print(obj.color)


But… it didn’t work in the game engine nor running the script alone.

Any ideas?

This is not really LOD. Level of detail means you reduse the amount of details when increasing the distance to the camera.

But fading away could be part of it. But this has some negative impacts.

Having alpha on the complete object will increase the workload for the renderer quite alot. So it woul be better to do that with the least detailed mesh only.

Here is why:
To get alpha working you have to switch each face of the mesh to alpha (<F9>editing in edit mode). That means it falls out of the z-Buffer processing and goes into face sorting, which is quite heavy if you use that with a lot of faces.

I can’t tell for 2.50 but for 2.49b you can use this code:


alpha = 0.5
mesh = obj.meshes[0]
for mat in range(mesh.getNumMaterials()):
    for v_index in range(mesh.getVertexArrayLength(mat)):
        vertex = mesh.getVertex(mat, v_index)
        vertex.colour = [1.0, 0.0, 0.0, alpha]

Can you make a .blend to demonstrate that? Or a video tutorial? I’m kind of lost…