2012-09-18 108 views
7

有沒有辦法改變現有網格物體的materialIndex?這在How to Update Things文檔中沒有討論,所以我不確定是否可能。我爲此使用了WebGLRenderer上下文。three.js更新幾何體材質索引

這基本上就是我想要做的事:

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

回答

13

此行爲已改變。

如果您正在使用THREE.Geometry,您可以使用下面的模式來改變面材指數:

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

如果幾何先前已渲染的,你還必須設置

mesh.geometry.groupsNeedUpdate = true; 

three.js r.88

+0

啊,這是有道理的。感謝您解釋行爲背後的原因。 – JDS