MotionCapture/Assets/ModelBehaviour.cs

155 lines
4.9 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using Models;
using UnityEngine;
public class ModelBehaviour : MonoBehaviour
{
private Animator _animator;
private readonly UdpListener _listener = new UdpListener();
2023-02-25 11:19:47 +08:00
//private static readonly List<PosTransformator> _posLandmarks = new List<PosTransformator>();
private static readonly List<PoseTransform> PoseTransforms = new List<PoseTransform>();
private void Start()
{
_animator = GetComponent<Animator>(); // 获取动画控件
2023-02-25 11:19:47 +08:00
InitPoseTransformList();
2023-02-25 11:19:47 +08:00
//_listener.AddHandler(LogLandmarks);
_listener.AddHandler(RigPoint);
_listener.Connect(5000);
}
// Update is called once per frame
private void Update()
{
2023-02-25 11:19:47 +08:00
foreach (var landmark in PoseTransforms)
{
2023-02-25 11:19:47 +08:00
if (landmark.UnityName != HumanBodyBones.LastBone)
{
_animator.GetBoneTransform(landmark.UnityName).rotation = landmark.CurrentQuaternion;
}
}
}
private void OnDisable()
{
_listener.DisConnect();
}
private static void LogLandmarks(List<PoseLandmark> landmarks)
{
foreach (var landmark in landmarks)
{
Debug.Log(landmark.ToString());
}
}
//获取传上来的数据点坐标转换成四元数的回调函数
private static void RigPoint(List<PoseLandmark> landmarks)
{
2023-02-25 11:19:47 +08:00
foreach (var landmark in landmarks)
{
2023-02-25 11:19:47 +08:00
var temp = PoseTransforms[(int)landmark.Type];
temp.CalculateCurrentQuaternion(landmark);
PoseTransforms[(int)landmark.Type] = temp;
//PoseTransforms[(int)landmark.Type].CalculateCurrentQuaternion(landmark);
// var temp = _posLandmarks.Find(t=>t.mediaPipeName == landmark.Type.ToString());
// var sum = new Vector3(0,0,0);
//var mediatemp = new Vector3(landmark.X, landmark.Y, landmark.Z);
//temp.mediaPipePos = new Vector3(landmark.X, landmark.Y, landmark.Z);
//temp.mediaPipePos.x = landmark.X;
//temp.mediaPipePos.y = landmark.Y;
//temp.mediaPipePos.z = landmark.Z;
//foreach (var parents in temp.parent)
//{
// if (parents != null)
// {
// sum += parents.mediaPipePos;
// }
//}
//var currentPos = sum / temp.parent.Count();
//temp.currentQ = Quaternion.Euler(temp.mediaPipePos - currentPos);
//Debug.Log("Here!!");
}
}
/// <summary>
/// 初始化骨骼变化结构体列表
/// </summary>
private void InitPoseTransformList()
{
for (var type = 0; type <= (int)PoseLandmarkType.RightFootIndex; type++)
{
var item = new PoseTransform((PoseLandmarkType)type);
if (item.UnityName != HumanBodyBones.LastBone)
{
item.PreviousQuaternion = _animator.GetBoneTransform(item.UnityName).rotation;
item.CurrentQuaternion = item.PreviousQuaternion;
}
2023-02-25 11:19:47 +08:00
PoseTransforms.Add(item);
}
2023-02-25 11:19:47 +08:00
// 在这里添加父节点
AddParentTransform(PoseLandmarkType.LeftShoulder, PoseLandmarkType.LeftHip);
AddParentTransform(PoseLandmarkType.LeftShoulder, PoseLandmarkType.RightHip);
AddParentTransform(PoseLandmarkType.RightShoulder, PoseLandmarkType.LeftHip);
AddParentTransform(PoseLandmarkType.RightShoulder, PoseLandmarkType.RightHip);
2023-02-25 11:19:47 +08:00
AddParentTransform(PoseLandmarkType.Nose, PoseLandmarkType.LeftShoulder);
AddParentTransform(PoseLandmarkType.Nose, PoseLandmarkType.RightShoulder);
2023-02-25 11:19:47 +08:00
AddParentTransform(PoseLandmarkType.LeftElbow, PoseLandmarkType.LeftShoulder);
AddParentTransform(PoseLandmarkType.RightElbow, PoseLandmarkType.RightShoulder);
2023-02-25 11:19:47 +08:00
AddParentTransform(PoseLandmarkType.LeftWrist, PoseLandmarkType.LeftElbow);
AddParentTransform(PoseLandmarkType.RightWrist, PoseLandmarkType.RightElbow);
2023-02-25 11:19:47 +08:00
AddParentTransform(PoseLandmarkType.LeftKnee, PoseLandmarkType.LeftHip);
AddParentTransform(PoseLandmarkType.RightKnee, PoseLandmarkType.RightHip);
2023-02-25 11:19:47 +08:00
AddParentTransform(PoseLandmarkType.LeftAnkle, PoseLandmarkType.LeftKnee);
AddParentTransform(PoseLandmarkType.RightAnkle, PoseLandmarkType.RightKnee);
2023-02-25 11:19:47 +08:00
AddParentTransform(PoseLandmarkType.LeftFootIndex, PoseLandmarkType.LeftAnkle);
AddParentTransform(PoseLandmarkType.RightFootIndex, PoseLandmarkType.RightAnkle);
}
/// <summary>
/// 给指定的骨骼变化结构体添加父节点
/// </summary>
/// <param name="target">需要添加的节点</param>
/// <param name="parent">添加的父节点</param>
private void AddParentTransform(PoseLandmarkType target, PoseLandmarkType parent)
{
var parents = PoseTransforms[(int)target].Parent;
parents.Add(PoseTransforms[(int)parent]);
}
2023-02-25 11:19:47 +08:00
}