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

查看:72
本文介绍了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天全站免登陆