Reputation: 631
What I want to do is to get the center of a Scene obtained from ColladaLoader (as the loaded model is the Scene) in Three.js and then set is as the target of Camera.
Upvotes: 1
Views: 4201
Reputation: 11571
@gaitat's solution will work as long as the model's origin just so happens to also be its center, but I don't think that's likely to be the case. The origin of the model might be the bottom left corner, or the middle of the top plane, or any other point that has no relation to the "center" of the model.
To get the "center" of a model, you need to calculate it from its geometry. Fortunately, THREE.Geometry has some convenience methods to help with that. You can calculate the "bounding sphere" for geometry, i.e. the smallest possible sphere that fits in all the geometry's points. The center of that bounding sphere will be the "center" of the total geometry.
var mesh; // this should be your model's mesh
// it might be buried inside the DAE object somewhere
mesh.geometry.computeBoundingSphere(); // this isn't calculated automatically
// you need to call it
mesh.geometry.boundingSphere.center; // THREE.Vector3 which is the center of the sphere
I'm not sure if you'll need to compute the world matrix transformation for the resulting boundingSphere.center or not, but it's a start.
Keep in mind that the bounding sphere for a geometry doesn't account for the distribution of points. A single solitary point in the geometry that is very very far away from the others will throw off the center of the bounding sphere because it needs to shift to include that point. Still, the bounding sphere method is the quickest and easiest method and should work in most cases.
Upvotes: 3