Size of a .obj mesh

Hi everybody,

I want to import obj meshes in my scene which are downloaded from internet platforms such as TurboSquid or Sketchfab.

When i Import these meshes into my scene, i never know the initial scaling and i’m not able to resize them in an appropriate scaling without using gizmos.

Do you know how to do that ?

Thanks,

Boris

Size units in Babylon are relative; one never knows initial Sketchfab model scaling, which may differ a lot from your specific application needs.
I believe that for scaling meshes you may try to use something like (change ‘2’ to desired scale for all 3 axis)
mesh.scaling.scaleInPlace(2);
or
scene.getMeshByName("Mesh_Name").scaling.scaleInPlace(2);
and use it inside the callback function for mesh importing.
Example - line 152, https://www.babylonjs-playground.com/#E8C51D#36

Ok, may be i was not clear enough.

I know the scaling functions but i don’t know how to find the initial size of the object to resize it automatically after the import

You may try
mesh.getBoundingInfo().maximum

but the thing with Sketchfab and other similar models is that one never knows where the transform origin will be situated and is there any need to apply proper rotation.

I use code like this:

public get boundingInfo(): Nullable<BoundingInfo> {
    if (!this.rootMesh) {
      return null;
    }

    // meshes are already parented to root mesh, so we do not need to look further.
    let min: Nullable<Vector3> = null;
    let max: Nullable<Vector3> = null;

    this.rootMesh.getChildMeshes().forEach(childMesh => {
      const { minimumWorld, maximumWorld } = childMesh.getBoundingInfo().boundingBox;
      if (min === null) {
        min = minimumWorld;
      } else {
        min = Vector3.Minimize(min, minimumWorld);
      }

      if (max === null) {
        max = maximumWorld;
      } else {
        max = Vector3.Maximize(max, maximumWorld);
      }
    })

    if (min !== null && max != null) {
      return new BoundingInfo(min, max);
    }

    return null;
  }

  public scaleTo(maxDimension: number): void {
    const boundingInfo = this.boundingInfo; // will be null when no meshes are loaded
    if (boundingInfo && this.rootMesh) {
      const longestDimension = Math.max(
        Math.abs(boundingInfo.minimum.x - boundingInfo.maximum.x),
        Math.abs(boundingInfo.minimum.y - boundingInfo.maximum.y),
        Math.abs(boundingInfo.minimum.z - boundingInfo.maximum.z)
      );

      const dimension = maxDimension / longestDimension;
      this.rootMesh.scaling.scaleInPlace(dimension);
      this._scaledToDimension = maxDimension;
    }
  }

For a .obj I create a root mesh, which is just a transformNode or empty mesh and then parent the ones that aren’t parented. Something like this:

let rootMesh = new AbstractMesh(sceneFilename + "-root-model", scene);

modelMeshes.forEach(mesh => {
  // leave meshes already parented to maintain model hierarchy:
  if (!mesh.parent) {
    mesh.parent = loadedModel.rootMesh!;
  }
})

So, what that code does is scale the mesh to, for example, 2 at the most and maintain aspect ratios.

3 Likes

NormalizeToUnitCube is super useful too: Mesh | Babylon.js Documentation (babylonjs.com)

4 Likes

Hi @bvaisman did the suggestions on the thread help? :smiley:

1 Like

Oh sorry, you’re right, i didn’t mention i had found the solution.

Happy new year to all the forum !!!

3 Likes