9

有没有办法改变现有网格面上的材质索引?How to Update Things文档中没有对此进行讨论,因此我不确定是否可行。我为此使用了 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?
4

1 回答 1

17

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

这种行为已经改变。

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

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

如果几何图形已预先渲染,您还必须设置

mesh.geometry.groupsNeedUpdate = true;

三.js r.124

于 2012-09-18T01:24:14.717 回答