three.js updating geometry face materialindex

怎甘沉沦 提交于 2019-11-27 06:47:05

问题


Is there a way to change the materialIndex on a face of an existing Mesh? This isn't discussed in the How to Update Things documentation, so I am not sure if it's possible. I'm using a WebGLRenderer context for this.

This is basically what I'm trying to do:

// 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:


This behavior has changed.

If you are using THREE.Geometry, you can use the following pattern to change a face material index:

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

If the geometry has been previously-rendered, you must also set

mesh.geometry.groupsNeedUpdate = true;

three.js r.88



来源:https://stackoverflow.com/questions/12468906/three-js-updating-geometry-face-materialindex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!