unity 工具包代码 矩阵分解位置、旋转
2018-03-14 11:49
781 查看
public static Vector3 GetPosition(Matrix4x4 matrix)
{
// Convert from ARKit's right-handed coordinate
// system to Unity's left-handed
Vector3 position = matrix.GetColumn(3);
position.z = -position.z;
return position;
}
public static Quaternion GetRotation(Matrix4x4 matrix)
{
// Convert from ARKit's right-handed coordinate
// system to Unity's left-handed
Quaternion rotation = QuaternionFromMatrix(matrix);
rotation.z = -rotation.z;
rotation.w = -rotation.w;
return rotation;
}
static Quaternion QuaternionFromMatrix(Matrix4x4 m) {
// Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
Quaternion q = new Quaternion();
q.w = Mathf.Sqrt( Mathf.Max( 0, 1 + m[0,0] + m[1,1] + m[2,2] ) ) / 2;
q.x = Mathf.Sqrt( Mathf.Max( 0, 1 + m[0,0] - m[1,
ca49
1] - m[2,2] ) ) / 2;
q.y = Mathf.Sqrt( Mathf.Max( 0, 1 - m[0,0] + m[1,1] - m[2,2] ) ) / 2;
q.z = Mathf.Sqrt( Mathf.Max( 0, 1 - m[0,0] - m[1,1] + m[2,2] ) ) / 2;
q.x *= Mathf.Sign( q.x * ( m[2,1] - m[1,2] ) );
q.y *= Mathf.Sign( q.y * ( m[0,2] - m[2,0] ) );
q.z *= Mathf.Sign( q.z * ( m[1,0] - m[0,1] ) );
return q;
}
}
{
// Convert from ARKit's right-handed coordinate
// system to Unity's left-handed
Vector3 position = matrix.GetColumn(3);
position.z = -position.z;
return position;
}
public static Quaternion GetRotation(Matrix4x4 matrix)
{
// Convert from ARKit's right-handed coordinate
// system to Unity's left-handed
Quaternion rotation = QuaternionFromMatrix(matrix);
rotation.z = -rotation.z;
rotation.w = -rotation.w;
return rotation;
}
static Quaternion QuaternionFromMatrix(Matrix4x4 m) {
// Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
Quaternion q = new Quaternion();
q.w = Mathf.Sqrt( Mathf.Max( 0, 1 + m[0,0] + m[1,1] + m[2,2] ) ) / 2;
q.x = Mathf.Sqrt( Mathf.Max( 0, 1 + m[0,0] - m[1,
ca49
1] - m[2,2] ) ) / 2;
q.y = Mathf.Sqrt( Mathf.Max( 0, 1 - m[0,0] + m[1,1] - m[2,2] ) ) / 2;
q.z = Mathf.Sqrt( Mathf.Max( 0, 1 - m[0,0] - m[1,1] + m[2,2] ) ) / 2;
q.x *= Mathf.Sign( q.x * ( m[2,1] - m[1,2] ) );
q.y *= Mathf.Sign( q.y * ( m[0,2] - m[2,0] ) );
q.z *= Mathf.Sign( q.z * ( m[1,0] - m[0,1] ) );
return q;
}
}
相关文章推荐
- Unity-关于Rigidbody如何使用代码冻结位置和旋转
- 编程算法 - 旋转矩阵 代码(C)
- [Unity&Photon&代码问题&代码分析]右键移动 目标 位置偏差
- 矩阵分解在推荐系统的应用以及python代码的实现
- Decomposing a matrix(用于分解变换矩阵至旋转、平移,缩放分量)
- unity基础开发----物体位移和旋转实用代码
- IOS5开发-控件位置适应屏幕旋转代码
- Codeforces Round #348 DIV2 C. Little Artem and Matrix(将矩阵的第x行向左边旋转一位,将矩阵的第y列向上边旋转一位,现在的第x,y位置为z)
- Android OpenGL ES(六)----进入三维在代码中创建投影矩阵和旋转矩阵
- JAVA代码—算法基础:将N*N的矩阵顺时针旋转90度输出元素
- Chapter 1 | Arrays and Strings--旋转N*N矩阵的图像和处理M*N矩阵,若某位置为0则所在行和列均置0
- Decomposing a matrix(用于分解变换矩阵至旋转、平移,缩放分量)
- 编程算法 - 旋转矩阵 代码(C)
- Unity之一天一个技术点(十三)---以指定对象为中心,根据鼠标位置旋转照相机
- Unity 3D 中实现对物体 位置(position) 旋转(rotation) 大小(scale) 的全面控制
- 怎么将变换矩阵分解为缩放和旋转,尽量不用矩阵表示
- 【Ogre-windows】旋转矩阵及位置解析
- 编程算法 - 旋转矩阵 代码(C)
- 一行代码搞定矩阵旋转——python
- 看opengl写代码(3) 实现矩阵的旋转