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

use*_*715 25 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 );
Run Code Online (Sandbox Code Playgroud)

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

if(distance > 20)
{
cube.material.color = 0xffffff;
}
Run Code Online (Sandbox Code Playgroud)

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

Wes*_*ley 64

您没有正确指定颜色值.

cube.material.color.setHex( 0xffffff );
Run Code Online (Sandbox Code Playgroud)

  • 您还可以使用以10为底的整数等效值作为setHex的参数,因为JS中的两个等效值。 (2认同)

k26*_*6dr 8

cube.material.color 
Run Code Online (Sandbox Code Playgroud)

会给你THREE.Color对象:

颜色

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

  • 在链接死亡的情况下,需要在答案中有实际的方法. (2认同)

Har*_*GUL 6

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

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 );
Run Code Online (Sandbox Code Playgroud)

  • 不要实例化一个新的`Color`。使用`cube.material.color.set(color)`。 (7认同)