2023-02-19 20:10:57 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using Models;
|
|
|
|
using UnityEngine;
|
2023-03-01 22:07:39 +08:00
|
|
|
using Utils.PoseTransformHandlers;
|
2023-02-19 20:10:57 +08:00
|
|
|
|
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-28 23:35:49 +08:00
|
|
|
private static readonly PoseTransform[] PoseTransforms = new PoseTransform[PoseLandmarkType.MaxValue];
|
2023-02-20 11:34:21 +08:00
|
|
|
|
|
|
|
private void Start()
|
2023-02-19 20:10:57 +08:00
|
|
|
{
|
2023-02-20 11:34:21 +08:00
|
|
|
_animator = GetComponent<Animator>(); // 获取动画控件
|
|
|
|
InitPoseTransformList();
|
|
|
|
|
2023-02-28 22:59:38 +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)
|
|
|
|
{
|
2023-02-28 22:59:38 +08:00
|
|
|
var transfrom = _animator.GetBoneTransform(landmark.UnityName);
|
|
|
|
|
|
|
|
var parentsum = new Vector3(0, 0, 0);
|
|
|
|
|
|
|
|
foreach (var parent in landmark.Parent)
|
|
|
|
{
|
2023-03-01 22:07:39 +08:00
|
|
|
parentsum += PoseTransforms[parent.Value].ResultPosition;
|
2023-02-28 22:59:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
parentsum /= landmark.Parent.Count;
|
2023-02-25 11:19:47 +08:00
|
|
|
|
2023-03-01 22:07:39 +08:00
|
|
|
transfrom.rotation = Quaternion.FromToRotation(transfrom.rotation.eulerAngles,landmark.ResultPosition-parentsum);
|
2023-02-28 22:59:38 +08:00
|
|
|
|
2023-02-25 11:19:47 +08:00
|
|
|
}
|
2023-02-19 20:10:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
{
|
|
|
|
_listener.DisConnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void LogLandmarks(List<PoseLandmark> landmarks)
|
|
|
|
{
|
2023-02-28 22:59:38 +08:00
|
|
|
|
2023-02-19 20:10:57 +08:00
|
|
|
foreach (var landmark in landmarks)
|
|
|
|
{
|
|
|
|
Debug.Log(landmark.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//获取传上来的数据点坐标转换成四元数的回调函数
|
|
|
|
private static void RigPoint(List<PoseLandmark> landmarks)
|
|
|
|
{
|
|
|
|
|
|
|
|
foreach (var landmark in landmarks)
|
|
|
|
{
|
2023-02-28 12:55:21 +08:00
|
|
|
|
2023-02-28 23:35:49 +08:00
|
|
|
PoseTransform.UpdatePosition(ref PoseTransforms[landmark.Type.Value], landmark);
|
2023-02-28 22:59:38 +08:00
|
|
|
|
2023-03-01 22:07:39 +08:00
|
|
|
Debug.Log(PoseTransforms[landmark.Type.Value].UnityName+":"+PoseTransforms[landmark.Type.Value].ResultPosition);
|
2023-02-19 20:10:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 初始化骨骼变化结构体列表
|
|
|
|
/// </summary>
|
|
|
|
private void InitPoseTransformList()
|
|
|
|
{
|
2023-02-28 23:35:49 +08:00
|
|
|
for (var type = 0; type <= PoseLandmarkType.RightFootIndex; type++)
|
2023-02-20 11:34:21 +08:00
|
|
|
{
|
2023-03-01 22:07:39 +08:00
|
|
|
var item = new PoseTransform(type, new NormalHandler());
|
2023-02-20 11:34:21 +08:00
|
|
|
|
|
|
|
if (item.UnityName != HumanBodyBones.LastBone)
|
|
|
|
{
|
|
|
|
item.PreviousQuaternion = _animator.GetBoneTransform(item.UnityName).rotation;
|
|
|
|
item.CurrentQuaternion = item.PreviousQuaternion;
|
|
|
|
}
|
|
|
|
|
2023-02-28 22:59:38 +08:00
|
|
|
PoseTransforms[type] = item;
|
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);
|
|
|
|
|
|
|
|
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>
|
2023-02-28 23:35:49 +08:00
|
|
|
private void AddParentTransform(int target, int parent)
|
2023-02-20 11:34:21 +08:00
|
|
|
{
|
2023-02-28 23:35:49 +08:00
|
|
|
var parents = PoseTransforms[target].Parent;
|
|
|
|
parents.Add(PoseTransforms[parent].MediaPipeName);
|
2023-02-20 11:34:21 +08:00
|
|
|
}
|
|
|
|
|
2023-02-25 11:19:47 +08:00
|
|
|
}
|