如何解决将几何体与平面对齐选择 3 个相交点
我有几天时间尝试对齐 3D 对象,选择与 3D 对象相交的 3 个点。
我必须将 GEOMETRY 与平面对齐。
我做的第一步是将选定的第一个点移动到 0,0 位置:
移动后,如您所见,点没有更新。
let matrixToApply = new Matrix4().makeTranslation(
-this.aligningPoints[0].x,-this.aligningPoints[0].y,-this.aligningPoints[0].z
);
this.mesh.geometry.applyMatrix4(matrixToApply)
在这一步之后,接下来是旋转第一个点和第二个点,获得它们之间的角度,代码太愚蠢了,我需要一些帮助来获得更好的解决方案,例如在欧拉上应用四分法。我只需要旋转并匹配到平面。
if (crossvectors.x > 0 && crossvectors.y > 0 && crossvectors.z < 0) {
console.log('+ + -');
var rotateX = new THREE.Vector3(0,100).angleto(new THREE.Vector3(0,crossvectors.y,crossvectors.z));
this.mesh.geometry.rotateX(rotateX);
} else if (crossvectors.x > 0 && crossvectors.y < 0 && crossvectors.z < 0) {
console.log('+ - -');
var rotateX = new THREE.Vector3(0,-100).angleto(new THREE.Vector3(0,crossvectors.z));
this.mesh.geometry.rotateX(rotateX);
} else if (crossvectors.x > 0 && crossvectors.y < 0 && crossvectors.z > 0) {
console.log(' + - +');
var rotateX = new THREE.Vector3(0,100).normalize().angleto(new THREE.Vector3(crossvectors.x,crossvectors.z).normalize());
this.mesh.geometry.rotateY(-rotateX);
} else if (crossvectors.x < 0 && crossvectors.y < 0 && crossvectors.z > 0) {
console.log('- - +');
var rotateX = new THREE.Vector3(0,100).normalize().angleto(new THREE.Vector3(0,crossvectors.z).normalize());
this.mesh.geometry.rotateX(-rotateX);
} else if (crossvectors.x < 0 && crossvectors.y < 0 && crossvectors.z < 0) {
console.log('- - -');
var rotateX = new THREE.Vector3(0,-100).normalize().angleto(new THREE.Vector3(0,crossvectors.z).normalize());
this.mesh.geometry.rotateX(rotateX);
} else if (crossvectors.x > 0 && crossvectors.y > 0 && crossvectors.z > 0) {
console.log('+ + +');
var rotateX = new THREE.Vector3(0,crossvectors.z).normalize());
this.mesh.geometry.rotateX(rotateX);
} else if (crossvectors.x < 0 && crossvectors.y > 0 && crossvectors.z < 0) {
console.log('- + -');
var rotateX = new THREE.Vector3(0,crossvectors.z).normalize());
this.mesh.geometry.rotateX(rotateX);
} else if (crossvectors.x < 0 && crossvectors.y > 0 && crossvectors.z > 0) {
console.log('- + +');
var rotateX = new THREE.Vector3(0,crossvectors.z).normalize());
this.mesh.geometry.rotateX(rotateX);
}
else {
alert('IMPOSSIBLE TO ALIGN');
}
最后一步对齐第 2 点和第 3 点,计算两者之间的角度。
真的谢谢大家,我有点疯了,我是新来的。
我想要一个更简单的代码,我 90% 确定它有更好的方法来做到这一点。
这个想法是让平面上的对象由 3 个点对齐:
真的很感谢!
解决方法
我已经通过@WestLangley 的回复解决了这个问题,非常感谢您的帮助。
有对齐网格以匹配平面的代码:
let subvector1,subvector2,crossVectors = new THREE.Vector3();
// Create the cross vector with the 3 points
subvector1.subVectors(this.aligningPoints[0],this.aligningPoints[2]);
subvector2.subVectors(this.aligningPoints[0],this.aligningPoints[1]);
crossVectors.crossVectors(subvector1,subvector2);
// Move the mesh (0,0) plane
this.mesh.geometry.applyMatrix4(new Matrix4().makeTranslation(-this.aligningPoints[0].x,-this.aligningPoints[0].y,-this.aligningPoints[0].z))
// Align the mesh to the plane with quaternion and matrix4
const quaternion = new THREE.Quaternion();
quaternion.setFromUnitVectors( crossVectors.normalize(),new THREE.Vector3(0,1).normalize() );
let matrix4 = new THREE.Matrix4()
matrix4.makeRotationFromQuaternion( quaternion );
this.mesh.geometry.applyMatrix4( matrix4 );
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。