简体   繁体   English

three.js:2 x方向矢量到3 x旋转或矩阵

[英]three.js : 2 x Directions vectors to 3 x Rotations or Matrix

I have a coordinates system coded by 2 unit vectors (I have called 'Ox' and 'Oy'), 1 insertion point and, near, is an 'Oz' vector which seems to always be {0 0 1}. 我有一个由2个单位向量(我称为“ Ox”和“ Oy”),一个插入点编码的坐标系,并且附近是一个“ Oz”向量,似乎总是{0 0 1}。

For example (after 2 rotations) : 例如(两次旋转后):

Ox:{x: 0.956304755963036, y: -0.292371704722735, z: 0}
Oy:{x: -0.250611464938861, y: -0.819713166317425, z: 0.51503807491006}
move:{x: 889.08282028218, y: -845.071708420642, z: -396.787982804802}

The question is : how to align my_mesh to this system within three.js ? 问题是:如何在three.js中将my_mesh与此系统对齐?

I can't really figure out how it projects, but Thanks to this answer I came to this : 我真的不能弄清楚它是如何设计的,但是由于这个答案,我才得出这个结论

if( 'Ox' in align && 'Oy' in align ){
    var mx = new THREE.Matrix4().lookAt( align.Ox , new THREE.Vector3(0,0,0), new THREE.Vector3(0,1,0) );
    var my = new THREE.Matrix4().lookAt( align.Oy , new THREE.Vector3(0,0,0), new THREE.Vector3(1,0,0) );
    mx.multiply(my);
    var qt = new THREE.Quaternion().setFromRotationMatrix(mx);

    my_mesh.matrix.makeRotationFromQuaternion(qt);
}
// move
mesh.matrix.setPosition( align.move );
mesh.matrixAutoUpdate = false;

which seems to approach the solution ? 哪个似乎可以解决问题?


EDIT 编辑

After West's answer, I realize that I should not have called this 'coordinates system' since the vectors are not orthogonal. 在West回答之后,我意识到我不应该称这个“坐标系”为矢量,因为它们不是正交的。

Below is some records after rotations ('z30' means rotate 30° around Oz).. What I called Oz is always set to {x: 0, y: 0, z: 1} 以下是旋转后的一些记录(“ z30”表示围绕Oz旋转30°)。我所说的Oz始终设置为{x: 0, y: 0, z: 1}

// base
Ox: {x: 1, y: 0, z: 0}
Oy: {x: 0, y: 1, z: 0}
move: {x: 6.42995404746696, y: -500, z: -268.028464077285}

// base x45
Ox: {x: 1, y: 0, z: 0}
Oy: {x: 0, y: -0.707106781186548, z: 0.707106781186547}
move: {x: 6.42995404746696, y: -73.2233047033605, z: -444.805159373921}

// base y60
Ox: {x: 0.500000000000001, y: 0, z: 0.866025403784438}
Oy: {x: 0, y: 1, z: 0}
move: {x: 6.42995404746766, y: -500, z: -268.028464077285}

// base z30
Ox: {x: 0.866025403784439, y: 0.5, z: 0}
Oy: {x: 0.5, y: -0.866025403784439, z: 0}
move: {x: -118.570045952533, y: -33.4936490538881, z: -268.028464077284}

// base z30 x45
Ox: {x: 0.866025403784439, y: 0.353553390593274, z: -0.353553390593273}
Oy: {x: 0.5, y: -0.612372435695795, z: 0.612372435695794}
move: {x: -118.570045952533, y: -96.9068910760492, z: -421.121573001233}

// base z30 x45 y60
Ox: {x: 0.739198919740117, y: 0.353553390593274, z: 0.573223304703363}
Oy: {x: 0.28033008588991, y: 0.612372435695795, z: -0.739198919740117}
move: {x: -63.6525674250102, y: -403.093108923947, z: -83.228734142255}

You want to set an object's orientation to align to a rotated coordinate system specified by three orthogonal unit-length vectors ox , oy , and oz . 您想要设置对象的方向,使其与由三个正交单位长度向量oxoyoz指定的旋转坐标系对齐。

If you don't know oz , you can compute it: 如果您不知道oz ,则可以计算它:

var oz = new THREE.Vector3(); // create once and reuse

oz.crossVectors( ox, oy ).normalize();

Then, since you appear to want to set mesh.matrix directly, do this: 然后,由于您似乎想直接设置mesh.matrix ,请执行以下操作:

mesh.matrix.makeBasis( ox, oy, oz ).setPosition( new_position );

mesh.matrixAutoUpdate = false; // prevent matrix from being overwritten

three.js r.87 three.js r.87

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM