/// <summary> /// 旋转轴和旋转角(单位四元数有效) /// </summary> /// <param name="angle"></param> /// <param name="axisNormal"></param> public void ToAngleAxis(out float angle, out Vector3 axisNormal) { Vector3 axis = new Vector3( MathS.Abs(x) <= Threshold ? 0f : x, MathS.Abs(y) <= Threshold ? 0f : y, MathS.Abs(z) <= Threshold ? 0f : z ); if (axis == Vector3.zero) { angle = 0; axisNormal = Vector3.zero; } else { float length = axis.magnitude; angle = MathS.Atan2(length, w) * 2 * MathS.RadToDeg; axisNormal = axis / length; } }
/// <summary> /// 欧拉角(单位四元数有效) /// </summary> /// <returns></returns> public Vector3 ToEulers() { Vector3 euler = Vector3.zero; Matrix m = ToMatrix(); if (MathS.Abs(m[1, 0]) <= Threshold && MathS.Abs(m[1, 1]) <= Threshold) { euler.x = m[1, 2] <= 0 ? 90 : -90; euler.y = 0; euler.z = MathS.Atan2(-m[0, 1], m[0, 0]) * MathS.RadToDeg; } else { euler.x = MathS.Asin(-m[1, 2]); euler.y = MathS.Atan2(m[0, 2], m[2, 2]); euler.z = MathS.Atan2(m[1, 0], m[1, 1]); euler *= MathS.RadToDeg; } return(euler); }