diff --git a/Assets/ModelBehaviour.cs b/Assets/ModelBehaviour.cs index b1e633c..c153a57 100644 --- a/Assets/ModelBehaviour.cs +++ b/Assets/ModelBehaviour.cs @@ -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 _posLandmarks = new List(); + private static readonly List _posLandmarks = new List(); - void Start() + private static readonly List PoseTransforms = new List(); + + private void Start() { - animator = this.GetComponent(); // 获取动画控件 + _animator = GetComponent(); // 获取动画控件 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 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 landmarks) } + /// + /// 初始化骨骼变化结构体列表 + /// + 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); + } + + /// + /// 给指定的骨骼变化结构体添加父节点 + /// + /// 需要添加的节点 + /// 添加的父节点 + 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; diff --git a/Assets/Models/PoseTransform.cs b/Assets/Models/PoseTransform.cs new file mode 100644 index 0000000..d20e446 --- /dev/null +++ b/Assets/Models/PoseTransform.cs @@ -0,0 +1,108 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace Models +{ + /// + /// 捕捉骨骼的动作转换 + /// + public struct PoseTransform + { + /// + /// MediaPipe中动捕节点标识 + /// + public readonly PoseLandmarkType MediaPipeName; + + /// + /// Unity中绑定骨骼名称 + /// + public readonly HumanBodyBones UnityName; + + /// + /// MediaPipe捕捉空间坐标 + /// + public Vector3 MediaPipePos; + + /// + /// 节点的父节点列表 + /// + public readonly List Parent; + + /// + /// 骨骼节点的初始角度 + /// + public Quaternion PreviousQuaternion; + + /// + /// 骨骼节点的当前角度 + /// + public Quaternion CurrentQuaternion; + + public PoseTransform( + PoseLandmarkType type + ) + { + MediaPipeName = type; + UnityName = GetRelatedBone(type); + MediaPipePos = new Vector3(); + Parent = new List(); + PreviousQuaternion = new Quaternion(); + CurrentQuaternion = new Quaternion(); + } + + /// + /// 计算当前的旋转四元数 + /// + /// + public void CalculateCurrentQuaternion(PoseLandmark landmark) + { + + } + + /// + /// 获得同相关捕捉点关联的骨骼 + /// + /// 捕捉点的种类 + /// 关联骨骼的种类 + 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; + } + } + + + } +} \ No newline at end of file diff --git a/Assets/Models/PoseTransform.cs.meta b/Assets/Models/PoseTransform.cs.meta new file mode 100644 index 0000000..8447b13 --- /dev/null +++ b/Assets/Models/PoseTransform.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 9e38fe43bac14375a4f16690d139411f +timeCreated: 1676859644 \ No newline at end of file