134 lines
3.9 KiB
C#
134 lines
3.9 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Models;
|
||
using Unity.VisualScripting;
|
||
using UnityEngine;
|
||
|
||
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()
|
||
{
|
||
CreateNodes();
|
||
|
||
_listener.AddHandler(OnReceive);
|
||
_listener.Connect(5000);
|
||
}
|
||
|
||
// Update is called once per frame
|
||
private void Update()
|
||
{
|
||
foreach (var poseTransform in _poseTransforms)
|
||
{
|
||
var index = (int)poseTransform.MediaPipeName;
|
||
|
||
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;
|
||
}
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
_listener.DisConnect();
|
||
}
|
||
|
||
private void CreateNodes()
|
||
{
|
||
for (var i = 0; i < 33; i++)
|
||
{
|
||
var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
||
|
||
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);
|
||
|
||
var poseTransform = new PoseTransform((PoseLandmarkType)i);
|
||
|
||
_poseTransforms.Add(poseTransform);
|
||
}
|
||
}
|
||
|
||
private void OnReceive(List<PoseLandmark> landmarks)
|
||
{
|
||
foreach (var landmark in landmarks)
|
||
{
|
||
var poseTransform = new PoseTransform(landmark.Type)
|
||
{
|
||
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;
|
||
}
|
||
}
|