Three.js移动和旋转几何中心 [英] Three.js move and rotate the center of geometry

查看:72
本文介绍了Three.js移动和旋转几何中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 OBJLoader 加载的复杂模型.我想移动和旋转它的中心,让用户(我的应用程序的用户)将中心点放在他/她想要的地方.我知道这一点:老问题,但对我来说不起作用.这是小提琴测试:小提琴示例

I have a complex model loaded with OBJLoader. I want to move and rotate its center to let the user ( the user of my application ) to place the center point where he/she wants. I'm aware of this: old question, but for me does not work. This is a test in fiddle : fiddle example

按下示例中的按钮,我希望中心被移动.按钮中的代码是:

Pressing the button in the example I expect that the center is moved. The code in the button is :

objMesh.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(new THREE.Vector3(1, 0.1, 0.1)));

推荐答案

我凭经验找到了解决方案,但我不明白它为什么有效.我发布了代码以提供帮助,也许有人可以解释我的发现.

I found the solution empirically, but I don't understand why it works. I post the code to help and maybe someone could explain me what I found.

下面的代码将 x 中的中心移动 0.1 个单位.

The code below shift the center in x for 0.1 units.

var x = 0.1;
var y = 0;
var z = 0;

for (var i = 0; i < object.children.length; i++)
{
    var geometry = object.children[i].geometry;

    object.children[i].centroid.divideScalar(geometry.vertices.length); // Why this line ?

    var offset = object.children[i].centroid.clone(); 
    object.children[i].geometry.applyMatrix(new THREE.Matrix4().makeTranslation(-offset.x+x, -offset.y+y, -offset.z+z));
    object.children[i].position.x -= x;
    object.children[i].position.y -= y;
    object.children[i].position.z -= z;
    object.children[i].geometry.verticesNeedUpdate = true;
}

这篇关于Three.js移动和旋转几何中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆