Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

webgl - three.js updating geometry face materialindex

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?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This answer only applies to versions of three.js prior to r.125.

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.124


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...