unity还原three之旋转

http://www.360doc.com/content/16/0829/14/12282510_586760119.shtml

unity使用左手坐标系,另外在做旋转的时候必须弄清楚旋转坐标轴旋转顺序

一:edit中Transform组件

脚本重置:

transform.localEulerAngles = new Vector3(60,60,60);

绕父节点坐标轴旋转,旋转顺序z-x-y;

二:在脚本中使用Rotate()函数,参数为Space.Self

transform.Rotate(new Vector3(45,45,45),Space.Self);

绕本地坐标系坐标轴旋转,旋转顺序z-x-y;

注:每次使用Space.self进行rotate时,都是绕着调用时刻的坐标轴进行旋转的

三:在脚本中使用Rotate()函数,参数为Space.World

transform.Rotate(new Vector3(45,45,45),Space.World);

绕世界坐标系坐标轴旋转,旋转顺序z-x-y;

四:关于静态欧拉角和动态欧拉角

静态欧拉角,就是其旋转轴使用的是静止不动的参考系。

动态欧拉角,使用的是模型本身作为参考系,因而会随着模型的旋转而旋转。

        因此,再看看前面的三种情况,使用Space.World旋转,以及 Editor 中的旋转,是静态欧拉角;使用Space.self,是动态欧拉角。

五:还原three的旋转

three使用右手坐标系,edit中,根对象绕本地坐标系坐标轴旋转,旋转顺序x-y-z;

子对象绕父对象的坐标轴旋转,旋转顺序x-y-z;(即除根对象外,其他全部绕父对象的坐标轴旋转)

(此处是个坑,当初真是too young too simple,最开始以为旋转坐标轴都是本地坐标轴。。。)

Clipboard Image.png

旋转在unity中的还原最直观方法(只适用于root):

gameObject.transform.Rotate(new Vector3(matrixParse.GetAngles.x,0,0),Space.Self);
gameObject.transform.Rotate(new Vector3(0,matrixParse.GetAngles.y,0),Space.Self);
gameObject.transform.Rotate(new Vector3(0,0,matrixParse.GetAngles.z),Space.Self);

非root:

transform.localEulerAngles = matrixParse.GetAngles;//这样写讲道理是有问题的,没考虑旋转顺序.

找到解决办法:

Vector3 ang = matrixParse.GetAngles;
gameObject.transform.localRotation = Quaternion.AngleAxis(ang.x,                                       Vector3.right)
* Quaternion.AngleAxis(ang.y,                                       Vector3.up)
* Quaternion.AngleAxis(ang.z,                                       Vector3.forward);

始终遵循的原则:旋转顺序和旋转方式与three统一。

特殊化,平面和摄像机初始时与three存在差异,采用进一步旋转进行补偿:

transform.Rotate(new Vector3(90,0,0),Space.Self);//平面
transform.Rotate(0,180,0),Space.Self);//摄像机

六:左右反转

由于左右手坐标系的差异,导致unity中看到的场景与three相比左右颠倒。

解决办法:

1.缩放无效的模型,例如摄像机

//反转
Vector3 pos = transform.position;
transform.position = new Vector3(-pos.x,pos.y,pos.z);
Vector3 angs = transform.eulerAngles;
gameObject.transform.Rotate(new Vector3(0,-2 * angs.y,0),Space.World);

2.可以调整缩放的模型

//反转
Vector3 pos = transform.position;
transform.position = new Vector3(-pos.x,pos.y,pos.z);

Vector3 scale = transform.localScale;
transform.localScale = new Vector3(-scale.x,-scale.y,-scale.z);

transform.Rotate(new Vector3(180,0,0),Space.World);

其中缩放可根据显示需要调整正负。



原文地址:https://www.cnblogs.com/leeplogs/p/6813713.html