Three.js如何基于内部网格设置Object3D的中心点

3

我有一个Object3D中的网格集,当我获取顶点时,它们不在对象的中心。因此,我需要计算Object3D的中心,然后移动网格以使其与中心对齐。我尝试计算每个网格的边界框,然后使用max-min/2;这种方法不起作用。任何帮助都将是fantastic的。我尝试了Object3D.setFromObject();但它只返回无穷大。


1
你能分享一些代码吗?有点难以想象你的问题... - mrdoob
2个回答

2
为了让一个Object3D居中,需要根据其子元素进行迭代,据我所知。代码如下所示:
// 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;

希望我理解您的问题是正确的!

0
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;
}

1
你能否添加注释来解释你的代码在做什么? - epo3
center(obj)obj -- Object3D -- 要显示世界轴对齐边界框的Object3D。这将居中Object3D。 - Ahmad Ali

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接