Change the colors of a cube's faces

折月煮酒 提交于 2019-11-27 06:44:39

问题


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

回答1:


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: THREE.FaceColors } );

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




回答2:


Here is a fiddle for people who end up here and want to see this code working.

I made a box and tied 3 colors to the faces:

// colors
red = new THREE.Color(1, 0, 0);
green = new THREE.Color(0, 1, 0);
blue = new THREE.Color(0, 0, 1);
var colors = [red, green, blue];

for (var i = 0; i < 3; i++) {
    geometry.faces[4 * i].color = colors[i];
    geometry.faces[4 * i + 1].color = colors[i];
    geometry.faces[4 * i + 2].color = colors[i];
    geometry.faces[4 * i + 3].color = colors[i];
}

The face colors are changed in the animate loop.


Also check a related question here with a great answer demonstrating the use of material indices and groups on THREE.BufferGeometry instances.



来源:https://stackoverflow.com/questions/14924187/change-the-colors-of-a-cubes-faces

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