【问题标题】:Three.js set the center of a Object3D based on internal meshesThree.js根据内部网格设置Object3D的中心
【发布时间】:2021-08-13 13:45:09
【问题描述】:

当我得到未以对象为中心的顶点时,我有一个位于 Object3D 中的网格集。所以我需要计算 object3D 的中心,然后移动网格以将它们与中心对齐。我尝试计算每个网格的边界框,然后是 max - min /2;这不起作用。这里的任何帮助都会很棒。我试过 Object3D.setFromObject();这只会返回无穷大。

【问题讨论】:

  • 你能分享一些代码吗?有点难以想象你的问题......

标签: three.js


【解决方案1】:

据我所知,要使 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;

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

【讨论】:

    【解决方案2】:
    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;
    }
    

    【讨论】:

    • 你能添加 cmets 来解释你的代码在做什么吗?
    • center(obj) obj -- Object3D -- 显示世界轴对齐边界框的 object3D。这以 Object3D 为中心。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2012-04-11
    • 2016-06-03
    相关资源
    最近更新 更多