在three.js中更改立方体的颜色

时间:2013-01-06 11:28:46

标签: colors three.js

我正在尝试根据变量更改多维数据集的颜色。我创建了两个立方体,我想根据它们之间的距离改变颜色。

立方体的创建如下:

geometry = new THREE.CubeGeometry( 50, 50, 50 );
material = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );
cube = new THREE.Mesh( geometry, material );
scene.add( cube );

现在我尝试过这样的事情:

if(distance > 20)
{
cube.material.color = 0xffffff;
}

但它不起作用。我查看了示例,但找不到合适的内容。

3 个答案:

答案 0 :(得分:62)

您没有正确指定颜色值。

cube.material.color.setHex( 0xffffff );

答案 1 :(得分:8)

cube.material.color 

将为您提供THREE.Color对象:

Color

有一堆方法可以用来设置颜色。

答案 2 :(得分:3)

我的建议是将一个函数附加到您的对象,然后您可以轻松地在运行时更改对象的颜色。
根据您的代码

geometry = new THREE.CubeGeometry( 50, 50, 50 );
material = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );
cube = new THREE.Mesh( geometry, material );

//here is the funcion defined and attached to the  object
cube.setColor = function(color){
     cube.material.color.set(color);
}


cube.setColor(0xFFFFFF)  //change color using hex value or
cube.setColor("blue")    //set material color by using color name

scene.add( cube );