简体   繁体   English

Three.js 在一个网格上使用多种材料

[英]Three.js use multiple materials on one mesh

I am trying to understand how to add multiple materials to a mesh.我正在尝试了解如何将多种材质添加到网格中。 I want to take the existing material and apply another slightly transparent material to my existing mesh.我想采用现有材料并将另一种稍微透明的材料应用于现有网格。 Right now, passing it as an array it just disappears.现在,将它作为一个数组传递它就消失了。 I know I am missing something, just not sure what that is.我知道我错过了一些东西,只是不确定那是什么。 The end goal is so I can animate the opacity of the new material in/out over the existing one.最终目标是使新材料的不透明度在现有材料上输入/输出。

Original Material原始材料

const originalMaterial = child.material.clone();

New Material新材料

const newMaterial = new THREE.MeshStandardMaterial({
 name: 'New Mat',
 map: newTexture,
 emissiveMap: newTextureMap,
 side: THREE.DoubleSide,
 opacity: .5,
 transparent: true
});

Combining them结合它们

child.material = [originalMaterial, newMaterial]
child.material.needsUpdate = true

WebGL doesn't allow for multiple materials on a single mesh. WebGL 不允许在单个网格上使用多种材质。 That's why the THREE.Mesh constructor only allows one geometry, and one material .这就是为什么THREE.Mesh构造函数只允许一种几何体和一种材料

To do what you want, you could create two meshes, with one material's transparency set to 0.5.为了做你想做的事,你可以创建两个网格,其中一种材质的透明度设置为 0.5。 But more frequently you would just use a single mesh, and assign variations in opacity through the .alphaMap texture .但更常见的是,您只使用一个网格,并通过.alphaMap纹理分配不透明度的变化。 This would give you more flexibility because you can have many more transparency values on a single object, without having to create new materials for each:这将为您提供更大的灵活性,因为您可以在单个 object 上拥有更多透明度值,而无需为每个创建新材料:

var textureLoader = new THREE.TextureLoader();
var alphaTexture = textureLoader.load("path/to/alpha.png");
mesh.material.alphaMap = alphaTexture;

mesh.material.transparent = true; // <- don't forget this!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM