我想通过将几何定义为一组顶点和面来创建自己的网格(三个.js,使用 CanvasRenderer):
geometry = new THREE.Geometry();
...
geometry.vertices.push(new THREE.Vector3(0, 0, 0));
geometry.vertices.push(new THREE.Vector3(1, 0, 0));
geometry.vertices.push(new THREE.Vector3(2, 0, 0));
...
...
geometry.faces.push(new THREE.Face3(1, 4, 3));
geometry.faces.push(new THREE.Face3(1, 2, 4));
geometry.faces.push(new THREE.Face3(3, 4, 5));
...
geometry.computeFaceNormals();
因为我希望我的网格具有不同颜色的面,所以我生成了一组材质。正如我在一些教程中所读到的,我将此数组称为geometry.materials:
geometry.materials = [
new THREE.MeshBasicMaterial({ color: 0xFF00A0 }),
new THREE.MeshBasicMaterial({ color: 0x00FF00 }),
new THREE.MeshBasicMaterial({ color: 0x0000FF }),
...
];
然后,我将索引分配给 geometry.faces[i].materialIndex:
geometry.faces[0].materialIndex = 0;
geometry.faces[1].materialIndex = 2;
geometry.faces[2].materialIndex = 1;
...
最后,我生成网格并将其添加到场景中:
mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial());
scene.add(mesh);
好吧,这段代码不起作用。我得到的只是一个不可见的网格(看不到任何面孔)。我的问题是:
- 'geometry.materials' 是定义材质数组的默认变量名吗?faces[i].materialIndex 中的值指的是哪个数组?
- 为什么我的代码不起作用?