3

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),
  ...
);
gman
  • 100,619
  • 31
  • 269
  • 393
Fede G
  • 197
  • 10
  • 1
    You might find [this article](https://threejsfundamentals.org/threejs/lessons/threejs-custom-buffergeometry.html) helpful – gman Sep 30 '20 at 03:05

1 Answers1

4

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 );
M -
  • 26,908
  • 11
  • 49
  • 81