Intelligent centering and scaling after importing the model in three.js

Is there a way to determine the size and position of the model, and then the auto center and scale the model so that it is located at the beginning and at the point of view of the camera? I find that when I import a Collada model from Sketchup, if the model was not centered at the origin in Sketchup, then it is not centered on three .js. Although this makes sense, it would be nice to automatically autocenter your original position after import.

I saw some discussion in different file downloaders about getting the boundaries of the imported model, but I could not find any links to this.

The scaling issue is less important, but I feel it is related to the border function, so I asked it too.

EDIT:

Additional information after the game a little and a few more Google searches ...

The code for my callback function when loading the collada file now looks like this:

loader.load(mURL, function colladaReady( collada ) { dae = collada.scene; skin = collada.skins[ 0 ]; dae.scale.x = dae.scale.y = dae.scale.z = 1; dae.updateMatrix(); //set arbitrary min and max for comparison var minX = 100000; var minY = 100000; var minZ = 100000; var maxX = 0; var maxY = 0; var maxZ = 0; var geometries = collada.dae.geometries; for(var propName in geometries){ if(geometries.hasOwnProperty(propName) && geometries[propName].mesh){ dae.geometry = geometries[propName].mesh.geometry3js; dae.geometry.computeBoundingBox(); bBox = dae.geometry.boundingBox; if(bBox.min.x < minX) minX = bBox.min.x; if(bBox.min.y < minY) minY = bBox.min.x; if(bBox.min.z < minZ) minZ = bBox.min.z; if(bBox.max.x > maxX) maxX = bBox.max.x; if(bBox.max.y > maxY) maxY = bBox.max.x; if(bBox.max.z > maxZ) maxZ = bBox.max.z; } } //rest of function.... 

This generates some interesting model data. I can get the general extreme coordinate for the model, which I suppose (possibly incorrectly) to be close to the general bounding box for the model. But an attempt to do something with these coordinates (for example, averaging and moving the model to average values) gives rise to inconsistent results.

In addition, it seems that it is inefficient to punch every geometry for the model, is there a better way? If not, can this logic apply to other bootloaders?

+6
source share
1 answer

You can use THREE.Box3#setFromObject to get the bounding box of any Object3D, including the imported model, without having to scroll the geometry itself. So you can do something like

 var bBox = new THREE.Box3().setFromObject(collada.scene); 

to get the extreme bounding box of the model; then you can use any of the methods in gaitat related answers to correctly set the camera position. For example, you can follow this technique ( How to put a camera in an object ) and do something like:

 var height = bBox.size().y; var dist = height / (2 * Math.tan(f * Math.PI / 360)); var pos = collada.scene.position; camera.position.set(pos.x, pos.y, dist * 1.1); // fudge factor so you can see the boundaries camera.lookAt(pos); 

Quick script: http://jsfiddle.net/p19r9re2/ .

+2
source

Source: https://habr.com/ru/post/921881/


All Articles