代码之家  ›  专栏  ›  技术社区  ›  Vlad

SlimPath中的LookAt旋转具有Unity3D中的预期值

  •  0
  • Vlad  · 技术社区  · 10 年前

    假设v1=(36.5f,10.7f,15.9f),v2=(29.4f,10.8f,19.7f),up=(0f,1f,0f)。

    我尝试使用Matrix.LookAtLH和LookAtRH,但所有的原因都是错误的:

    四元数.RotationMatrix(Matrix.LookAtLH(v1,v2,up))输出

            w: 0.9    x: 0    y: 0.5    z: 0
    

    四元数.RotationMatrix(Matrix.LookAtRH(v1,v2,up))输出

            w: 0.5    x: 0    y: -0.9   z: 0
    

    但我希望(来自Unity3D)

            w: 0.9   x: 0    y: -0.5     z: 0
    

    另一个示例:v1=(106.5f,35.7f,15.9f),v2=(29.4f,10.8f,19.7f),up=(0f,1f,0f)。

    四元数.RotationMatrix(Matrix.LookAtLH(v1,v2,up))输出

           w: 0.7     x: -0.1 y: 0.7     z: -0.1
    

    四元数.RotationMatrix(Matrix.LookAtRH(v1,v2,up))输出

           w: 0.7     x: 0.1  y: -0.7    z: -0.1
    

    但我希望(来自Unity3D)

           w: 0.7     x: 0.1  y: -0.7    z: 0.1
    

    SlimPath矩阵.cs:

        /// <summary>
        /// Creates a left-handed, look-at matrix.
        /// </summary>
        /// <param name="eye">The position of the viewer's eye.</param>
        /// <param name="target">The camera look-at target.</param>
        /// <param name="up">The camera's up vector.</param>
        /// <param name="result">When the method completes, contains the created look-at matrix.</param>
        public static void LookAtLH(ref SlimVector3 eye, ref SlimVector3 target, ref SlimVector3 up, out SlimMatrix result)
        {
            SlimVector3 xaxis, yaxis, zaxis;
            SlimVector3.Subtract(ref target, ref eye, out zaxis); zaxis.Normalize();
            SlimVector3.Cross(ref up, ref zaxis, out xaxis); xaxis.Normalize();
            SlimVector3.Cross(ref zaxis, ref xaxis, out yaxis);
    
            result = SlimMatrix.Identity;
            result.M11 = xaxis.X; result.M21 = xaxis.Y; result.M31 = xaxis.Z;
            result.M12 = yaxis.X; result.M22 = yaxis.Y; result.M32 = yaxis.Z;
            result.M13 = zaxis.X; result.M23 = zaxis.Y; result.M33 = zaxis.Z;
    
            SlimVector3.Dot(ref xaxis, ref eye, out result.M41);
            SlimVector3.Dot(ref yaxis, ref eye, out result.M42);
            SlimVector3.Dot(ref zaxis, ref eye, out result.M43);
    
            result.M41 = -result.M41;
            result.M42 = -result.M42;
            result.M43 = -result.M43;
        }
    
        /// <summary>
        /// Creates a right-handed, look-at matrix.
        /// </summary>
        /// <param name="eye">The position of the viewer's eye.</param>
        /// <param name="target">The camera look-at target.</param>
        /// <param name="up">The camera's up vector.</param>
        /// <param name="result">When the method completes, contains the created look-at matrix.</param>
        public static void LookAtRH(ref SlimVector3 eye, ref SlimVector3 target, ref SlimVector3 up, out SlimMatrix result)
        {
            SlimVector3 xaxis, yaxis, zaxis;
            SlimVector3.Subtract(ref eye, ref target, out zaxis); zaxis.Normalize();
            SlimVector3.Cross(ref up, ref zaxis, out xaxis); xaxis.Normalize();
            SlimVector3.Cross(ref zaxis, ref xaxis, out yaxis);
    
            result = SlimMatrix.Identity;
            result.M11 = xaxis.X; result.M21 = xaxis.Y; result.M31 = xaxis.Z;
            result.M12 = yaxis.X; result.M22 = yaxis.Y; result.M32 = yaxis.Z;
            result.M13 = zaxis.X; result.M23 = zaxis.Y; result.M33 = zaxis.Z;
    
            SlimVector3.Dot(ref xaxis, ref eye, out result.M41);
            SlimVector3.Dot(ref yaxis, ref eye, out result.M42);
            SlimVector3.Dot(ref zaxis, ref eye, out result.M43);
    
            result.M41 = -result.M41;
            result.M42 = -result.M42;
            result.M43 = -result.M43;
        }
    

    如何修改它以接收与Unity3D相同的结果?

    1 回复  |  直到 10 年前
        1
  •  0
  •   Vlad    10 年前

    我不明白为什么它看起来是颠倒的。我反转了LookAtLH的轴值,现在它开始工作了。。。