From d901880eeaab4d6a534b6a87afb6e1d564216307 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 27 Mar 2023 13:10:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=96=B0=E8=A7=84?= =?UTF-8?q?=E5=88=92=E4=BA=86=E9=A1=B9=E7=9B=AE=E7=BB=93=E6=9E=84=20?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BA=86=E5=86=97=E4=BD=99=E7=9A=84=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BallStickBehaviour.cs} | 0 .../BallStickBehaviour.cs.meta} | 0 Assets/{ => Behaviours}/CameraBehaviour.cs | 0 .../{ => Behaviours}/CameraBehaviour.cs.meta | 0 Assets/ModelBehaviour.cs | 135 --------- Assets/ModelBehaviour.cs.meta | 11 - Assets/Models/PoseLandmark.cs | 262 +++++++++--------- Assets/Models/PoseTransform.cs | 18 -- Assets/Scenes/SampleScene.unity | 16 +- 9 files changed, 139 insertions(+), 303 deletions(-) rename Assets/{MainBehaviour.cs => Behaviours/BallStickBehaviour.cs} (100%) rename Assets/{MainBehaviour.cs.meta => Behaviours/BallStickBehaviour.cs.meta} (100%) rename Assets/{ => Behaviours}/CameraBehaviour.cs (100%) rename Assets/{ => Behaviours}/CameraBehaviour.cs.meta (100%) delete mode 100644 Assets/ModelBehaviour.cs delete mode 100644 Assets/ModelBehaviour.cs.meta diff --git a/Assets/MainBehaviour.cs b/Assets/Behaviours/BallStickBehaviour.cs similarity index 100% rename from Assets/MainBehaviour.cs rename to Assets/Behaviours/BallStickBehaviour.cs diff --git a/Assets/MainBehaviour.cs.meta b/Assets/Behaviours/BallStickBehaviour.cs.meta similarity index 100% rename from Assets/MainBehaviour.cs.meta rename to Assets/Behaviours/BallStickBehaviour.cs.meta diff --git a/Assets/CameraBehaviour.cs b/Assets/Behaviours/CameraBehaviour.cs similarity index 100% rename from Assets/CameraBehaviour.cs rename to Assets/Behaviours/CameraBehaviour.cs diff --git a/Assets/CameraBehaviour.cs.meta b/Assets/Behaviours/CameraBehaviour.cs.meta similarity index 100% rename from Assets/CameraBehaviour.cs.meta rename to Assets/Behaviours/CameraBehaviour.cs.meta diff --git a/Assets/ModelBehaviour.cs b/Assets/ModelBehaviour.cs deleted file mode 100644 index 908fe6e..0000000 --- a/Assets/ModelBehaviour.cs +++ /dev/null @@ -1,135 +0,0 @@ -using System.Collections.Generic; -using Models; -using UnityEngine; -using Utils.PoseTransformHandlers; - -public class ModelBehaviour : MonoBehaviour -{ - private Animator _animator; - - private readonly UdpListener _listener = new UdpListener(); - private static readonly PoseTransform[] PoseTransforms = new PoseTransform[PoseLandmarkType.MaxValue]; - - private void Start() - { - _animator = GetComponent(); // 获取动画控件 - InitPoseTransformList(); - - //_listener.AddHandler(LogLandmarks); - _listener.AddHandler(RigPoint); - _listener.Connect(5000); - } - - // Update is called once per frame - private void Update() - { - - - foreach (var landmark in PoseTransforms) - { - if (landmark.UnityName != HumanBodyBones.LastBone) - { - var transfrom = _animator.GetBoneTransform(landmark.UnityName); - - var parentsum = new Vector3(0, 0, 0); - - foreach (var parent in landmark.Parent) - { - parentsum += PoseTransforms[parent.Value].ResultPosition; - } - - parentsum /= landmark.Parent.Count; - - transfrom.rotation = Quaternion.FromToRotation(transfrom.rotation.eulerAngles,landmark.ResultPosition-parentsum); - - } - } - - - } - - private void OnDisable() - { - _listener.DisConnect(); - } - - private static void LogLandmarks(List landmarks) - { - - foreach (var landmark in landmarks) - { - Debug.Log(landmark.ToString()); - } - } - - - //获取传上来的数据点坐标转换成四元数的回调函数 - private static void RigPoint(List landmarks) - { - - foreach (var landmark in landmarks) - { - - PoseTransform.UpdatePosition(ref PoseTransforms[landmark.Type.Value], landmark); - - Debug.Log(PoseTransforms[landmark.Type.Value].UnityName+":"+PoseTransforms[landmark.Type.Value].ResultPosition); - } - - } - - /// - /// 初始化骨骼变化结构体列表 - /// - private void InitPoseTransformList() - { - for (var type = 0; type <= PoseLandmarkType.RightFootIndex; type++) - { - var item = new PoseTransform(type, new NormalHandler()); - - if (item.UnityName != HumanBodyBones.LastBone) - { - item.PreviousQuaternion = _animator.GetBoneTransform(item.UnityName).rotation; - item.CurrentQuaternion = item.PreviousQuaternion; - } - - PoseTransforms[type] = 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(int target, int parent) - { - var parents = PoseTransforms[target].Parent; - parents.Add(PoseTransforms[parent].MediaPipeName); - } - -} diff --git a/Assets/ModelBehaviour.cs.meta b/Assets/ModelBehaviour.cs.meta deleted file mode 100644 index 295604d..0000000 --- a/Assets/ModelBehaviour.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: a2b30b6aa6e08c4468a02e1fcabcd485 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Models/PoseLandmark.cs b/Assets/Models/PoseLandmark.cs index 45608f0..a68a987 100644 --- a/Assets/Models/PoseLandmark.cs +++ b/Assets/Models/PoseLandmark.cs @@ -7,154 +7,154 @@ namespace Models public class PoseLandmark { private const double Tolerance = 1E-6; - - /// - /// 单个包的长度 - /// - public const int PacketLength = 28; - - /// - /// 坐标点的种类 - /// - public PoseLandmarkType Type { get; } - - /// - /// 真实世界x坐标 - /// 以米为单位 - /// 以臀部为重心 hip - /// - public float X { get; } - - /// - /// 真实世界y坐标 - /// 以米为单位 - /// 以臀部为重心 - /// - public float Y { get; } - - /// - /// 真实世界z坐标 - /// 以米为单位 - /// 以臀部为重心 - /// - public float Z { get; } - - /// - /// 该坐标点估计的可见性 - /// [0,1] - /// - public float Visibility { get; } - - public long TimeStamp { get; } - public PoseLandmark(int type, float x, float y, float z, float visibility, long timeStamp) - { - Type = new PoseLandmarkType(type); - X = x; - Y = y; - Z = z; - Visibility = visibility; - TimeStamp = timeStamp; - } + /// + /// 单个包的长度 + /// + public const int PacketLength = 28; - /// - /// 转换成字节数组 - /// 便于UDP发送 - /// - /// - public byte[] ToByteArray() - { - var result = new byte[PacketLength]; + /// + /// 坐标点的种类 + /// + public PoseLandmarkType Type { get; } - BitConverter.GetBytes(Type.Value).CopyTo(result, 0); - BitConverter.GetBytes(X).CopyTo(result, 4); - BitConverter.GetBytes(Y).CopyTo(result, 8); - BitConverter.GetBytes(Z).CopyTo(result, 12); - BitConverter.GetBytes(Visibility).CopyTo(result, 16); - BitConverter.GetBytes(TimeStamp).CopyTo(result, 20); - - return result; - } + /// + /// 真实世界x坐标 + /// 以米为单位 + /// 以臀部为重心 hip + /// + public float X { get; } - /// - /// 解析字节数组 - /// - /// 收到的字节数组 - /// 字节数组中代表的坐标对象 - public static PoseLandmark ValueOf(byte[] bytes) - { - var result = new PoseLandmark( - BitConverter.ToInt32(bytes, 0), - BitConverter.ToSingle(bytes, 4), - BitConverter.ToSingle(bytes, 8), - BitConverter.ToSingle(bytes, 12), - BitConverter.ToSingle(bytes, 16), - BitConverter.ToInt64(bytes, 20)); + /// + /// 真实世界y坐标 + /// 以米为单位 + /// 以臀部为重心 + /// + public float Y { get; } - return result; - } + /// + /// 真实世界z坐标 + /// 以米为单位 + /// 以臀部为重心 + /// + public float Z { get; } - /// - /// 解析字节数组为对象列表 - /// 单个 - /// - /// 字节数组 - /// - public static List ArrayOf(byte[] bytes) - { - var result = new List(); + /// + /// 该坐标点估计的可见性 + /// [0,1] + /// + public float Visibility { get; } - for (var i = 0; i < bytes.Length; i = i + PacketLength) + public long TimeStamp { get; } + + public PoseLandmark(int type, float x, float y, float z, float visibility, long timeStamp) { - var landmark = new PoseLandmark(BitConverter.ToInt32(bytes, i), - BitConverter.ToSingle(bytes, i + 4), - BitConverter.ToSingle(bytes, i + 8), - BitConverter.ToSingle(bytes, i + 12), - BitConverter.ToSingle(bytes, i + 16), - BitConverter.ToInt64(bytes, i + 20)); - - result.Add(landmark); + Type = new PoseLandmarkType(type); + X = x; + Y = y; + Z = z; + Visibility = visibility; + TimeStamp = timeStamp; } - return result; - } - - public override bool Equals(object obj) - { - if (obj is not PoseLandmark landmark) + /// + /// 转换成字节数组 + /// 便于UDP发送 + /// + /// + public byte[] ToByteArray() { - return false; + var result = new byte[PacketLength]; + + BitConverter.GetBytes(Type.Value).CopyTo(result, 0); + BitConverter.GetBytes(X).CopyTo(result, 4); + BitConverter.GetBytes(Y).CopyTo(result, 8); + BitConverter.GetBytes(Z).CopyTo(result, 12); + BitConverter.GetBytes(Visibility).CopyTo(result, 16); + BitConverter.GetBytes(TimeStamp).CopyTo(result, 20); + + return result; } - else + + /// + /// 解析字节数组 + /// + /// 收到的字节数组 + /// 字节数组中代表的坐标对象 + public static PoseLandmark ValueOf(byte[] bytes) { - return Type.Value == landmark.Type.Value - && Math.Abs(X - landmark.X) < Tolerance - && Math.Abs(Y - landmark.Y) < Tolerance - && Math.Abs(Z - landmark.Z) < Tolerance - && Math.Abs(Visibility - landmark.Visibility) < Tolerance - && TimeStamp == landmark.TimeStamp; + var result = new PoseLandmark( + BitConverter.ToInt32(bytes, 0), + BitConverter.ToSingle(bytes, 4), + BitConverter.ToSingle(bytes, 8), + BitConverter.ToSingle(bytes, 12), + BitConverter.ToSingle(bytes, 16), + BitConverter.ToInt64(bytes, 20)); + + return result; } - } - public override int GetHashCode() - { - var hashCode = Type.GetHashCode(); - hashCode ^= X.GetHashCode(); - hashCode ^= Y.GetHashCode(); - hashCode ^= Z.GetHashCode(); - hashCode ^= Visibility.GetHashCode(); - hashCode ^= TimeStamp.GetHashCode(); + /// + /// 解析字节数组为对象列表 + /// 单个 + /// + /// 字节数组 + /// + public static List ArrayOf(byte[] bytes) + { + var result = new List(); - return hashCode; - } + for (var i = 0; i < bytes.Length; i = i + PacketLength) + { + var landmark = new PoseLandmark(BitConverter.ToInt32(bytes, i), + BitConverter.ToSingle(bytes, i + 4), + BitConverter.ToSingle(bytes, i + 8), + BitConverter.ToSingle(bytes, i + 12), + BitConverter.ToSingle(bytes, i + 16), + BitConverter.ToInt64(bytes, i + 20)); - public override string ToString() - { - var builder = new StringBuilder(); - builder.Append($"Time: {TimeStamp}, Type: {Type}:\n"); - builder.Append($"\tX:{X}, Y:{Y}, Z:{Z}, Visibility: {Visibility}\n"); + result.Add(landmark); + } - return builder.ToString(); - } + return result; + } + + public override bool Equals(object obj) + { + if (obj is not PoseLandmark landmark) + { + return false; + } + else + { + return Type.Value == landmark.Type.Value + && Math.Abs(X - landmark.X) < Tolerance + && Math.Abs(Y - landmark.Y) < Tolerance + && Math.Abs(Z - landmark.Z) < Tolerance + && Math.Abs(Visibility - landmark.Visibility) < Tolerance + && TimeStamp == landmark.TimeStamp; + } + } + + public override int GetHashCode() + { + var hashCode = Type.GetHashCode(); + hashCode ^= X.GetHashCode(); + hashCode ^= Y.GetHashCode(); + hashCode ^= Z.GetHashCode(); + hashCode ^= Visibility.GetHashCode(); + hashCode ^= TimeStamp.GetHashCode(); + + return hashCode; + } + + public override string ToString() + { + var builder = new StringBuilder(); + builder.Append($"Time: {TimeStamp}, Type: {Type}:\n"); + builder.Append($"\tX:{X}, Y:{Y}, Z:{Z}, Visibility: {Visibility}\n"); + + return builder.ToString(); + } } } \ No newline at end of file diff --git a/Assets/Models/PoseTransform.cs b/Assets/Models/PoseTransform.cs index 318ffd6..b540465 100644 --- a/Assets/Models/PoseTransform.cs +++ b/Assets/Models/PoseTransform.cs @@ -28,22 +28,11 @@ public struct PoseTransform /// 取平均之后的结果 /// public Vector3 ResultPosition => _transformHandler.GetResultPosition(); - /// /// 节点的父节点列表 /// public readonly List Parent; - /// - /// 骨骼节点的初始角度 - /// - public Quaternion PreviousQuaternion; - - /// - /// 骨骼节点的当前角度 - /// - public Quaternion CurrentQuaternion; - private readonly IPoseTransformHandler _transformHandler; public PoseTransform( @@ -54,8 +43,6 @@ IPoseTransformHandler handler MediaPipeName = new PoseLandmarkType(type); UnityName = GetRelatedBone(MediaPipeName); Parent = new List(); - PreviousQuaternion = new Quaternion(); - CurrentQuaternion = new Quaternion(); _transformHandler = handler; } @@ -65,11 +52,6 @@ public static void UpdatePosition(ref PoseTransform pose, PoseLandmark landmark) pose._transformHandler.ReceivePoseLandmark(landmark); } - public static void CalculateRotation(ref PoseTransform poseTransform, PoseLandmark landmark) - { - - } - public static Vector3 operator +(PoseTransform a) => a.ResultPosition; public static Vector3 operator -(PoseTransform a) => -a.ResultPosition; diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 72a28c8..6646bb2 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 705507994} - m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1} + m_IndirectSpecularColor: {r: 0.18029127, g: 0.22572401, b: 0.3069303, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -189,7 +189,12 @@ GameObject: m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} m_PrefabInstance: {fileID: 404755305} m_PrefabAsset: {fileID: 0} ---- !u!114 &404755307 +--- !u!4 &404755309 stripped +Transform: + m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + m_PrefabInstance: {fileID: 404755305} + m_PrefabAsset: {fileID: 0} +--- !u!114 &404755310 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -198,14 +203,9 @@ MonoBehaviour: m_GameObject: {fileID: 404755306} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: a2b30b6aa6e08c4468a02e1fcabcd485, type: 3} + m_Script: {fileID: 11500000, guid: a2b060336532a01bfb4593b385955175, type: 3} m_Name: m_EditorClassIdentifier: ---- !u!4 &404755309 stripped -Transform: - m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} - m_PrefabInstance: {fileID: 404755305} - m_PrefabAsset: {fileID: 0} --- !u!1 &705507993 GameObject: m_ObjectHideFlags: 0