Three.js - 在运行时更改材料

时间:2022-06-21 05:12:15

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

我有一些从Blender导出的.js文件并用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?

我怎样才能做到这一点?

1 个解决方案

#1


34  

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:

当您为每个网格创建一个新材质时,我假设您只想更改一个网格的颜色而不是inArr数组中的所有颜色,您可能需要某种选择。但是改变材料的颜色非常容易:

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

object是要更改的网格。密码可以在这里找到:http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

#1


34  

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:

当您为每个网格创建一个新材质时,我假设您只想更改一个网格的颜色而不是inArr数组中的所有颜色,您可能需要某种选择。但是改变材料的颜色非常容易:

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

object是要更改的网格。密码可以在这里找到:http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes