MotionCapture/Assets/Utils/RotateUtils.cs

67 lines
2.0 KiB
C#
Raw Normal View History

using System;
using UnityEngine;
namespace Utils
{
/// <summary>
/// 旋转工具类
/// </summary>
public static class RotateUtils
{
private static Matrix4x4 _matrix;
/// <summary>
/// 获得绕X轴旋转矩阵
/// </summary>
/// <param name="degree">旋转的角度</param>
/// <returns></returns>
public static Matrix4x4 RotateAxisX(float degree)
{
var sin = MathF.Sin(degree);
var cos = MathF.Cos(degree);
_matrix.SetRow(0 ,new Vector4(1,0,0,0));
_matrix.SetRow(1, new Vector4(0, cos, -sin, 0));
_matrix.SetRow(2, new Vector4(0, sin, cos, 0));
_matrix.SetRow(3, new Vector4(0, 0, 0, 1));
return _matrix;
}
/// <summary>
/// 获得绕Y轴旋转矩阵
/// </summary>
/// <param name="degree">旋转的角度</param>
/// <returns></returns>
public static Matrix4x4 RotateAxisY(float degree)
{
var sin = MathF.Sin(degree);
var cos = MathF.Cos(degree);
_matrix.SetRow(0, new Vector4(cos, 0, sin, 0));
_matrix.SetRow(1, new Vector4(0, 1, 0, 1));
_matrix.SetRow(2, new Vector4(-sin, 0, cos, 0));
_matrix.SetRow(3, new Vector4(0, 0, 0, 1));
return _matrix;
}
/// <summary>
/// 获得绕Z轴旋转矩阵
/// </summary>
/// <param name="degree">旋转的角度</param>
/// <returns></returns>
public static Matrix4x4 RotateAxisZ(float degree)
{
var sin = MathF.Sin(degree);
var cos = MathF.Cos(degree);
_matrix.SetRow(0, new Vector4(cos, -sin, 0, 0));
_matrix.SetRow(1, new Vector4(sin, cos, 0, 0));
_matrix.SetRow(2, new Vector4(0, 0, 1, 0));
_matrix.SetRow(3, new Vector4(0, 0, 0, 1));
return _matrix;
}
}
}