简体   繁体   中英

How to calculate the center position of the .dae file and reposition it?

I'm currently dealing with a lot of 3d models and I find that many of the models are centered off the center of the model. Is there a way to get the length (x), width (z), height (y) of the model, and divide it by two to calculate the confidence position of the model?

let loaderDae = new ColladaLoader();
loaderDae.load(`model.dae`, (dae: any) => {
      this.buildingModel = dae.scene;
      // Is there a way to get the length, width and height of the model and calculate the center point?
      this.buildingModel.position.set(x_length/2, y_length/2, z_length/2);
      this.sceneS.add(this.buildingModel);
});

在此处输入图像描述

Create a bounding box then get the size of the bounding box with getSize()

let bbox = new THREE.Box3().setFromObject(this.buildingModel);
let dimensions = new THREE.Vector3();
dimensions = bbox.getSize(dimensions);
this.buildingModel.position.set(dimensions.x/2, dimensions.y/2, dimensions.z/2);

For the avoidance of doubt, this code should work with all supported 3D model file types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM