Check triangles vertices and size of a model?

Hi, what’s the most accurate way to check triangles vertices and size of a model (based on x,z,y axis ocupied distance)?

If we load a model like this

model entity

var LoadGlbExternalContainer = pc.createScript('loadGlbExternalContainer');

// initialize code called once per entity
LoadGlbExternalContainer.prototype.initialize = function() {
    var app = this.app;
    var self = this;

    app.assets.loadFromUrlAndFilename('https://threejs.org/examples/models/gltf/Soldier.glb', null, "container", function (err, asset) {
        // Add the loaded scene to the hierarchy
        self.entity.addComponent('model', {
            asset: asset.resource.model
        });

               self.entity.addComponent('collision', {
        type: 'mesh',
         asset: asset.resource.model
    });
    
   self.entity.addComponent('rigidbody', {
        type: pc.BODYTYPE_KINEMATIC
    });
         
        if (asset.resource.animations.length > 0) {
            self.entity.addComponent('animation', {
                assets: asset.resource.animations    
            });
        }
    });

I want to prevent model from loading if it doesn’t match these criterias let’s say model takes 11,9,8 of the world space and the limit is 10,10,10 so since 1 axis is more than limit it gets rejected

Thanks

Hi @Newbie_Coder,

You can get the size of the model by adding the bounding boxes of all mesh instances. That will give you the total size of the model in world units. There is an example method that does that in the orbit camera script, to find the right distance to better frame the model in view:

To get the number of vertices of the model you can loop through the mesh instances and use the Mesh API on the mesh of each, to get the number of unique positions (vertices) included:

https://developer.playcanvas.com/api/pc.Mesh.html#getPositions

1 Like

Bear in mind that you still can’t do this until you load the model.

If you want to prevent the model from loading in the first place, you need to have this meta data somewhere else (eg a JSON file) that you can use to do checks before attempting to load the model.

1 Like

Okay, found a simplier way by @Leonidas
Pretty accurate I guess

 entity.aabb = new pc.BoundingBox();

        var meshes = entity.model.model.meshInstances;
if (meshes.length) {
    entity.aabb.copy(meshes[0].aabb);
    for(var i = 1; i < meshes.length; i++)
        entity.aabb.add(meshes[i].aabb);
        console.log(meshes);
        x = entity.aabb.halfExtents.x * 2;
        console.log(x);
        y = entity.aabb.halfExtents.y * 2;
        console.log(y);
        z = entity.aabb.halfExtents.z * 2;
        console.log(z);        
          var meshInstances = entity.model.meshInstances;
          var mat;
          for(var i = 0; i < meshInstances.length; i++) {
          mat = meshInstances[i].mesh.vertexBuffer.numVertices;
          console.log(mat);

The last issue is output

console.log(mat) does loop trough instances but logs it in seperate lines, tried to sum up with .sum or .add -no luck, how to sum it up? Thanks (NON SOLVED)

And to get triangles, its vertices /3?

mesh.indexBuffer.numIndices / 3

1 Like

Why would it log 0?

var sum = 0;
          for(var i = 0; i < meshInstances.length; i++) {
     //  mat = meshInstances[i].mesh.vertexBuffer.numVertices;
         sum + meshInstances[i].mesh.indexBuffer[0].numIndices / 3;
          console.log(sum);

Isn’t there a smart way to sum up multiple responses?

Fiksavimas

Isn’t that meant to be

sum += meshInstances[i].mesh.indexBuffer[0].numIndices / 3;
Then it outputs 9 NaN

Debug to see why it would give you a NaN value on the second loop. Chances are for some reason meshInstances[i].mesh.indexBuffer[0].numIndices is not giving a number. It could be giving null which is odd

It is seen as a number, even adding parseFloat getting the same result
Maybe my code is wrong?

If you can share a quick public repro project, that can help people to look into a bit more?

https://playcanvas.com/editor/project/924019

I don’t see any NaNs?

I’ve edited it, still doesn’t return just one line with final count of

Okay, this did it

var meshInstances = entity.model.meshInstances;
var mat;
var total = 0;
for (var i = 0; i < meshInstances.length; i++) {
    //  mat = meshInstances[i].mesh.vertexBuffer.numVertices;
    mat = parseFloat(meshInstances[i].mesh.indexBuffer[0].numIndices / 3);
    total += mat;
}
console.log(total);

We can mark this as solved, thanks for help everyone
See you soon in the next topics

Can a model have more than 1 indexBuffer?

Each mesh instance has just a single index buffer, at index 0.
Internally there could be one on the index 1, for wireframe only rendering. So that is not normally created / used.

2 Likes