Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing color of cube in three.js

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.

like image 458
user1952715 Avatar asked Jan 06 '13 11:01

user1952715


People also ask

How do I change the color of an object in 3 JS?

You can use Material. color property in the objects' materials (it's available for all materials, not only MeshBasicMaterial.) var color = new THREE.

What is a mesh in three Js?

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.


2 Answers

You are not specifying the color value correctly.

cube.material.color.setHex( 0xffffff ); 
like image 100
WestLangley Avatar answered Sep 25 '22 09:09

WestLangley


cube.material.color  

will give you the THREE.Color object:

Color

which has a bunch of methods you can use to set the color.

like image 37
k26dr Avatar answered Sep 26 '22 09:09

k26dr