答案 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))
}