fix:将父节点的类型改为PoseLandmarkType

This commit is contained in:
Ichirinko 2023-02-28 22:59:38 +08:00
parent 2f9e21fe66
commit 87831fd869
2 changed files with 31 additions and 9 deletions

View File

@ -7,14 +7,14 @@ public class ModelBehaviour : MonoBehaviour
private Animator _animator; private Animator _animator;
private readonly UdpListener _listener = new UdpListener(); private readonly UdpListener _listener = new UdpListener();
private static readonly List<PoseTransform> PoseTransforms = new List<PoseTransform>(); private static readonly PoseTransform[] PoseTransforms = new PoseTransform[(int)PoseLandmarkType.MaxValue];
private void Start() private void Start()
{ {
_animator = GetComponent<Animator>(); // 获取动画控件 _animator = GetComponent<Animator>(); // 获取动画控件
InitPoseTransformList(); InitPoseTransformList();
_listener.AddHandler(LogLandmarks); //_listener.AddHandler(LogLandmarks);
_listener.AddHandler(RigPoint); _listener.AddHandler(RigPoint);
_listener.Connect(5000); _listener.Connect(5000);
} }
@ -28,8 +28,18 @@ private void Update()
{ {
if (landmark.UnityName != HumanBodyBones.LastBone) if (landmark.UnityName != HumanBodyBones.LastBone)
{ {
var transfrom = _animator.GetBoneTransform(landmark.UnityName);
_animator.GetBoneTransform(landmark.UnityName).rotation = landmark.CurrentQuaternion; var parentsum = new Vector3(0, 0, 0);
foreach (var parent in landmark.Parent)
{
parentsum += PoseTransforms[(int)parent].AveragePos;
}
parentsum /= landmark.Parent.Count;
transfrom.rotation = Quaternion.FromToRotation(transfrom.rotation.eulerAngles,landmark.AveragePos-parentsum);
} }
} }
@ -44,7 +54,7 @@ private void OnDisable()
private static void LogLandmarks(List<PoseLandmark> landmarks) private static void LogLandmarks(List<PoseLandmark> landmarks)
{ {
Debug.Log("Here");
foreach (var landmark in landmarks) foreach (var landmark in landmarks)
{ {
Debug.Log(landmark.ToString()); Debug.Log(landmark.ToString());
@ -59,6 +69,9 @@ private static void RigPoint(List<PoseLandmark> landmarks)
foreach (var landmark in landmarks) foreach (var landmark in landmarks)
{ {
PoseTransform.UpdatePosition(ref PoseTransforms[(int)landmark.Type], landmark);
Debug.Log(PoseTransforms[(int)landmark.Type].UnityName+":"+PoseTransforms[(int)landmark.Type].AveragePos);
} }
} }
@ -78,7 +91,7 @@ private void InitPoseTransformList()
item.CurrentQuaternion = item.PreviousQuaternion; item.CurrentQuaternion = item.PreviousQuaternion;
} }
PoseTransforms.Add(item); PoseTransforms[type] = item;
} }
// 在这里添加父节点 // 在这里添加父节点
@ -115,7 +128,7 @@ private void InitPoseTransformList()
private void AddParentTransform(PoseLandmarkType target, PoseLandmarkType parent) private void AddParentTransform(PoseLandmarkType target, PoseLandmarkType parent)
{ {
var parents = PoseTransforms[(int)target].Parent; var parents = PoseTransforms[(int)target].Parent;
parents.Add(PoseTransforms[(int)parent]); parents.Add(PoseTransforms[(int)parent].MediaPipeName);
} }
} }

View File

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using System; using System;
using System.Collections;
namespace Models namespace Models
{ {
@ -32,7 +33,7 @@ public struct PoseTransform
/// <summary> /// <summary>
/// 节点的父节点列表 /// 节点的父节点列表
/// </summary> /// </summary>
public readonly List<PoseTransform> Parent; public readonly List<PoseLandmarkType> Parent;
/// <summary> /// <summary>
/// 骨骼节点的初始角度 /// 骨骼节点的初始角度
@ -55,7 +56,7 @@ public PoseTransform(
MediaPipeName = type; MediaPipeName = type;
UnityName = GetRelatedBone(type); UnityName = GetRelatedBone(type);
AveragePos = new Vector3(); AveragePos = new Vector3();
Parent = new List<PoseTransform>(); Parent = new List<PoseLandmarkType>();
PreviousQuaternion = new Quaternion(); PreviousQuaternion = new Quaternion();
CurrentQuaternion = new Quaternion(); CurrentQuaternion = new Quaternion();
AverageLength = averageLength; AverageLength = averageLength;
@ -78,12 +79,20 @@ public static void UpdatePosition(ref PoseTransform pose, PoseLandmark landmark)
var sum = new Vector3(); var sum = new Vector3();
var parentsum = new Vector3(0f,0f,0f);
foreach (var poseLandmark in pose._landmarkQueue) foreach (var poseLandmark in pose._landmarkQueue)
{ {
sum += new Vector3(poseLandmark.X, poseLandmark.Y, poseLandmark.Z); sum += new Vector3(poseLandmark.X, poseLandmark.Y, poseLandmark.Z);
} }
pose.AveragePos = sum / AverageLength; pose.AveragePos = sum / AverageLength;
//pose.CurrentQuaternion = Quaternion.LookRotation(pose.AveragePos - parentsum).normalized;
} }
} }