Change the colors of a cube's faces

David Gomes picture David Gomes · Feb 17, 2013 · Viewed 14k times · Source

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 }));

Answer

WestLangley picture WestLangley · Feb 17, 2013

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. ( This is not required for canvasRenderer. )

three.js r.84