Polygon count function

Is there a function in PlayCanvas to count the number of polygons/vertices on a meshInstance?

Best regards,
Leonidas

Good question! Alas, no. But how’s this:

function getPolyCount(meshInstance) {
    var mesh = meshInstance.mesh;
    return mesh.indexBuffer ? 
        mesh.indexBuffer[pc.RENDERSTYLE_SOLID].getNumIndices() / 3 :
        mesh.vertexBuffer.getNumVertices() / 3;
}

function getVertexCount(meshInstance) {
    var mesh = meshInstance.mesh;
    return mesh.vertexBuffer.getNumVertices();
}

getPolyCount assumes that the mesh’s primitive type is pc.PRIMITIVE_TRIANGLES, but this is true 99% of the time.

Thank you Will for your prompt reply. That will do just fine.

Best regards,
Leonidas

Hmmm. Actually, just thinking about it, it’s not that simple. Multiple mesh instances can refer to the same vertex and index buffers. It might be better to do:

function getPolyCount(meshInstance) {
    return meshInstance.mesh.primitive.count / 3;
}

function getVertexCount(meshInstance) {
    return meshInstance.mesh.primitive.count;
}

I think this is a bit more accurate, but it doesn’t account for vertex sharing when the mesh is indexed.

Ah, yes, vertex sharing in indexed meshes won’t be accounted for.

Thank you for the correction.