136 lines
4.5 KiB
C#
136 lines
4.5 KiB
C#
using System.Collections.Generic;
|
|
using Models;
|
|
using UnityEngine;
|
|
using Utils.PoseTransformHandlers;
|
|
|
|
public class ModelBehaviour : MonoBehaviour
|
|
{
|
|
private Animator _animator;
|
|
|
|
private readonly UdpListener _listener = new UdpListener();
|
|
private static readonly PoseTransform[] PoseTransforms = new PoseTransform[PoseLandmarkType.MaxValue];
|
|
|
|
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)
|
|
{
|
|
var transfrom = _animator.GetBoneTransform(landmark.UnityName);
|
|
|
|
var parentsum = new Vector3(0, 0, 0);
|
|
|
|
foreach (var parent in landmark.Parent)
|
|
{
|
|
parentsum += PoseTransforms[parent.Value].ResultPosition;
|
|
}
|
|
|
|
parentsum /= landmark.Parent.Count;
|
|
|
|
transfrom.rotation = Quaternion.FromToRotation(transfrom.rotation.eulerAngles,landmark.ResultPosition-parentsum);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
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)
|
|
{
|
|
|
|
foreach (var landmark in landmarks)
|
|
{
|
|
|
|
PoseTransform.UpdatePosition(ref PoseTransforms[landmark.Type.Value], landmark);
|
|
|
|
Debug.Log(PoseTransforms[landmark.Type.Value].UnityName+":"+PoseTransforms[landmark.Type.Value].ResultPosition);
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化骨骼变化结构体列表
|
|
/// </summary>
|
|
private void InitPoseTransformList()
|
|
{
|
|
for (var type = 0; type <= PoseLandmarkType.RightFootIndex; type++)
|
|
{
|
|
var item = new PoseTransform(type, new NormalHandler());
|
|
|
|
if (item.UnityName != HumanBodyBones.LastBone)
|
|
{
|
|
item.PreviousQuaternion = _animator.GetBoneTransform(item.UnityName).rotation;
|
|
item.CurrentQuaternion = item.PreviousQuaternion;
|
|
}
|
|
|
|
PoseTransforms[type] = 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(int target, int parent)
|
|
{
|
|
var parents = PoseTransforms[target].Parent;
|
|
parents.Add(PoseTransforms[parent].MediaPipeName);
|
|
}
|
|
|
|
}
|