somdats
somdats

Reputation: 11

cannot change material color using three.js

I have a obj file which contains vertex groups along with the main object. I am trying to assign color to each of the groups tagged through vertex group. What I could find is that in three.js each vertex group is of type Object3D and there is no function like setColor, instead each such children has two children of the type THREE.Mesh and it has setColor function accessed through a material property. I am not clear about this hierarchy. How do I set color to each vertex group.

One more thing earlier version of three.js had different hierarchy of object children relationship. please provide me some input. I am profoundly looking for a solution

Upvotes: 0

Views: 1414

Answers (1)

Wilt
Wilt

Reputation: 44326

There are several similar questions on stackoverflow that can help you like here and here.

To be able set colors to vertexes you need to walk the object tree till you find the faces of the mesh geometry and there you can assign a vertexColors array:

var numberOfSides, j, vertexIndex;

for ( var i = 0; i < geometry.faces.length; i++ ) 
{
    face = geometry.faces[ i ];   
    face.color = baseColor;      
    numberOfSides = ( face instanceof THREE.Face3 ) ? 3 : 4;
    for( j = 0; j < numberOfSides; j++ ) 
    {
        vertexIndex = face[ faceIndices[ j ] ];
        face.vertexColors[ j ] = geometry.colors[ vertexIndex ];
    }
}

For more details check the linked questions.

Upvotes: 2

Related Questions