How get faces( triangle ) data from buffergeometry

Hey guys,
How can I get faces data ( a,b,c vectors ) to generate Triangle from buffergeometry loaded via GLTF ? I tried with Index data but just got wrong results :confused:

Buffer geometries can be indexed or non-indexed. In the first case, you get the triangle indices (a,b,c) like so:

const index = geometry.getIndex();

for ( let i = 0; i < index.count; i += 3 ) {

    const a = index.getX( i );
    const b = index.getX( i + 1 );
    const c = index.getX( i + 2 );

}

If the geometry is non-indexed, use the position attribute which holds the actual vertices.

const position = geometry.getAttribute( 'position' );

for ( let i = 0; i < position.count; i += 3 ) {

    const a = i;
    const b = i + 1;
    const c = i + 2;

}

The face indices can now be used to retrieve the actual vertex data from the buffer attributes.

8 Likes

hmm, Thank you, but as I read in the Doc :

.getX ( index : Integer ) : Number

Returns the x component of the vector at the given index.

.getY ( index : Integer ) : Number

Returns the y component of the vector at the given index.

.getZ ( index : Integer ) : Number

Returns the z component of the vector at the given index.

so β€œa” is a Number of x component or no, it’s the β€œa” Vector of a face/triangle ?
const a = index.getX( i );

The methods are just intended to make the access to buffer data easier so you don’t have to worry about item size or interleaved data.

The Number definition just means the type of the return value is a Number.