更新Threejs BufferGeometry的索引(添加面)



添加顶点到BufferGeometry和更新位置属性工作良好。但我开始感到绝望,我如何能做同样的索引(添加面孔)。有什么办法可以做到这一点吗?

const vertices = new Float32Array(100 * 3);
const indices = new Array(100 * 3);

这些是所使用的数组。

更新:这似乎可以奏效:

const indices = new Uint16Array(100 * 3);
geometry.setIndex(new THREE.BufferAttribute(indices, 1));

不知道具体原因,但它确实有效。

geometry.index.needsUpdate = true似乎没有效果

更新属性时,通常使用geometry.setAttribute()。然而,当更新索引时,您需要使用geometry.setIndex(),因为它本身并不完全是一个属性。你可以在文档中阅读到这个方法。

关于如何使用setIndex的简单演示,您可以查看这个实时示例及其源代码。实际上,您只需传递一个数组,引擎就会在幕后为您将其转换为BufferAttribute:

const indices = [0, 1, 2, 2, 3, 0];
geometry.setIndex(indices);