我正在尝试根据变量更改多维数据集的颜色.我创建了两个立方体,我想根据它们之间的距离改变颜色.
立方体的创建方式如下:
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)
cube.material.color
Run Code Online (Sandbox Code Playgroud)
会给你THREE.Color对象:
它有一堆方法可以用来设置颜色.
我的建议是为您的对象附加一个函数,然后您可以在运行时轻松更改对象的颜色。
根据您的代码
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)