Changing color of cube in three.js

前端 未结 3 801
南笙
南笙 2020-12-30 19:59

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

相关标签:
3条回答
  • 2020-12-30 20:09
    cube.material.color 
    

    will give you the THREE.Color object:

    Color

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

    0 讨论(0)
  • 2020-12-30 20:19

    You are not specifying the color value correctly.

    cube.material.color.setHex( 0xffffff );
    
    0 讨论(0)
  • 2020-12-30 20:33

    My suggestion is attach a function to your object and then you can change the color of object during runtime easily.
    Based on your code

    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 );
    
    0 讨论(0)
提交回复
热议问题