Miger
Miger

Reputation: 1247

ThreeJS GLTFLoader Get Vertices, Edges, Faces And Triangles From GLB Model

For my model viewer I want to display some basic info about the model. Basically I want to get the vertex, edge, face and triangle count of my object or at minimum the vertex count.

I've tried doing it this way so far:

gltf.scene.traverse( function ( child ) {
    if ( child.isMesh ) {
        console.log(child.geometry.vertices)    
    }
})

However this returns undefinded and therefore does not seem to work. The geometry part is of the Type BufferGeometry however I do not seem to find a way to get the information I need.

I've also tried using this:

console.log(child.geometry.attributes.position.count)   

Which does infact return some number.. however I have no idea if this is the vertex count or something else entirely.

How can I get the vertex count of my model and ideally also edges, faces and triangles?

Upvotes: 2

Views: 2700

Answers (1)

Mugen87
Mugen87

Reputation: 31026

Which does infact return some number.. however I have no idea if this is the vertex count or something else entirely.

It is indeed the number of vertices.

If you want to compute the number of faces (which is just a synonym for triangles) it depends on whether the geometry is indexed or not. If it has an index, you can do this:

const faceCount = child.geometry.index.count / 3;

Otherwise it is:

const faceCount = child.geometry.attributes.position.count / 3;

You have to compute the number of edges by yourself, too. Normally it is just the face count times three.

Upvotes: 3

Related Questions