我有一个Object3D中的网格集,当我获取顶点时,它们不在对象的中心。因此,我需要计算Object3D的中心,然后移动网格以使其与中心对齐。我尝试计算每个网格的边界框,然后使用max-min/2;这种方法不起作用。任何帮助都将是fantastic的。我尝试了Object3D.setFromObject();但它只返回无穷大。
我有一个Object3D中的网格集,当我获取顶点时,它们不在对象的中心。因此,我需要计算Object3D的中心,然后移动网格以使其与中心对齐。我尝试计算每个网格的边界框,然后使用max-min/2;这种方法不起作用。任何帮助都将是fantastic的。我尝试了Object3D.setFromObject();但它只返回无穷大。
// myObject3D is your Object3D
var children = myObject3D.children,
completeBoundingBox = new THREE.Box3(); // create a new box which will contain the entire values
for(var i = 0, j = children.length; i < j; i++){ // iterate through the children
children[i].geometry.computeBoundingBox(); // compute the bounding box of the the meshes geometry
var box = children[i].geometry.boundingBox.clone(); // clone the calculated bounding box, because we have to translate it
box.translate(children[i].position); // translate the geometries bounding box by the meshes position
completeBoundingBox.addPoint(box.max).addPoint(box.min); // add the max and min values to your completeBoundingBox
}
var objectCenter = completeBoundingBox.center()
console.log('This is the center of your Object3D:', objectCenter );
// You want the center of you bounding box to be at 0|0|0
myObject3D.position.x -= objectCenter.x;
myObject3D.position.y -= objectCenter.y;
myObject3D.position.z -= objectCenter.z;
center = function(obj) {
var children = obj.children,
completeBoundingBox = new THREE.Box3();
for(var i = 0, j = children.length; i < j; i++) {
children[i].geometry.computeBoundingBox();
var box = children[i].geometry.boundingBox.clone();
box.translate(children[i].position);
completeBoundingBox.set(box.max, box.min);
}
var objectCenter = completeBoundingBox.center()
console.log('This is the center of your Object3D:', objectCenter );
obj.position.x -= objectCenter.x;
obj.position.y -= objectCenter.y;
obj.position.z -= objectCenter.z;
}