Why is the triangle I counted different from the triangle counted by the inspector?

The below is the triangle statistics code implemented by myself.

private getActiveTriangles() {
    if (this.scene) {
      let totalIndices = 0;
      const { data, length } = this.scene.getActiveMeshes();
      const meshes = data;

      for (let i = 0; i < length; i++) {
        const mesh = meshes[i];
        if (mesh?.getTotalIndices) {
          totalIndices += mesh.getTotalIndices();
        }
      }

      return totalIndices / 3;
    }

    return 0;
  }

The below is the triangle statistics code used by inspector.

const triangles = scene.getActiveIndices() / 3

Why are their results different?

They can be different if your meshes are drawn multiple times. For eg, if shadows are enabled, the meshes drawn in the shadow map are also taken into account in the stats. Same thing for the effect layers.

Is there a scene.getTotalIndices() implementation method similar to scene.getActiveIndices()?
Is the way I implemented it myself correct?

Actually, a scene is rendered at the submesh level: if a mesh is made of several submeshes and some of these submeshes are not visible by the camera, they won’t be rendered. So, using mesh.getTotalIndices() is not valid in this regard.

The mesh class is doing this to count the indices when a submesh is displayed:

this.getScene()._activeIndices.addCount(subMesh.indexCount * instancesCount, false);

If the mesh is not instanciated / we are not dealing with thin instances, instancesCount = 1.

If you want to count yourself, it’s a bit involved because you will have to loop over the rendering groups of the rendering manager, and loop over the opaque / transparent / alpha test / … submesh lists, which is not even possible because those lists are not publicly available…

1 Like