2023-02-19 20:10:57 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using Models;
|
|
|
|
using UnityEngine;
|
|
|
|
|
2023-02-19 22:35:47 +08:00
|
|
|
public class ModelBehaviour : MonoBehaviour
|
2023-02-19 20:10:57 +08:00
|
|
|
{
|
2023-02-20 11:34:21 +08:00
|
|
|
private Animator _animator;
|
2023-02-19 20:10:57 +08:00
|
|
|
|
|
|
|
private readonly UdpListener _listener = new UdpListener();
|
|
|
|
|
2023-02-25 11:19:47 +08:00
|
|
|
//private static readonly List<PosTransformator> _posLandmarks = new List<PosTransformator>();
|
2023-02-19 20:10:57 +08:00
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
private static readonly List<PoseTransform> PoseTransforms = new List<PoseTransform>();
|
|
|
|
|
|
|
|
private void Start()
|
2023-02-19 20:10:57 +08:00
|
|
|
{
|
2023-02-20 11:34:21 +08:00
|
|
|
_animator = GetComponent<Animator>(); // 获取动画控件
|
2023-02-19 20:10:57 +08:00
|
|
|
|
2023-02-25 11:19:47 +08:00
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
InitPoseTransformList();
|
|
|
|
|
2023-02-25 11:19:47 +08:00
|
|
|
//_listener.AddHandler(LogLandmarks);
|
2023-02-19 20:10:57 +08:00
|
|
|
_listener.AddHandler(RigPoint);
|
|
|
|
_listener.Connect(5000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
2023-02-20 11:34:21 +08:00
|
|
|
private void Update()
|
2023-02-19 20:10:57 +08:00
|
|
|
{
|
|
|
|
|
2023-02-25 11:19:47 +08:00
|
|
|
|
|
|
|
foreach (var landmark in PoseTransforms)
|
2023-02-19 20:10:57 +08:00
|
|
|
{
|
2023-02-25 11:19:47 +08:00
|
|
|
if (landmark.UnityName != HumanBodyBones.LastBone)
|
|
|
|
{
|
|
|
|
|
|
|
|
_animator.GetBoneTransform(landmark.UnityName).rotation = landmark.CurrentQuaternion;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-19 20:10:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-02-19 20:10:57 +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);
|
|
|
|
|
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
// var temp = _posLandmarks.Find(t=>t.mediaPipeName == landmark.Type.ToString());
|
|
|
|
// var sum = new Vector3(0,0,0);
|
2023-02-19 20:10:57 +08:00
|
|
|
//var mediatemp = new Vector3(landmark.X, landmark.Y, landmark.Z);
|
|
|
|
//temp.mediaPipePos = new Vector3(landmark.X, landmark.Y, landmark.Z);
|
2023-02-20 11:34:21 +08:00
|
|
|
//temp.mediaPipePos.x = landmark.X;
|
|
|
|
//temp.mediaPipePos.y = landmark.Y;
|
|
|
|
//temp.mediaPipePos.z = landmark.Z;
|
2023-02-19 20:10:57 +08:00
|
|
|
//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!!");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
/// <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
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
PoseTransforms.Add(item);
|
|
|
|
}
|
2023-02-25 11:19:47 +08:00
|
|
|
|
2023-02-20 11:34:21 +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
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
AddParentTransform(PoseLandmarkType.Nose, PoseLandmarkType.LeftShoulder);
|
|
|
|
AddParentTransform(PoseLandmarkType.Nose, PoseLandmarkType.RightShoulder);
|
2023-02-25 11:19:47 +08:00
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
AddParentTransform(PoseLandmarkType.LeftElbow, PoseLandmarkType.LeftShoulder);
|
|
|
|
AddParentTransform(PoseLandmarkType.RightElbow, PoseLandmarkType.RightShoulder);
|
2023-02-25 11:19:47 +08:00
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
AddParentTransform(PoseLandmarkType.LeftWrist, PoseLandmarkType.LeftElbow);
|
|
|
|
AddParentTransform(PoseLandmarkType.RightWrist, PoseLandmarkType.RightElbow);
|
2023-02-25 11:19:47 +08:00
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
AddParentTransform(PoseLandmarkType.LeftKnee, PoseLandmarkType.LeftHip);
|
|
|
|
AddParentTransform(PoseLandmarkType.RightKnee, PoseLandmarkType.RightHip);
|
2023-02-25 11:19:47 +08:00
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
AddParentTransform(PoseLandmarkType.LeftAnkle, PoseLandmarkType.LeftKnee);
|
|
|
|
AddParentTransform(PoseLandmarkType.RightAnkle, PoseLandmarkType.RightKnee);
|
2023-02-25 11:19:47 +08:00
|
|
|
|
2023-02-20 11:34:21 +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
|
|
|
}
|