three.js更新几何体面的材质索引

9
有没有一种方法可以更改现有网格中某个面的materialIndex?在如何更新内容文档中没有讨论这个问题,所以我不确定是否可行。我正在使用WebGLRenderer上下文进行操作。
基本上我想做的就是这样:
// Make a mesh with two materials
var texture = THREE.ImageUtils.loadTexture(IMAGE_URL);
var geometry = new THREE.Geometry();
geometry.materials.push(new THREE.MeshBasicMaterial({ wireframe: true, vertexColors: THREE.FaceColors}));
geometry.materials.push(new THREE.MeshBasicMaterial({ map: texture, overdraw: true}));

whatever.forEach(function(w, i) { 
  geometry.vertices.push(makeVerts(w));

  var materialIndex = 0;
  var color = new THREE.Color(i);
  geometry.faces.push(new THREE.Face4(i*4+0, i*4+1, i*4+2, i*4+3, 
                    null, color, materialIndex));
});

var mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial());
scene.add(mesh)


// Later on, change some faces to a different material

geometry.faces.filter(someFilter).forEach(function(face) {
  face.color = someOtherColor;
  face.materialIndex = 1;
}

geometry.colorsNeedUpdate = true; // This is how to update the color, and it works.
//geometry.materialsNeedUpdate = true; // This isn't a thing, is it?
1个回答

18

本答案仅适用于three.js版本在r.125之前的情况。

这种行为已经发生了改变。

如果您使用的是THREE.Geometry,您可以使用以下模式来更改面材质索引:

mesh.geometry.faces[ 0 ].materialIndex = 1;

如果该几何图形之前已经被渲染过,那么您还必须设置

mesh.geometry.groupsNeedUpdate = true;

three.js r.124


啊,那就有道理了。谢谢你解释行为背后的原因。 - JDS
1
materialIndex的作用是什么?只是好奇。 - B''H Bi'ezras -- Boruch Hashem

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接