I'm trying to change the color of a cube based on a variable. I created two cubes and I want to change their color depending on the distance between them.
The cubes are created like this:
geometry = new THREE.CubeGeometry( 50, 50, 50 ); material = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } ); cube = new THREE.Mesh( geometry, material ); scene.add( cube );
Now I tried something like this:
if(distance > 20) { cube.material.color = 0xffffff; }
But it does not work. I looked in the examples but couldn't find anything appropriate.
You can use Material. color property in the objects' materials (it's available for all materials, not only MeshBasicMaterial.) var color = new THREE.
A Three. js Mesh is a base class that inherits from Object3d and is used to instantiate polygonal objects by combining a Geometry with a Material. Mesh is also the base class for the more advanced MorphAnimMesh and SkinnedMesh classes.
You are not specifying the color value correctly.
cube.material.color.setHex( 0xffffff );
cube.material.color
will give you the THREE.Color object:
Color
which has a bunch of methods you can use to set the color.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With