javascriptcolorsthree.jsmeshface

Change the colors of a cube's faces


I actually found this question, but it says material.color doesn't exist. I need to know how to change the colors of the various faces of a cube I'm drawing:

var newCube = new THREE.Mesh(new three.CubeGeometry(size, size, size), new three.MeshNormalMaterial({ vertexColors: three.FaceColors }));

Solution

  • This answer only applies to versions of three.js prior to r.125.

    Here is how you set and change the colors of a cube's faces:

    var geometry = new THREE.BoxGeometry( size, size, size );
    for ( var i = 0; i < geometry.faces.length; i ++ ) {
        geometry.faces[ i ].color.setHex( Math.random() * 0xffffff );
    }
    
    var material = new THREE.MeshBasicMaterial( { color: 0xffffff, vertexColors: true } );
    

    If geometry.faces[i].color is changed after the geometry has been rendered, you must set geometry.colorsNeedUpdate = true.

    three.js r.124