c# - 在具有不同坐标轴的系统之间转换欧拉角(Unity 和 Threejs)

标签 c# math unity3d three.js euler-angles

我正在尝试在 Unity 和 Threejs 之间转换欧拉角旋转。这有两个主要问题。

问题 1:

Unity和Threejs有不同的坐标系

统一:

enter image description here

三个

enter image description here

问题 2:

Unity 按照 ZXY 的顺序进行欧拉数学运算,而 Threejs 默认为 XYZ。我在 Threejs 方面找到了一些公式,用于使用不同的乘法顺序创建欧拉角,但我想知道这背后的数学原理,以便我可以在两个系统之间来回切换。我也不确定不同的坐标系如何影响这个转换数学。

编辑 1

我发现这篇关于将 Unity 四元数转换为 Threejs 的堆栈溢出帖子:

Convert Unity transforms to THREE.js rotations

但是,我无法让这段代码工作,以实现从 Threejs 到 Unity 的相反方向,而这正是我所需要的。

最佳答案

我终于使用下面的链接找到了解决这个问题的方法。可能有一个更简单的解决方案,但我尝试过的其他方法都没有达到预期的效果。值得注意的是,这是使用 -z 朝向 +y 向上的 Threejs 相机进行测试的。我的统一相机是 -z 面朝上 +y 面朝上。如果您有一个在 Unity 中很常见的 +z 朝向相机,只需将 GameObject 子化为一个空的 GameObject,然后将 180 度 Euler 旋转应用到该空的 GameObject。这还假设 Threejs 欧拉旋转是默认的 XYZ 顺序。

http://answers.unity3d.com/storage/temp/12048-lefthandedtorighthanded.pdf

http://en.wikipedia.org/wiki/Euler_angles

http://forum.unity3d.com/threads/how-to-assign-matrix4x4-to-transform.121966/

    /// <summary>
    /// Converts the given XYZ euler rotation taken from Threejs to a Unity Euler rotation
    /// </summary>
    public static Vector3 ConvertThreejsEulerToUnity(Vector3 eulerThreejs)
    {
        eulerThreejs.x *= -1;
        eulerThreejs.z *= -1;
        Matrix4x4 threejsMatrix = CreateRotationalMatrixThreejs(ref eulerThreejs);

        Matrix4x4 unityMatrix = threejsMatrix;
        unityMatrix.m02 *= -1;
        unityMatrix.m12 *= -1;
        unityMatrix.m20 *= -1;
        unityMatrix.m21 *= -1;

        Quaternion rotation = ExtractRotationFromMatrix(ref unityMatrix);
        Vector3 eulerRotation = rotation.eulerAngles;
        return eulerRotation;
    }

    /// <summary>
    /// Creates a rotation matrix for the given threejs euler rotation
    /// </summary>
    private static Matrix4x4 CreateRotationalMatrixThreejs(ref Vector3 eulerThreejs)
    {
        float c1 = Mathf.Cos(eulerThreejs.x);
        float c2 = Mathf.Cos(eulerThreejs.y);
        float c3 = Mathf.Cos(eulerThreejs.z);
        float s1 = Mathf.Sin(eulerThreejs.x);
        float s2 = Mathf.Sin(eulerThreejs.y);
        float s3 = Mathf.Sin(eulerThreejs.z);
        Matrix4x4 threejsMatrix = new Matrix4x4();
        threejsMatrix.m00 = c2 * c3;
        threejsMatrix.m01 = -c2 * s3;
        threejsMatrix.m02 = s2;
        threejsMatrix.m10 = c1 * s3 + c3 * s1 * s2;
        threejsMatrix.m11 = c1 * c3 - s1 * s2 * s3;
        threejsMatrix.m12 = -c2 * s1;
        threejsMatrix.m20 = s1 * s3 - c1 * c3 * s2;
        threejsMatrix.m21 = c3 * s1 + c1 * s2 * s3;
        threejsMatrix.m22 = c1 * c2;
        threejsMatrix.m33 = 1;
        return threejsMatrix;
    }

    /// <summary>
    /// Extract rotation quaternion from transform matrix.
    /// </summary>
    /// <param name="matrix">Transform matrix. This parameter is passed by reference
    /// to improve performance; no changes will be made to it.</param>
    /// <returns>
    /// Quaternion representation of rotation transform.
    /// </returns>
    public static Quaternion ExtractRotationFromMatrix(ref Matrix4x4 matrix)
    {
        Vector3 forward;
        forward.x = matrix.m02;
        forward.y = matrix.m12;
        forward.z = matrix.m22;

        Vector3 upwards;
        upwards.x = matrix.m01;
        upwards.y = matrix.m11;
        upwards.z = matrix.m21;

        return Quaternion.LookRotation(forward, upwards);
    }

关于c# - 在具有不同坐标轴的系统之间转换欧拉角(Unity 和 Threejs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29378998/

相关文章:

python - 凸包的增量面积

c# - Unity/HoloLens 确定角度的分色

c# - Json 转换空字符串而不是 null

c# - 按任意时间间隔对 DateTime 进行分组

c# - 从字符串 [,] 中删除空值

c# - 如何使用 IMAP 在 C# 中从 gmail 下载附件?

python - 总结数字!

math - 在不失去一致性的情况下将数据从 double 映射到整数的最简单方法

c# - Unity 5 - UNET - 将拖动的游戏对象同步为客户端(HL2 重力枪风格)

c# - 如何停止默认动画?