refact: 将旋转矩阵的计算抽象为旋转工具类

This commit is contained in:
2023-02-26 23:50:14 +08:00
parent 31ce446737
commit 7199424893
3 changed files with 71 additions and 48 deletions

View File

@@ -0,0 +1,67 @@
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;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d748c6669d714e65a5ca9164c5798583
timeCreated: 1677319275