refactor: 使用struct重构PoseTransform

This commit is contained in:
jackfiled 2023-02-20 11:34:21 +08:00
parent baea96db48
commit 2d19970492
3 changed files with 184 additions and 23 deletions

View File

@ -1,39 +1,31 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;
using UnityEngine;
public class ModelBehaviour : MonoBehaviour
{
Animator animator;
private Animator _animator;
private readonly UdpListener _listener = new UdpListener();
private static List<PosTransformator> _posLandmarks = new List<PosTransformator>();
private static readonly List<PosTransformator> _posLandmarks = new List<PosTransformator>();
void Start()
private static readonly List<PoseTransform> PoseTransforms = new List<PoseTransform>();
private void Start()
{
animator = this.GetComponent<Animator>(); // 获取动画控件
_animator = GetComponent<Animator>(); // 获取动画控件
TransformatorInit(); //匹配初始化
InitPoseTransformList();
_listener.AddHandler(LogLandmarks);
_listener.AddHandler(RigPoint);
_listener.Connect(5000);
}
// Update is called once per frame
void Update()
private void Update()
{
@ -66,13 +58,16 @@ private static void RigPoint(List<PoseLandmark> landmarks)
foreach (var landmark in landmarks)
{
var temp = _posLandmarks.Find(t=>t.mediaPipeName == landmark.Type.ToString());
var sum = new Vector3(0,0,0);
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;
//temp.mediaPipePos.x = landmark.X;
//temp.mediaPipePos.y = landmark.Y;
//temp.mediaPipePos.z = landmark.Z;
//foreach (var parents in temp.parent)
//{
// if (parents != null)
@ -92,6 +87,61 @@ private static void RigPoint(List<PoseLandmark> 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]);
}
private void TransformatorInit()
{
@ -254,7 +304,7 @@ private void TransformatorInit()
foreach (var temp in transformator)
{
temp.prevQ = animator.GetBoneTransform(temp.unityName).rotation; //获得初始角度
temp.prevQ = _animator.GetBoneTransform(temp.unityName).rotation; //获得初始角度
temp.currentQ = temp.prevQ;

View File

@ -0,0 +1,108 @@
using System.Collections.Generic;
using UnityEngine;
namespace Models
{
/// <summary>
/// 捕捉骨骼的动作转换
/// </summary>
public struct PoseTransform
{
/// <summary>
/// MediaPipe中动捕节点标识
/// </summary>
public readonly PoseLandmarkType MediaPipeName;
/// <summary>
/// Unity中绑定骨骼名称
/// </summary>
public readonly HumanBodyBones UnityName;
/// <summary>
/// MediaPipe捕捉空间坐标
/// </summary>
public Vector3 MediaPipePos;
/// <summary>
/// 节点的父节点列表
/// </summary>
public readonly List<PoseTransform> Parent;
/// <summary>
/// 骨骼节点的初始角度
/// </summary>
public Quaternion PreviousQuaternion;
/// <summary>
/// 骨骼节点的当前角度
/// </summary>
public Quaternion CurrentQuaternion;
public PoseTransform(
PoseLandmarkType type
)
{
MediaPipeName = type;
UnityName = GetRelatedBone(type);
MediaPipePos = new Vector3();
Parent = new List<PoseTransform>();
PreviousQuaternion = new Quaternion();
CurrentQuaternion = new Quaternion();
}
/// <summary>
/// 计算当前的旋转四元数
/// </summary>
/// <param name="landmark"></param>
public void CalculateCurrentQuaternion(PoseLandmark landmark)
{
}
/// <summary>
/// 获得同相关捕捉点关联的骨骼
/// </summary>
/// <param name="type">捕捉点的种类</param>
/// <returns>关联骨骼的种类</returns>
private static HumanBodyBones GetRelatedBone(PoseLandmarkType type)
{
switch (type)
{
case PoseLandmarkType.LeftHip:
return HumanBodyBones.LeftUpperLeg;
case PoseLandmarkType.RightHip:
return HumanBodyBones.RightUpperLeg;
case PoseLandmarkType.LeftShoulder:
return HumanBodyBones.LeftUpperArm;
case PoseLandmarkType.RightShoulder:
return HumanBodyBones.RightUpperArm;
case PoseLandmarkType.Nose:
return HumanBodyBones.Head;
case PoseLandmarkType.LeftElbow:
return HumanBodyBones.LeftLowerArm;
case PoseLandmarkType.RightElbow:
return HumanBodyBones.RightLowerArm;
case PoseLandmarkType.LeftWrist:
return HumanBodyBones.LeftHand;
case PoseLandmarkType.RightWrist:
return HumanBodyBones.RightHand;
case PoseLandmarkType.LeftKnee:
return HumanBodyBones.LeftLowerLeg;
case PoseLandmarkType.RightKnee:
return HumanBodyBones.RightLowerLeg;
case PoseLandmarkType.LeftAnkle:
return HumanBodyBones.LeftFoot;
case PoseLandmarkType.RightAnkle:
return HumanBodyBones.RightFoot;
case PoseLandmarkType.LeftFootIndex:
return HumanBodyBones.LeftToes;
case PoseLandmarkType.RightFootIndex:
return HumanBodyBones.RightToes;
default:
return HumanBodyBones.LastBone;
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9e38fe43bac14375a4f16690d139411f
timeCreated: 1676859644