29

I have some .js files exported from Blender and load them with THREE.JSONLoader();

my callback:

var callback   = function( geometry ) { createMesh(geometry);

my loading:

loader.load( "Models/sculp.js", callback );

my create method:

function createMesh(geometry){

    inArr[id] = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0xbbbbbb} ) );
    inArr[id].scale.set( 100, 100, 100 );
    scene.add( inArr[id] );
    id++;
}

Now I want to change my material on runtime by using my keyboard (changes color and opacity).

How can I do that?

Chad
  • 17,852
  • 4
  • 44
  • 70
Phipps
  • 345
  • 1
  • 3
  • 10

1 Answers1

48

As you create a new material for each mesh I assume you only want to change the color of one mesh and not of all in the inArr array, and you probably need some sort of select for that. But changing the color of the material alone is quite easy:

var onKeyDown = function(event) {
  if (event.keyCode == 67) { // when 'c' is pressed
    object.material.color.setHex(0xff0000); // there is also setHSV and setRGB
  }
};
document.addEventListener('keydown', onKeyDown, false);

object is the mesh you want to change. Key codes can be found here: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

kaipr
  • 626
  • 6
  • 5
  • 1
    thanks a lot. It works fine now I tryed it with cube and sphere some days before and i used: `THREE.SceneUtils.traverseHierarchy( obj, function ( geo ) { geo.material = new THREE.MeshLambertMaterial( { color: 0x900000 } ); } );`but now i only can use `obj.material = new THREE.MeshLambertMaterial( .. );` – Phipps Sep 25 '12 at 07:05