unity 工具包代碼 矩陣分解位置、旋轉

        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,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;
        }

    }
發佈了15 篇原創文章 · 獲贊 11 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章