【问题标题】:How to determine the width/height/depth of an Object3D (as if it wasn't rotated)?如何确定 Object3D 的宽度/高度/深度(就好像它没有旋转一样)?
【发布时间】:2018-03-02 04:39:39
【问题描述】:

我正在计算 Object3D 的高度,如下所示:

let obj = ... ; // An Object3D instance. Could be a Mesh, Group, etc.
let boundingBox = new THREE.Box3().setFromObject(obj);
let height = Math.abs(boundingBox.min.y - boundingBox.max.y);

obj 旋转(在X 和/或Z 轴上)时,boundingBox.min.yboundingBox.max.y 之间的差异会增加/减少,从而导致高度与未旋转时不同。

但我想计算obj 的高度,就好像它根本没有旋转一样。我该怎么做?

我猜我需要根据旋转角度来转换boundingBox 的尺寸,但我不知道该怎么做。


旋转前:

旋转后:

(红色 = obj,蓝色 = boundingBox

【问题讨论】:

  • 为什么在计算前不能保存旋转并将旋转设置为零?

标签: javascript three.js rotation


【解决方案1】:

THREE.Box3().setFromObject(obj) 将为您提供对象的“世界轴对齐边界框”。它将显式计算所有顶点的世界坐标(阅读:包括对象及其所有父对象的旋转、位置和缩放)。

如果你只想要几何体的边界框(没有对象的位置、旋转、缩放),你可以在调用computeBoundingBox()之后使用obj.geometry.boundingBox

obj.geometry.computeBoundingBox(); 
let boundingBox = obj.geometry.boundingBox;

对于对象层次结构,您可以执行以下操作来获取聚合边界框:

function getCombinedBoundingBox(object) {
  const result = new THREE.Box();
  object.traverse(child => {
    // skip everything that doesn't have a geometry
    if (!child.geometry) { return; }

    child.geometry.computeBoundingBox();
    result.union(child.geometry.boundingBox);
  });

  return result;
}

请注意,这仅在子对象未以任何方式转换时才有效。

【讨论】:

  • 并非Object3D 的每个子类都有一个已定义的geometry 属性,例如THREE.Group。我需要一个适用于任何Object3D 的通用解决方案。谢谢。
猜你喜欢
  • 2018-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多