如何解决如何将对象位置设置为其重心?
现在,当我上传模型时,其中心(由transformControls突出显示)不在模型的质心中。我该如何将其移到那里?
我尝试过:
(model) => {
box = new Box3();
box.expandByObject(model);
const centerOfBox = new Vector3();
box.getCenter(centerOfBox);
const size = new Vector3();
box.getSize(size);
const centerOfWorld = new Vector3(0,size.y/2,0);
const offset = new Vector3();
offset.subVectors(centerOfWorld,centerOfBox);
const scale = model.scale;
model.translateX(scale.x*offset.x);
model.translateY(scale.y*offset.y);
model.translateZ(scale.z*offset.z);
}
还有这个:
(model) => {
model.traverse(function(child) {
const rememberPosition = child.getWorldPosition(new Vector3());
const box = new Box3();
box.expandByObject(child);
const centerOfBox = new Vector3();
box.getCenter(centerOfBox);
const offset = new Vector3();
offset.subVectors(rememberPosition,centerOfBox);
child.traverse(function(ch) {
if (child instanceof Mesh) {
ch.geometry.translate(offset.x,offset.y,offset.z);
ch.translateX(-offset.x);
ch.translateY(-offset.y);
ch.translateZ(-offset.z);
}
});
});
model.position.copy(new Vector3());
}
在后一版本中,我尝试将几何形状移动到对象的位置,然后将所有内容(几何形状+位置)都移回原始位置(原始位置=可见的原始位置)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。