简体   繁体   English

在 Three.js 中将面数组添加到 BufferGeometry

[英]Add array of faces to BufferGeometry in three.js

Given a BufferGeometry , I can set its vertices from an array of type Float32Array like so:给定一个BufferGeometry ,我可以从一个Float32Array类型的数组中设置它的顶点, Float32Array所示:

geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );

Is there a way to set the BufferGeometry's faces in a similar way using an array of type Int32Array ?有没有办法使用Int32Array类型的数组以类似的方式设置 BufferGeometry 的面? I want to avoid this:我想避免这种情况:

geometry.faces.push(
  new THREE.Face3(0, 3, 2),
  new THREE.Face3(0, 1, 3),
  new THREE.Face3(1, 7, 3),
  new THREE.Face3(1, 5, 7),
  ...
);

Yeah, what you're looking for is BufferGeometry.setIndex() , which allows for vertices to be re-used across multiple triangles, as outlined in the docs .是的,您正在寻找的是BufferGeometry.setIndex() ,它允许在多个三角形中重复使用顶点,如文档中所述 And here's the the official working demo .是官方的工作演示

You can essentially create an array, then push sets of 3 vertex indices (based on the order of your other attributes):您基本上可以创建一个数组,然后推送一组 3 个顶点索引(基于其他属性的顺序):

var indices = [];

indices.push( 0, 3, 2); // face one
indices.push( 0, 1, 3 ); // face two

geometry.setIndex( indices );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM