简体   繁体   中英

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:

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 ? 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 . 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):

var indices = [];

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

geometry.setIndex( indices );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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