122 lines
3.9 KiB
C#
122 lines
3.9 KiB
C#
using System.Collections.Generic;
|
|
using Models;
|
|
using UnityEngine;
|
|
|
|
public class ModelBehaviour : MonoBehaviour
|
|
{
|
|
private Animator _animator;
|
|
|
|
private readonly UdpListener _listener = new UdpListener();
|
|
private static readonly List<PoseTransform> PoseTransforms = new List<PoseTransform>();
|
|
|
|
private void Start()
|
|
{
|
|
_animator = GetComponent<Animator>(); // 获取动画控件
|
|
InitPoseTransformList();
|
|
|
|
_listener.AddHandler(LogLandmarks);
|
|
_listener.AddHandler(RigPoint);
|
|
_listener.Connect(5000);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
private void Update()
|
|
{
|
|
|
|
|
|
foreach (var landmark in PoseTransforms)
|
|
{
|
|
if (landmark.UnityName != HumanBodyBones.LastBone)
|
|
{
|
|
|
|
_animator.GetBoneTransform(landmark.UnityName).rotation = landmark.CurrentQuaternion;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
_listener.DisConnect();
|
|
}
|
|
|
|
private static void LogLandmarks(List<PoseLandmark> landmarks)
|
|
{
|
|
Debug.Log("Here");
|
|
foreach (var landmark in landmarks)
|
|
{
|
|
Debug.Log(landmark.ToString());
|
|
}
|
|
}
|
|
|
|
|
|
//获取传上来的数据点坐标转换成四元数的回调函数
|
|
private static void RigPoint(List<PoseLandmark> landmarks)
|
|
{
|
|
|
|
foreach (var landmark in landmarks)
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
|
|
PoseTransforms.Add(item);
|
|
}
|
|
|
|
// 在这里添加父节点
|
|
AddParentTransform(PoseLandmarkType.LeftShoulder, PoseLandmarkType.LeftHip);
|
|
AddParentTransform(PoseLandmarkType.LeftShoulder, PoseLandmarkType.RightHip);
|
|
|
|
AddParentTransform(PoseLandmarkType.RightShoulder, PoseLandmarkType.LeftHip);
|
|
AddParentTransform(PoseLandmarkType.RightShoulder, PoseLandmarkType.RightHip);
|
|
|
|
AddParentTransform(PoseLandmarkType.Nose, PoseLandmarkType.LeftShoulder);
|
|
AddParentTransform(PoseLandmarkType.Nose, PoseLandmarkType.RightShoulder);
|
|
|
|
AddParentTransform(PoseLandmarkType.LeftElbow, PoseLandmarkType.LeftShoulder);
|
|
AddParentTransform(PoseLandmarkType.RightElbow, PoseLandmarkType.RightShoulder);
|
|
|
|
AddParentTransform(PoseLandmarkType.LeftWrist, PoseLandmarkType.LeftElbow);
|
|
AddParentTransform(PoseLandmarkType.RightWrist, PoseLandmarkType.RightElbow);
|
|
|
|
AddParentTransform(PoseLandmarkType.LeftKnee, PoseLandmarkType.LeftHip);
|
|
AddParentTransform(PoseLandmarkType.RightKnee, PoseLandmarkType.RightHip);
|
|
|
|
AddParentTransform(PoseLandmarkType.LeftAnkle, PoseLandmarkType.LeftKnee);
|
|
AddParentTransform(PoseLandmarkType.RightAnkle, PoseLandmarkType.RightKnee);
|
|
|
|
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]);
|
|
}
|
|
|
|
}
|