Nicolas S.Xu
Nicolas S.Xu

Reputation: 14524

How to understand setIndex and index in BufferGeometry in ThreeJS?

I've read the document here, and from previous question, I can see

The setIndex function is used to specify triangle indices that reference the vertex attribute buffers on the BufferGeometry.

I think I understand 50% of these concepts, but in this interleaved example, (code is here) What is the purpose of setting index (I know it is specifying triangle indices)? but why?

var indices = new Uint16Array( [
            0, 1, 2,
            2, 1, 3,
            4, 5, 6,
            6, 5, 7,
            8, 9, 10,
            10, 9, 11,
            12, 13, 14,
            14, 13, 15,
            16, 17, 18,
            18, 17, 19,
            20, 21, 22,
            22, 21, 23
        ] );

geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );

My understanding is there are 24 vertexes, and the set index tells the renderer to use vertex at a specific index (not natural order) to arrange a triangle. But why is a new arrangement needed? Do I have to do setIndex every time in my own code?

Upvotes: 1

Views: 6502

Answers (1)

Mugen87
Mugen87

Reputation: 31026

Using an indexed geometry is not required. If you are not using indices, the triangles are defined by the order of vertices in the buffer. In this case, you talk about non-indexed geometries. Indices are especially useful if many vertices are shared between triangles/faces. Using an index buffer allows you to save some memory then.

The official documentation page about BufferGeometry provides more information about both geometry types.

Upvotes: 3

Related Questions