【问题标题】:three.js updating geometry face materialindex三.js更新几何面materialindex
【发布时间】:2012-09-10 05:43:48
【问题描述】:

有没有办法改变现有网格面上的材质索引?这在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?

【问题讨论】:

    标签: three.js webgl


    【解决方案1】:

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

    这种行为已经改变。

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

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

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

    mesh.geometry.groupsNeedUpdate = true;
    

    three.js r.124

    【讨论】:

    • 啊,有道理。感谢您解释行为背后的原因。
    • materialIndex 的用途是什么?只是好奇
    猜你喜欢
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 2018-06-19
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多