将物体的一侧放在平面上

时间:2018-02-08 00:58:40

标签: three.js geometry

我有这样的对象: ][1]

我需要自动找到物体的最大平面(在这种情况下它是面的背面)并将其放在一些简单的平面几何体上。

有可能吗?

1 个答案:

答案 0 :(得分:0)

哦,@ memegoesthedog和TheJim01,谢谢你们的想法。也许我的代码对任何人都有用..

const faceCentering = (model) => {

  const quickhull = new QuickHull()
  quickhull.setFromObject(model)

  const ePoints = quickhull.computeExtremes()
  const centerX = 0.5 * ( ePoints.max[0].point.x + ePoints.min[0].point.x );
  const centerY = 0.5 * ( ePoints.max[1].point.y + ePoints.min[1].point.y );
  const centerZ = 0.5 * ( ePoints.max[2].point.z + ePoints.min[2].point.z );

  const maxAriaFace = quickhull.faces
                      .map(face => face.area )
                      .reduce((a, b) => Math.max(a, b) )

  const pivotFace = quickhull.faces
                    .filter(obj => obj.area === maxAriaFace)[0]


  //rotating with the "back side model" face's normal along yAxis
  const newDir = new Vector3(pivotFace.normal.x, pivotFace.normal.y, -pivotFace.normal.z);
  const pos = new Vector3().addVectors(newDir, model.position);
  model.lookAt(pos);

  // centroid position
  model.children[0].geometry.applyMatrix( new Matrix4()
  .makeTranslation(-centerX, -centerY, -pivotFace.midpoint.z))

}