Exemplo n.º 1
0
        public static QuaternionRightHand LookRotation(Vector3RightHand forward, Vector3RightHand up)
        {
            forward = Vector3RightHand.Normalize(forward);
            Vector3RightHand right = Vector3RightHand.Normalize(Vector3RightHand.Cross(up, forward));

            up = Vector3RightHand.Cross(forward, right);
            var m00 = right.x;
            var m01 = right.y;
            var m02 = right.z;
            var m10 = up.x;
            var m11 = up.y;
            var m12 = up.z;
            var m20 = forward.x;
            var m21 = forward.y;
            var m22 = forward.z;


            float num8       = (m00 + m11) + m22;
            var   quaternion = new QuaternionRightHand();

            if (num8 > 0f)
            {
                var num = (float)Mathf.Sqrt(num8 + 1f);
                quaternion.w = num * 0.5f;
                num          = 0.5f / num;
                quaternion.x = (m12 - m21) * num;
                quaternion.y = (m20 - m02) * num;
                quaternion.z = (m01 - m10) * num;
                return(quaternion);
            }
            if ((m00 >= m11) && (m00 >= m22))
            {
                var num7 = (float)Mathf.Sqrt(((1f + m00) - m11) - m22);
                var num4 = 0.5f / num7;
                quaternion.x = 0.5f * num7;
                quaternion.y = (m01 + m10) * num4;
                quaternion.z = (m02 + m20) * num4;
                quaternion.w = (m12 - m21) * num4;
                return(quaternion);
            }
            if (m11 > m22)
            {
                var num6 = (float)Mathf.Sqrt(((1f + m11) - m00) - m22);
                var num3 = 0.5f / num6;
                quaternion.x = (m10 + m01) * num3;
                quaternion.y = 0.5f * num6;
                quaternion.z = (m21 + m12) * num3;
                quaternion.w = (m20 - m02) * num3;
                return(quaternion);
            }
            var num5 = (float)Mathf.Sqrt(((1f + m22) - m00) - m11);
            var num2 = 0.5f / num5;

            quaternion.x = (m20 + m02) * num2;
            quaternion.y = (m21 + m12) * num2;
            quaternion.z = 0.5f * num5;
            quaternion.w = (m01 - m10) * num2;
            return(quaternion);
        }
Exemplo n.º 2
0
        //static Quaternion FromToRotation(Vector3 fromDiection, Vector3 toDirection)
        //根据两个向量计算出旋转量,计算出来的旋转量为从fromDiection,旋转到toDirection的旋转量。
        //这句话意思很明显了。就是计算旋转量。
        //那么LookRotation(Vector3 forward)计算的是,Z轴旋转到forward的旋转量。
        //推出:Quaternion.LookRotation(new Vector3(1,0,0)) == Quaternion.FromToRotation(Vector3.forward, new Vector3(1,0,0));
        //因为前者就是计算向前向量到当前向量(1,0,0)的旋转量的,其实现过程就是后者喽。

        public static QuaternionRightHand FromToRotation(Vector3RightHand v1, Vector3RightHand v2)
        {
            //         fromDirection.Normalize();
            //         toDirection.Normalize();
            //         Quaternion fromQua = LookRotation(fromDirection);
            //         Quaternion toQua = LookRotation(toDirection);
            //         fromQua = Normalize(fromQua);
            //         toQua = Normalize(toQua);

            //Vector3 fromDir = fromQua * Vector3.forward;
            //Vector3 toDir = toQua * Vector3.forward;

            //Quaternion ret = RotateTowards(fromQua, toQua, float.MaxValue);
            //return ret;
            return(QuaternionRightHand.AngleAxis(Vector3RightHand.Angle(v1, v2), Vector3RightHand.Cross(v1, v2)));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 将向量from向向量to旋转角度angle
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        static Vector3RightHand RotateTo(Vector3RightHand from, Vector3RightHand to, float angle)
        {
            //如果两向量角度为0
            if (Vector3RightHand.Angle(from, to) == 0)
            {
                return(from);
            }

            //旋转轴
            Vector3RightHand n = Vector3RightHand.Cross(from, to);

            //旋转轴规范化
            n.Normalize();

            //旋转矩阵
            Matrix4x4RightHand rotateMatrix = new Matrix4x4RightHand();

            //旋转的弧度
            double radian   = angle * Mathf.PI / 180;
            float  cosAngle = (float)Mathf.Cos((float)radian);
            float  sinAngle = (float)Mathf.Sin((float)radian);

            //矩阵的数据
            //这里看不懂的自行科普矩阵知识
            rotateMatrix.SetRow(0, new Vector4(n.x * n.x * (1 - cosAngle) + cosAngle, n.x * n.y * (1 - cosAngle) + n.z * sinAngle, n.x * n.z * (1 - cosAngle) - n.y * sinAngle, 0));
            rotateMatrix.SetRow(1, new Vector4(n.x * n.y * (1 - cosAngle) - n.z * sinAngle, n.y * n.y * (1 - cosAngle) + cosAngle, n.y * n.z * (1 - cosAngle) + n.x * sinAngle, 0));
            rotateMatrix.SetRow(2, new Vector4(n.x * n.z * (1 - cosAngle) + n.y * sinAngle, n.y * n.z * (1 - cosAngle) - n.x * sinAngle, n.z * n.z * (1 - cosAngle) + cosAngle, 0));
            rotateMatrix.SetRow(3, new Vector4(0, 0, 0, 1));

            Vector4          v      = Vector3RightHand.ToVector4(from);
            Vector3RightHand vector = new Vector3RightHand();

            for (int i = 0; i < 3; ++i)
            {
                for (int j = 0; j < 3; j++)
                {
                    vector[i] += v[j] * rotateMatrix[j, i];
                }
            }
            return(vector);
        }