cannot change material color using three.js

只谈情不闲聊 提交于 2020-04-18 06:21:35

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/28556048/cannot-change-material-color-using-three-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!