add:三维旋转的三个方法;同时给不同部位上色方便区分

This commit is contained in:
Ichirinko 2023-02-22 19:51:22 +08:00
parent d699e357db
commit dba10abb29

View File

@ -10,6 +10,7 @@ public class MainBehaviour : MonoBehaviour
private readonly List<GameObject> _nodes = new List<GameObject>();
private readonly List<PoseTransform> _poseTransforms = new List<PoseTransform>();
private readonly UdpListener _listener = new UdpListener();
// Start is called before the first frame update
private void Start()
@ -26,7 +27,15 @@ private void Update()
foreach (var poseTransform in _poseTransforms)
{
var index = (int)poseTransform.MediaPipeName;
_nodes[index].transform.position = _poseTransforms[index].MediaPipePos * 5;
var temp = new Vector4(_poseTransforms[index].MediaPipePos.x,
_poseTransforms[index].MediaPipePos.y,
_poseTransforms[index].MediaPipePos.z,1);
//temp = RotateAxisZ(180) * temp;
//warning乘法的顺序不能改
_nodes[index].transform.position = new Vector3(temp.x, temp.y, temp.z) * 5;
//_nodes[index].transform.position = _poseTransforms[index].MediaPipePos * 5;
}
}
@ -41,7 +50,20 @@ private void CreateNodes()
{
var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere);
ball.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
ball.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
if (i <= 10)
{
ball.GetComponent<Renderer>().material.color = Color.red;//给头部添加颜色
}
else if (i > 10 && i <= 22)
{
ball.GetComponent<Renderer>().material.color = Color.blue;//给手部添加颜色
}
else if (i > 12 && i <= 32)
{
ball.GetComponent<Renderer>().material.color = Color.green;//给脚部添加颜色
}
_nodes.Add(ball);
@ -57,10 +79,55 @@ private void OnReceive(List<PoseLandmark> landmarks)
{
var poseTransform = new PoseTransform(landmark.Type)
{
MediaPipePos = new Vector3(landmark.X, -landmark.Y, landmark.Z)
MediaPipePos = new Vector3(landmark.X, landmark.Y, landmark.Z)
};
_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;
}
}