How to get the global Mesh center position of an MeshInstance?

Is it possible to calculate the global center position of a MeshInstance? I have a model mit multiple meshes. Now I want to place something above a single mesh of the model, but I’m not able to determine the correct global center position of the single mesh. I tried “meshInstances[index].mesh.aabb.center” and “meshInstances[index].node.getPosition()”, but this does not give me the correct position.
“aabb.center” are way off the correct global position and always returns the same position, also if I move the model. So I think I can not use “aabb.center”.
“node.getPosition()” seems to return the position of the model and not the position of the single mesh. It also returns the same position for all meshes of the model.
I also tried to iterate through the vertex data of the mesh and calculate the center, but it gives me always zero position and also does not change if I move the model.

Does anyone know how to get the global center position of a single mesh?

Hi @Alexfedel1,

Yes that’s possible, one way of doing it is by adding all the individual aabb of each mesh instance to find the sum center.

The model viewer template in its orbit-camera.js script has an example of doing it (this method runs recursively):

OrbitCamera.prototype._buildAabb = function (entity, modelsAdded) {
    var i = 0;

    if (entity.model) {
        var mi = entity.model.meshInstances;
        for (i = 0; i < mi.length; i++) {
            if (modelsAdded === 0) {
                this._modelsAabb.copy(mi[i].aabb);
            } else {
                this._modelsAabb.add(mi[i].aabb);
            }

            modelsAdded += 1;
        }
    }

    for (i = 0; i < entity.children.length; ++i) {
        modelsAdded += this._buildAabb(entity.children[i], modelsAdded);
    }

    return modelsAdded;
};

I’m not sure if this is what I want. The functions seems to calculate the center of an entity not a single MeshInstance?
This is how I try it and it returns zero position:

Tools.prototype.getMeshInstanceCenter = function(myEntity, meshInstanceIndex) {
                
    if( myEntity.model ){
        
        for (var meshInstances = myEntity.model.meshInstances, index = 0; index < meshInstances.length; index++) {

            if( index === meshInstanceIndex ){         
                
                // I want the center of meshInstances[index]
                
                this._modelsAabb = new pc.BoundingBox();
                this._buildAabb(meshInstances[index].node, 0);
                return this._modelsAabb.center;
            }         
        }
    }
    return myEntity.getPosition();
};

Not sure then what’s stopping you of using the mesh instance aabb center? That’s in world units and will give you the center of volume of that specific mesh instance.

image

var center = meshInstance.aabb.center;
1 Like

Well, I’m stupid. I was using “meshInstances[index].mesh.aabb.center” instead of “meshInstances[index].aabb.center”.
Thank you very much.

1 Like