refact: 将旋转矩阵的计算抽象为旋转工具类
This commit is contained in:
parent
31ce446737
commit
7199424893
|
@ -88,9 +88,7 @@ private void CreateNodes()
|
||||||
|
|
||||||
private void CreateBonds()
|
private void CreateBonds()
|
||||||
{
|
{
|
||||||
//shit code,it's my fault
|
var temp = new Bonds(_nodes[0], _nodes[4], _scale);
|
||||||
Bonds temp;
|
|
||||||
temp = new Bonds(_nodes[0], _nodes[4], _scale);
|
|
||||||
_bonds.Add(temp);
|
_bonds.Add(temp);
|
||||||
|
|
||||||
temp = new Bonds(_nodes[0], _nodes[1], _scale);
|
temp = new Bonds(_nodes[0], _nodes[1], _scale);
|
||||||
|
@ -219,49 +217,4 @@ private void OnReceive(List<PoseLandmark> landmarks)
|
||||||
_poseTransforms[(int)landmark.Type] = poseTransform;
|
_poseTransforms[(int)landmark.Type] = poseTransform;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//绕X轴旋转
|
|
||||||
private Matrix4x4 RotateAxisX(float rot)
|
|
||||||
{
|
|
||||||
var matrix = new Matrix4x4();
|
|
||||||
float cosrot = MathF.Cos(rot);
|
|
||||||
float sinrot = MathF.Sin(rot);
|
|
||||||
|
|
||||||
matrix.SetRow(0, new Vector4(1,0,0,0));
|
|
||||||
matrix.SetRow(1, new Vector4(0,cosrot,-sinrot,0));
|
|
||||||
matrix.SetRow(2, new Vector4(0,sinrot,cosrot,0));
|
|
||||||
matrix.SetRow(3, new Vector4(0,0,0,1));
|
|
||||||
|
|
||||||
return matrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
//绕Y轴旋转
|
|
||||||
private Matrix4x4 RotateAxisY(float rot)
|
|
||||||
{
|
|
||||||
var matrix = new Matrix4x4();
|
|
||||||
float cosrot = MathF.Cos(rot);
|
|
||||||
float sinrot = MathF.Sin(rot);
|
|
||||||
|
|
||||||
matrix.SetRow(0, new Vector4(cosrot,0,sinrot,0));
|
|
||||||
matrix.SetRow(1, new Vector4(0,1,0,0));
|
|
||||||
matrix.SetRow(2, new Vector4(-sinrot,0,cosrot,0));
|
|
||||||
matrix.SetRow(3, new Vector4(0,0,0,1));
|
|
||||||
|
|
||||||
return matrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
//绕Z轴旋转
|
|
||||||
private Matrix4x4 RotateAxisZ(float rot)
|
|
||||||
{
|
|
||||||
var matrix = new Matrix4x4();
|
|
||||||
float cosrot = MathF.Cos(rot);
|
|
||||||
float sinrot = MathF.Sin(rot);
|
|
||||||
|
|
||||||
matrix.SetRow(0, new Vector4(cosrot,-sinrot,0,0));
|
|
||||||
matrix.SetRow(1, new Vector4(sinrot,cosrot,0,0));
|
|
||||||
matrix.SetRow(2, new Vector4(0,0,1,0));
|
|
||||||
matrix.SetRow(3, new Vector4(0,0,0,1));
|
|
||||||
|
|
||||||
return matrix;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
67
Assets/Utils/RotateUtils.cs
Normal file
67
Assets/Utils/RotateUtils.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
Assets/Utils/RotateUtils.cs.meta
Normal file
3
Assets/Utils/RotateUtils.cs.meta
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d748c6669d714e65a5ca9164c5798583
|
||||||
|
timeCreated: 1677319275
|
Loading…
Reference in New Issue
Block a user