Three.js - 在运行时更改材料 [英] Three.js - Change Material on Runtime

查看:398
本文介绍了Three.js - 在运行时更改材料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些从Blender导出的.js文件,并加载 THREE.JSONLoader();

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

我的回调:

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

我的装货:

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

我的创建方法:

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).

我该怎么做?

推荐答案

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

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 是您要更改的网格。密码可在此处找到: http://www.cambiaresearch。 com / articles / 15 / javascript-char-codes-key-codes

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

这篇关于Three.js - 在运行时更改材料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆