Change color dynamically on model

I load a body model (3DS) like the following, anyway to change color on the model's arm and leg's color similar to a heat map.



when the application receive sensor data the arm / leg of the model will change color dynamically depends on data received from heat sensor.



color will change from dark red -> light red -> light green -> drak green ( gradient )



http://www.oniva.com/upload/1356/TestMaxJmeWrite.java


You can modify the vertex color data on the model, using TriMesh.getColorBuffer and TriMesh.getVertexBuffer to get the vertex locations. Then you just apply some function to the coordinates of the vertex and store the result ColorRGBA in the color buffer.

any source code example or psedo code on  how to do it?

After I load the model and get the Node object, how to get the TriMesh.getColorBuffer and TriMesh.getVertexBuffer?



MaxToJme C1 = new MaxToJme();

ByteArrayOutputStream BO = new ByteArrayOutputStream();

C1.convert(new BufferedInputStream(modelToLoad.openStream()), BO);

Node r1 = (Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));

Do (TriMesh) r1.getChild(0). If that doesn't work, try  (TriMesh) r1.getChild(0).getChild(0). Somewhere inside your imported scene sits your TriMesh, you just have to find it…

If you are using lighting on the model, would the colour on the vertex be seen anyway ?

Go through the scene graph, when you hit a TriMesh, take the buffers and start looping through each vertex, convert the vertex to world coordinates using Spatial.localToWorld, apply your function to get the color and store it in the color buffer.

This can be done easily with recursion:


void colorFunction(Vector3f pos, ColorRGBA store);

void updateColors(Spatial s){
    if (s instanceof Node){
        Node n = (Node) s;
        for (Spatial child: n.getChildren()) updateColors(child);
    }else if (s instanceof TriMesh){
        TriMesh mesh = (TriMesh) s;
        FloatBuffer vb = mesh.getVertexBuffer(0);
        FloatBuffer cb = mesh.getColorBuffer(0);
        vertex.rewind();
        color.rewind();
        Vector3f vpos = new Vector3f();
        ColorRGBA vcolor = new ColorRGBA();
        for (int vindex = 0; vindex < vb.limit(); vindex+:3){
              vpos.set(vb.get(), vb.get(), vb.get());
              mesh.localToWorld(vpos, vpos);
              colorFunction(vpos, vcolor);
              cb.putInt(vcolor.asIntRGBA());
        }
    }
}