From 17553850356539781be50211b5a35aad5ecb6a58 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 28 Feb 2023 12:23:15 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E5=B9=B3=E5=9D=87?= =?UTF-8?q?=E6=BB=A4=E6=B3=A2=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/MainBehaviour.cs | 51 ++++++++++------------------- Assets/ModelBehaviour.cs | 24 -------------- Assets/Models/PoseLandmarkType.cs | 3 +- Assets/Models/PoseTransform.cs | 54 ++++++++++++++++++++++++------- 4 files changed, 61 insertions(+), 71 deletions(-) diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs index 3c92bfc..00ff8bb 100644 --- a/Assets/MainBehaviour.cs +++ b/Assets/MainBehaviour.cs @@ -4,16 +4,17 @@ public class MainBehaviour : MonoBehaviour { - private readonly List _nodes = new List(); + private readonly GameObject[] _nodes = new GameObject[(int)PoseLandmarkType.MaxValue]; private readonly List _bonds = new List(); - private readonly List _virtualSkeletons = new List(); - private readonly List _poseTransforms = new List(); + private readonly PoseTransform[] _poseTransforms = new PoseTransform[(int)PoseLandmarkType.MaxValue]; private readonly UdpListener _listener = new UdpListener(); private const float Scale = 0.2f; // Start is called before the first frame update private void Start() { + PoseTransform.AverageLength = 3; + CreateNodes(); CreateBonds(); @@ -24,35 +25,33 @@ private void Start() // Update is called once per frame private void Update() { - for (var i = 0; i < 33; i++) + for (var i = 0; i < (int)PoseLandmarkType.MaxValue; i++) { - var temp = new Vector4(_poseTransforms[i].MediaPipePos.x, - _poseTransforms[i].MediaPipePos.y, - _poseTransforms[i].MediaPipePos.z, - 1); - - _nodes[i].transform.position = new Vector3(-temp.x, -temp.y, -temp.z) * 5; + _nodes[i].transform.position = _poseTransforms[i].AveragePos * -5; } foreach (var bond in _bonds) { bond.UpdateBond(); } - - /*foreach (var vs in _virtualSkeletons) - { - vs.UpdateVS(); - }*/ } private void OnDisable() { _listener.DisConnect(); } + + private void OnReceive(List landmarks) + { + foreach (var landmark in landmarks) + { + PoseTransform.UpdatePosition(ref _poseTransforms[(int)landmark.Type], landmark); + } + } private void CreateNodes() { - for (var i = 0; i < 33; i++) + for (var i = 0; i < (int)PoseLandmarkType.MaxValue; i++) { var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere); @@ -70,12 +69,9 @@ private void CreateNodes() { ball.GetComponent().material.color = Color.green;//给脚部添加颜色 } - - _nodes.Add(ball); - var poseTransform = new PoseTransform((PoseLandmarkType)i); - - _poseTransforms.Add(poseTransform); + _nodes[i] = ball; + _poseTransforms[i] = new PoseTransform((PoseLandmarkType)i); } } @@ -160,19 +156,6 @@ private void CreateBonds() _nodes[(int)PoseLandmarkType.RightHip], Scale)); } - - private void OnReceive(List landmarks) - { - foreach (var landmark in landmarks) - { - var poseTransform = new PoseTransform(landmark.Type) - { - MediaPipePos = new Vector3(landmark.X, landmark.Y, landmark.Z) - }; - - _poseTransforms[(int)landmark.Type] = poseTransform; - } - } /// /// 创建棍子列表 diff --git a/Assets/ModelBehaviour.cs b/Assets/ModelBehaviour.cs index c153a57..f7e640b 100644 --- a/Assets/ModelBehaviour.cs +++ b/Assets/ModelBehaviour.cs @@ -58,31 +58,7 @@ private static void RigPoint(List landmarks) foreach (var landmark in landmarks) { - 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; - //foreach (var parents in temp.parent) - //{ - // if (parents != null) - // { - // sum += parents.mediaPipePos; - // } - - - //} - - //var currentPos = sum / temp.parent.Count(); - - //temp.currentQ = Quaternion.Euler(temp.mediaPipePos - currentPos); - - //Debug.Log("Here!!"); } } diff --git a/Assets/Models/PoseLandmarkType.cs b/Assets/Models/PoseLandmarkType.cs index c32eda7..7b86f39 100644 --- a/Assets/Models/PoseLandmarkType.cs +++ b/Assets/Models/PoseLandmarkType.cs @@ -35,6 +35,7 @@ public enum PoseLandmarkType LeftHeel, RightHeel, LeftFootIndex, - RightFootIndex + RightFootIndex, + MaxValue } } \ No newline at end of file diff --git a/Assets/Models/PoseTransform.cs b/Assets/Models/PoseTransform.cs index f62c320..c2919b1 100644 --- a/Assets/Models/PoseTransform.cs +++ b/Assets/Models/PoseTransform.cs @@ -19,9 +19,14 @@ public struct PoseTransform public readonly HumanBodyBones UnityName; /// - /// MediaPipe捕捉空间坐标 + /// 取平均的长度 /// - public Vector3 MediaPipePos; + public static int AverageLength; + + /// + /// 取平均之后的结果 + /// + public Vector3 AveragePos; /// /// 节点的父节点列表 @@ -38,27 +43,52 @@ public struct PoseTransform /// public Quaternion CurrentQuaternion; + private int _count; + private readonly Queue _landmarkQueue; + public PoseTransform( - PoseLandmarkType type + PoseLandmarkType type, + int averageLength = 5 ) { MediaPipeName = type; UnityName = GetRelatedBone(type); - MediaPipePos = new Vector3(); + AveragePos = new Vector3(); Parent = new List(); PreviousQuaternion = new Quaternion(); CurrentQuaternion = new Quaternion(); + AverageLength = averageLength; + + _count = 0; + _landmarkQueue = new Queue(); } - /// - /// 计算当前的旋转四元数 - /// - /// - public void CalculateCurrentQuaternion(PoseLandmark landmark) + public static void UpdatePosition(ref PoseTransform pose, PoseLandmark landmark) { - MediaPipePos.x = landmark.X; - MediaPipePos.y = landmark.Y; - MediaPipePos.z = landmark.Z; + if (pose._count < AverageLength) + { + pose._landmarkQueue.Enqueue(landmark); + pose._count++; + } + else + { + pose._landmarkQueue.Dequeue(); + pose._landmarkQueue.Enqueue(landmark); + + var sum = new Vector3(); + + foreach (var poseLandmark in pose._landmarkQueue) + { + sum += new Vector3(poseLandmark.X, poseLandmark.Y, poseLandmark.Z); + } + + pose.AveragePos = sum / AverageLength; + } + } + + public static void CalculateRotation(PoseTransform poseTransform) + { + } ///