From b0a695bb3b1f4a82915fbff1ae2500f9061fa00a Mon Sep 17 00:00:00 2001 From: Ichirinko <1621543655@qq.com> Date: Wed, 15 Feb 2023 23:24:47 +0800 Subject: [PATCH 01/29] add ModelControler.cs and change model to humanoid type --- Assets/Avatars/Stormtrigger/troop.fbx.meta | 6 +- Assets/CameraBehaviour.cs | 7 +- Assets/Models/ModelControler.cs | 179 +++++++++++++++++++ Assets/Models/ModelControler.cs.meta | 11 ++ Assets/Scenes/SampleScene.unity | 103 +++++++++-- Packages/packages-lock.json | 30 ++-- ProjectSettings/PackageManagerSettings.asset | 6 +- ProjectSettings/ProjectVersion.txt | 4 +- 8 files changed, 310 insertions(+), 36 deletions(-) create mode 100644 Assets/Models/ModelControler.cs create mode 100644 Assets/Models/ModelControler.cs.meta diff --git a/Assets/Avatars/Stormtrigger/troop.fbx.meta b/Assets/Avatars/Stormtrigger/troop.fbx.meta index 99b2f9b..e8195e3 100644 --- a/Assets/Avatars/Stormtrigger/troop.fbx.meta +++ b/Assets/Avatars/Stormtrigger/troop.fbx.meta @@ -39,7 +39,7 @@ ModelImporter: animationDoRetargetingWarnings: 0 importAnimatedCustomProperties: 0 importConstraints: 0 - animationCompression: 1 + animationCompression: 3 animationRotationError: 0.5 animationPositionError: 0.5 animationScaleError: 0.5 @@ -106,11 +106,11 @@ ModelImporter: globalScale: 1 rootMotionBoneName: mixamorig:Hips hasTranslationDoF: 0 - hasExtraRoot: 0 + hasExtraRoot: 1 skeletonHasParents: 1 lastHumanDescriptionAvatarSource: {instanceID: 0} autoGenerateAvatarMappingIfUnspecified: 1 - animationType: 2 + animationType: 3 humanoidOversampling: 1 avatarSetup: 1 addHumanoidExtraRootOnlyWhenUsingAvatar: 1 diff --git a/Assets/CameraBehaviour.cs b/Assets/CameraBehaviour.cs index 445877b..e5a8fea 100644 --- a/Assets/CameraBehaviour.cs +++ b/Assets/CameraBehaviour.cs @@ -34,8 +34,8 @@ private void Start() _eulerX = angles.y; _eulerY = angles.x; _targetPosition = target.position; - _cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0); - + //_cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0); + _cameraRotation = Quaternion.Euler(_eulerY, _eulerX, 0); // 引入中间变量 提高代码效率 var transform1 = transform; //设置相机姿态 @@ -57,7 +57,8 @@ private void Update() _eulerY = ClampAngle(_eulerY, YMinLimit, YMaxLimit); - _cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0); + //_cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0); + _cameraRotation = Quaternion.Euler(_eulerY, _eulerX, 0); var position = _cameraRotation * new Vector3(0.0f, 0.0f, -_distance) + _targetPosition; // 引入中间变量 提高代码效率 diff --git a/Assets/Models/ModelControler.cs b/Assets/Models/ModelControler.cs new file mode 100644 index 0000000..6c85fe8 --- /dev/null +++ b/Assets/Models/ModelControler.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; +using Models; +using UnityEngine; + +public class ModelControler : MonoBehaviour +{ + Animator animator; + //float deltaz = 0f; + //Vector3 upperArm = new Vector3(0f, 0f, 0f); + private Quaternion[] prevQ = new Quaternion[15]; + + private static Quaternion[] uniRotation = new Quaternion[15]; //全局两坐标系通用四元数 + + private readonly UdpListener _listener = new UdpListener(); + + private static Dictionary Landmarks_Mapping= new Dictionary(); + + + void Start() + { + + _listener.AddHandler(LogLandmarks); + _listener.AddHandler(RigPoint); + _listener.Connect(5000); + + + DictProcess(); //匹配字典初始化 + + + // 获取动画控件 + animator = this.GetComponent(); + + GetOriginStatus(); // 获取骨骼的原始旋转角 + + + + + + } + + // Update is called once per frame + void Update() + { + //deltaz += 1f; + //upperArm.z = deltaz; + + //animator.GetBoneTransform(HumanBodyBones.RightLowerArm).rotation = uniRotation * prevQ ; + int pos=0; + foreach(var landmark in Landmarks_Mapping.Values) + { + animator.GetBoneTransform(landmark).rotation = uniRotation[pos]; + pos++; + } + //animator.GetBoneTransform(HumanBodyBones.RightLowerArm).rotation = uniRotation[14]; + + } + + 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) + { + int pos = 0; + + foreach (var landmark in landmarks) + { + + if(Landmarks_Mapping.ContainsKey(landmark.Type.ToString())) + { + + Vector3 realPosition = new Vector3(landmark.X,landmark.Y,landmark.Z); + + if(landmark.Visibility<0.8){ + + //uniRotation=(0f,0f); + + } + + else + { + + uniRotation[pos] = Quaternion.LookRotation(realPosition); + pos++; + } + + } + } + + /* + Vector3 realPosition = new Vector3(landmarks[14].X,landmarks[14].Y,landmarks[14].Z); + + if(landmarks[14].Visibility<0.8){ + + //uniRotation=(0f,0f); + + } + else{ + + uniRotation[14] = Quaternion.LookRotation(realPosition); + + } + + //Debug.Log(uniRotation); + */ + } + + private void DictProcess() + { + + /*下面的语句的顺序最好别调换,不然可能会发生难以预料的事情*/ + + Landmarks_Mapping.Add("Nose",HumanBodyBones.Head); //0 10 + + Landmarks_Mapping.Add("LeftShoulder",HumanBodyBones.LeftUpperArm); //11 + + Landmarks_Mapping.Add("RightShoulder",HumanBodyBones.RightUpperArm); //12 + + Landmarks_Mapping.Add("LeftElbow",HumanBodyBones.LeftLowerArm); //13 + + Landmarks_Mapping.Add("RightElbow",HumanBodyBones.RightLowerArm); //14 + + Landmarks_Mapping.Add("LeftWrist",HumanBodyBones.LeftHand); //15 + + Landmarks_Mapping.Add("RightWrist",HumanBodyBones.RightHand); //16 + + Landmarks_Mapping.Add("LeftHip",HumanBodyBones.LeftUpperLeg); //23 + + Landmarks_Mapping.Add("RightHip",HumanBodyBones.RightUpperLeg); //24 + + Landmarks_Mapping.Add("LeftKnee",HumanBodyBones.LeftLowerLeg); //25 + + Landmarks_Mapping.Add("RightKnee",HumanBodyBones.RightLowerLeg); //26 + + Landmarks_Mapping.Add("LeftAnkle",HumanBodyBones.LeftFoot); //27 + + Landmarks_Mapping.Add("RightAnkle",HumanBodyBones.RightFoot); //28 + + Landmarks_Mapping.Add("LeftFootIndex",HumanBodyBones.LeftToes); //31 + + Landmarks_Mapping.Add("RightFootIndex",HumanBodyBones.RightToes); //32 + + } + + private void GetOriginStatus() + { + + int pos=0; + + foreach(var landmarks in Landmarks_Mapping.Values) + { + + + prevQ[pos] = animator.GetBoneTransform(landmarks).rotation; + //Quaternion currentQ = Quaternion.Euler(upperArm.x, upperArm.y, upperArm.z); + //animator.GetBoneTransform(HumanBodyBones.LeftUpperArm).rotation = currentQ * prevQ; + uniRotation[pos] = prevQ[pos]; + + pos++; + + } + + } + +} diff --git a/Assets/Models/ModelControler.cs.meta b/Assets/Models/ModelControler.cs.meta new file mode 100644 index 0000000..27c22a3 --- /dev/null +++ b/Assets/Models/ModelControler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f3dc31402d1b12d4faa2846675feff35 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 0465b9b..277da21 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.18029127, g: 0.22572401, b: 0.3069303, a: 1} + m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -150,7 +150,7 @@ MonoBehaviour: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 320693305} - m_Enabled: 1 + m_Enabled: 0 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: 11e8a976fe328b1c3bdf9f44ef9f6fa1, type: 3} m_Name: @@ -232,8 +232,91 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 2 + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1001 &421937415 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_RootOrder + value: 1 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalPosition.z + value: 5 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -2845779275431937956, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_IsActive + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_Name + value: troop + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} +--- !u!1 &437000000 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + m_PrefabInstance: {fileID: 421937415} + m_PrefabAsset: {fileID: 0} +--- !u!114 &437000001 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 437000000} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f3dc31402d1b12d4faa2846675feff35, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!4 &437000003 stripped +Transform: + m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + m_PrefabInstance: {fileID: 421937415} + m_PrefabAsset: {fileID: 0} --- !u!1 &705507993 GameObject: m_ObjectHideFlags: 0 @@ -326,7 +409,7 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 1 + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} --- !u!1 &963194225 GameObject: @@ -405,14 +488,14 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 963194225} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 1, z: -10} + m_LocalRotation: {x: 0, y: 1, z: 0, w: 0} + m_LocalPosition: {x: 0, y: 9.29, z: 20} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} m_RootOrder: 0 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_LocalEulerAnglesHint: {x: 0, y: 180, z: 0} --- !u!114 &963194229 MonoBehaviour: m_ObjectHideFlags: 0 @@ -420,12 +503,12 @@ MonoBehaviour: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 963194225} - m_Enabled: 1 + m_Enabled: 0 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: 9189b565e8ba9bfc3bdaa38149e3e13f, type: 3} m_Name: m_EditorClassIdentifier: - target: {fileID: 320693310} + target: {fileID: 437000003} --- !u!1 &1880773849 GameObject: m_ObjectHideFlags: 0 @@ -492,5 +575,5 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 3 + m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 5bc5a0b..ecacec9 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -7,21 +7,21 @@ "dependencies": { "com.unity.services.core": "1.0.1" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.editorcoroutines": { "version": "1.0.0", "depth": 1, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.ext.nunit": { "version": "1.0.6", "depth": 1, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.feature.development": { "version": "1.0.1", @@ -44,7 +44,7 @@ "dependencies": { "com.unity.ext.nunit": "1.0.6" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.ide.visualstudio": { "version": "2.0.16", @@ -53,28 +53,28 @@ "dependencies": { "com.unity.test-framework": "1.1.9" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.ide.vscode": { "version": "1.2.5", "depth": 0, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.nuget.newtonsoft-json": { "version": "3.0.2", "depth": 2, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.performance.profile-analyzer": { "version": "1.1.1", "depth": 1, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.services.core": { "version": "1.6.0", @@ -85,14 +85,14 @@ "com.unity.nuget.newtonsoft-json": "3.0.2", "com.unity.modules.androidjni": "1.0.0" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.settings-manager": { "version": "1.0.3", "depth": 2, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.test-framework": { "version": "1.1.31", @@ -103,7 +103,7 @@ "com.unity.modules.imgui": "1.0.0", "com.unity.modules.jsonserialize": "1.0.0" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.testtools.codecoverage": { "version": "1.2.2", @@ -113,7 +113,7 @@ "com.unity.test-framework": "1.0.16", "com.unity.settings-manager": "1.0.1" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.textmeshpro": { "version": "3.0.6", @@ -122,7 +122,7 @@ "dependencies": { "com.unity.ugui": "1.0.0" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.timeline": { "version": "1.6.4", @@ -134,7 +134,7 @@ "com.unity.modules.audio": "1.0.0", "com.unity.modules.particlesystem": "1.0.0" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.ugui": { "version": "1.0.0", @@ -153,7 +153,7 @@ "com.unity.ugui": "1.0.0", "com.unity.modules.jsonserialize": "1.0.0" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.modules.ai": { "version": "1.0.0", diff --git a/ProjectSettings/PackageManagerSettings.asset b/ProjectSettings/PackageManagerSettings.asset index 6c3e794..93543c4 100644 --- a/ProjectSettings/PackageManagerSettings.asset +++ b/ProjectSettings/PackageManagerSettings.asset @@ -21,7 +21,7 @@ MonoBehaviour: m_Registries: - m_Id: main m_Name: - m_Url: https://packages.unity.com + m_Url: https://packages.unity.cn m_Scopes: [] m_IsDefault: 1 m_Capabilities: 7 @@ -31,6 +31,6 @@ MonoBehaviour: m_RegistryInfoDraft: m_Modified: 0 m_ErrorMessage: - m_UserModificationsInstanceId: -824 - m_OriginalInstanceId: -826 + m_UserModificationsInstanceId: -846 + m_OriginalInstanceId: -848 m_LoadAssets: 0 diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index 3643b02..edfab77 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2021.3.16f1 -m_EditorVersionWithRevision: 2021.3.16f1 (4016570cf34f) +m_EditorVersion: 2021.3.16f1c1 +m_EditorVersionWithRevision: 2021.3.16f1c1 (56dbfdd6697f) From be507d250e43503c8417fc0fb62cf10331bc79a5 Mon Sep 17 00:00:00 2001 From: Ichirinko <1621543655@qq.com> Date: Sun, 19 Feb 2023 20:10:57 +0800 Subject: [PATCH 02/29] rewrite ModelController.cs But may get stuck --- Assets/Models/ModelController.cs | 265 +++++++++++++++++++++++++ Assets/Models/ModelController.cs.meta | 11 + Assets/Models/PosTransformator.cs | 26 +++ Assets/Models/PosTransformator.cs.meta | 11 + Assets/Scenes/SampleScene.unity | 35 +++- 5 files changed, 347 insertions(+), 1 deletion(-) create mode 100644 Assets/Models/ModelController.cs create mode 100644 Assets/Models/ModelController.cs.meta create mode 100644 Assets/Models/PosTransformator.cs create mode 100644 Assets/Models/PosTransformator.cs.meta diff --git a/Assets/Models/ModelController.cs b/Assets/Models/ModelController.cs new file mode 100644 index 0000000..5a8c096 --- /dev/null +++ b/Assets/Models/ModelController.cs @@ -0,0 +1,265 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Models; +using UnityEngine; + +public class ModelController : MonoBehaviour +{ + Animator animator; + + private readonly UdpListener _listener = new UdpListener(); + + private static List _posLandmarks = new List(); + + void Start() + { + animator = this.GetComponent(); // 获取动画控件 + + TransformatorInit(); //匹配初始化 + + _listener.AddHandler(LogLandmarks); + _listener.AddHandler(RigPoint); + _listener.Connect(5000); + + + + + + + + } + + // Update is called once per frame + void Update() + { + + + foreach (var landmark in _posLandmarks) + { + //animator.GetBoneTransform(landmark.unityName).rotation = landmark.currentQ; + + } + + + } + + 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) + { + 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!!"); + } + + } + + private void TransformatorInit() + { + + /*下面的语句的顺序最好别调换,不然可能会发生难以预料的事情*/ + + PosTransformator[] transformator = new PosTransformator[15]; + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "LeftHip"; + temp.unityName = HumanBodyBones.LeftUpperLeg; + temp.parent.Add(null); + transformator[0] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "RightHip"; + temp.unityName = HumanBodyBones.RightUpperLeg; + temp.parent.Add(null); + transformator[1] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "LeftShoulder"; + temp.unityName = HumanBodyBones.LeftUpperArm; + temp.parent.Add(transformator[0]); + temp.parent.Add(transformator[1]); + transformator[2] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "RightShoulder"; + temp.unityName = HumanBodyBones.RightUpperArm; + temp.parent.Add(transformator[0]); + temp.parent.Add(transformator[1]); + transformator[3] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "Nose"; + temp.unityName = HumanBodyBones.Head; + temp.parent.Add(transformator[2]); + temp.parent.Add(transformator[3]); + transformator[4] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "LeftElbow"; + temp.unityName = HumanBodyBones.LeftLowerArm; + temp.parent.Add(transformator[2]); + transformator[5] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "RightElbow"; + temp.unityName = HumanBodyBones.RightLowerArm; + temp.parent.Add(transformator[3]); + transformator[6] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "LeftWrist"; + temp.unityName = HumanBodyBones.LeftHand; + temp.parent.Add(transformator[5]); + transformator[7] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "RightWrist"; + temp.unityName = HumanBodyBones.RightHand; + temp.parent.Add(transformator[6]); + transformator[8] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "LeftKnee"; + temp.unityName = HumanBodyBones.LeftLowerLeg; + temp.parent.Add(transformator[0]); + transformator[9] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "RightKnee"; + temp.unityName = HumanBodyBones.RightLowerLeg; + temp.parent.Add(transformator[1]); + transformator[10] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "LeftAnkle"; + temp.unityName = HumanBodyBones.LeftFoot; + temp.parent.Add(transformator[9]); + transformator[11] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "RightAnkle"; + temp.unityName = HumanBodyBones.RightFoot; + temp.parent.Add(transformator[10]); + transformator[12] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "LeftFootIndex"; + temp.unityName = HumanBodyBones.LeftToes; + temp.parent.Add(transformator[11]); + transformator[13] = temp; + } + + { + var temp = new PosTransformator(); + temp.mediaPipeName = "RightFootIndex"; + temp.unityName = HumanBodyBones.RightToes; + temp.parent.Add(transformator[12]); + transformator[14] = temp; + } + + //Landmarks_Mapping.Add("Nose",HumanBodyBones.Head); //0 10 + + //Landmarks_Mapping.Add("LeftShoulder",HumanBodyBones.LeftUpperArm); //11 + + //Landmarks_Mapping.Add("RightShoulder",HumanBodyBones.RightUpperArm); //12 + + //Landmarks_Mapping.Add("LeftElbow",HumanBodyBones.LeftLowerArm); //13 + + //Landmarks_Mapping.Add("RightElbow",HumanBodyBones.RightLowerArm); //14 + + //Landmarks_Mapping.Add("LeftWrist",HumanBodyBones.LeftHand); //15 + + //Landmarks_Mapping.Add("RightWrist",HumanBodyBones.RightHand); //16 + + //Landmarks_Mapping.Add("LeftHip",HumanBodyBones.LeftUpperLeg); //23 + + //Landmarks_Mapping.Add("RightHip",HumanBodyBones.RightUpperLeg); //24 + + //Landmarks_Mapping.Add("LeftKnee",HumanBodyBones.LeftLowerLeg); //25 + + //Landmarks_Mapping.Add("RightKnee",HumanBodyBones.RightLowerLeg); //26 + + //Landmarks_Mapping.Add("LeftAnkle",HumanBodyBones.LeftFoot); //27 + + //Landmarks_Mapping.Add("RightAnkle",HumanBodyBones.RightFoot); //28 + + //Landmarks_Mapping.Add("LeftFootIndex",HumanBodyBones.LeftToes); //31 + + //Landmarks_Mapping.Add("RightFootIndex",HumanBodyBones.RightToes); //32 + + foreach (var temp in transformator) + { + temp.prevQ = animator.GetBoneTransform(temp.unityName).rotation; //获得初始角度 + + temp.currentQ = temp.prevQ; + + _posLandmarks.Add(temp); + } + + } +} \ No newline at end of file diff --git a/Assets/Models/ModelController.cs.meta b/Assets/Models/ModelController.cs.meta new file mode 100644 index 0000000..295604d --- /dev/null +++ b/Assets/Models/ModelController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a2b30b6aa6e08c4468a02e1fcabcd485 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Models/PosTransformator.cs b/Assets/Models/PosTransformator.cs new file mode 100644 index 0000000..ae4386e --- /dev/null +++ b/Assets/Models/PosTransformator.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace Models +{ + public class PosTransformator + { + public string mediaPipeName; + //动捕手机端关节点标识 + + public HumanBodyBones unityName; + //unity内部avatar系统关节点标识 + + public Vector3 mediaPipePos = new Vector3(0,0,0); + //动捕手机端位置数据 + + public List parent = new List(); + //该骨骼节点的父关节点列表(因为传上来的数据没有脖子和骨盆节点) + + public Quaternion prevQ; + //该骨骼节点的初始角度 + + public Quaternion currentQ; + //该骨骼节点的当前角度 + } +} \ No newline at end of file diff --git a/Assets/Models/PosTransformator.cs.meta b/Assets/Models/PosTransformator.cs.meta new file mode 100644 index 0000000..dd25407 --- /dev/null +++ b/Assets/Models/PosTransformator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dfbef38b30e17e44b831e51debbd5087 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 277da21..91b9be0 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -176,7 +176,7 @@ MeshRenderer: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 320693305} - m_Enabled: 1 + m_Enabled: 0 m_CastShadows: 1 m_ReceiveShadows: 1 m_DynamicOccludee: 1 @@ -234,7 +234,11 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +<<<<<<< Updated upstream --- !u!1001 &421937415 +======= +--- !u!1001 &404755305 +>>>>>>> Stashed changes PrefabInstance: m_ObjectHideFlags: 0 serializedVersion: 2 @@ -255,7 +259,11 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} propertyPath: m_LocalPosition.z +<<<<<<< Updated upstream value: 5 +======= + value: 0 +>>>>>>> Stashed changes objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} propertyPath: m_LocalRotation.w @@ -285,27 +293,40 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} +<<<<<<< Updated upstream - target: {fileID: -2845779275431937956, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} propertyPath: m_IsActive value: 0 objectReference: {fileID: 0} +======= +>>>>>>> Stashed changes - target: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} propertyPath: m_Name value: troop objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} +<<<<<<< Updated upstream --- !u!1 &437000000 stripped GameObject: m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} m_PrefabInstance: {fileID: 421937415} m_PrefabAsset: {fileID: 0} --- !u!114 &437000001 +======= +--- !u!1 &404755306 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + m_PrefabInstance: {fileID: 404755305} + m_PrefabAsset: {fileID: 0} +--- !u!114 &404755307 +>>>>>>> Stashed changes MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} +<<<<<<< Updated upstream m_GameObject: {fileID: 437000000} m_Enabled: 1 m_EditorHideFlags: 0 @@ -317,6 +338,14 @@ Transform: m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} m_PrefabInstance: {fileID: 421937415} m_PrefabAsset: {fileID: 0} +======= + m_GameObject: {fileID: 404755306} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a2b30b6aa6e08c4468a02e1fcabcd485, type: 3} + m_Name: + m_EditorClassIdentifier: +>>>>>>> Stashed changes --- !u!1 &705507993 GameObject: m_ObjectHideFlags: 0 @@ -489,7 +518,11 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 963194225} m_LocalRotation: {x: 0, y: 1, z: 0, w: 0} +<<<<<<< Updated upstream m_LocalPosition: {x: 0, y: 9.29, z: 20} +======= + m_LocalPosition: {x: 0, y: 7, z: 15} +>>>>>>> Stashed changes m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] From e6a19beb0ccc20bd34fb1fbeed06d5ac032b4653 Mon Sep 17 00:00:00 2001 From: Ichirinko <1621543655@qq.com> Date: Sun, 19 Feb 2023 21:09:46 +0800 Subject: [PATCH 03/29] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E9=97=AE=E9=A2=98=EF=BC=8C=E7=A7=BB=E9=99=A4=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Models/ModelControler.cs | 179 ------------------- Assets/Models/ModelControler.cs.meta | 11 -- Assets/Scenes/SampleScene.unity | 42 ----- Packages/packages-lock.json | 49 +++-- ProjectSettings/PackageManagerSettings.asset | 7 +- ProjectSettings/ProjectVersion.txt | 4 +- 6 files changed, 25 insertions(+), 267 deletions(-) delete mode 100644 Assets/Models/ModelControler.cs delete mode 100644 Assets/Models/ModelControler.cs.meta diff --git a/Assets/Models/ModelControler.cs b/Assets/Models/ModelControler.cs deleted file mode 100644 index 6c85fe8..0000000 --- a/Assets/Models/ModelControler.cs +++ /dev/null @@ -1,179 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Text; -using Models; -using UnityEngine; - -public class ModelControler : MonoBehaviour -{ - Animator animator; - //float deltaz = 0f; - //Vector3 upperArm = new Vector3(0f, 0f, 0f); - private Quaternion[] prevQ = new Quaternion[15]; - - private static Quaternion[] uniRotation = new Quaternion[15]; //全局两坐标系通用四元数 - - private readonly UdpListener _listener = new UdpListener(); - - private static Dictionary Landmarks_Mapping= new Dictionary(); - - - void Start() - { - - _listener.AddHandler(LogLandmarks); - _listener.AddHandler(RigPoint); - _listener.Connect(5000); - - - DictProcess(); //匹配字典初始化 - - - // 获取动画控件 - animator = this.GetComponent(); - - GetOriginStatus(); // 获取骨骼的原始旋转角 - - - - - - } - - // Update is called once per frame - void Update() - { - //deltaz += 1f; - //upperArm.z = deltaz; - - //animator.GetBoneTransform(HumanBodyBones.RightLowerArm).rotation = uniRotation * prevQ ; - int pos=0; - foreach(var landmark in Landmarks_Mapping.Values) - { - animator.GetBoneTransform(landmark).rotation = uniRotation[pos]; - pos++; - } - //animator.GetBoneTransform(HumanBodyBones.RightLowerArm).rotation = uniRotation[14]; - - } - - 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) - { - int pos = 0; - - foreach (var landmark in landmarks) - { - - if(Landmarks_Mapping.ContainsKey(landmark.Type.ToString())) - { - - Vector3 realPosition = new Vector3(landmark.X,landmark.Y,landmark.Z); - - if(landmark.Visibility<0.8){ - - //uniRotation=(0f,0f); - - } - - else - { - - uniRotation[pos] = Quaternion.LookRotation(realPosition); - pos++; - } - - } - } - - /* - Vector3 realPosition = new Vector3(landmarks[14].X,landmarks[14].Y,landmarks[14].Z); - - if(landmarks[14].Visibility<0.8){ - - //uniRotation=(0f,0f); - - } - else{ - - uniRotation[14] = Quaternion.LookRotation(realPosition); - - } - - //Debug.Log(uniRotation); - */ - } - - private void DictProcess() - { - - /*下面的语句的顺序最好别调换,不然可能会发生难以预料的事情*/ - - Landmarks_Mapping.Add("Nose",HumanBodyBones.Head); //0 10 - - Landmarks_Mapping.Add("LeftShoulder",HumanBodyBones.LeftUpperArm); //11 - - Landmarks_Mapping.Add("RightShoulder",HumanBodyBones.RightUpperArm); //12 - - Landmarks_Mapping.Add("LeftElbow",HumanBodyBones.LeftLowerArm); //13 - - Landmarks_Mapping.Add("RightElbow",HumanBodyBones.RightLowerArm); //14 - - Landmarks_Mapping.Add("LeftWrist",HumanBodyBones.LeftHand); //15 - - Landmarks_Mapping.Add("RightWrist",HumanBodyBones.RightHand); //16 - - Landmarks_Mapping.Add("LeftHip",HumanBodyBones.LeftUpperLeg); //23 - - Landmarks_Mapping.Add("RightHip",HumanBodyBones.RightUpperLeg); //24 - - Landmarks_Mapping.Add("LeftKnee",HumanBodyBones.LeftLowerLeg); //25 - - Landmarks_Mapping.Add("RightKnee",HumanBodyBones.RightLowerLeg); //26 - - Landmarks_Mapping.Add("LeftAnkle",HumanBodyBones.LeftFoot); //27 - - Landmarks_Mapping.Add("RightAnkle",HumanBodyBones.RightFoot); //28 - - Landmarks_Mapping.Add("LeftFootIndex",HumanBodyBones.LeftToes); //31 - - Landmarks_Mapping.Add("RightFootIndex",HumanBodyBones.RightToes); //32 - - } - - private void GetOriginStatus() - { - - int pos=0; - - foreach(var landmarks in Landmarks_Mapping.Values) - { - - - prevQ[pos] = animator.GetBoneTransform(landmarks).rotation; - //Quaternion currentQ = Quaternion.Euler(upperArm.x, upperArm.y, upperArm.z); - //animator.GetBoneTransform(HumanBodyBones.LeftUpperArm).rotation = currentQ * prevQ; - uniRotation[pos] = prevQ[pos]; - - pos++; - - } - - } - -} diff --git a/Assets/Models/ModelControler.cs.meta b/Assets/Models/ModelControler.cs.meta deleted file mode 100644 index 27c22a3..0000000 --- a/Assets/Models/ModelControler.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: f3dc31402d1b12d4faa2846675feff35 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 91b9be0..5b0ed3d 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -234,11 +234,7 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} -<<<<<<< Updated upstream ---- !u!1001 &421937415 -======= --- !u!1001 &404755305 ->>>>>>> Stashed changes PrefabInstance: m_ObjectHideFlags: 0 serializedVersion: 2 @@ -259,11 +255,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} propertyPath: m_LocalPosition.z -<<<<<<< Updated upstream - value: 5 -======= value: 0 ->>>>>>> Stashed changes objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} propertyPath: m_LocalRotation.w @@ -293,59 +285,29 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} -<<<<<<< Updated upstream - - target: {fileID: -2845779275431937956, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} - propertyPath: m_IsActive - value: 0 - objectReference: {fileID: 0} -======= ->>>>>>> Stashed changes - target: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} propertyPath: m_Name value: troop objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} -<<<<<<< Updated upstream ---- !u!1 &437000000 stripped -GameObject: - m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} - m_PrefabInstance: {fileID: 421937415} - m_PrefabAsset: {fileID: 0} ---- !u!114 &437000001 -======= --- !u!1 &404755306 stripped GameObject: m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} m_PrefabInstance: {fileID: 404755305} m_PrefabAsset: {fileID: 0} --- !u!114 &404755307 ->>>>>>> Stashed changes MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} -<<<<<<< Updated upstream - m_GameObject: {fileID: 437000000} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: f3dc31402d1b12d4faa2846675feff35, type: 3} - m_Name: - m_EditorClassIdentifier: ---- !u!4 &437000003 stripped -Transform: - m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} - m_PrefabInstance: {fileID: 421937415} - m_PrefabAsset: {fileID: 0} -======= m_GameObject: {fileID: 404755306} m_Enabled: 1 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: a2b30b6aa6e08c4468a02e1fcabcd485, type: 3} m_Name: m_EditorClassIdentifier: ->>>>>>> Stashed changes --- !u!1 &705507993 GameObject: m_ObjectHideFlags: 0 @@ -518,11 +480,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 963194225} m_LocalRotation: {x: 0, y: 1, z: 0, w: 0} -<<<<<<< Updated upstream - m_LocalPosition: {x: 0, y: 9.29, z: 20} -======= m_LocalPosition: {x: 0, y: 7, z: 15} ->>>>>>> Stashed changes m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index ecacec9..247163d 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -7,34 +7,34 @@ "dependencies": { "com.unity.services.core": "1.0.1" }, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.editorcoroutines": { "version": "1.0.0", "depth": 1, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.ext.nunit": { "version": "1.0.6", "depth": 1, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.feature.development": { "version": "1.0.1", "depth": 0, "source": "builtin", "dependencies": { - "com.unity.ide.visualstudio": "2.0.16", - "com.unity.ide.rider": "3.0.16", + "com.unity.ide.visualstudio": "2.0.14", + "com.unity.ide.rider": "3.0.12", "com.unity.ide.vscode": "1.2.5", "com.unity.editorcoroutines": "1.0.0", "com.unity.performance.profile-analyzer": "1.1.1", "com.unity.test-framework": "1.1.31", - "com.unity.testtools.codecoverage": "1.2.2" + "com.unity.testtools.codecoverage": "1.0.1" } }, "com.unity.ide.rider": { @@ -44,7 +44,7 @@ "dependencies": { "com.unity.ext.nunit": "1.0.6" }, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.ide.visualstudio": { "version": "2.0.16", @@ -53,46 +53,37 @@ "dependencies": { "com.unity.test-framework": "1.1.9" }, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.ide.vscode": { "version": "1.2.5", "depth": 0, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.cn" - }, - "com.unity.nuget.newtonsoft-json": { - "version": "3.0.2", - "depth": 2, - "source": "registry", - "dependencies": {}, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.performance.profile-analyzer": { "version": "1.1.1", "depth": 1, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.services.core": { - "version": "1.6.0", + "version": "1.0.1", "depth": 1, "source": "registry", "dependencies": { - "com.unity.modules.unitywebrequest": "1.0.0", - "com.unity.nuget.newtonsoft-json": "3.0.2", - "com.unity.modules.androidjni": "1.0.0" + "com.unity.modules.unitywebrequest": "1.0.0" }, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.settings-manager": { "version": "1.0.3", "depth": 2, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.test-framework": { "version": "1.1.31", @@ -103,17 +94,17 @@ "com.unity.modules.imgui": "1.0.0", "com.unity.modules.jsonserialize": "1.0.0" }, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.testtools.codecoverage": { - "version": "1.2.2", + "version": "1.0.1", "depth": 1, "source": "registry", "dependencies": { "com.unity.test-framework": "1.0.16", "com.unity.settings-manager": "1.0.1" }, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.textmeshpro": { "version": "3.0.6", @@ -122,7 +113,7 @@ "dependencies": { "com.unity.ugui": "1.0.0" }, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.timeline": { "version": "1.6.4", @@ -134,7 +125,7 @@ "com.unity.modules.audio": "1.0.0", "com.unity.modules.particlesystem": "1.0.0" }, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.ugui": { "version": "1.0.0", @@ -153,7 +144,7 @@ "com.unity.ugui": "1.0.0", "com.unity.modules.jsonserialize": "1.0.0" }, - "url": "https://packages.unity.cn" + "url": "https://packages.unity.com" }, "com.unity.modules.ai": { "version": "1.0.0", diff --git a/ProjectSettings/PackageManagerSettings.asset b/ProjectSettings/PackageManagerSettings.asset index 93543c4..9990e02 100644 --- a/ProjectSettings/PackageManagerSettings.asset +++ b/ProjectSettings/PackageManagerSettings.asset @@ -21,16 +21,15 @@ MonoBehaviour: m_Registries: - m_Id: main m_Name: - m_Url: https://packages.unity.cn + m_Url: https://packages.unity.com m_Scopes: [] m_IsDefault: 1 m_Capabilities: 7 - m_ConfigSource: 0 m_UserSelectedRegistryName: m_UserAddingNewScopedRegistry: 0 m_RegistryInfoDraft: m_Modified: 0 m_ErrorMessage: - m_UserModificationsInstanceId: -846 - m_OriginalInstanceId: -848 + m_UserModificationsInstanceId: -824 + m_OriginalInstanceId: -826 m_LoadAssets: 0 diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index edfab77..0d22176 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2021.3.16f1c1 -m_EditorVersionWithRevision: 2021.3.16f1c1 (56dbfdd6697f) +m_EditorVersion: 2021.2.18f1 +m_EditorVersionWithRevision: 2021.2.18f1 (0c6e675195cf) From baea96db48bd2b1423ca701502495f1947f51785 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 19 Feb 2023 22:35:47 +0800 Subject: [PATCH 04/29] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0=E8=87=B32021.3.?= =?UTF-8?q?18f1=E7=89=88=E6=9C=AC=20=E9=87=8D=E6=9E=84=E4=BA=86=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/CubeBehaviour.cs | 38 ------ Assets/CubeBehaviour.cs.meta | 11 -- .../ModelController.cs => ModelBehaviour.cs} | 2 +- ...troller.cs.meta => ModelBehaviour.cs.meta} | 0 Assets/Scenes/SampleScene.unity | 117 +----------------- Packages/manifest.json | 8 +- Packages/packages-lock.json | 33 ++--- ProjectSettings/ProjectVersion.txt | 4 +- 8 files changed, 21 insertions(+), 192 deletions(-) delete mode 100644 Assets/CubeBehaviour.cs delete mode 100644 Assets/CubeBehaviour.cs.meta rename Assets/{Models/ModelController.cs => ModelBehaviour.cs} (99%) rename Assets/{Models/ModelController.cs.meta => ModelBehaviour.cs.meta} (100%) diff --git a/Assets/CubeBehaviour.cs b/Assets/CubeBehaviour.cs deleted file mode 100644 index 5388f67..0000000 --- a/Assets/CubeBehaviour.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Text; -using Models; -using UnityEngine; - -public class CubeBehaviour : MonoBehaviour -{ - public Vector3 rotateAmount = new Vector3(0, 1, 0); - private readonly UdpListener _listener = new UdpListener(); - - // Start is called before the first frame update - private void Start() - { - _listener.AddHandler(LogLandmarks); - _listener.Connect(5000); - } - - // Update is called once per frame - private void Update() - { - transform.Rotate(rotateAmount); - } - - private void OnDisable() - { - _listener.DisConnect(); - } - - private static void LogLandmarks(List landmarks) - { - foreach (var landmark in landmarks) - { - Debug.Log(landmark.ToString()); - } - } -} diff --git a/Assets/CubeBehaviour.cs.meta b/Assets/CubeBehaviour.cs.meta deleted file mode 100644 index bf7446b..0000000 --- a/Assets/CubeBehaviour.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 11e8a976fe328b1c3bdf9f44ef9f6fa1 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Models/ModelController.cs b/Assets/ModelBehaviour.cs similarity index 99% rename from Assets/Models/ModelController.cs rename to Assets/ModelBehaviour.cs index 5a8c096..b1e633c 100644 --- a/Assets/Models/ModelController.cs +++ b/Assets/ModelBehaviour.cs @@ -6,7 +6,7 @@ using Models; using UnityEngine; -public class ModelController : MonoBehaviour +public class ModelBehaviour : MonoBehaviour { Animator animator; diff --git a/Assets/Models/ModelController.cs.meta b/Assets/ModelBehaviour.cs.meta similarity index 100% rename from Assets/Models/ModelController.cs.meta rename to Assets/ModelBehaviour.cs.meta diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5b0ed3d..4aadfb8 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: @@ -123,117 +123,6 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} ---- !u!1 &320693305 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 320693310} - - component: {fileID: 320693309} - - component: {fileID: 320693308} - - component: {fileID: 320693307} - - component: {fileID: 320693306} - m_Layer: 0 - m_Name: Cube - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!114 &320693306 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 320693305} - m_Enabled: 0 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 11e8a976fe328b1c3bdf9f44ef9f6fa1, type: 3} - m_Name: - m_EditorClassIdentifier: - rotateAmount: {x: 0, y: 1, z: 0} ---- !u!65 &320693307 -BoxCollider: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 320693305} - m_Material: {fileID: 0} - m_IsTrigger: 0 - m_Enabled: 1 - serializedVersion: 2 - m_Size: {x: 1, y: 1, z: 1} - m_Center: {x: 0, y: 0, z: 0} ---- !u!23 &320693308 -MeshRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 320693305} - m_Enabled: 0 - m_CastShadows: 1 - m_ReceiveShadows: 1 - m_DynamicOccludee: 1 - m_StaticShadowCaster: 0 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 2 - m_RayTraceProcedural: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 1 - m_SelectedEditorRenderState: 3 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 - m_AdditionalVertexStreams: {fileID: 0} ---- !u!33 &320693309 -MeshFilter: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 320693305} - m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} ---- !u!4 &320693310 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 320693305} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -0.24, y: 1.1, z: -1.8} - m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 - m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 3 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1001 &404755305 PrefabInstance: m_ObjectHideFlags: 0 @@ -499,7 +388,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 9189b565e8ba9bfc3bdaa38149e3e13f, type: 3} m_Name: m_EditorClassIdentifier: - target: {fileID: 437000003} + target: {fileID: 0} --- !u!1 &1880773849 GameObject: m_ObjectHideFlags: 0 @@ -566,5 +455,5 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 4 + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Packages/manifest.json b/Packages/manifest.json index de2f422..752cde3 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -1,15 +1,15 @@ { "dependencies": { - "com.unity.collab-proxy": "1.17.7", + "com.unity.collab-proxy": "2.0.0", "com.unity.feature.development": "1.0.1", - "com.unity.ide.rider": "3.0.16", - "com.unity.ide.visualstudio": "2.0.16", + "com.unity.ide.rider": "3.0.18", + "com.unity.ide.visualstudio": "2.0.17", "com.unity.ide.vscode": "1.2.5", "com.unity.test-framework": "1.1.31", "com.unity.textmeshpro": "3.0.6", "com.unity.timeline": "1.6.4", "com.unity.ugui": "1.0.0", - "com.unity.visualscripting": "1.7.8", + "com.unity.visualscripting": "1.8.0", "com.unity.modules.ai": "1.0.0", "com.unity.modules.androidjni": "1.0.0", "com.unity.modules.animation": "1.0.0", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 247163d..60586d4 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -1,12 +1,10 @@ { "dependencies": { "com.unity.collab-proxy": { - "version": "1.17.7", + "version": "2.0.0", "depth": 0, "source": "registry", - "dependencies": { - "com.unity.services.core": "1.0.1" - }, + "dependencies": {}, "url": "https://packages.unity.com" }, "com.unity.editorcoroutines": { @@ -28,17 +26,17 @@ "depth": 0, "source": "builtin", "dependencies": { - "com.unity.ide.visualstudio": "2.0.14", - "com.unity.ide.rider": "3.0.12", + "com.unity.ide.visualstudio": "2.0.17", + "com.unity.ide.rider": "3.0.18", "com.unity.ide.vscode": "1.2.5", "com.unity.editorcoroutines": "1.0.0", - "com.unity.performance.profile-analyzer": "1.1.1", + "com.unity.performance.profile-analyzer": "1.2.2", "com.unity.test-framework": "1.1.31", - "com.unity.testtools.codecoverage": "1.0.1" + "com.unity.testtools.codecoverage": "1.2.2" } }, "com.unity.ide.rider": { - "version": "3.0.16", + "version": "3.0.18", "depth": 0, "source": "registry", "dependencies": { @@ -47,7 +45,7 @@ "url": "https://packages.unity.com" }, "com.unity.ide.visualstudio": { - "version": "2.0.16", + "version": "2.0.17", "depth": 0, "source": "registry", "dependencies": { @@ -63,21 +61,12 @@ "url": "https://packages.unity.com" }, "com.unity.performance.profile-analyzer": { - "version": "1.1.1", + "version": "1.2.2", "depth": 1, "source": "registry", "dependencies": {}, "url": "https://packages.unity.com" }, - "com.unity.services.core": { - "version": "1.0.1", - "depth": 1, - "source": "registry", - "dependencies": { - "com.unity.modules.unitywebrequest": "1.0.0" - }, - "url": "https://packages.unity.com" - }, "com.unity.settings-manager": { "version": "1.0.3", "depth": 2, @@ -97,7 +86,7 @@ "url": "https://packages.unity.com" }, "com.unity.testtools.codecoverage": { - "version": "1.0.1", + "version": "1.2.2", "depth": 1, "source": "registry", "dependencies": { @@ -137,7 +126,7 @@ } }, "com.unity.visualscripting": { - "version": "1.7.8", + "version": "1.8.0", "depth": 0, "source": "registry", "dependencies": { diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index 0d22176..8e3af85 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2021.2.18f1 -m_EditorVersionWithRevision: 2021.2.18f1 (0c6e675195cf) +m_EditorVersion: 2021.3.19f1 +m_EditorVersionWithRevision: 2021.3.19f1 (c9714fde33b6) From 2d1997049288a4a1d2269385dfacfef0a6d8d494 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 20 Feb 2023 11:34:21 +0800 Subject: [PATCH 05/29] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8struct?= =?UTF-8?q?=E9=87=8D=E6=9E=84PoseTransform?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/ModelBehaviour.cs | 96 +++++++++++++++++++------ Assets/Models/PoseTransform.cs | 108 ++++++++++++++++++++++++++++ Assets/Models/PoseTransform.cs.meta | 3 + 3 files changed, 184 insertions(+), 23 deletions(-) create mode 100644 Assets/Models/PoseTransform.cs create mode 100644 Assets/Models/PoseTransform.cs.meta 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 From d699e357dbb0480b0233565be6b750be1841bde5 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 21 Feb 2023 21:40:12 +0800 Subject: [PATCH 06/29] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E7=90=83?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=A4=BA=E6=84=8F=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/MainBehaviour.cs | 66 ++++++ Assets/MainBehaviour.cs.meta | 11 + Assets/Models/PoseTransform.cs | 4 +- Assets/Scenes/BallScene.unity | 361 +++++++++++++++++++++++++++++ Assets/Scenes/BallScene.unity.meta | 7 + 5 files changed, 448 insertions(+), 1 deletion(-) create mode 100644 Assets/MainBehaviour.cs create mode 100644 Assets/MainBehaviour.cs.meta create mode 100644 Assets/Scenes/BallScene.unity create mode 100644 Assets/Scenes/BallScene.unity.meta diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs new file mode 100644 index 0000000..2e7ebf4 --- /dev/null +++ b/Assets/MainBehaviour.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using Models; +using Unity.VisualScripting; +using UnityEngine; + +public class MainBehaviour : MonoBehaviour +{ + private readonly List _nodes = new List(); + private readonly List _poseTransforms = new List(); + private readonly UdpListener _listener = new UdpListener(); + + // Start is called before the first frame update + private void Start() + { + CreateNodes(); + + _listener.AddHandler(OnReceive); + _listener.Connect(5000); + } + + // Update is called once per frame + private void Update() + { + foreach (var poseTransform in _poseTransforms) + { + var index = (int)poseTransform.MediaPipeName; + _nodes[index].transform.position = _poseTransforms[index].MediaPipePos * 5; + } + } + + private void OnDisable() + { + _listener.DisConnect(); + } + + private void CreateNodes() + { + for (var i = 0; i < 33; i++) + { + var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere); + + ball.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f); + + _nodes.Add(ball); + + var poseTransform = new PoseTransform((PoseLandmarkType)i); + + _poseTransforms.Add(poseTransform); + } + } + + 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/MainBehaviour.cs.meta b/Assets/MainBehaviour.cs.meta new file mode 100644 index 0000000..e8b942d --- /dev/null +++ b/Assets/MainBehaviour.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 574a3724e040787f7afd9341fba89cf2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Models/PoseTransform.cs b/Assets/Models/PoseTransform.cs index d20e446..f62c320 100644 --- a/Assets/Models/PoseTransform.cs +++ b/Assets/Models/PoseTransform.cs @@ -56,7 +56,9 @@ PoseLandmarkType type /// public void CalculateCurrentQuaternion(PoseLandmark landmark) { - + MediaPipePos.x = landmark.X; + MediaPipePos.y = landmark.Y; + MediaPipePos.z = landmark.Z; } /// diff --git a/Assets/Scenes/BallScene.unity b/Assets/Scenes/BallScene.unity new file mode 100644 index 0000000..3252943 --- /dev/null +++ b/Assets/Scenes/BallScene.unity @@ -0,0 +1,361 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +OcclusionCullingSettings: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: 0.25 + backfaceThreshold: 100 + m_SceneGUID: 00000000000000000000000000000000 + m_OcclusionCullingData: {fileID: 0} +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 9 + m_Fog: 0 + m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} + m_FogMode: 3 + m_FogDensity: 0.01 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} + m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} + m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 0 + m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} + m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0} + m_HaloStrength: 0.5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} + m_IndirectSpecularColor: {r: 0.18029127, g: 0.22572401, b: 0.3069303, a: 1} + m_UseRadianceAmbientProbe: 0 +--- !u!157 &3 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 12 + m_GIWorkflowMode: 1 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 1 + m_EnableRealtimeLightmaps: 0 + m_LightmapEditorSettings: + serializedVersion: 12 + m_Resolution: 2 + m_BakeResolution: 40 + m_AtlasSize: 1024 + m_AO: 0 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_ExtractAmbientOcclusion: 0 + m_Padding: 2 + m_LightmapParameters: {fileID: 0} + m_LightmapsBakeMode: 1 + m_TextureCompression: 1 + m_FinalGather: 0 + m_FinalGatherFiltering: 1 + m_FinalGatherRayCount: 256 + m_ReflectionCompression: 2 + m_MixedBakeMode: 2 + m_BakeBackend: 1 + m_PVRSampling: 1 + m_PVRDirectSampleCount: 32 + m_PVRSampleCount: 512 + m_PVRBounces: 2 + m_PVREnvironmentSampleCount: 256 + m_PVREnvironmentReferencePointCount: 2048 + m_PVRFilteringMode: 1 + m_PVRDenoiserTypeDirect: 1 + m_PVRDenoiserTypeIndirect: 1 + m_PVRDenoiserTypeAO: 1 + m_PVRFilterTypeDirect: 0 + m_PVRFilterTypeIndirect: 0 + m_PVRFilterTypeAO: 0 + m_PVREnvironmentMIS: 1 + m_PVRCulling: 1 + m_PVRFilteringGaussRadiusDirect: 1 + m_PVRFilteringGaussRadiusIndirect: 5 + m_PVRFilteringGaussRadiusAO: 2 + m_PVRFilteringAtrousPositionSigmaDirect: 0.5 + m_PVRFilteringAtrousPositionSigmaIndirect: 2 + m_PVRFilteringAtrousPositionSigmaAO: 1 + m_ExportTrainingData: 0 + m_TrainingDataDestination: TrainingData + m_LightProbeSampleCountMultiplier: 4 + m_LightingDataAsset: {fileID: 0} + m_LightingSettings: {fileID: 0} +--- !u!196 &4 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 2 + agentTypeID: 0 + agentRadius: 0.5 + agentHeight: 2 + agentSlope: 45 + agentClimb: 0.4 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + minRegionArea: 2 + manualCellSize: 0 + cellSize: 0.16666667 + manualTileSize: 0 + tileSize: 256 + accuratePlacement: 0 + maxJobWorkers: 0 + preserveTilesOutsideBounds: 0 + debug: + m_Flags: 0 + m_NavMeshData: {fileID: 0} +--- !u!1 &182244630 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 182244631} + - component: {fileID: 182244632} + m_Layer: 0 + m_Name: GameObject + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &182244631 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 182244630} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 2 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &182244632 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 182244630} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 574a3724e040787f7afd9341fba89cf2, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!1 &443683957 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 443683959} + - component: {fileID: 443683958} + m_Layer: 0 + m_Name: Directional Light + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!108 &443683958 +Light: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 443683957} + m_Enabled: 1 + serializedVersion: 10 + m_Type: 1 + m_Shape: 0 + m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1} + m_Intensity: 1 + m_Range: 10 + m_SpotAngle: 30 + m_InnerSpotAngle: 21.80208 + m_CookieSize: 10 + m_Shadows: + m_Type: 2 + m_Resolution: -1 + m_CustomResolution: -1 + m_Strength: 1 + m_Bias: 0.05 + m_NormalBias: 0.4 + m_NearPlane: 0.2 + m_CullingMatrixOverride: + e00: 1 + e01: 0 + e02: 0 + e03: 0 + e10: 0 + e11: 1 + e12: 0 + e13: 0 + e20: 0 + e21: 0 + e22: 1 + e23: 0 + e30: 0 + e31: 0 + e32: 0 + e33: 1 + m_UseCullingMatrixOverride: 0 + m_Cookie: {fileID: 0} + m_DrawHalo: 0 + m_Flare: {fileID: 0} + m_RenderMode: 0 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingLayerMask: 1 + m_Lightmapping: 4 + m_LightShadowCasterMode: 0 + m_AreaSize: {x: 1, y: 1} + m_BounceIntensity: 1 + m_ColorTemperature: 6570 + m_UseColorTemperature: 0 + m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0} + m_UseBoundingSphereOverride: 0 + m_UseViewFrustumForShadowCasterCull: 1 + m_ShadowRadius: 0 + m_ShadowAngle: 0 +--- !u!4 &443683959 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 443683957} + m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261} + m_LocalPosition: {x: 0, y: 3, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} +--- !u!1 &2045151417 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 2045151420} + - component: {fileID: 2045151419} + - component: {fileID: 2045151418} + - component: {fileID: 2045151421} + m_Layer: 0 + m_Name: Main Camera + m_TagString: MainCamera + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!81 &2045151418 +AudioListener: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2045151417} + m_Enabled: 1 +--- !u!20 &2045151419 +Camera: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2045151417} + m_Enabled: 1 + serializedVersion: 2 + m_ClearFlags: 1 + m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} + m_projectionMatrixMode: 1 + m_GateFitMode: 2 + m_FOVAxisMode: 0 + m_SensorSize: {x: 36, y: 24} + m_LensShift: {x: 0, y: 0} + m_FocalLength: 50 + m_NormalizedViewPortRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 + near clip plane: 0.3 + far clip plane: 1000 + field of view: 60 + orthographic: 0 + orthographic size: 5 + m_Depth: -1 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingPath: -1 + m_TargetTexture: {fileID: 0} + m_TargetDisplay: 0 + m_TargetEye: 3 + m_HDR: 1 + m_AllowMSAA: 1 + m_AllowDynamicResolution: 0 + m_ForceIntoRT: 0 + m_OcclusionCulling: 1 + m_StereoConvergence: 10 + m_StereoSeparation: 0.022 +--- !u!4 &2045151420 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2045151417} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 1, z: -7.44} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &2045151421 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2045151417} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9189b565e8ba9bfc3bdaa38149e3e13f, type: 3} + m_Name: + m_EditorClassIdentifier: + target: {fileID: 182244631} diff --git a/Assets/Scenes/BallScene.unity.meta b/Assets/Scenes/BallScene.unity.meta new file mode 100644 index 0000000..867553f --- /dev/null +++ b/Assets/Scenes/BallScene.unity.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 53edd235eaf7da249a8204f606eb1489 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: From dba10abb293c63c80cf9a33f29a8fff35d375733 Mon Sep 17 00:00:00 2001 From: Ichirinko <1621543655@qq.com> Date: Wed, 22 Feb 2023 19:51:22 +0800 Subject: [PATCH 07/29] =?UTF-8?q?add:=E4=B8=89=E7=BB=B4=E6=97=8B=E8=BD=AC?= =?UTF-8?q?=E7=9A=84=E4=B8=89=E4=B8=AA=E6=96=B9=E6=B3=95=EF=BC=9B=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E7=BB=99=E4=B8=8D=E5=90=8C=E9=83=A8=E4=BD=8D=E4=B8=8A?= =?UTF-8?q?=E8=89=B2=E6=96=B9=E4=BE=BF=E5=8C=BA=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/MainBehaviour.cs | 73 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs index 2e7ebf4..56668c0 100644 --- a/Assets/MainBehaviour.cs +++ b/Assets/MainBehaviour.cs @@ -10,6 +10,7 @@ public class MainBehaviour : MonoBehaviour private readonly List _nodes = new List(); private readonly List _poseTransforms = new List(); private readonly UdpListener _listener = new UdpListener(); + // Start is called before the first frame update private void Start() @@ -26,7 +27,15 @@ private void Update() foreach (var poseTransform in _poseTransforms) { var index = (int)poseTransform.MediaPipeName; - _nodes[index].transform.position = _poseTransforms[index].MediaPipePos * 5; + + var temp = new Vector4(_poseTransforms[index].MediaPipePos.x, + _poseTransforms[index].MediaPipePos.y, + _poseTransforms[index].MediaPipePos.z,1); + //temp = RotateAxisZ(180) * temp; + //warning:乘法的顺序不能改! + + _nodes[index].transform.position = new Vector3(temp.x, temp.y, temp.z) * 5; + //_nodes[index].transform.position = _poseTransforms[index].MediaPipePos * 5; } } @@ -41,7 +50,20 @@ private void CreateNodes() { var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere); - ball.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f); + ball.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); + + if (i <= 10) + { + ball.GetComponent().material.color = Color.red;//给头部添加颜色 + } + else if (i > 10 && i <= 22) + { + ball.GetComponent().material.color = Color.blue;//给手部添加颜色 + } + else if (i > 12 && i <= 32) + { + ball.GetComponent().material.color = Color.green;//给脚部添加颜色 + } _nodes.Add(ball); @@ -57,10 +79,55 @@ private void OnReceive(List landmarks) { var poseTransform = new PoseTransform(landmark.Type) { - MediaPipePos = new Vector3(landmark.X, -landmark.Y, landmark.Z) + MediaPipePos = new Vector3(landmark.X, landmark.Y, landmark.Z) }; _poseTransforms[(int)landmark.Type] = poseTransform; } } + + //绕X轴旋转 + private Matrix4x4 RotateAxisX(float rot) + { + var matrix = new Matrix4x4(); + float cosrot = MathF.Cos(rot); + float sinrot = MathF.Sin(rot); + + matrix.SetRow(0, new Vector4(1,0,0,0)); + matrix.SetRow(1, new Vector4(0,cosrot,-sinrot,0)); + matrix.SetRow(2, new Vector4(0,sinrot,cosrot,0)); + matrix.SetRow(3, new Vector4(0,0,0,1)); + + return matrix; + } + + //绕Y轴旋转 + private Matrix4x4 RotateAxisY(float rot) + { + var matrix = new Matrix4x4(); + float cosrot = MathF.Cos(rot); + float sinrot = MathF.Sin(rot); + + matrix.SetRow(0, new Vector4(cosrot,0,sinrot,0)); + matrix.SetRow(1, new Vector4(0,1,0,0)); + matrix.SetRow(2, new Vector4(-sinrot,0,cosrot,0)); + matrix.SetRow(3, new Vector4(0,0,0,1)); + + return matrix; + } + + //绕Z轴旋转 + private Matrix4x4 RotateAxisZ(float rot) + { + var matrix = new Matrix4x4(); + float cosrot = MathF.Cos(rot); + float sinrot = MathF.Sin(rot); + + matrix.SetRow(0, new Vector4(cosrot,-sinrot,0,0)); + matrix.SetRow(1, new Vector4(sinrot,cosrot,0,0)); + matrix.SetRow(2, new Vector4(0,0,1,0)); + matrix.SetRow(3, new Vector4(0,0,0,1)); + + return matrix; + } } From ff2ae6b25ce0c6b956a6b8903956bc0dac85c133 Mon Sep 17 00:00:00 2001 From: Ichirinko <1621543655@qq.com> Date: Wed, 22 Feb 2023 19:52:36 +0800 Subject: [PATCH 08/29] =?UTF-8?q?add:=E4=B8=89=E7=BB=B4=E6=97=8B=E8=BD=AC?= =?UTF-8?q?=E7=9A=84=E4=B8=89=E4=B8=AA=E6=96=B9=E6=B3=95=EF=BC=9B=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E7=BB=99=E4=B8=8D=E5=90=8C=E9=83=A8=E4=BD=8D=E4=B8=8A?= =?UTF-8?q?=E8=89=B2=E6=96=B9=E4=BE=BF=E5=8C=BA=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/MainBehaviour.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs index 56668c0..ac1b512 100644 --- a/Assets/MainBehaviour.cs +++ b/Assets/MainBehaviour.cs @@ -32,7 +32,7 @@ private void Update() _poseTransforms[index].MediaPipePos.y, _poseTransforms[index].MediaPipePos.z,1); //temp = RotateAxisZ(180) * temp; - //warning:乘法的顺序不能改! + //warning:乘法的顺序不能改! _nodes[index].transform.position = new Vector3(temp.x, temp.y, temp.z) * 5; //_nodes[index].transform.position = _poseTransforms[index].MediaPipePos * 5; From 7de82ae644b9954a017cc85b17b6c5347c37297e Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 22 Feb 2023 20:35:20 +0800 Subject: [PATCH 09/29] =?UTF-8?q?fix:=20=E4=BA=BA=E7=89=A9=E5=9D=90?= =?UTF-8?q?=E6=A0=87=E6=97=8B=E8=BD=AC=E4=B8=BA=E6=AD=A3=E5=AF=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/MainBehaviour.cs | 13 ++-- Assets/Materials.meta | 8 ++ Assets/Materials/PlaneMaterial.mat | 80 ++++++++++++++++++++ Assets/Materials/PlaneMaterial.mat.meta | 8 ++ Assets/Scenes/BallScene.unity | 98 +++++++++++++++++++++++++ 5 files changed, 200 insertions(+), 7 deletions(-) create mode 100644 Assets/Materials.meta create mode 100644 Assets/Materials/PlaneMaterial.mat create mode 100644 Assets/Materials/PlaneMaterial.mat.meta diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs index ac1b512..b6fc64a 100644 --- a/Assets/MainBehaviour.cs +++ b/Assets/MainBehaviour.cs @@ -1,8 +1,6 @@ using System; -using System.Collections; using System.Collections.Generic; using Models; -using Unity.VisualScripting; using UnityEngine; public class MainBehaviour : MonoBehaviour @@ -10,6 +8,7 @@ public class MainBehaviour : MonoBehaviour private readonly List _nodes = new List(); private readonly List _poseTransforms = new List(); private readonly UdpListener _listener = new UdpListener(); + private const float _scale = 0.2f; // Start is called before the first frame update @@ -31,10 +30,10 @@ private void Update() var temp = new Vector4(_poseTransforms[index].MediaPipePos.x, _poseTransforms[index].MediaPipePos.y, _poseTransforms[index].MediaPipePos.z,1); - //temp = RotateAxisZ(180) * temp; + temp = RotateAxisX(-45) * temp; //warning:乘法的顺序不能改! - _nodes[index].transform.position = new Vector3(temp.x, temp.y, temp.z) * 5; + _nodes[index].transform.position = new Vector3(-temp.x, -temp.y, -temp.z) * 5; //_nodes[index].transform.position = _poseTransforms[index].MediaPipePos * 5; } } @@ -50,17 +49,17 @@ private void CreateNodes() { var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere); - ball.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); + ball.transform.localScale = new Vector3(_scale, _scale, _scale); if (i <= 10) { ball.GetComponent().material.color = Color.red;//给头部添加颜色 } - else if (i > 10 && i <= 22) + else if (i <= 22) { ball.GetComponent().material.color = Color.blue;//给手部添加颜色 } - else if (i > 12 && i <= 32) + else if (i <= 32) { ball.GetComponent().material.color = Color.green;//给脚部添加颜色 } diff --git a/Assets/Materials.meta b/Assets/Materials.meta new file mode 100644 index 0000000..c309ed8 --- /dev/null +++ b/Assets/Materials.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c2963c03969c7f6d480483b45e36590b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Materials/PlaneMaterial.mat b/Assets/Materials/PlaneMaterial.mat new file mode 100644 index 0000000..28c017d --- /dev/null +++ b/Assets/Materials/PlaneMaterial.mat @@ -0,0 +1,80 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 8 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: PlaneMaterial + m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0} + m_ValidKeywords: [] + m_InvalidKeywords: [] + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Ints: [] + m_Floats: + - _BumpScale: 1 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 1, g: 0.29922488, b: 0, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + m_BuildTextureStacks: [] diff --git a/Assets/Materials/PlaneMaterial.mat.meta b/Assets/Materials/PlaneMaterial.mat.meta new file mode 100644 index 0000000..9f5d305 --- /dev/null +++ b/Assets/Materials/PlaneMaterial.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6083fe04391087ca294999838984a59c +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/BallScene.unity b/Assets/Scenes/BallScene.unity index 3252943..b62cd46 100644 --- a/Assets/Scenes/BallScene.unity +++ b/Assets/Scenes/BallScene.unity @@ -123,6 +123,104 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} +--- !u!1 &99618223 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 99618227} + - component: {fileID: 99618226} + - component: {fileID: 99618225} + - component: {fileID: 99618224} + m_Layer: 0 + m_Name: Plane + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!64 &99618224 +MeshCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 99618223} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + serializedVersion: 4 + m_Convex: 0 + m_CookingOptions: 30 + m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0} +--- !u!23 &99618225 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 99618223} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 2 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: 6083fe04391087ca294999838984a59c, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_AdditionalVertexStreams: {fileID: 0} +--- !u!33 &99618226 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 99618223} + m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0} +--- !u!4 &99618227 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 99618223} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 1 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 3 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &182244630 GameObject: m_ObjectHideFlags: 0 From d13b0b0b2bddfaa6458f351535c9c869c58179ff Mon Sep 17 00:00:00 2001 From: Ichirinko <1621543655@qq.com> Date: Fri, 24 Feb 2023 18:18:56 +0800 Subject: [PATCH 10/29] =?UTF-8?q?add:=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=90=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E4=B9=8B=E9=97=B4=E7=9A=84=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=9E=84=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Bonds.cs | 49 ++++++++++++++++ Assets/Bonds.cs.meta | 3 + Assets/MainBehaviour.cs | 122 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 Assets/Bonds.cs create mode 100644 Assets/Bonds.cs.meta diff --git a/Assets/Bonds.cs b/Assets/Bonds.cs new file mode 100644 index 0000000..bd58eac --- /dev/null +++ b/Assets/Bonds.cs @@ -0,0 +1,49 @@ +using UnityEngine; + +namespace Models +{ + public class Bonds + { + private GameObject start; + private GameObject end; + private GameObject bond; + + + public Bonds(GameObject start,GameObject end,float scale) + { + this.start = start; + this.end = end; + + bond = GameObject.CreatePrimitive(PrimitiveType.Cylinder); + + bond.transform.localScale = new Vector3(scale/2, scale/2, scale/2); + } + + + public void UpdateBond() + { + var startpos = start.transform.position; + var endpos = end.transform.position; + Vector3 rightPosition = (startpos + endpos) / 2; + Vector3 rightRotation = endpos - startpos; + float HalfLength = Vector3.Distance(startpos, endpos) / 2; + float LThickness = 0.1f;//线的粗细 + + //创建圆柱体 + + //bond.gameObject.transform.parent = transform; + bond.transform.position = rightPosition; + bond.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation); + bond.transform.localScale = new Vector3(LThickness, HalfLength, LThickness); + + //这里可以设置材质,具体自己设置 + bond.GetComponent().material.color = Color.cyan; + + } + } + + + + + +} \ No newline at end of file diff --git a/Assets/Bonds.cs.meta b/Assets/Bonds.cs.meta new file mode 100644 index 0000000..5dc5c31 --- /dev/null +++ b/Assets/Bonds.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 94a8f8c83e58421d9447173d38482d74 +timeCreated: 1677230676 \ No newline at end of file diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs index b6fc64a..7b3787e 100644 --- a/Assets/MainBehaviour.cs +++ b/Assets/MainBehaviour.cs @@ -6,6 +6,7 @@ public class MainBehaviour : MonoBehaviour { private readonly List _nodes = new List(); + private readonly List _bonds = new List(); private readonly List _poseTransforms = new List(); private readonly UdpListener _listener = new UdpListener(); private const float _scale = 0.2f; @@ -15,7 +16,7 @@ public class MainBehaviour : MonoBehaviour private void Start() { CreateNodes(); - + CreateBonds(); _listener.AddHandler(OnReceive); _listener.Connect(5000); } @@ -30,12 +31,17 @@ private void Update() var temp = new Vector4(_poseTransforms[index].MediaPipePos.x, _poseTransforms[index].MediaPipePos.y, _poseTransforms[index].MediaPipePos.z,1); - temp = RotateAxisX(-45) * temp; + //temp = RotateAxisX(-45) * temp; //warning:乘法的顺序不能改! _nodes[index].transform.position = new Vector3(-temp.x, -temp.y, -temp.z) * 5; //_nodes[index].transform.position = _poseTransforms[index].MediaPipePos * 5; } + + foreach (var bond in _bonds) + { + bond.UpdateBond(); + } } private void OnDisable() @@ -72,6 +78,118 @@ private void CreateNodes() } } + private void CreateBonds() + { + //shit code,it's my fault + Bonds temp; + temp = new Bonds(_nodes[0], _nodes[4], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[0], _nodes[1], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[1], _nodes[2], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[2], _nodes[3], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[3], _nodes[7], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[4], _nodes[5], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[5], _nodes[6], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[6], _nodes[8], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[9], _nodes[10], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[2], _nodes[3], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[11], _nodes[12], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[12], _nodes[14], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[14], _nodes[16], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[16], _nodes[22], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[16], _nodes[18], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[18], _nodes[20], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[16], _nodes[20], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[11], _nodes[13], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[13], _nodes[15], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[15], _nodes[17], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[15], _nodes[21], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[15], _nodes[19], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[17], _nodes[19], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[12], _nodes[24], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[23], _nodes[24], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[11], _nodes[23], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[24], _nodes[26], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[26], _nodes[28], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[28], _nodes[32], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[28], _nodes[30], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[23], _nodes[25], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[25], _nodes[27], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[27], _nodes[29], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[27], _nodes[31], _scale); + _bonds.Add(temp); + + temp = new Bonds(_nodes[29], _nodes[31], _scale); + _bonds.Add(temp); + } + + + private void OnReceive(List landmarks) { foreach (var landmark in landmarks) From 3685ecee0813dd3b8a37d874b7e8420e02695d97 Mon Sep 17 00:00:00 2001 From: Ichirinko <1621543655@qq.com> Date: Sat, 25 Feb 2023 11:19:47 +0800 Subject: [PATCH 11/29] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=97=8B?= =?UTF-8?q?=E8=BD=AC=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/ModelBehaviour.cs | 219 ++++-------------------------- Assets/Models/PosTransformator.cs | 6 +- Assets/Models/PoseTransform.cs | 78 ++++++++++- Assets/Scenes/SampleScene.unity | 13 +- 4 files changed, 121 insertions(+), 195 deletions(-) diff --git a/Assets/ModelBehaviour.cs b/Assets/ModelBehaviour.cs index c153a57..5353c58 100644 --- a/Assets/ModelBehaviour.cs +++ b/Assets/ModelBehaviour.cs @@ -8,7 +8,7 @@ public class ModelBehaviour : MonoBehaviour private readonly UdpListener _listener = new UdpListener(); - private static readonly List _posLandmarks = new List(); + //private static readonly List _posLandmarks = new List(); private static readonly List PoseTransforms = new List(); @@ -16,10 +16,10 @@ private void Start() { _animator = GetComponent(); // 获取动画控件 - TransformatorInit(); //匹配初始化 + InitPoseTransformList(); - _listener.AddHandler(LogLandmarks); + //_listener.AddHandler(LogLandmarks); _listener.AddHandler(RigPoint); _listener.Connect(5000); } @@ -28,11 +28,17 @@ private void Start() private void Update() { - - foreach (var landmark in _posLandmarks) + + foreach (var landmark in PoseTransforms) { - //animator.GetBoneTransform(landmark.unityName).rotation = landmark.currentQ; - + if (landmark.UnityName != HumanBodyBones.LastBone) + { + + _animator.GetBoneTransform(landmark.UnityName).rotation = landmark.CurrentQuaternion; + + } + + } @@ -55,12 +61,15 @@ private static void LogLandmarks(List landmarks) //获取传上来的数据点坐标转换成四元数的回调函数 private static void RigPoint(List landmarks) { - + foreach (var landmark in landmarks) { - PoseTransforms[(int)landmark.Type].CalculateCurrentQuaternion(landmark); - - + var temp = PoseTransforms[(int)landmark.Type]; + temp.CalculateCurrentQuaternion(landmark); + PoseTransforms[(int)landmark.Type] = temp; + //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); @@ -101,32 +110,32 @@ private void InitPoseTransformList() 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); } @@ -142,174 +151,4 @@ private void AddParentTransform(PoseLandmarkType target, PoseLandmarkType parent parents.Add(PoseTransforms[(int)parent]); } - private void TransformatorInit() - { - - /*下面的语句的顺序最好别调换,不然可能会发生难以预料的事情*/ - - PosTransformator[] transformator = new PosTransformator[15]; - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "LeftHip"; - temp.unityName = HumanBodyBones.LeftUpperLeg; - temp.parent.Add(null); - transformator[0] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "RightHip"; - temp.unityName = HumanBodyBones.RightUpperLeg; - temp.parent.Add(null); - transformator[1] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "LeftShoulder"; - temp.unityName = HumanBodyBones.LeftUpperArm; - temp.parent.Add(transformator[0]); - temp.parent.Add(transformator[1]); - transformator[2] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "RightShoulder"; - temp.unityName = HumanBodyBones.RightUpperArm; - temp.parent.Add(transformator[0]); - temp.parent.Add(transformator[1]); - transformator[3] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "Nose"; - temp.unityName = HumanBodyBones.Head; - temp.parent.Add(transformator[2]); - temp.parent.Add(transformator[3]); - transformator[4] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "LeftElbow"; - temp.unityName = HumanBodyBones.LeftLowerArm; - temp.parent.Add(transformator[2]); - transformator[5] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "RightElbow"; - temp.unityName = HumanBodyBones.RightLowerArm; - temp.parent.Add(transformator[3]); - transformator[6] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "LeftWrist"; - temp.unityName = HumanBodyBones.LeftHand; - temp.parent.Add(transformator[5]); - transformator[7] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "RightWrist"; - temp.unityName = HumanBodyBones.RightHand; - temp.parent.Add(transformator[6]); - transformator[8] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "LeftKnee"; - temp.unityName = HumanBodyBones.LeftLowerLeg; - temp.parent.Add(transformator[0]); - transformator[9] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "RightKnee"; - temp.unityName = HumanBodyBones.RightLowerLeg; - temp.parent.Add(transformator[1]); - transformator[10] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "LeftAnkle"; - temp.unityName = HumanBodyBones.LeftFoot; - temp.parent.Add(transformator[9]); - transformator[11] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "RightAnkle"; - temp.unityName = HumanBodyBones.RightFoot; - temp.parent.Add(transformator[10]); - transformator[12] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "LeftFootIndex"; - temp.unityName = HumanBodyBones.LeftToes; - temp.parent.Add(transformator[11]); - transformator[13] = temp; - } - - { - var temp = new PosTransformator(); - temp.mediaPipeName = "RightFootIndex"; - temp.unityName = HumanBodyBones.RightToes; - temp.parent.Add(transformator[12]); - transformator[14] = temp; - } - - //Landmarks_Mapping.Add("Nose",HumanBodyBones.Head); //0 10 - - //Landmarks_Mapping.Add("LeftShoulder",HumanBodyBones.LeftUpperArm); //11 - - //Landmarks_Mapping.Add("RightShoulder",HumanBodyBones.RightUpperArm); //12 - - //Landmarks_Mapping.Add("LeftElbow",HumanBodyBones.LeftLowerArm); //13 - - //Landmarks_Mapping.Add("RightElbow",HumanBodyBones.RightLowerArm); //14 - - //Landmarks_Mapping.Add("LeftWrist",HumanBodyBones.LeftHand); //15 - - //Landmarks_Mapping.Add("RightWrist",HumanBodyBones.RightHand); //16 - - //Landmarks_Mapping.Add("LeftHip",HumanBodyBones.LeftUpperLeg); //23 - - //Landmarks_Mapping.Add("RightHip",HumanBodyBones.RightUpperLeg); //24 - - //Landmarks_Mapping.Add("LeftKnee",HumanBodyBones.LeftLowerLeg); //25 - - //Landmarks_Mapping.Add("RightKnee",HumanBodyBones.RightLowerLeg); //26 - - //Landmarks_Mapping.Add("LeftAnkle",HumanBodyBones.LeftFoot); //27 - - //Landmarks_Mapping.Add("RightAnkle",HumanBodyBones.RightFoot); //28 - - //Landmarks_Mapping.Add("LeftFootIndex",HumanBodyBones.LeftToes); //31 - - //Landmarks_Mapping.Add("RightFootIndex",HumanBodyBones.RightToes); //32 - - foreach (var temp in transformator) - { - temp.prevQ = _animator.GetBoneTransform(temp.unityName).rotation; //获得初始角度 - - temp.currentQ = temp.prevQ; - - _posLandmarks.Add(temp); - } - - } -} \ No newline at end of file +} diff --git a/Assets/Models/PosTransformator.cs b/Assets/Models/PosTransformator.cs index ae4386e..2f82a7b 100644 --- a/Assets/Models/PosTransformator.cs +++ b/Assets/Models/PosTransformator.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +/* +using System.Collections.Generic; using UnityEngine; namespace Models @@ -23,4 +24,5 @@ public class PosTransformator public Quaternion currentQ; //该骨骼节点的当前角度 } -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/Assets/Models/PoseTransform.cs b/Assets/Models/PoseTransform.cs index d20e446..1fb2f9a 100644 --- a/Assets/Models/PoseTransform.cs +++ b/Assets/Models/PoseTransform.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using UnityEngine; +using System; namespace Models { @@ -21,7 +22,7 @@ public struct PoseTransform /// /// MediaPipe捕捉空间坐标 /// - public Vector3 MediaPipePos; + public Vector3 MediaPipePos; /// /// 节点的父节点列表 @@ -57,6 +58,35 @@ PoseLandmarkType type public void CalculateCurrentQuaternion(PoseLandmark landmark) { + //var temp = _posLandmarks.Find(t=>t.mediaPipeName == landmark.Type.ToString()); + var sum = new Vector3(0,0,0); + + if (landmark.Visibility > 0.6) + { + var temp = new Vector4(landmark.X, landmark.Y, landmark.Z, 1); + + + MediaPipePos = new Vector3(-temp.x, -temp.y, -temp.z); + + } + + if (UnityName != HumanBodyBones.LastBone) + { + Debug.Log(UnityName+".MediaPipePos:"+MediaPipePos); + } + + foreach (var parents in Parent) + { + + sum += parents.MediaPipePos; + //Debug.Log("parents:"+parents.UnityName+".MediaPipePos:"+parents.MediaPipePos); + + } + //Debug.Log("sum:"+sum); + var currentPos = sum / Parent.Count; + + CurrentQuaternion = Quaternion.LookRotation(MediaPipePos - currentPos); + } /// @@ -104,5 +134,51 @@ private static HumanBodyBones GetRelatedBone(PoseLandmarkType type) } + //绕X轴旋转 + private Matrix4x4 RotateAxisX(float rot) + { + var matrix = new Matrix4x4(); + float cosrot = MathF.Cos(rot); + float sinrot = MathF.Sin(rot); + + matrix.SetRow(0, new Vector4(1,0,0,0)); + matrix.SetRow(1, new Vector4(0,cosrot,-sinrot,0)); + matrix.SetRow(2, new Vector4(0,sinrot,cosrot,0)); + matrix.SetRow(3, new Vector4(0,0,0,1)); + + return matrix; + } + + //绕Y轴旋转 + private Matrix4x4 RotateAxisY(float rot) + { + var matrix = new Matrix4x4(); + float cosrot = MathF.Cos(rot); + float sinrot = MathF.Sin(rot); + + matrix.SetRow(0, new Vector4(cosrot,0,sinrot,0)); + matrix.SetRow(1, new Vector4(0,1,0,0)); + matrix.SetRow(2, new Vector4(-sinrot,0,cosrot,0)); + matrix.SetRow(3, new Vector4(0,0,0,1)); + + return matrix; + } + + //绕Z轴旋转 + private Matrix4x4 RotateAxisZ(float rot) + { + var matrix = new Matrix4x4(); + float cosrot = MathF.Cos(rot); + float sinrot = MathF.Sin(rot); + + matrix.SetRow(0, new Vector4(cosrot,-sinrot,0,0)); + matrix.SetRow(1, new Vector4(sinrot,cosrot,0,0)); + matrix.SetRow(2, new Vector4(0,0,1,0)); + matrix.SetRow(3, new Vector4(0,0,0,1)); + + return matrix; + } + + } } \ No newline at end of file diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 4aadfb8..72a28c8 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.18029127, g: 0.22572401, b: 0.3069303, a: 1} + m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -174,6 +174,10 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} + - target: {fileID: -2845779275431937956, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_IsActive + value: 0 + objectReference: {fileID: 0} - target: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} propertyPath: m_Name value: troop @@ -197,6 +201,11 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: a2b30b6aa6e08c4468a02e1fcabcd485, 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 @@ -388,7 +397,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 9189b565e8ba9bfc3bdaa38149e3e13f, type: 3} m_Name: m_EditorClassIdentifier: - target: {fileID: 0} + target: {fileID: 404755309} --- !u!1 &1880773849 GameObject: m_ObjectHideFlags: 0 From 31ce446737d7795837a2df8b25df41d9d3a8c967 Mon Sep 17 00:00:00 2001 From: Ichirinko <1621543655@qq.com> Date: Sun, 26 Feb 2023 21:02:08 +0800 Subject: [PATCH 12/29] =?UTF-8?q?add:=E5=A2=9E=E5=8A=A0=E4=BA=86=E8=99=9A?= =?UTF-8?q?=E6=8B=9F=E9=AA=A8=E9=AA=BC=EF=BC=8C=E7=94=A8=E4=BA=8E=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E6=96=B9=E5=90=91=E8=AE=A1=E7=AE=97=E7=9A=84=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Bonds.cs | 11 +++++--- Assets/MainBehaviour.cs | 23 ++++++++++++++--- Assets/Scenes/BallScene.unity | 8 +++--- Assets/VirtualSkeleton.cs | 46 ++++++++++++++++++++++++++++++++++ Assets/VirtualSkeleton.cs.meta | 3 +++ 5 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 Assets/VirtualSkeleton.cs create mode 100644 Assets/VirtualSkeleton.cs.meta diff --git a/Assets/Bonds.cs b/Assets/Bonds.cs index bd58eac..574f966 100644 --- a/Assets/Bonds.cs +++ b/Assets/Bonds.cs @@ -4,8 +4,8 @@ namespace Models { public class Bonds { - private GameObject start; - private GameObject end; + public GameObject start; + public GameObject end; private GameObject bond; @@ -17,6 +17,9 @@ public Bonds(GameObject start,GameObject end,float scale) bond = GameObject.CreatePrimitive(PrimitiveType.Cylinder); bond.transform.localScale = new Vector3(scale/2, scale/2, scale/2); + + //这里可以设置材质,具体自己设置 + bond.GetComponent().material.color = Color.cyan; } @@ -36,8 +39,8 @@ public void UpdateBond() bond.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation); bond.transform.localScale = new Vector3(LThickness, HalfLength, LThickness); - //这里可以设置材质,具体自己设置 - bond.GetComponent().material.color = Color.cyan; + + } } diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs index 7b3787e..c3272ce 100644 --- a/Assets/MainBehaviour.cs +++ b/Assets/MainBehaviour.cs @@ -7,16 +7,19 @@ public class MainBehaviour : MonoBehaviour { private readonly List _nodes = new List(); private readonly List _bonds = new List(); + private readonly List _virtualSkeletons = new List(); private readonly List _poseTransforms = new List(); private readonly UdpListener _listener = new UdpListener(); private const float _scale = 0.2f; - + private GameObject sample ; // Start is called before the first frame update private void Start() { CreateNodes(); CreateBonds(); + + _listener.AddHandler(OnReceive); _listener.Connect(5000); } @@ -42,6 +45,11 @@ private void Update() { bond.UpdateBond(); } + + /*foreach (var vs in _virtualSkeletons) + { + vs.UpdateVS(); + }*/ } private void OnDisable() @@ -186,9 +194,18 @@ private void CreateBonds() temp = new Bonds(_nodes[29], _nodes[31], _scale); _bonds.Add(temp); + + + + /*foreach (var bond in _bonds) + { + var temp2 = new VirtualSkeleton(bond.start, bond.end, _scale); + _virtualSkeletons.Add(temp2); + }*/ + } - - + + private void OnReceive(List landmarks) { diff --git a/Assets/Scenes/BallScene.unity b/Assets/Scenes/BallScene.unity index b62cd46..b8324e2 100644 --- a/Assets/Scenes/BallScene.unity +++ b/Assets/Scenes/BallScene.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} - m_IndirectSpecularColor: {r: 0.18029127, g: 0.22572401, b: 0.3069303, a: 1} + m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -436,14 +436,14 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 2045151417} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 1, z: -7.44} + m_LocalRotation: {x: 0, y: 1, z: 0, w: 0} + m_LocalPosition: {x: 0, y: 0, z: 7} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} m_RootOrder: 0 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_LocalEulerAnglesHint: {x: 0, y: 180, z: 0} --- !u!114 &2045151421 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/VirtualSkeleton.cs b/Assets/VirtualSkeleton.cs new file mode 100644 index 0000000..4e286b2 --- /dev/null +++ b/Assets/VirtualSkeleton.cs @@ -0,0 +1,46 @@ +using UnityEngine; +using UnityEngine.PlayerLoop; + +namespace Models +{ + public class VirtualSkeleton : Bonds + { + //private GameObject start; + //private GameObject end; + private GameObject virtualskeleton; + + + + public VirtualSkeleton(GameObject start,GameObject end,float scale) : base(start,end,scale) + { + this.start = start; + this.end = end; + + virtualskeleton = GameObject.CreatePrimitive(PrimitiveType.Cylinder); + + virtualskeleton.transform.localScale = new Vector3(scale/2, scale/2, scale/2); + + virtualskeleton.GetComponent().material.color = Color.white; + } + + public void UpdateVS() + { + + var startpos = start.transform.position; + var endpos = end.transform.position; + Vector3 rightPosition = (startpos + endpos) / 2; + Vector3 rightRotation = endpos - startpos; + + float LThickness = 0.2f;//线的粗细 + + //创建圆柱体 + + //bond.gameObject.transform.parent = transform; + virtualskeleton.transform.position = rightPosition; + virtualskeleton.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation); + //virtualskeleton.transform.rotation = Quaternion.LookRotation(rightRotation, Vector3.forward); + virtualskeleton.transform.localScale = new Vector3(LThickness, LThickness, LThickness); + } + + } +} \ No newline at end of file diff --git a/Assets/VirtualSkeleton.cs.meta b/Assets/VirtualSkeleton.cs.meta new file mode 100644 index 0000000..067f9c7 --- /dev/null +++ b/Assets/VirtualSkeleton.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5f725336fc6a4481a4c7d74ec0e3b17b +timeCreated: 1677413910 \ No newline at end of file From 7199424893ca3bc08e9b30caa2e0778d5a72b8d9 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 26 Feb 2023 23:50:14 +0800 Subject: [PATCH 13/29] =?UTF-8?q?refact:=20=E5=B0=86=E6=97=8B=E8=BD=AC?= =?UTF-8?q?=E7=9F=A9=E9=98=B5=E7=9A=84=E8=AE=A1=E7=AE=97=E6=8A=BD=E8=B1=A1?= =?UTF-8?q?=E4=B8=BA=E6=97=8B=E8=BD=AC=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/MainBehaviour.cs | 49 +---------------------- Assets/Utils/RotateUtils.cs | 67 ++++++++++++++++++++++++++++++++ Assets/Utils/RotateUtils.cs.meta | 3 ++ 3 files changed, 71 insertions(+), 48 deletions(-) create mode 100644 Assets/Utils/RotateUtils.cs create mode 100644 Assets/Utils/RotateUtils.cs.meta diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs index c3272ce..347103e 100644 --- a/Assets/MainBehaviour.cs +++ b/Assets/MainBehaviour.cs @@ -88,9 +88,7 @@ private void CreateNodes() private void CreateBonds() { - //shit code,it's my fault - Bonds temp; - temp = new Bonds(_nodes[0], _nodes[4], _scale); + var temp = new Bonds(_nodes[0], _nodes[4], _scale); _bonds.Add(temp); temp = new Bonds(_nodes[0], _nodes[1], _scale); @@ -219,49 +217,4 @@ private void OnReceive(List landmarks) _poseTransforms[(int)landmark.Type] = poseTransform; } } - - //绕X轴旋转 - private Matrix4x4 RotateAxisX(float rot) - { - var matrix = new Matrix4x4(); - float cosrot = MathF.Cos(rot); - float sinrot = MathF.Sin(rot); - - matrix.SetRow(0, new Vector4(1,0,0,0)); - matrix.SetRow(1, new Vector4(0,cosrot,-sinrot,0)); - matrix.SetRow(2, new Vector4(0,sinrot,cosrot,0)); - matrix.SetRow(3, new Vector4(0,0,0,1)); - - return matrix; - } - - //绕Y轴旋转 - private Matrix4x4 RotateAxisY(float rot) - { - var matrix = new Matrix4x4(); - float cosrot = MathF.Cos(rot); - float sinrot = MathF.Sin(rot); - - matrix.SetRow(0, new Vector4(cosrot,0,sinrot,0)); - matrix.SetRow(1, new Vector4(0,1,0,0)); - matrix.SetRow(2, new Vector4(-sinrot,0,cosrot,0)); - matrix.SetRow(3, new Vector4(0,0,0,1)); - - return matrix; - } - - //绕Z轴旋转 - private Matrix4x4 RotateAxisZ(float rot) - { - var matrix = new Matrix4x4(); - float cosrot = MathF.Cos(rot); - float sinrot = MathF.Sin(rot); - - matrix.SetRow(0, new Vector4(cosrot,-sinrot,0,0)); - matrix.SetRow(1, new Vector4(sinrot,cosrot,0,0)); - matrix.SetRow(2, new Vector4(0,0,1,0)); - matrix.SetRow(3, new Vector4(0,0,0,1)); - - return matrix; - } } diff --git a/Assets/Utils/RotateUtils.cs b/Assets/Utils/RotateUtils.cs new file mode 100644 index 0000000..5a8e52f --- /dev/null +++ b/Assets/Utils/RotateUtils.cs @@ -0,0 +1,67 @@ +using System; +using UnityEngine; + +namespace Utils +{ + /// + /// 旋转工具类 + /// + public static class RotateUtils + { + private static Matrix4x4 _matrix; + + /// + /// 获得绕X轴旋转矩阵 + /// + /// 旋转的角度 + /// + public static Matrix4x4 RotateAxisX(float degree) + { + var sin = MathF.Sin(degree); + var cos = MathF.Cos(degree); + + _matrix.SetRow(0 ,new Vector4(1,0,0,0)); + _matrix.SetRow(1, new Vector4(0, cos, -sin, 0)); + _matrix.SetRow(2, new Vector4(0, sin, cos, 0)); + _matrix.SetRow(3, new Vector4(0, 0, 0, 1)); + + return _matrix; + } + + /// + /// 获得绕Y轴旋转矩阵 + /// + /// 旋转的角度 + /// + public static Matrix4x4 RotateAxisY(float degree) + { + var sin = MathF.Sin(degree); + var cos = MathF.Cos(degree); + + _matrix.SetRow(0, new Vector4(cos, 0, sin, 0)); + _matrix.SetRow(1, new Vector4(0, 1, 0, 1)); + _matrix.SetRow(2, new Vector4(-sin, 0, cos, 0)); + _matrix.SetRow(3, new Vector4(0, 0, 0, 1)); + + return _matrix; + } + + /// + /// 获得绕Z轴旋转矩阵 + /// + /// 旋转的角度 + /// + public static Matrix4x4 RotateAxisZ(float degree) + { + var sin = MathF.Sin(degree); + var cos = MathF.Cos(degree); + + _matrix.SetRow(0, new Vector4(cos, -sin, 0, 0)); + _matrix.SetRow(1, new Vector4(sin, cos, 0, 0)); + _matrix.SetRow(2, new Vector4(0, 0, 1, 0)); + _matrix.SetRow(3, new Vector4(0, 0, 0, 1)); + + return _matrix; + } + } +} \ No newline at end of file diff --git a/Assets/Utils/RotateUtils.cs.meta b/Assets/Utils/RotateUtils.cs.meta new file mode 100644 index 0000000..3f6599f --- /dev/null +++ b/Assets/Utils/RotateUtils.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d748c6669d714e65a5ca9164c5798583 +timeCreated: 1677319275 \ No newline at end of file From 5113929728e2040685c3e21fc0fd747dfeab53d8 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 27 Feb 2023 11:07:44 +0800 Subject: [PATCH 14/29] =?UTF-8?q?feature:=20=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=96=B0=E5=BB=BA=E6=A3=8D=E5=AD=90=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20refact:=20=E9=87=8D=E6=9E=84=E4=BA=86Bond?= =?UTF-8?q?=E5=92=8CVirutalSkeleton=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Bonds.cs | 52 ----- Assets/MainBehaviour.cs | 183 +++++++++++++----- Assets/Models/Bond.cs | 44 +++++ Assets/{Bonds.cs.meta => Models/Bond.cs.meta} | 0 Assets/Models/VirtualSkeleton.cs | 35 ++++ Assets/{ => Models}/VirtualSkeleton.cs.meta | 0 Assets/VirtualSkeleton.cs | 46 ----- 7 files changed, 214 insertions(+), 146 deletions(-) delete mode 100644 Assets/Bonds.cs create mode 100644 Assets/Models/Bond.cs rename Assets/{Bonds.cs.meta => Models/Bond.cs.meta} (100%) create mode 100644 Assets/Models/VirtualSkeleton.cs rename Assets/{ => Models}/VirtualSkeleton.cs.meta (100%) delete mode 100644 Assets/VirtualSkeleton.cs diff --git a/Assets/Bonds.cs b/Assets/Bonds.cs deleted file mode 100644 index 574f966..0000000 --- a/Assets/Bonds.cs +++ /dev/null @@ -1,52 +0,0 @@ -using UnityEngine; - -namespace Models -{ - public class Bonds - { - public GameObject start; - public GameObject end; - private GameObject bond; - - - public Bonds(GameObject start,GameObject end,float scale) - { - this.start = start; - this.end = end; - - bond = GameObject.CreatePrimitive(PrimitiveType.Cylinder); - - bond.transform.localScale = new Vector3(scale/2, scale/2, scale/2); - - //这里可以设置材质,具体自己设置 - bond.GetComponent().material.color = Color.cyan; - } - - - public void UpdateBond() - { - var startpos = start.transform.position; - var endpos = end.transform.position; - Vector3 rightPosition = (startpos + endpos) / 2; - Vector3 rightRotation = endpos - startpos; - float HalfLength = Vector3.Distance(startpos, endpos) / 2; - float LThickness = 0.1f;//线的粗细 - - //创建圆柱体 - - //bond.gameObject.transform.parent = transform; - bond.transform.position = rightPosition; - bond.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation); - bond.transform.localScale = new Vector3(LThickness, HalfLength, LThickness); - - - - - } - } - - - - - -} \ No newline at end of file diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs index 347103e..51b5518 100644 --- a/Assets/MainBehaviour.cs +++ b/Assets/MainBehaviour.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using Models; using UnityEngine; @@ -6,11 +5,11 @@ public class MainBehaviour : MonoBehaviour { private readonly List _nodes = new List(); - private readonly List _bonds = new List(); + private readonly List _bonds = new List(); private readonly List _virtualSkeletons = new List(); private readonly List _poseTransforms = new List(); private readonly UdpListener _listener = new UdpListener(); - private const float _scale = 0.2f; + private const float Scale = 0.2f; private GameObject sample ; // Start is called before the first frame update @@ -63,7 +62,7 @@ private void CreateNodes() { var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere); - ball.transform.localScale = new Vector3(_scale, _scale, _scale); + ball.transform.localScale = new Vector3(Scale, Scale, Scale); if (i <= 10) { @@ -88,122 +87,190 @@ private void CreateNodes() private void CreateBonds() { - var temp = new Bonds(_nodes[0], _nodes[4], _scale); + /*var temp = new Bonds(_nodes[0], _nodes[4], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[0], _nodes[1], _scale); + temp = new Bonds(_nodes[0], _nodes[1], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[1], _nodes[2], _scale); + temp = new Bonds(_nodes[1], _nodes[2], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[2], _nodes[3], _scale); + temp = new Bonds(_nodes[2], _nodes[3], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[3], _nodes[7], _scale); + temp = new Bonds(_nodes[3], _nodes[7], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[4], _nodes[5], _scale); + temp = new Bonds(_nodes[4], _nodes[5], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[5], _nodes[6], _scale); + temp = new Bonds(_nodes[5], _nodes[6], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[6], _nodes[8], _scale); + temp = new Bonds(_nodes[6], _nodes[8], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[9], _nodes[10], _scale); + temp = new Bonds(_nodes[9], _nodes[10], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[2], _nodes[3], _scale); + temp = new Bonds(_nodes[2], _nodes[3], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[11], _nodes[12], _scale); + temp = new Bonds(_nodes[11], _nodes[12], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[12], _nodes[14], _scale); + temp = new Bonds(_nodes[12], _nodes[14], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[14], _nodes[16], _scale); + temp = new Bonds(_nodes[14], _nodes[16], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[16], _nodes[22], _scale); + temp = new Bonds(_nodes[16], _nodes[22], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[16], _nodes[18], _scale); + temp = new Bonds(_nodes[16], _nodes[18], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[18], _nodes[20], _scale); + temp = new Bonds(_nodes[18], _nodes[20], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[16], _nodes[20], _scale); + temp = new Bonds(_nodes[16], _nodes[20], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[11], _nodes[13], _scale); + temp = new Bonds(_nodes[11], _nodes[13], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[13], _nodes[15], _scale); + temp = new Bonds(_nodes[13], _nodes[15], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[15], _nodes[17], _scale); + temp = new Bonds(_nodes[15], _nodes[17], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[15], _nodes[21], _scale); + temp = new Bonds(_nodes[15], _nodes[21], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[15], _nodes[19], _scale); + temp = new Bonds(_nodes[15], _nodes[19], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[17], _nodes[19], _scale); + temp = new Bonds(_nodes[17], _nodes[19], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[12], _nodes[24], _scale); + temp = new Bonds(_nodes[12], _nodes[24], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[23], _nodes[24], _scale); + temp = new Bonds(_nodes[23], _nodes[24], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[11], _nodes[23], _scale); + temp = new Bonds(_nodes[11], _nodes[23], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[24], _nodes[26], _scale); + temp = new Bonds(_nodes[24], _nodes[26], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[26], _nodes[28], _scale); + temp = new Bonds(_nodes[26], _nodes[28], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[28], _nodes[32], _scale); + temp = new Bonds(_nodes[28], _nodes[32], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[28], _nodes[30], _scale); + temp = new Bonds(_nodes[28], _nodes[30], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[23], _nodes[25], _scale); + temp = new Bonds(_nodes[23], _nodes[25], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[25], _nodes[27], _scale); + temp = new Bonds(_nodes[25], _nodes[27], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[27], _nodes[29], _scale); + temp = new Bonds(_nodes[27], _nodes[29], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[27], _nodes[31], _scale); + temp = new Bonds(_nodes[27], _nodes[31], Scale); _bonds.Add(temp); - temp = new Bonds(_nodes[29], _nodes[31], _scale); - _bonds.Add(temp); + temp = new Bonds(_nodes[29], _nodes[31], Scale); + _bonds.Add(temp);*/ - - - /*foreach (var bond in _bonds) + var headBonds = new[] { - var temp2 = new VirtualSkeleton(bond.start, bond.end, _scale); - _virtualSkeletons.Add(temp2); - }*/ + PoseLandmarkType.RightEar, + PoseLandmarkType.RightEyeOuter, + PoseLandmarkType.RightEye, + PoseLandmarkType.RightEyeInner, + PoseLandmarkType.Nose, + PoseLandmarkType.LeftEyeInner, + PoseLandmarkType.LeftEye, + PoseLandmarkType.LeftEyeOuter, + PoseLandmarkType.LeftEar + }; + _bonds.AddRange(GenerateBondsList(headBonds)); + + var monthBonds = new[] + { + PoseLandmarkType.MouthLeft, + PoseLandmarkType.MouthRight, + }; + _bonds.AddRange(GenerateBondsList(monthBonds)); + + var leftArmBonds = new[] + { + PoseLandmarkType.LeftShoulder, + PoseLandmarkType.LeftElbow, + PoseLandmarkType.LeftWrist, + PoseLandmarkType.LeftPinky, + PoseLandmarkType.LeftIndex, + PoseLandmarkType.LeftWrist, + PoseLandmarkType.LeftThumb + }; + _bonds.AddRange(GenerateBondsList(leftArmBonds)); + + var rightArmBonds = new[] + { + PoseLandmarkType.RightShoulder, + PoseLandmarkType.RightElbow, + PoseLandmarkType.RightWrist, + PoseLandmarkType.RightPinky, + PoseLandmarkType.RightIndex, + PoseLandmarkType.RightWrist, + PoseLandmarkType.RightThumb + }; + _bonds.AddRange(GenerateBondsList(rightArmBonds)); + + var leftLegBonds = new[] + { + PoseLandmarkType.LeftShoulder, + PoseLandmarkType.LeftHip, + PoseLandmarkType.LeftKnee, + PoseLandmarkType.LeftAnkle, + PoseLandmarkType.LeftHeel, + PoseLandmarkType.LeftFootIndex, + PoseLandmarkType.LeftAnkle + }; + _bonds.AddRange(GenerateBondsList(leftLegBonds)); + + var rightLegBonds = new[] + { + PoseLandmarkType.RightShoulder, + PoseLandmarkType.RightHip, + PoseLandmarkType.RightKnee, + PoseLandmarkType.RightAnkle, + PoseLandmarkType.RightHeel, + PoseLandmarkType.RightFootIndex, + PoseLandmarkType.RightAnkle + }; + _bonds.AddRange(GenerateBondsList(rightLegBonds)); + // 最后手动添加身体上的两条横线 + _bonds.Add(new Bond( + _nodes[(int)PoseLandmarkType.LeftShoulder], + _nodes[(int)PoseLandmarkType.RightShoulder], + Scale)); + _bonds.Add(new Bond( + _nodes[(int)PoseLandmarkType.LeftHip], + _nodes[(int)PoseLandmarkType.RightHip], + Scale)); } - - private void OnReceive(List landmarks) { @@ -217,4 +284,24 @@ private void OnReceive(List landmarks) _poseTransforms[(int)landmark.Type] = poseTransform; } } + + /// + /// 创建棍子列表 + /// + /// 需要连接起来的关键点 需要按顺序设置 + /// + private List GenerateBondsList(PoseLandmarkType[] nodes) + { + var bonds = new List(); + + for (var i = 0; i < nodes.Length - 1; i++) + { + bonds.Add(new Bond( + _nodes[(int)nodes[i]], + _nodes[(int)nodes[i + 1]], + Scale)); + } + + return bonds; + } } diff --git a/Assets/Models/Bond.cs b/Assets/Models/Bond.cs new file mode 100644 index 0000000..a6868d1 --- /dev/null +++ b/Assets/Models/Bond.cs @@ -0,0 +1,44 @@ +using UnityEngine; + +namespace Models +{ + /// + /// 捕捉点之间的连接线 + /// + public class Bond + { + protected readonly GameObject Start; + protected readonly GameObject End; + private readonly GameObject _bond; + + public Bond(GameObject start,GameObject end,float scale) + { + Start = start; + End = end; + + _bond = GameObject.CreatePrimitive(PrimitiveType.Cylinder); + + _bond.transform.localScale = new Vector3(scale/2, scale/2, scale/2); + + //这里可以设置材质,具体自己设置 + _bond.GetComponent().material.color = Color.cyan; + } + + + public void UpdateBond() + { + var startPos = Start.transform.position; + var endPos = End.transform.position; + var rightPosition = (startPos + endPos) / 2; + var rightRotation = endPos - startPos; + var halfLength = Vector3.Distance(startPos, endPos) / 2; + var thickness = 0.1f;//线的粗细 + + //创建圆柱体 + //bond.gameObject.transform.parent = transform; + _bond.transform.position = rightPosition; + _bond.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation); + _bond.transform.localScale = new Vector3(thickness, halfLength, thickness); + } + } +} \ No newline at end of file diff --git a/Assets/Bonds.cs.meta b/Assets/Models/Bond.cs.meta similarity index 100% rename from Assets/Bonds.cs.meta rename to Assets/Models/Bond.cs.meta diff --git a/Assets/Models/VirtualSkeleton.cs b/Assets/Models/VirtualSkeleton.cs new file mode 100644 index 0000000..d325d3f --- /dev/null +++ b/Assets/Models/VirtualSkeleton.cs @@ -0,0 +1,35 @@ +using UnityEngine; + +namespace Models +{ + public class VirtualSkeleton : Bond + { + private readonly GameObject _virtualSkeleton; + + public VirtualSkeleton(GameObject start,GameObject end,float scale) : base(start,end,scale) + { + _virtualSkeleton = GameObject.CreatePrimitive(PrimitiveType.Cylinder); + + _virtualSkeleton.transform.localScale = new Vector3(scale/2, scale/2, scale/2); + + _virtualSkeleton.GetComponent().material.color = Color.white; + } + + /// + /// 覆盖基类的更新方法 + /// + public new void UpdateBond() + { + var startPos = Start.transform.position; + var endPos = End.transform.position; + + var rightPosition = (startPos + endPos) / 2; + var rightRotation = endPos - startPos; + var lThickness = 0.2f; + + _virtualSkeleton.transform.position = rightPosition; + _virtualSkeleton.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation); + _virtualSkeleton.transform.localScale = new Vector3(lThickness, lThickness, lThickness); + } + } +} \ No newline at end of file diff --git a/Assets/VirtualSkeleton.cs.meta b/Assets/Models/VirtualSkeleton.cs.meta similarity index 100% rename from Assets/VirtualSkeleton.cs.meta rename to Assets/Models/VirtualSkeleton.cs.meta diff --git a/Assets/VirtualSkeleton.cs b/Assets/VirtualSkeleton.cs deleted file mode 100644 index 4e286b2..0000000 --- a/Assets/VirtualSkeleton.cs +++ /dev/null @@ -1,46 +0,0 @@ -using UnityEngine; -using UnityEngine.PlayerLoop; - -namespace Models -{ - public class VirtualSkeleton : Bonds - { - //private GameObject start; - //private GameObject end; - private GameObject virtualskeleton; - - - - public VirtualSkeleton(GameObject start,GameObject end,float scale) : base(start,end,scale) - { - this.start = start; - this.end = end; - - virtualskeleton = GameObject.CreatePrimitive(PrimitiveType.Cylinder); - - virtualskeleton.transform.localScale = new Vector3(scale/2, scale/2, scale/2); - - virtualskeleton.GetComponent().material.color = Color.white; - } - - public void UpdateVS() - { - - var startpos = start.transform.position; - var endpos = end.transform.position; - Vector3 rightPosition = (startpos + endpos) / 2; - Vector3 rightRotation = endpos - startpos; - - float LThickness = 0.2f;//线的粗细 - - //创建圆柱体 - - //bond.gameObject.transform.parent = transform; - virtualskeleton.transform.position = rightPosition; - virtualskeleton.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation); - //virtualskeleton.transform.rotation = Quaternion.LookRotation(rightRotation, Vector3.forward); - virtualskeleton.transform.localScale = new Vector3(LThickness, LThickness, LThickness); - } - - } -} \ No newline at end of file From 6eba30e2685bd3410be7f5608114d7850ec68dfc Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 27 Feb 2023 13:34:19 +0800 Subject: [PATCH 15/29] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86foreach?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E8=AD=A6=E5=91=8A=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20refact:=20=E7=A7=BB=E5=8A=A8UdpListener=E5=88=B0Utils?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/MainBehaviour.cs | 127 ++----------------------- Assets/{ => Utils}/UdpListener.cs | 0 Assets/{ => Utils}/UdpListener.cs.meta | 0 3 files changed, 8 insertions(+), 119 deletions(-) rename Assets/{ => Utils}/UdpListener.cs (100%) rename Assets/{ => Utils}/UdpListener.cs.meta (100%) diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs index 51b5518..3c92bfc 100644 --- a/Assets/MainBehaviour.cs +++ b/Assets/MainBehaviour.cs @@ -10,15 +10,13 @@ public class MainBehaviour : MonoBehaviour private readonly List _poseTransforms = new List(); private readonly UdpListener _listener = new UdpListener(); private const float Scale = 0.2f; - private GameObject sample ; // Start is called before the first frame update private void Start() { CreateNodes(); CreateBonds(); - - + _listener.AddHandler(OnReceive); _listener.Connect(5000); } @@ -26,18 +24,14 @@ private void Start() // Update is called once per frame private void Update() { - foreach (var poseTransform in _poseTransforms) + for (var i = 0; i < 33; i++) { - var index = (int)poseTransform.MediaPipeName; - - var temp = new Vector4(_poseTransforms[index].MediaPipePos.x, - _poseTransforms[index].MediaPipePos.y, - _poseTransforms[index].MediaPipePos.z,1); - //temp = RotateAxisX(-45) * temp; - //warning:乘法的顺序不能改! - - _nodes[index].transform.position = new Vector3(-temp.x, -temp.y, -temp.z) * 5; - //_nodes[index].transform.position = _poseTransforms[index].MediaPipePos * 5; + 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; } foreach (var bond in _bonds) @@ -87,111 +81,6 @@ private void CreateNodes() private void CreateBonds() { - /*var temp = new Bonds(_nodes[0], _nodes[4], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[0], _nodes[1], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[1], _nodes[2], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[2], _nodes[3], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[3], _nodes[7], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[4], _nodes[5], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[5], _nodes[6], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[6], _nodes[8], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[9], _nodes[10], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[2], _nodes[3], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[11], _nodes[12], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[12], _nodes[14], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[14], _nodes[16], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[16], _nodes[22], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[16], _nodes[18], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[18], _nodes[20], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[16], _nodes[20], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[11], _nodes[13], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[13], _nodes[15], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[15], _nodes[17], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[15], _nodes[21], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[15], _nodes[19], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[17], _nodes[19], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[12], _nodes[24], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[23], _nodes[24], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[11], _nodes[23], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[24], _nodes[26], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[26], _nodes[28], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[28], _nodes[32], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[28], _nodes[30], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[23], _nodes[25], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[25], _nodes[27], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[27], _nodes[29], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[27], _nodes[31], Scale); - _bonds.Add(temp); - - temp = new Bonds(_nodes[29], _nodes[31], Scale); - _bonds.Add(temp);*/ - var headBonds = new[] { PoseLandmarkType.RightEar, diff --git a/Assets/UdpListener.cs b/Assets/Utils/UdpListener.cs similarity index 100% rename from Assets/UdpListener.cs rename to Assets/Utils/UdpListener.cs diff --git a/Assets/UdpListener.cs.meta b/Assets/Utils/UdpListener.cs.meta similarity index 100% rename from Assets/UdpListener.cs.meta rename to Assets/Utils/UdpListener.cs.meta From 17553850356539781be50211b5a35aad5ecb6a58 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 28 Feb 2023 12:23:15 +0800 Subject: [PATCH 16/29] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E5=B9=B3?= =?UTF-8?q?=E5=9D=87=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) + { + } /// From 2f9e21fe66df5cfb43e24fa024257b6362601ca0 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 28 Feb 2023 12:55:21 +0800 Subject: [PATCH 17/29] =?UTF-8?q?refact:=20=E5=88=A0=E9=99=A4=E4=BA=86?= =?UTF-8?q?=E5=86=97=E4=BD=99=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/ModelBehaviour.cs | 37 ++------------------------ Assets/Models/PosTransformator.cs | 28 ------------------- Assets/Models/PosTransformator.cs.meta | 11 -------- Assets/Models/PoseTransform.cs | 31 +-------------------- 4 files changed, 3 insertions(+), 104 deletions(-) delete mode 100644 Assets/Models/PosTransformator.cs delete mode 100644 Assets/Models/PosTransformator.cs.meta diff --git a/Assets/ModelBehaviour.cs b/Assets/ModelBehaviour.cs index 619a088..3db17e3 100644 --- a/Assets/ModelBehaviour.cs +++ b/Assets/ModelBehaviour.cs @@ -7,16 +7,11 @@ public class ModelBehaviour : MonoBehaviour private Animator _animator; private readonly UdpListener _listener = new UdpListener(); - - private static readonly List _posLandmarks = new List(); - private static readonly List PoseTransforms = new List(); private void Start() { _animator = GetComponent(); // 获取动画控件 - - TransformatorInit(); //匹配初始化 InitPoseTransformList(); _listener.AddHandler(LogLandmarks); @@ -37,8 +32,6 @@ private void Update() _animator.GetBoneTransform(landmark.UnityName).rotation = landmark.CurrentQuaternion; } - - } @@ -51,6 +44,7 @@ private void OnDisable() private static void LogLandmarks(List landmarks) { + Debug.Log("Here"); foreach (var landmark in landmarks) { Debug.Log(landmark.ToString()); @@ -64,34 +58,7 @@ private static void RigPoint(List landmarks) foreach (var landmark in landmarks) { - var temp = PoseTransforms[(int)landmark.Type]; - temp.CalculateCurrentQuaternion(landmark); - PoseTransforms[(int)landmark.Type] = temp; - //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/PosTransformator.cs b/Assets/Models/PosTransformator.cs deleted file mode 100644 index 2f82a7b..0000000 --- a/Assets/Models/PosTransformator.cs +++ /dev/null @@ -1,28 +0,0 @@ -/* -using System.Collections.Generic; -using UnityEngine; - -namespace Models -{ - public class PosTransformator - { - public string mediaPipeName; - //动捕手机端关节点标识 - - public HumanBodyBones unityName; - //unity内部avatar系统关节点标识 - - public Vector3 mediaPipePos = new Vector3(0,0,0); - //动捕手机端位置数据 - - public List parent = new List(); - //该骨骼节点的父关节点列表(因为传上来的数据没有脖子和骨盆节点) - - public Quaternion prevQ; - //该骨骼节点的初始角度 - - public Quaternion currentQ; - //该骨骼节点的当前角度 - } -} -*/ \ No newline at end of file diff --git a/Assets/Models/PosTransformator.cs.meta b/Assets/Models/PosTransformator.cs.meta deleted file mode 100644 index dd25407..0000000 --- a/Assets/Models/PosTransformator.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: dfbef38b30e17e44b831e51debbd5087 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Models/PoseTransform.cs b/Assets/Models/PoseTransform.cs index 8d4570d..b65351e 100644 --- a/Assets/Models/PoseTransform.cs +++ b/Assets/Models/PoseTransform.cs @@ -87,38 +87,9 @@ public static void UpdatePosition(ref PoseTransform pose, PoseLandmark landmark) } } - public static void CalculateRotation(PoseTransform poseTransform) + public static void CalculateRotation(ref PoseTransform poseTransform, PoseLandmark landmark) { - //var temp = _posLandmarks.Find(t=>t.mediaPipeName == landmark.Type.ToString()); - var sum = new Vector3(0,0,0); - - if (landmark.Visibility > 0.6) - { - var temp = new Vector4(landmark.X, landmark.Y, landmark.Z, 1); - - - MediaPipePos = new Vector3(-temp.x, -temp.y, -temp.z); - - } - - if (UnityName != HumanBodyBones.LastBone) - { - Debug.Log(UnityName+".MediaPipePos:"+MediaPipePos); - } - - foreach (var parents in Parent) - { - - sum += parents.MediaPipePos; - //Debug.Log("parents:"+parents.UnityName+".MediaPipePos:"+parents.MediaPipePos); - - } - //Debug.Log("sum:"+sum); - var currentPos = sum / Parent.Count; - - CurrentQuaternion = Quaternion.LookRotation(MediaPipePos - currentPos); - } /// From 87831fd869530495826d417a76dbbd97ac666c0e Mon Sep 17 00:00:00 2001 From: Ichirinko <1621543655@qq.com> Date: Tue, 28 Feb 2023 22:59:38 +0800 Subject: [PATCH 18/29] =?UTF-8?q?fix:=E5=B0=86=E7=88=B6=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E7=9A=84=E7=B1=BB=E5=9E=8B=E6=94=B9=E4=B8=BAPoseLandmarkType?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/ModelBehaviour.cs | 27 ++++++++++++++++++++------- Assets/Models/PoseTransform.cs | 13 +++++++++++-- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Assets/ModelBehaviour.cs b/Assets/ModelBehaviour.cs index 3db17e3..56a81dd 100644 --- a/Assets/ModelBehaviour.cs +++ b/Assets/ModelBehaviour.cs @@ -7,14 +7,14 @@ public class ModelBehaviour : MonoBehaviour private Animator _animator; private readonly UdpListener _listener = new UdpListener(); - private static readonly List PoseTransforms = new List(); + private static readonly PoseTransform[] PoseTransforms = new PoseTransform[(int)PoseLandmarkType.MaxValue]; private void Start() { _animator = GetComponent(); // 获取动画控件 InitPoseTransformList(); - _listener.AddHandler(LogLandmarks); + //_listener.AddHandler(LogLandmarks); _listener.AddHandler(RigPoint); _listener.Connect(5000); } @@ -28,9 +28,19 @@ private void Update() { 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[(int)parent].AveragePos; + } + + parentsum /= landmark.Parent.Count; - _animator.GetBoneTransform(landmark.UnityName).rotation = landmark.CurrentQuaternion; - + transfrom.rotation = Quaternion.FromToRotation(transfrom.rotation.eulerAngles,landmark.AveragePos-parentsum); + } } @@ -44,7 +54,7 @@ private void OnDisable() private static void LogLandmarks(List landmarks) { - Debug.Log("Here"); + foreach (var landmark in landmarks) { Debug.Log(landmark.ToString()); @@ -59,6 +69,9 @@ private static void RigPoint(List 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; } - PoseTransforms.Add(item); + PoseTransforms[type] = item; } // 在这里添加父节点 @@ -115,7 +128,7 @@ private void InitPoseTransformList() private void AddParentTransform(PoseLandmarkType target, PoseLandmarkType parent) { var parents = PoseTransforms[(int)target].Parent; - parents.Add(PoseTransforms[(int)parent]); + parents.Add(PoseTransforms[(int)parent].MediaPipeName); } } diff --git a/Assets/Models/PoseTransform.cs b/Assets/Models/PoseTransform.cs index b65351e..fbe50e1 100644 --- a/Assets/Models/PoseTransform.cs +++ b/Assets/Models/PoseTransform.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using UnityEngine; using System; +using System.Collections; namespace Models { @@ -32,7 +33,7 @@ public struct PoseTransform /// /// 节点的父节点列表 /// - public readonly List Parent; + public readonly List Parent; /// /// 骨骼节点的初始角度 @@ -55,7 +56,7 @@ public PoseTransform( MediaPipeName = type; UnityName = GetRelatedBone(type); AveragePos = new Vector3(); - Parent = new List(); + Parent = new List(); PreviousQuaternion = new Quaternion(); CurrentQuaternion = new Quaternion(); AverageLength = averageLength; @@ -78,12 +79,20 @@ public static void UpdatePosition(ref PoseTransform pose, PoseLandmark landmark) var sum = new Vector3(); + var parentsum = new Vector3(0f,0f,0f); + foreach (var poseLandmark in pose._landmarkQueue) { sum += new Vector3(poseLandmark.X, poseLandmark.Y, poseLandmark.Z); } pose.AveragePos = sum / AverageLength; + + + + + //pose.CurrentQuaternion = Quaternion.LookRotation(pose.AveragePos - parentsum).normalized; + } } From aaba183c48c26747ef38d6e7a198bd5bd4ce87be Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 28 Feb 2023 23:35:49 +0800 Subject: [PATCH 19/29] =?UTF-8?q?refact:=20=E4=BD=BF=E7=94=A8int=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E4=BA=86PoseLandmarkType?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/MainBehaviour.cs | 24 +++---- Assets/ModelBehaviour.cs | 18 +++--- Assets/Models/PoseLandmark.cs | 12 ++-- Assets/Models/PoseLandmarkType.cs | 103 +++++++++++++++++++----------- Assets/Models/PoseTransform.cs | 8 +-- 5 files changed, 97 insertions(+), 68 deletions(-) diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs index 00ff8bb..b4499c5 100644 --- a/Assets/MainBehaviour.cs +++ b/Assets/MainBehaviour.cs @@ -4,9 +4,9 @@ public class MainBehaviour : MonoBehaviour { - private readonly GameObject[] _nodes = new GameObject[(int)PoseLandmarkType.MaxValue]; + private readonly GameObject[] _nodes = new GameObject[PoseLandmarkType.MaxValue]; private readonly List _bonds = new List(); - private readonly PoseTransform[] _poseTransforms = new PoseTransform[(int)PoseLandmarkType.MaxValue]; + private readonly PoseTransform[] _poseTransforms = new PoseTransform[PoseLandmarkType.MaxValue]; private readonly UdpListener _listener = new UdpListener(); private const float Scale = 0.2f; @@ -25,7 +25,7 @@ private void Start() // Update is called once per frame private void Update() { - for (var i = 0; i < (int)PoseLandmarkType.MaxValue; i++) + for (var i = 0; i < PoseLandmarkType.MaxValue; i++) { _nodes[i].transform.position = _poseTransforms[i].AveragePos * -5; } @@ -45,7 +45,7 @@ private void OnReceive(List landmarks) { foreach (var landmark in landmarks) { - PoseTransform.UpdatePosition(ref _poseTransforms[(int)landmark.Type], landmark); + PoseTransform.UpdatePosition(ref _poseTransforms[landmark.Type.Value], landmark); } } @@ -71,7 +71,7 @@ private void CreateNodes() } _nodes[i] = ball; - _poseTransforms[i] = new PoseTransform((PoseLandmarkType)i); + _poseTransforms[i] = new PoseTransform(i); } } @@ -148,12 +148,12 @@ private void CreateBonds() // 最后手动添加身体上的两条横线 _bonds.Add(new Bond( - _nodes[(int)PoseLandmarkType.LeftShoulder], - _nodes[(int)PoseLandmarkType.RightShoulder], + _nodes[PoseLandmarkType.LeftShoulder], + _nodes[PoseLandmarkType.RightShoulder], Scale)); _bonds.Add(new Bond( - _nodes[(int)PoseLandmarkType.LeftHip], - _nodes[(int)PoseLandmarkType.RightHip], + _nodes[PoseLandmarkType.LeftHip], + _nodes[PoseLandmarkType.RightHip], Scale)); } @@ -162,15 +162,15 @@ private void CreateBonds() /// /// 需要连接起来的关键点 需要按顺序设置 /// - private List GenerateBondsList(PoseLandmarkType[] nodes) + private List GenerateBondsList(int[] nodes) { var bonds = new List(); for (var i = 0; i < nodes.Length - 1; i++) { bonds.Add(new Bond( - _nodes[(int)nodes[i]], - _nodes[(int)nodes[i + 1]], + _nodes[nodes[i]], + _nodes[nodes[i + 1]], Scale)); } diff --git a/Assets/ModelBehaviour.cs b/Assets/ModelBehaviour.cs index 56a81dd..826aff0 100644 --- a/Assets/ModelBehaviour.cs +++ b/Assets/ModelBehaviour.cs @@ -7,7 +7,7 @@ public class ModelBehaviour : MonoBehaviour private Animator _animator; private readonly UdpListener _listener = new UdpListener(); - private static readonly PoseTransform[] PoseTransforms = new PoseTransform[(int)PoseLandmarkType.MaxValue]; + private static readonly PoseTransform[] PoseTransforms = new PoseTransform[PoseLandmarkType.MaxValue]; private void Start() { @@ -34,7 +34,7 @@ private void Update() foreach (var parent in landmark.Parent) { - parentsum += PoseTransforms[(int)parent].AveragePos; + parentsum += PoseTransforms[parent.Value].AveragePos; } parentsum /= landmark.Parent.Count; @@ -69,9 +69,9 @@ private static void RigPoint(List landmarks) foreach (var landmark in landmarks) { - PoseTransform.UpdatePosition(ref PoseTransforms[(int)landmark.Type], landmark); + PoseTransform.UpdatePosition(ref PoseTransforms[landmark.Type.Value], landmark); - Debug.Log(PoseTransforms[(int)landmark.Type].UnityName+":"+PoseTransforms[(int)landmark.Type].AveragePos); + Debug.Log(PoseTransforms[landmark.Type.Value].UnityName+":"+PoseTransforms[landmark.Type.Value].AveragePos); } } @@ -81,9 +81,9 @@ private static void RigPoint(List landmarks) /// private void InitPoseTransformList() { - for (var type = 0; type <= (int)PoseLandmarkType.RightFootIndex; type++) + for (var type = 0; type <= PoseLandmarkType.RightFootIndex; type++) { - var item = new PoseTransform((PoseLandmarkType)type); + var item = new PoseTransform(type); if (item.UnityName != HumanBodyBones.LastBone) { @@ -125,10 +125,10 @@ private void InitPoseTransformList() /// /// 需要添加的节点 /// 添加的父节点 - private void AddParentTransform(PoseLandmarkType target, PoseLandmarkType parent) + private void AddParentTransform(int target, int parent) { - var parents = PoseTransforms[(int)target].Parent; - parents.Add(PoseTransforms[(int)parent].MediaPipeName); + var parents = PoseTransforms[target].Parent; + parents.Add(PoseTransforms[parent].MediaPipeName); } } diff --git a/Assets/Models/PoseLandmark.cs b/Assets/Models/PoseLandmark.cs index 6520ef2..45608f0 100644 --- a/Assets/Models/PoseLandmark.cs +++ b/Assets/Models/PoseLandmark.cs @@ -47,9 +47,9 @@ public class PoseLandmark public long TimeStamp { get; } - public PoseLandmark(PoseLandmarkType type, float x, float y, float z, float visibility, long timeStamp) + public PoseLandmark(int type, float x, float y, float z, float visibility, long timeStamp) { - Type = type; + Type = new PoseLandmarkType(type); X = x; Y = y; Z = z; @@ -66,7 +66,7 @@ public byte[] ToByteArray() { var result = new byte[PacketLength]; - BitConverter.GetBytes((int)Type).CopyTo(result, 0); + 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); @@ -84,7 +84,7 @@ public byte[] ToByteArray() public static PoseLandmark ValueOf(byte[] bytes) { var result = new PoseLandmark( - (PoseLandmarkType)BitConverter.ToInt32(bytes, 0), + BitConverter.ToInt32(bytes, 0), BitConverter.ToSingle(bytes, 4), BitConverter.ToSingle(bytes, 8), BitConverter.ToSingle(bytes, 12), @@ -106,7 +106,7 @@ public static List ArrayOf(byte[] bytes) for (var i = 0; i < bytes.Length; i = i + PacketLength) { - var landmark = new PoseLandmark((PoseLandmarkType)BitConverter.ToInt32(bytes, i), + var landmark = new PoseLandmark(BitConverter.ToInt32(bytes, i), BitConverter.ToSingle(bytes, i + 4), BitConverter.ToSingle(bytes, i + 8), BitConverter.ToSingle(bytes, i + 12), @@ -127,7 +127,7 @@ public override bool Equals(object obj) } else { - return Type == landmark.Type + return Type.Value == landmark.Type.Value && Math.Abs(X - landmark.X) < Tolerance && Math.Abs(Y - landmark.Y) < Tolerance && Math.Abs(Z - landmark.Z) < Tolerance diff --git a/Assets/Models/PoseLandmarkType.cs b/Assets/Models/PoseLandmarkType.cs index 7b86f39..a781564 100644 --- a/Assets/Models/PoseLandmarkType.cs +++ b/Assets/Models/PoseLandmarkType.cs @@ -1,41 +1,70 @@ +using System; + namespace Models { //名字 - public enum PoseLandmarkType - { - Nose, - LeftEyeInner, - LeftEye, - LeftEyeOuter, - RightEyeInner, - RightEye, - RightEyeOuter, - LeftEar, - RightEar, - MouthLeft, - MouthRight, - LeftShoulder, - RightShoulder, - LeftElbow, - RightElbow, - LeftWrist, - RightWrist, - LeftPinky, - RightPinky, - LeftIndex, - RightIndex, - LeftThumb, - RightThumb, - LeftHip, - RightHip, - LeftKnee, - RightKnee, - LeftAnkle, - RightAnkle, - LeftHeel, - RightHeel, - LeftFootIndex, - RightFootIndex, - MaxValue - } + public struct PoseLandmarkType + { + public const int Nose = 0; + public const int LeftEyeInner = 1; + public const int LeftEye = 2; + public const int LeftEyeOuter = 3; + public const int RightEyeInner = 4; + public const int RightEye = 5; + public const int RightEyeOuter = 6; + public const int LeftEar = 7; + public const int RightEar = 8; + public const int MouthLeft = 9; + public const int MouthRight = 10; + public const int LeftShoulder = 11; + public const int RightShoulder = 12; + public const int LeftElbow = 13; + public const int RightElbow = 14; + public const int LeftWrist = 15; + public const int RightWrist = 16; + public const int LeftPinky = 17; + public const int RightPinky = 18; + public const int LeftIndex = 19; + public const int RightIndex = 20; + public const int LeftThumb = 21; + public const int RightThumb = 22; + public const int LeftHip = 23; + public const int RightHip = 24; + public const int LeftKnee = 25; + public const int RightKnee = 26; + public const int LeftAnkle = 27; + public const int RightAnkle = 28; + public const int LeftHeel = 29; + public const int RightHeel = 30; + public const int LeftFootIndex = 31; + public const int RightFootIndex = 32; + public const int MaxValue = 33; + + public int Value { get; } + + public PoseLandmarkType(int value) + { + if (value >= MaxValue) + { + throw new ArgumentOutOfRangeException(nameof(value)); + } + + Value = value; + } + + public override bool Equals(object obj) + { + return Value.Equals(obj); + } + + public override int GetHashCode() + { + return Value.GetHashCode(); + } + + public override string ToString() + { + return Value.ToString(); + } + } } \ No newline at end of file diff --git a/Assets/Models/PoseTransform.cs b/Assets/Models/PoseTransform.cs index fbe50e1..293a22c 100644 --- a/Assets/Models/PoseTransform.cs +++ b/Assets/Models/PoseTransform.cs @@ -49,12 +49,12 @@ public struct PoseTransform private readonly Queue _landmarkQueue; public PoseTransform( - PoseLandmarkType type, + int type, int averageLength = 5 ) { - MediaPipeName = type; - UnityName = GetRelatedBone(type); + MediaPipeName = new PoseLandmarkType(type); + UnityName = GetRelatedBone(MediaPipeName); AveragePos = new Vector3(); Parent = new List(); PreviousQuaternion = new Quaternion(); @@ -108,7 +108,7 @@ public static void CalculateRotation(ref PoseTransform poseTransform, PoseLandma /// 关联骨骼的种类 private static HumanBodyBones GetRelatedBone(PoseLandmarkType type) { - switch (type) + switch (type.Value) { case PoseLandmarkType.LeftHip: return HumanBodyBones.LeftUpperLeg; From 2d2adefb9617e6708dc2bbb6b4982fae63b5aa1e Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 1 Mar 2023 22:07:39 +0800 Subject: [PATCH 20/29] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0PoseTransformHa?= =?UTF-8?q?ndler=E6=8E=A5=E5=8F=A3=20=E6=8F=90=E4=BE=9B=E7=BB=93=E6=AC=A2?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E6=96=B9=E6=B3=95=E7=9A=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=20=E6=B7=BB=E5=8A=A0=E7=9B=B4=E6=8E=A5=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=92=8C=E5=B9=B3=E5=9D=87=E8=BF=87=E6=BB=A4=E4=B8=A4=E7=A7=8D?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/MainBehaviour.cs | 7 +-- Assets/ModelBehaviour.cs | 9 ++-- Assets/Models/PoseLandmarkType.cs | 2 +- Assets/Models/PoseTransform.cs | 47 ++++--------------- Assets/Utils/IPoseTransformHandler.cs | 23 +++++++++ Assets/Utils/IPoseTransformHandler.cs.meta | 3 ++ Assets/Utils/PoseTransformHandlers.meta | 3 ++ .../PoseTransformHandlers/AverageHandler.cs | 41 ++++++++++++++++ .../AverageHandler.cs.meta | 3 ++ .../PoseTransformHandlers/NormalHandler.cs | 20 ++++++++ .../NormalHandler.cs.meta | 3 ++ 11 files changed, 114 insertions(+), 47 deletions(-) create mode 100644 Assets/Utils/IPoseTransformHandler.cs create mode 100644 Assets/Utils/IPoseTransformHandler.cs.meta create mode 100644 Assets/Utils/PoseTransformHandlers.meta create mode 100644 Assets/Utils/PoseTransformHandlers/AverageHandler.cs create mode 100644 Assets/Utils/PoseTransformHandlers/AverageHandler.cs.meta create mode 100644 Assets/Utils/PoseTransformHandlers/NormalHandler.cs create mode 100644 Assets/Utils/PoseTransformHandlers/NormalHandler.cs.meta diff --git a/Assets/MainBehaviour.cs b/Assets/MainBehaviour.cs index b4499c5..2823943 100644 --- a/Assets/MainBehaviour.cs +++ b/Assets/MainBehaviour.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using Models; using UnityEngine; +using Utils.PoseTransformHandlers; public class MainBehaviour : MonoBehaviour { @@ -27,7 +28,7 @@ private void Update() { for (var i = 0; i < PoseLandmarkType.MaxValue; i++) { - _nodes[i].transform.position = _poseTransforms[i].AveragePos * -5; + _nodes[i].transform.position = _poseTransforms[i].ResultPosition * -5; } foreach (var bond in _bonds) @@ -51,7 +52,7 @@ private void OnReceive(List landmarks) private void CreateNodes() { - for (var i = 0; i < (int)PoseLandmarkType.MaxValue; i++) + for (var i = 0; i < PoseLandmarkType.MaxValue; i++) { var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere); @@ -71,7 +72,7 @@ private void CreateNodes() } _nodes[i] = ball; - _poseTransforms[i] = new PoseTransform(i); + _poseTransforms[i] = new PoseTransform(i, new AverageHandler()); } } diff --git a/Assets/ModelBehaviour.cs b/Assets/ModelBehaviour.cs index 826aff0..908fe6e 100644 --- a/Assets/ModelBehaviour.cs +++ b/Assets/ModelBehaviour.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using Models; using UnityEngine; +using Utils.PoseTransformHandlers; public class ModelBehaviour : MonoBehaviour { @@ -34,12 +35,12 @@ private void Update() foreach (var parent in landmark.Parent) { - parentsum += PoseTransforms[parent.Value].AveragePos; + parentsum += PoseTransforms[parent.Value].ResultPosition; } parentsum /= landmark.Parent.Count; - transfrom.rotation = Quaternion.FromToRotation(transfrom.rotation.eulerAngles,landmark.AveragePos-parentsum); + transfrom.rotation = Quaternion.FromToRotation(transfrom.rotation.eulerAngles,landmark.ResultPosition-parentsum); } } @@ -71,7 +72,7 @@ private static void RigPoint(List landmarks) PoseTransform.UpdatePosition(ref PoseTransforms[landmark.Type.Value], landmark); - Debug.Log(PoseTransforms[landmark.Type.Value].UnityName+":"+PoseTransforms[landmark.Type.Value].AveragePos); + Debug.Log(PoseTransforms[landmark.Type.Value].UnityName+":"+PoseTransforms[landmark.Type.Value].ResultPosition); } } @@ -83,7 +84,7 @@ private void InitPoseTransformList() { for (var type = 0; type <= PoseLandmarkType.RightFootIndex; type++) { - var item = new PoseTransform(type); + var item = new PoseTransform(type, new NormalHandler()); if (item.UnityName != HumanBodyBones.LastBone) { diff --git a/Assets/Models/PoseLandmarkType.cs b/Assets/Models/PoseLandmarkType.cs index a781564..513df64 100644 --- a/Assets/Models/PoseLandmarkType.cs +++ b/Assets/Models/PoseLandmarkType.cs @@ -3,7 +3,7 @@ namespace Models { //名字 - public struct PoseLandmarkType + public readonly struct PoseLandmarkType { public const int Nose = 0; public const int LeftEyeInner = 1; diff --git a/Assets/Models/PoseTransform.cs b/Assets/Models/PoseTransform.cs index 293a22c..877e33c 100644 --- a/Assets/Models/PoseTransform.cs +++ b/Assets/Models/PoseTransform.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using UnityEngine; -using System; -using System.Collections; +using Utils; namespace Models { @@ -23,12 +22,12 @@ public struct PoseTransform /// /// 取平均的长度 /// - public static int AverageLength; + public static int AverageLength = 3; /// /// 取平均之后的结果 /// - public Vector3 AveragePos; + public Vector3 ResultPosition => _transformHandler.GetResultPosition(); /// /// 节点的父节点列表 @@ -44,56 +43,26 @@ public struct PoseTransform /// 骨骼节点的当前角度 /// public Quaternion CurrentQuaternion; - - private int _count; - private readonly Queue _landmarkQueue; + + private readonly IPoseTransformHandler _transformHandler; public PoseTransform( int type, - int averageLength = 5 + IPoseTransformHandler handler ) { MediaPipeName = new PoseLandmarkType(type); UnityName = GetRelatedBone(MediaPipeName); - AveragePos = new Vector3(); Parent = new List(); PreviousQuaternion = new Quaternion(); CurrentQuaternion = new Quaternion(); - AverageLength = averageLength; - _count = 0; - _landmarkQueue = new Queue(); + _transformHandler = handler; } public static void UpdatePosition(ref PoseTransform pose, PoseLandmark landmark) { - if (pose._count < AverageLength) - { - pose._landmarkQueue.Enqueue(landmark); - pose._count++; - } - else - { - pose._landmarkQueue.Dequeue(); - pose._landmarkQueue.Enqueue(landmark); - - var sum = new Vector3(); - - var parentsum = new Vector3(0f,0f,0f); - - foreach (var poseLandmark in pose._landmarkQueue) - { - sum += new Vector3(poseLandmark.X, poseLandmark.Y, poseLandmark.Z); - } - - pose.AveragePos = sum / AverageLength; - - - - - //pose.CurrentQuaternion = Quaternion.LookRotation(pose.AveragePos - parentsum).normalized; - - } + pose._transformHandler.ReceivePoseLandmark(landmark); } public static void CalculateRotation(ref PoseTransform poseTransform, PoseLandmark landmark) diff --git a/Assets/Utils/IPoseTransformHandler.cs b/Assets/Utils/IPoseTransformHandler.cs new file mode 100644 index 0000000..808d6ae --- /dev/null +++ b/Assets/Utils/IPoseTransformHandler.cs @@ -0,0 +1,23 @@ +using Models; +using UnityEngine; + +namespace Utils +{ + /// + /// 坐标处理接口 + /// + public interface IPoseTransformHandler + { + /// + /// 传入新收到的坐标点 + /// + /// + public void ReceivePoseLandmark(PoseLandmark landmark); + + /// + /// 获得处理之后的坐标点 + /// + /// + public Vector3 GetResultPosition(); + } +} \ No newline at end of file diff --git a/Assets/Utils/IPoseTransformHandler.cs.meta b/Assets/Utils/IPoseTransformHandler.cs.meta new file mode 100644 index 0000000..c08d74b --- /dev/null +++ b/Assets/Utils/IPoseTransformHandler.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 09e98a9502f0470f827a472058068bc4 +timeCreated: 1677676325 \ No newline at end of file diff --git a/Assets/Utils/PoseTransformHandlers.meta b/Assets/Utils/PoseTransformHandlers.meta new file mode 100644 index 0000000..b41ce0e --- /dev/null +++ b/Assets/Utils/PoseTransformHandlers.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 790fe29ab090465183945d02718de6a9 +timeCreated: 1677676888 \ No newline at end of file diff --git a/Assets/Utils/PoseTransformHandlers/AverageHandler.cs b/Assets/Utils/PoseTransformHandlers/AverageHandler.cs new file mode 100644 index 0000000..11c0442 --- /dev/null +++ b/Assets/Utils/PoseTransformHandlers/AverageHandler.cs @@ -0,0 +1,41 @@ +using Models; +using UnityEngine; +using System.Collections.Concurrent; + +namespace Utils.PoseTransformHandlers +{ + public class AverageHandler : IPoseTransformHandler + { + public static int AverageLength = 3; + // 线程安全的队列集合 + // 据文档描述甚至没有使用锁 + private readonly ConcurrentQueue _queue = new ConcurrentQueue(); + + public void ReceivePoseLandmark(PoseLandmark landmark) + { + if (_queue.Count < AverageLength) + { + _queue.Enqueue(landmark); + } + else + { + if (_queue.TryDequeue(out _)) + { + _queue.Enqueue(landmark); + } + } + } + + public Vector3 GetResultPosition() + { + var sum = new Vector3(); + + foreach (var item in _queue) + { + sum += new Vector3(item.X, item.Y, item.Z); + } + + return sum / AverageLength; + } + } +} \ No newline at end of file diff --git a/Assets/Utils/PoseTransformHandlers/AverageHandler.cs.meta b/Assets/Utils/PoseTransformHandlers/AverageHandler.cs.meta new file mode 100644 index 0000000..47ced42 --- /dev/null +++ b/Assets/Utils/PoseTransformHandlers/AverageHandler.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c02b0d03301d408faf81931a69679f5c +timeCreated: 1677676898 \ No newline at end of file diff --git a/Assets/Utils/PoseTransformHandlers/NormalHandler.cs b/Assets/Utils/PoseTransformHandlers/NormalHandler.cs new file mode 100644 index 0000000..3020fbf --- /dev/null +++ b/Assets/Utils/PoseTransformHandlers/NormalHandler.cs @@ -0,0 +1,20 @@ +using Models; +using UnityEngine; + +namespace Utils.PoseTransformHandlers +{ + public class NormalHandler : IPoseTransformHandler + { + private Vector3 _result; + + public void ReceivePoseLandmark(PoseLandmark landmark) + { + _result = new Vector3(landmark.X, landmark.Y, landmark.Z); + } + + public Vector3 GetResultPosition() + { + return _result; + } + } +} \ No newline at end of file diff --git a/Assets/Utils/PoseTransformHandlers/NormalHandler.cs.meta b/Assets/Utils/PoseTransformHandlers/NormalHandler.cs.meta new file mode 100644 index 0000000..40473c0 --- /dev/null +++ b/Assets/Utils/PoseTransformHandlers/NormalHandler.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 93a33f47f3654fd3b27dd18db88fcc55 +timeCreated: 1677678654 \ No newline at end of file From 1b3fe7069cfdfde9e5f05427d94b3162e91b4916 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 2 Mar 2023 19:06:32 +0800 Subject: [PATCH 21/29] =?UTF-8?q?feature:=20PoseTransform=E8=BF=90?= =?UTF-8?q?=E7=AE=97=E7=AC=A6=E9=87=8D=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Models/PoseTransform.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Assets/Models/PoseTransform.cs b/Assets/Models/PoseTransform.cs index 877e33c..318ffd6 100644 --- a/Assets/Models/PoseTransform.cs +++ b/Assets/Models/PoseTransform.cs @@ -70,6 +70,25 @@ public static void CalculateRotation(ref PoseTransform poseTransform, PoseLandma } + public static Vector3 operator +(PoseTransform a) => a.ResultPosition; + + public static Vector3 operator -(PoseTransform a) => -a.ResultPosition; + + public static Vector3 operator +(PoseTransform a, PoseTransform b) => + a.ResultPosition + b.ResultPosition; + + public static Vector3 operator -(PoseTransform a, PoseTransform b) => + a.ResultPosition - b.ResultPosition; + + public static Vector3 operator *(PoseTransform a, int b) => + a.ResultPosition * b; + + public static Vector3 operator *(int a, PoseTransform b) => + a * b.ResultPosition; + + public static Vector3 operator /(PoseTransform a, int b) => + a.ResultPosition / b; + /// /// 获得同相关捕捉点关联的骨骼 /// From e63a32b95ffc38540f657bf37a8f4f8e150fc63a Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 5 Mar 2023 19:55:37 +0800 Subject: [PATCH 22/29] =?UTF-8?q?feature:=20=E6=B7=BB=E5=8A=A0=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E6=A0=B9=E9=AA=A8=E9=AA=BC=E8=83=AF=E9=AA=A8=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=20=E4=BD=86=E6=98=AF=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=8F=91=E7=94=9F=E4=B8=8D=E5=8F=AF=E7=90=86=E8=A7=A3=E7=8E=B0?= =?UTF-8?q?=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Behaviours.meta | 8 + Assets/Behaviours/BoneBehaviour.cs | 57 +++ Assets/Behaviours/BoneBehaviour.cs.meta | 11 + Assets/BoneAnimator.controller | 55 +++ Assets/BoneAnimator.controller.meta | 8 + Assets/Scenes/BallHumanScene.unity | 441 ++++++++++++++++++++++++ Assets/Scenes/BallHumanScene.unity.meta | 7 + 7 files changed, 587 insertions(+) create mode 100644 Assets/Behaviours.meta create mode 100644 Assets/Behaviours/BoneBehaviour.cs create mode 100644 Assets/Behaviours/BoneBehaviour.cs.meta create mode 100644 Assets/BoneAnimator.controller create mode 100644 Assets/BoneAnimator.controller.meta create mode 100644 Assets/Scenes/BallHumanScene.unity create mode 100644 Assets/Scenes/BallHumanScene.unity.meta diff --git a/Assets/Behaviours.meta b/Assets/Behaviours.meta new file mode 100644 index 0000000..6c6f839 --- /dev/null +++ b/Assets/Behaviours.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 83bce6e0ab5db38aaaae7ec73b738130 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Behaviours/BoneBehaviour.cs b/Assets/Behaviours/BoneBehaviour.cs new file mode 100644 index 0000000..63d7275 --- /dev/null +++ b/Assets/Behaviours/BoneBehaviour.cs @@ -0,0 +1,57 @@ +using System.Collections.Generic; +using Models; +using UnityEngine; +using Utils.PoseTransformHandlers; + +namespace Behaviours +{ + public class BoneBehaviour : MonoBehaviour + { + private readonly UdpListener _listener = new UdpListener(); + private PoseTransform[] _transforms = new PoseTransform[PoseLandmarkType.MaxValue]; + + private Animator _animator; + + private void Start() + { + _animator = GetComponent(); + + for (var i = 0; i < PoseLandmarkType.MaxValue; i++) + { + _transforms[i] = new PoseTransform(i, new NormalHandler()); + } + + _listener.AddHandler(OnReceive); + _listener.Connect(5000); + } + + private void Update() + { + var frontLeft = Vector3.Cross( + _transforms[PoseLandmarkType.RightHip] - _transforms[PoseLandmarkType.LeftHip], + _transforms[PoseLandmarkType.RightShoulder] - _transforms[PoseLandmarkType.LeftHip]); + + var frontRight = Vector3.Cross( + _transforms[PoseLandmarkType.LeftShoulder] - _transforms[PoseLandmarkType.RightHip], + _transforms[PoseLandmarkType.LeftHip] - _transforms[PoseLandmarkType.RightHip]); + + var front = frontLeft + frontRight; + front.Normalize(); + + _animator.GetBoneTransform(HumanBodyBones.Hips).rotation = Quaternion.LookRotation(front); + } + + private void OnDisable() + { + _listener.DisConnect(); + } + + private void OnReceive(List landmarks) + { + foreach (var landmark in landmarks) + { + PoseTransform.UpdatePosition(ref _transforms[landmark.Type.Value], landmark); + } + } + } +} diff --git a/Assets/Behaviours/BoneBehaviour.cs.meta b/Assets/Behaviours/BoneBehaviour.cs.meta new file mode 100644 index 0000000..8a391ae --- /dev/null +++ b/Assets/Behaviours/BoneBehaviour.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a2b060336532a01bfb4593b385955175 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/BoneAnimator.controller b/Assets/BoneAnimator.controller new file mode 100644 index 0000000..b67b4fb --- /dev/null +++ b/Assets/BoneAnimator.controller @@ -0,0 +1,55 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &-6275755417433151442 +MonoBehaviour: + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a2b060336532a01bfb4593b385955175, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!1107 &-4834836044711832442 +AnimatorStateMachine: + serializedVersion: 6 + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: Base Layer + m_ChildStates: [] + m_ChildStateMachines: [] + m_AnyStateTransitions: [] + m_EntryTransitions: [] + m_StateMachineTransitions: {} + m_StateMachineBehaviours: [] + m_AnyStatePosition: {x: 50, y: 20, z: 0} + m_EntryPosition: {x: 50, y: 120, z: 0} + m_ExitPosition: {x: 800, y: 120, z: 0} + m_ParentStateMachinePosition: {x: 800, y: 20, z: 0} + m_DefaultState: {fileID: 0} +--- !u!91 &9100000 +AnimatorController: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: BoneAnimator + serializedVersion: 5 + m_AnimatorParameters: [] + m_AnimatorLayers: + - serializedVersion: 5 + m_Name: Base Layer + m_StateMachine: {fileID: -4834836044711832442} + m_Mask: {fileID: 0} + m_Motions: [] + m_Behaviours: [] + m_BlendingMode: 0 + m_SyncedLayerIndex: -1 + m_DefaultWeight: 0 + m_IKPass: 1 + m_SyncedLayerAffectsTiming: 0 + m_Controller: {fileID: 9100000} diff --git a/Assets/BoneAnimator.controller.meta b/Assets/BoneAnimator.controller.meta new file mode 100644 index 0000000..05399c0 --- /dev/null +++ b/Assets/BoneAnimator.controller.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 7406b3b3cd1a7812ca7823cf800c679b +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 9100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/BallHumanScene.unity b/Assets/Scenes/BallHumanScene.unity new file mode 100644 index 0000000..ff81877 --- /dev/null +++ b/Assets/Scenes/BallHumanScene.unity @@ -0,0 +1,441 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +OcclusionCullingSettings: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: 0.25 + backfaceThreshold: 100 + m_SceneGUID: 00000000000000000000000000000000 + m_OcclusionCullingData: {fileID: 0} +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 9 + m_Fog: 0 + m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} + m_FogMode: 3 + m_FogDensity: 0.01 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} + m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} + m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 0 + m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} + m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0} + m_HaloStrength: 0.5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} + m_IndirectSpecularColor: {r: 0.18029127, g: 0.22572401, b: 0.3069303, a: 1} + m_UseRadianceAmbientProbe: 0 +--- !u!157 &3 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 12 + m_GIWorkflowMode: 1 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 1 + m_EnableRealtimeLightmaps: 0 + m_LightmapEditorSettings: + serializedVersion: 12 + m_Resolution: 2 + m_BakeResolution: 40 + m_AtlasSize: 1024 + m_AO: 0 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_ExtractAmbientOcclusion: 0 + m_Padding: 2 + m_LightmapParameters: {fileID: 0} + m_LightmapsBakeMode: 1 + m_TextureCompression: 1 + m_FinalGather: 0 + m_FinalGatherFiltering: 1 + m_FinalGatherRayCount: 256 + m_ReflectionCompression: 2 + m_MixedBakeMode: 2 + m_BakeBackend: 1 + m_PVRSampling: 1 + m_PVRDirectSampleCount: 32 + m_PVRSampleCount: 512 + m_PVRBounces: 2 + m_PVREnvironmentSampleCount: 256 + m_PVREnvironmentReferencePointCount: 2048 + m_PVRFilteringMode: 1 + m_PVRDenoiserTypeDirect: 1 + m_PVRDenoiserTypeIndirect: 1 + m_PVRDenoiserTypeAO: 1 + m_PVRFilterTypeDirect: 0 + m_PVRFilterTypeIndirect: 0 + m_PVRFilterTypeAO: 0 + m_PVREnvironmentMIS: 1 + m_PVRCulling: 1 + m_PVRFilteringGaussRadiusDirect: 1 + m_PVRFilteringGaussRadiusIndirect: 5 + m_PVRFilteringGaussRadiusAO: 2 + m_PVRFilteringAtrousPositionSigmaDirect: 0.5 + m_PVRFilteringAtrousPositionSigmaIndirect: 2 + m_PVRFilteringAtrousPositionSigmaAO: 1 + m_ExportTrainingData: 0 + m_TrainingDataDestination: TrainingData + m_LightProbeSampleCountMultiplier: 4 + m_LightingDataAsset: {fileID: 0} + m_LightingSettings: {fileID: 0} +--- !u!196 &4 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 2 + agentTypeID: 0 + agentRadius: 0.5 + agentHeight: 2 + agentSlope: 45 + agentClimb: 0.4 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + minRegionArea: 2 + manualCellSize: 0 + cellSize: 0.16666667 + manualTileSize: 0 + tileSize: 256 + accuratePlacement: 0 + maxJobWorkers: 0 + preserveTilesOutsideBounds: 0 + debug: + m_Flags: 0 + m_NavMeshData: {fileID: 0} +--- !u!1 &306285767 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 306285770} + - component: {fileID: 306285769} + - component: {fileID: 306285768} + m_Layer: 0 + m_Name: Camera + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!81 &306285768 +AudioListener: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 306285767} + m_Enabled: 1 +--- !u!20 &306285769 +Camera: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 306285767} + m_Enabled: 1 + serializedVersion: 2 + m_ClearFlags: 1 + m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} + m_projectionMatrixMode: 1 + m_GateFitMode: 2 + m_FOVAxisMode: 0 + m_SensorSize: {x: 36, y: 24} + m_LensShift: {x: 0, y: 0} + m_FocalLength: 50 + m_NormalizedViewPortRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 + near clip plane: 0.3 + far clip plane: 1000 + field of view: 60 + orthographic: 0 + orthographic size: 5 + m_Depth: 0 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingPath: -1 + m_TargetTexture: {fileID: 0} + m_TargetDisplay: 0 + m_TargetEye: 3 + m_HDR: 1 + m_AllowMSAA: 1 + m_AllowDynamicResolution: 0 + m_ForceIntoRT: 0 + m_OcclusionCulling: 1 + m_StereoConvergence: 10 + m_StereoSeparation: 0.022 +--- !u!4 &306285770 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 306285767} + m_LocalRotation: {x: 0.0016336598, y: 0.99834925, z: 0.037567556, w: -0.043414135} + m_LocalPosition: {x: -1.83, y: 6.98, z: 16.38} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 2 + m_LocalEulerAnglesHint: {x: -4.31, y: 184.98, z: 0} +--- !u!1001 &670355942 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_RootOrder + value: 1 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalPosition.x + value: -0.93436813 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalPosition.y + value: 0.81755877 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalPosition.z + value: -0.6021528 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_Name + value: troop + objectReference: {fileID: 0} + - target: {fileID: 5465940246521907071, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5465940246521907071, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.w + value: 0.8707891 + objectReference: {fileID: 0} + - target: {fileID: 5465940246521907071, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.x + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 5465940246521907071, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.y + value: -0.49165684 + objectReference: {fileID: 0} + - target: {fileID: 5465940246521907071, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: -58.899 + objectReference: {fileID: 0} + - target: {fileID: 5866666021909216657, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_Controller + value: + objectReference: {fileID: 0} + - target: {fileID: 6502182839619065283, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalPosition.x + value: -0.000114262104 + objectReference: {fileID: 0} + - target: {fileID: 6502182839619065283, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalPosition.y + value: 1.8739636 + objectReference: {fileID: 0} + - target: {fileID: 6502182839619065283, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalPosition.z + value: -0.00004636313 + objectReference: {fileID: 0} + - target: {fileID: 6502182839619065283, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.w + value: 0.9621656 + objectReference: {fileID: 0} + - target: {fileID: 6502182839619065283, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.x + value: 0.023309091 + objectReference: {fileID: 0} + - target: {fileID: 6502182839619065283, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.y + value: -0.26867148 + objectReference: {fileID: 0} + - target: {fileID: 6502182839619065283, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalRotation.z + value: -0.03885668 + objectReference: {fileID: 0} + - target: {fileID: 6502182839619065283, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 1.374 + objectReference: {fileID: 0} + - target: {fileID: 6502182839619065283, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: -31.263 + objectReference: {fileID: 0} + - target: {fileID: 6502182839619065283, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: -5.01 + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} +--- !u!1 &670355943 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + m_PrefabInstance: {fileID: 670355942} + m_PrefabAsset: {fileID: 0} +--- !u!114 &670355944 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 670355943} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a2b060336532a01bfb4593b385955175, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!1 &1059201228 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1059201230} + - component: {fileID: 1059201229} + m_Layer: 0 + m_Name: Directional Light + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!108 &1059201229 +Light: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1059201228} + m_Enabled: 1 + serializedVersion: 10 + m_Type: 1 + m_Shape: 0 + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_Intensity: 1 + m_Range: 10 + m_SpotAngle: 30 + m_InnerSpotAngle: 21.80208 + m_CookieSize: 10 + m_Shadows: + m_Type: 0 + m_Resolution: -1 + m_CustomResolution: -1 + m_Strength: 1 + m_Bias: 0.05 + m_NormalBias: 0.4 + m_NearPlane: 0.2 + m_CullingMatrixOverride: + e00: 1 + e01: 0 + e02: 0 + e03: 0 + e10: 0 + e11: 1 + e12: 0 + e13: 0 + e20: 0 + e21: 0 + e22: 1 + e23: 0 + e30: 0 + e31: 0 + e32: 0 + e33: 1 + m_UseCullingMatrixOverride: 0 + m_Cookie: {fileID: 0} + m_DrawHalo: 0 + m_Flare: {fileID: 0} + m_RenderMode: 0 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingLayerMask: 1 + m_Lightmapping: 4 + m_LightShadowCasterMode: 0 + m_AreaSize: {x: 1, y: 1} + m_BounceIntensity: 1 + m_ColorTemperature: 6570 + m_UseColorTemperature: 0 + m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0} + m_UseBoundingSphereOverride: 0 + m_UseViewFrustumForShadowCasterCull: 1 + m_ShadowRadius: 0 + m_ShadowAngle: 0 +--- !u!4 &1059201230 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1059201228} + m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261} + m_LocalPosition: {x: 7.33, y: 1.28, z: -4.76} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} diff --git a/Assets/Scenes/BallHumanScene.unity.meta b/Assets/Scenes/BallHumanScene.unity.meta new file mode 100644 index 0000000..51a0d7c --- /dev/null +++ b/Assets/Scenes/BallHumanScene.unity.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: aa15f758637754fa394d6979dab2c3fa +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: From b45aee859b2a86cdb48111d88dea84c36ee049e1 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 5 Mar 2023 22:26:01 +0800 Subject: [PATCH 23/29] =?UTF-8?q?feature:=20=E6=B7=BB=E5=8A=A0z=E8=BD=B4?= =?UTF-8?q?=E5=8E=8B=E7=BC=A9=E5=A4=84=E7=90=86=E7=B1=BB=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E4=BA=86=E8=BA=AB=E4=BD=93=E5=89=8D=E5=80=BE=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Behaviours/BoneBehaviour.cs | 4 ++-- .../PoseTransformHandlers/ZAxisHandler.cs | 20 +++++++++++++++++++ .../ZAxisHandler.cs.meta | 3 +++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 Assets/Utils/PoseTransformHandlers/ZAxisHandler.cs create mode 100644 Assets/Utils/PoseTransformHandlers/ZAxisHandler.cs.meta diff --git a/Assets/Behaviours/BoneBehaviour.cs b/Assets/Behaviours/BoneBehaviour.cs index 63d7275..1a8437e 100644 --- a/Assets/Behaviours/BoneBehaviour.cs +++ b/Assets/Behaviours/BoneBehaviour.cs @@ -8,7 +8,7 @@ namespace Behaviours public class BoneBehaviour : MonoBehaviour { private readonly UdpListener _listener = new UdpListener(); - private PoseTransform[] _transforms = new PoseTransform[PoseLandmarkType.MaxValue]; + private readonly PoseTransform[] _transforms = new PoseTransform[PoseLandmarkType.MaxValue]; private Animator _animator; @@ -18,7 +18,7 @@ private void Start() for (var i = 0; i < PoseLandmarkType.MaxValue; i++) { - _transforms[i] = new PoseTransform(i, new NormalHandler()); + _transforms[i] = new PoseTransform(i, new ZAxisHandler()); } _listener.AddHandler(OnReceive); diff --git a/Assets/Utils/PoseTransformHandlers/ZAxisHandler.cs b/Assets/Utils/PoseTransformHandlers/ZAxisHandler.cs new file mode 100644 index 0000000..00d523e --- /dev/null +++ b/Assets/Utils/PoseTransformHandlers/ZAxisHandler.cs @@ -0,0 +1,20 @@ +using Models; +using UnityEngine; + +namespace Utils.PoseTransformHandlers +{ + public class ZAxisHandler : IPoseTransformHandler + { + private Vector3 _result; + + public void ReceivePoseLandmark(PoseLandmark landmark) + { + _result = new Vector3(landmark.X, landmark.Y, landmark.Z * 0.5f); + } + + public Vector3 GetResultPosition() + { + return _result; + } + } +} \ No newline at end of file diff --git a/Assets/Utils/PoseTransformHandlers/ZAxisHandler.cs.meta b/Assets/Utils/PoseTransformHandlers/ZAxisHandler.cs.meta new file mode 100644 index 0000000..31f7c86 --- /dev/null +++ b/Assets/Utils/PoseTransformHandlers/ZAxisHandler.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 12369669a7fc44eaae881f89678649b1 +timeCreated: 1678024599 \ No newline at end of file From 4cd3bad951d0c4ba6e037ec1884d50d8a47b58a7 Mon Sep 17 00:00:00 2001 From: Ichirinko <1621543655@qq.com> Date: Sat, 25 Mar 2023 11:43:42 +0800 Subject: [PATCH 24/29] =?UTF-8?q?add:=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=AD=90?= =?UTF-8?q?=E5=85=B3=E8=8A=82=E4=B8=8E=E7=88=B6=E5=85=B3=E8=8A=82=E7=9A=84?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Behaviours/BoneBehaviour.cs | 62 ++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/Assets/Behaviours/BoneBehaviour.cs b/Assets/Behaviours/BoneBehaviour.cs index 1a8437e..5d5dbcd 100644 --- a/Assets/Behaviours/BoneBehaviour.cs +++ b/Assets/Behaviours/BoneBehaviour.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Models; using UnityEngine; @@ -27,18 +28,9 @@ private void Start() private void Update() { - var frontLeft = Vector3.Cross( - _transforms[PoseLandmarkType.RightHip] - _transforms[PoseLandmarkType.LeftHip], - _transforms[PoseLandmarkType.RightShoulder] - _transforms[PoseLandmarkType.LeftHip]); - - var frontRight = Vector3.Cross( - _transforms[PoseLandmarkType.LeftShoulder] - _transforms[PoseLandmarkType.RightHip], - _transforms[PoseLandmarkType.LeftHip] - _transforms[PoseLandmarkType.RightHip]); - - var front = frontLeft + frontRight; - front.Normalize(); - - _animator.GetBoneTransform(HumanBodyBones.Hips).rotation = Quaternion.LookRotation(front); + HipUpdate(); + + RShoulderUpdate(); } private void OnDisable() @@ -53,5 +45,51 @@ private void OnReceive(List landmarks) PoseTransform.UpdatePosition(ref _transforms[landmark.Type.Value], landmark); } } + + + private void HipUpdate() + { + var frontLeft = Vector3.Cross( + _transforms[PoseLandmarkType.RightHip] - _transforms[PoseLandmarkType.LeftHip], + _transforms[PoseLandmarkType.RightShoulder] - _transforms[PoseLandmarkType.LeftHip]); + + var frontRight = Vector3.Cross( + _transforms[PoseLandmarkType.LeftShoulder] - _transforms[PoseLandmarkType.RightHip], + _transforms[PoseLandmarkType.LeftHip] - _transforms[PoseLandmarkType.RightHip]); + + var front = frontLeft + frontRight; + front.Normalize(); + + _animator.GetBoneTransform(HumanBodyBones.Hips).rotation = Quaternion.LookRotation(front); + } + + private void RShoulderUpdate() + { + + var relativeRot = _transforms[PoseLandmarkType.RightElbow] - _transforms[PoseLandmarkType.RightShoulder]; + relativeRot.Normalize(); + + var hipRot = _animator.GetBoneTransform(HumanBodyBones.Hips).rotation; + hipRot.Normalize(); + + var interal = Quaternion.FromToRotation(relativeRot, hipRot.eulerAngles); + interal.Normalize(); + + _animator.GetBoneTransform(HumanBodyBones.RightUpperArm).rotation = interal; //Quaternion.LookRotation(relativeRot + hipRot); + } + + + //计算向量a到向量b的旋转角 + //参数 a:起始向量; b:目标向量; n:旋转方向 (0, 1, 0)顺时针 (0, -1, 0)逆时针 + private static float SignedAngleBetween(Vector3 a, Vector3 b, Vector3 n) + { + var angle = Vector3.Angle(a,b); + var sign = Mathf.Sign(Vector3.Dot(n,Vector3.Cross(a,b))); + var signedAngle = angle * sign; + return (signedAngle <= 0) ? 360 + signedAngle : signedAngle; + } } + + + } From d901880eeaab4d6a534b6a87afb6e1d564216307 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 27 Mar 2023 13:10:42 +0800 Subject: [PATCH 25/29] =?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 From 479e64907f0e4ee092fcd87ec52dd8a4d537a297 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 27 Mar 2023 13:11:01 +0800 Subject: [PATCH 26/29] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E6=8E=A7=E5=88=B6=E6=80=9D=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Behaviours/BoneBehaviour.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/Assets/Behaviours/BoneBehaviour.cs b/Assets/Behaviours/BoneBehaviour.cs index 5d5dbcd..d86b700 100644 --- a/Assets/Behaviours/BoneBehaviour.cs +++ b/Assets/Behaviours/BoneBehaviour.cs @@ -65,17 +65,12 @@ private void HipUpdate() private void RShoulderUpdate() { + var direction = _transforms[PoseLandmarkType.RightElbow] + - _transforms[PoseLandmarkType.RightShoulder]; + var absolutelyRotation = Quaternion.LookRotation(direction); - var relativeRot = _transforms[PoseLandmarkType.RightElbow] - _transforms[PoseLandmarkType.RightShoulder]; - relativeRot.Normalize(); - - var hipRot = _animator.GetBoneTransform(HumanBodyBones.Hips).rotation; - hipRot.Normalize(); - - var interal = Quaternion.FromToRotation(relativeRot, hipRot.eulerAngles); - interal.Normalize(); - - _animator.GetBoneTransform(HumanBodyBones.RightUpperArm).rotation = interal; //Quaternion.LookRotation(relativeRot + hipRot); + _animator.GetBoneTransform(HumanBodyBones.RightUpperArm).rotation + = absolutelyRotation * Quaternion.Inverse(_animator.GetBoneTransform(HumanBodyBones.Hips).rotation); } From 77bf5230019787409fd372f57653f6eea125fe1a Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 29 Mar 2023 22:58:07 +0800 Subject: [PATCH 27/29] =?UTF-8?q?feature:=20=E8=88=B9=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E5=9F=BA=E4=BA=8E=E8=BF=87=E7=A8=8B=E7=9A=84=E5=8A=A8=E4=BD=9C?= =?UTF-8?q?=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Behaviours/BallStickBehaviour.cs | 295 +++++++++--------- Assets/Behaviours/BoneBehaviour.cs | 87 +++--- Assets/Models/PoseLandmark.cs | 3 + Assets/Models/RotationNode.cs | 29 ++ Assets/Models/RotationNode.cs.meta | 3 + .../PoseTransformHandlers/ZAxisHandler.cs | 3 +- 6 files changed, 226 insertions(+), 194 deletions(-) create mode 100644 Assets/Models/RotationNode.cs create mode 100644 Assets/Models/RotationNode.cs.meta diff --git a/Assets/Behaviours/BallStickBehaviour.cs b/Assets/Behaviours/BallStickBehaviour.cs index 2823943..62e20f8 100644 --- a/Assets/Behaviours/BallStickBehaviour.cs +++ b/Assets/Behaviours/BallStickBehaviour.cs @@ -3,178 +3,181 @@ using UnityEngine; using Utils.PoseTransformHandlers; -public class MainBehaviour : MonoBehaviour +namespace Behaviours { - private readonly GameObject[] _nodes = new GameObject[PoseLandmarkType.MaxValue]; - private readonly List _bonds = new List(); - private readonly PoseTransform[] _poseTransforms = new PoseTransform[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() + public class BallStickBehaviour : MonoBehaviour { - PoseTransform.AverageLength = 3; + private readonly GameObject[] _nodes = new GameObject[PoseLandmarkType.MaxValue]; + private readonly List _bonds = new List(); + private readonly PoseTransform[] _poseTransforms = new PoseTransform[PoseLandmarkType.MaxValue]; + private readonly UdpListener _listener = new UdpListener(); + private const float Scale = 0.2f; - CreateNodes(); - CreateBonds(); - - _listener.AddHandler(OnReceive); - _listener.Connect(5000); - } - - // Update is called once per frame - private void Update() - { - for (var i = 0; i < PoseLandmarkType.MaxValue; i++) + // Start is called before the first frame update + private void Start() { - _nodes[i].transform.position = _poseTransforms[i].ResultPosition * -5; + PoseTransform.AverageLength = 3; + + CreateNodes(); + CreateBonds(); + + _listener.AddHandler(OnReceive); + _listener.Connect(5000); } - foreach (var bond in _bonds) + // Update is called once per frame + private void Update() { - bond.UpdateBond(); - } - } + for (var i = 0; i < PoseLandmarkType.MaxValue; i++) + { + _nodes[i].transform.position = _poseTransforms[i].ResultPosition * -5; + } - private void OnDisable() - { - _listener.DisConnect(); - } + foreach (var bond in _bonds) + { + bond.UpdateBond(); + } + } + + private void OnDisable() + { + _listener.DisConnect(); + } - private void OnReceive(List landmarks) - { - foreach (var landmark in landmarks) + private void OnReceive(List landmarks) { - PoseTransform.UpdatePosition(ref _poseTransforms[landmark.Type.Value], landmark); + foreach (var landmark in landmarks) + { + PoseTransform.UpdatePosition(ref _poseTransforms[landmark.Type.Value], landmark); + } } - } - private void CreateNodes() - { - for (var i = 0; i < PoseLandmarkType.MaxValue; i++) + private void CreateNodes() { - var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere); - - ball.transform.localScale = new Vector3(Scale, Scale, Scale); - - if (i <= 10) + for (var i = 0; i < PoseLandmarkType.MaxValue; i++) { - ball.GetComponent().material.color = Color.red;//给头部添加颜色 - } - else if (i <= 22) - { - ball.GetComponent().material.color = Color.blue;//给手部添加颜色 - } - else if (i <= 32) - { - ball.GetComponent().material.color = Color.green;//给脚部添加颜色 - } + var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere); - _nodes[i] = ball; - _poseTransforms[i] = new PoseTransform(i, new AverageHandler()); + ball.transform.localScale = new Vector3(Scale, Scale, Scale); + + if (i <= 10) + { + ball.GetComponent().material.color = Color.red;//给头部添加颜色 + } + else if (i <= 22) + { + ball.GetComponent().material.color = Color.blue;//给手部添加颜色 + } + else if (i <= 32) + { + ball.GetComponent().material.color = Color.green;//给脚部添加颜色 + } + + _nodes[i] = ball; + _poseTransforms[i] = new PoseTransform(i, new AverageHandler()); + } } - } - private void CreateBonds() - { - var headBonds = new[] + private void CreateBonds() { - PoseLandmarkType.RightEar, - PoseLandmarkType.RightEyeOuter, - PoseLandmarkType.RightEye, - PoseLandmarkType.RightEyeInner, - PoseLandmarkType.Nose, - PoseLandmarkType.LeftEyeInner, - PoseLandmarkType.LeftEye, - PoseLandmarkType.LeftEyeOuter, - PoseLandmarkType.LeftEar - }; - _bonds.AddRange(GenerateBondsList(headBonds)); + var headBonds = new[] + { + PoseLandmarkType.RightEar, + PoseLandmarkType.RightEyeOuter, + PoseLandmarkType.RightEye, + PoseLandmarkType.RightEyeInner, + PoseLandmarkType.Nose, + PoseLandmarkType.LeftEyeInner, + PoseLandmarkType.LeftEye, + PoseLandmarkType.LeftEyeOuter, + PoseLandmarkType.LeftEar + }; + _bonds.AddRange(GenerateBondsList(headBonds)); - var monthBonds = new[] - { - PoseLandmarkType.MouthLeft, - PoseLandmarkType.MouthRight, - }; - _bonds.AddRange(GenerateBondsList(monthBonds)); + var monthBonds = new[] + { + PoseLandmarkType.MouthLeft, + PoseLandmarkType.MouthRight, + }; + _bonds.AddRange(GenerateBondsList(monthBonds)); - var leftArmBonds = new[] - { - PoseLandmarkType.LeftShoulder, - PoseLandmarkType.LeftElbow, - PoseLandmarkType.LeftWrist, - PoseLandmarkType.LeftPinky, - PoseLandmarkType.LeftIndex, - PoseLandmarkType.LeftWrist, - PoseLandmarkType.LeftThumb - }; - _bonds.AddRange(GenerateBondsList(leftArmBonds)); + var leftArmBonds = new[] + { + PoseLandmarkType.LeftShoulder, + PoseLandmarkType.LeftElbow, + PoseLandmarkType.LeftWrist, + PoseLandmarkType.LeftPinky, + PoseLandmarkType.LeftIndex, + PoseLandmarkType.LeftWrist, + PoseLandmarkType.LeftThumb + }; + _bonds.AddRange(GenerateBondsList(leftArmBonds)); - var rightArmBonds = new[] - { - PoseLandmarkType.RightShoulder, - PoseLandmarkType.RightElbow, - PoseLandmarkType.RightWrist, - PoseLandmarkType.RightPinky, - PoseLandmarkType.RightIndex, - PoseLandmarkType.RightWrist, - PoseLandmarkType.RightThumb - }; - _bonds.AddRange(GenerateBondsList(rightArmBonds)); + var rightArmBonds = new[] + { + PoseLandmarkType.RightShoulder, + PoseLandmarkType.RightElbow, + PoseLandmarkType.RightWrist, + PoseLandmarkType.RightPinky, + PoseLandmarkType.RightIndex, + PoseLandmarkType.RightWrist, + PoseLandmarkType.RightThumb + }; + _bonds.AddRange(GenerateBondsList(rightArmBonds)); - var leftLegBonds = new[] - { - PoseLandmarkType.LeftShoulder, - PoseLandmarkType.LeftHip, - PoseLandmarkType.LeftKnee, - PoseLandmarkType.LeftAnkle, - PoseLandmarkType.LeftHeel, - PoseLandmarkType.LeftFootIndex, - PoseLandmarkType.LeftAnkle - }; - _bonds.AddRange(GenerateBondsList(leftLegBonds)); + var leftLegBonds = new[] + { + PoseLandmarkType.LeftShoulder, + PoseLandmarkType.LeftHip, + PoseLandmarkType.LeftKnee, + PoseLandmarkType.LeftAnkle, + PoseLandmarkType.LeftHeel, + PoseLandmarkType.LeftFootIndex, + PoseLandmarkType.LeftAnkle + }; + _bonds.AddRange(GenerateBondsList(leftLegBonds)); - var rightLegBonds = new[] - { - PoseLandmarkType.RightShoulder, - PoseLandmarkType.RightHip, - PoseLandmarkType.RightKnee, - PoseLandmarkType.RightAnkle, - PoseLandmarkType.RightHeel, - PoseLandmarkType.RightFootIndex, - PoseLandmarkType.RightAnkle - }; - _bonds.AddRange(GenerateBondsList(rightLegBonds)); + var rightLegBonds = new[] + { + PoseLandmarkType.RightShoulder, + PoseLandmarkType.RightHip, + PoseLandmarkType.RightKnee, + PoseLandmarkType.RightAnkle, + PoseLandmarkType.RightHeel, + PoseLandmarkType.RightFootIndex, + PoseLandmarkType.RightAnkle + }; + _bonds.AddRange(GenerateBondsList(rightLegBonds)); - // 最后手动添加身体上的两条横线 - _bonds.Add(new Bond( - _nodes[PoseLandmarkType.LeftShoulder], - _nodes[PoseLandmarkType.RightShoulder], - Scale)); - _bonds.Add(new Bond( - _nodes[PoseLandmarkType.LeftHip], - _nodes[PoseLandmarkType.RightHip], - Scale)); - } - - /// - /// 创建棍子列表 - /// - /// 需要连接起来的关键点 需要按顺序设置 - /// - private List GenerateBondsList(int[] nodes) - { - var bonds = new List(); - - for (var i = 0; i < nodes.Length - 1; i++) - { - bonds.Add(new Bond( - _nodes[nodes[i]], - _nodes[nodes[i + 1]], + // 最后手动添加身体上的两条横线 + _bonds.Add(new Bond( + _nodes[PoseLandmarkType.LeftShoulder], + _nodes[PoseLandmarkType.RightShoulder], + Scale)); + _bonds.Add(new Bond( + _nodes[PoseLandmarkType.LeftHip], + _nodes[PoseLandmarkType.RightHip], Scale)); } - return bonds; + /// + /// 创建棍子列表 + /// + /// 需要连接起来的关键点 需要按顺序设置 + /// + private List GenerateBondsList(int[] nodes) + { + var bonds = new List(); + + for (var i = 0; i < nodes.Length - 1; i++) + { + bonds.Add(new Bond( + _nodes[nodes[i]], + _nodes[nodes[i + 1]], + Scale)); + } + + return bonds; + } } } diff --git a/Assets/Behaviours/BoneBehaviour.cs b/Assets/Behaviours/BoneBehaviour.cs index d86b700..eb816ea 100644 --- a/Assets/Behaviours/BoneBehaviour.cs +++ b/Assets/Behaviours/BoneBehaviour.cs @@ -1,38 +1,35 @@ -using System; using System.Collections.Generic; using Models; using UnityEngine; -using Utils.PoseTransformHandlers; namespace Behaviours { public class BoneBehaviour : MonoBehaviour { private readonly UdpListener _listener = new UdpListener(); - private readonly PoseTransform[] _transforms = new PoseTransform[PoseLandmarkType.MaxValue]; - + private Dictionary _rotationNodes; + private Animator _animator; private void Start() { _animator = GetComponent(); + CreateRotationNodes(); - for (var i = 0; i < PoseLandmarkType.MaxValue; i++) - { - _transforms[i] = new PoseTransform(i, new ZAxisHandler()); - } - _listener.AddHandler(OnReceive); _listener.Connect(5000); } private void Update() { - HipUpdate(); - - RShoulderUpdate(); + foreach (var node in _rotationNodes) + { + _animator.GetBoneTransform(node.Key).rotation = node.Value.Rotation; + } } + + private void OnDisable() { _listener.DisConnect(); @@ -40,51 +37,47 @@ private void OnDisable() private void OnReceive(List landmarks) { - foreach (var landmark in landmarks) - { - PoseTransform.UpdatePosition(ref _transforms[landmark.Type.Value], landmark); - } - } - - - private void HipUpdate() - { + // 计算腰部 var frontLeft = Vector3.Cross( - _transforms[PoseLandmarkType.RightHip] - _transforms[PoseLandmarkType.LeftHip], - _transforms[PoseLandmarkType.RightShoulder] - _transforms[PoseLandmarkType.LeftHip]); + landmarks[PoseLandmarkType.RightHip].Vector3 - landmarks[PoseLandmarkType.LeftHip].Vector3, + landmarks[PoseLandmarkType.RightShoulder].Vector3 - landmarks[PoseLandmarkType.LeftHip].Vector3); var frontRight = Vector3.Cross( - _transforms[PoseLandmarkType.LeftShoulder] - _transforms[PoseLandmarkType.RightHip], - _transforms[PoseLandmarkType.LeftHip] - _transforms[PoseLandmarkType.RightHip]); + landmarks[PoseLandmarkType.LeftShoulder].Vector3 - landmarks[PoseLandmarkType.RightHip].Vector3, + landmarks[PoseLandmarkType.LeftHip].Vector3 - landmarks[PoseLandmarkType.LeftHip].Vector3); var front = frontLeft + frontRight; front.Normalize(); - _animator.GetBoneTransform(HumanBodyBones.Hips).rotation = Quaternion.LookRotation(front); + var oldRotation = _rotationNodes[HumanBodyBones.Hips]; + _rotationNodes[HumanBodyBones.Hips] = new RotationNode( + oldRotation.UnityName, + oldRotation.Rotation, + Quaternion.LookRotation(front)); } - - private void RShoulderUpdate() - { - var direction = _transforms[PoseLandmarkType.RightElbow] - - _transforms[PoseLandmarkType.RightShoulder]; - var absolutelyRotation = Quaternion.LookRotation(direction); - _animator.GetBoneTransform(HumanBodyBones.RightUpperArm).rotation - = absolutelyRotation * Quaternion.Inverse(_animator.GetBoneTransform(HumanBodyBones.Hips).rotation); - } - - - //计算向量a到向量b的旋转角 - //参数 a:起始向量; b:目标向量; n:旋转方向 (0, 1, 0)顺时针 (0, -1, 0)逆时针 - private static float SignedAngleBetween(Vector3 a, Vector3 b, Vector3 n) + private void CreateRotationNodes() { - var angle = Vector3.Angle(a,b); - var sign = Mathf.Sign(Vector3.Dot(n,Vector3.Cross(a,b))); - var signedAngle = angle * sign; - return (signedAngle <= 0) ? 360 + signedAngle : signedAngle; + _rotationNodes = new Dictionary(); + + var bonesArray = new[] + { + HumanBodyBones.Hips, + HumanBodyBones.LeftUpperArm, + HumanBodyBones.LeftLowerArm, + HumanBodyBones.RightUpperArm, + HumanBodyBones.RightLowerArm, + HumanBodyBones.LeftUpperLeg, + HumanBodyBones.LeftLowerLeg, + HumanBodyBones.RightUpperLeg, + HumanBodyBones.RightLowerLeg + }; + + foreach (var bone in bonesArray) + { + var rotation = _animator.GetBoneTransform(bone).rotation; + _rotationNodes.Add(bone, new RotationNode(bone, rotation, rotation)); + } } } - - - } diff --git a/Assets/Models/PoseLandmark.cs b/Assets/Models/PoseLandmark.cs index a68a987..c0f4e2c 100644 --- a/Assets/Models/PoseLandmark.cs +++ b/Assets/Models/PoseLandmark.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using UnityEngine; namespace Models { @@ -39,6 +40,8 @@ public class PoseLandmark /// public float Z { get; } + public Vector3 Vector3 => new Vector3(X, Y, Z); + /// /// 该坐标点估计的可见性 /// [0,1] diff --git a/Assets/Models/RotationNode.cs b/Assets/Models/RotationNode.cs new file mode 100644 index 0000000..2eedcee --- /dev/null +++ b/Assets/Models/RotationNode.cs @@ -0,0 +1,29 @@ +using UnityEngine; + +namespace Models +{ + public struct RotationNode + { + public HumanBodyBones UnityName; + public Quaternion PreviousRotation; + public Quaternion Rotation; + + public Quaternion CalculateRotate => PreviousRotation * Quaternion.Inverse(PreviousRotation); + + public RotationNode(HumanBodyBones humanBodyBone, Quaternion previousRotation, Quaternion rotation) + { + UnityName = humanBodyBone; + PreviousRotation = previousRotation; + Rotation = rotation; + } + + public RotationNode(RotationNode node, PoseLandmark begin, PoseLandmark end) + { + UnityName = node.UnityName; + PreviousRotation = node.Rotation; + + var forward = new Vector3(end.X - begin.X, end.Y - begin.Y, end.Z - begin.Z); + Rotation = Quaternion.LookRotation(forward); + } + } +} \ No newline at end of file diff --git a/Assets/Models/RotationNode.cs.meta b/Assets/Models/RotationNode.cs.meta new file mode 100644 index 0000000..20d9271 --- /dev/null +++ b/Assets/Models/RotationNode.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 87e238cb51f34339a39945af13f43248 +timeCreated: 1680090845 \ No newline at end of file diff --git a/Assets/Utils/PoseTransformHandlers/ZAxisHandler.cs b/Assets/Utils/PoseTransformHandlers/ZAxisHandler.cs index 00d523e..918e581 100644 --- a/Assets/Utils/PoseTransformHandlers/ZAxisHandler.cs +++ b/Assets/Utils/PoseTransformHandlers/ZAxisHandler.cs @@ -9,7 +9,8 @@ public class ZAxisHandler : IPoseTransformHandler public void ReceivePoseLandmark(PoseLandmark landmark) { - _result = new Vector3(landmark.X, landmark.Y, landmark.Z * 0.5f); + _result = new Vector3(landmark.X, landmark.Y, landmark.Z * 0.2f); + _result = _result * -1; } public Vector3 GetResultPosition() From f812a1b8317f2a717b64fb0bdd371af0c1271c18 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 30 Mar 2023 14:26:38 +0800 Subject: [PATCH 28/29] =?UTF-8?q?feature:=20=E6=A8=A1=E5=9E=8B=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E5=9B=9B=E8=82=A2=E5=92=8C=E8=BA=AB=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Behaviours/BallStickBehaviour.cs | 19 ++- Assets/Behaviours/BoneBehaviour.cs | 186 +++++++++++++++++++++++- Assets/Models/Bond.cs | 2 + Assets/Models/PoseLandmark.cs | 2 +- Assets/Models/RotationNode.cs | 6 +- Assets/Scenes/SampleScene.unity | 8 +- 6 files changed, 203 insertions(+), 20 deletions(-) diff --git a/Assets/Behaviours/BallStickBehaviour.cs b/Assets/Behaviours/BallStickBehaviour.cs index 62e20f8..59054df 100644 --- a/Assets/Behaviours/BallStickBehaviour.cs +++ b/Assets/Behaviours/BallStickBehaviour.cs @@ -28,9 +28,12 @@ private void Start() // Update is called once per frame private void Update() { + var mid = _poseTransforms[PoseLandmarkType.LeftHip] + _poseTransforms[PoseLandmarkType.RightHip]; + mid = mid / 2; + for (var i = 0; i < PoseLandmarkType.MaxValue; i++) { - _nodes[i].transform.position = _poseTransforms[i].ResultPosition * -5; + _nodes[i].transform.position = (_poseTransforms[i].ResultPosition - mid) * -5f; } foreach (var bond in _bonds) @@ -43,7 +46,7 @@ private void OnDisable() { _listener.DisConnect(); } - + private void OnReceive(List landmarks) { foreach (var landmark in landmarks) @@ -62,15 +65,15 @@ private void CreateNodes() if (i <= 10) { - ball.GetComponent().material.color = Color.red;//给头部添加颜色 + ball.GetComponent().material.color = Color.red; //给头部添加颜色 } else if (i <= 22) { - ball.GetComponent().material.color = Color.blue;//给手部添加颜色 + ball.GetComponent().material.color = Color.blue; //给手部添加颜色 } else if (i <= 32) { - ball.GetComponent().material.color = Color.green;//给脚部添加颜色 + ball.GetComponent().material.color = Color.green; //给脚部添加颜色 } _nodes[i] = ball; @@ -148,7 +151,7 @@ private void CreateBonds() PoseLandmarkType.RightAnkle }; _bonds.AddRange(GenerateBondsList(rightLegBonds)); - + // 最后手动添加身体上的两条横线 _bonds.Add(new Bond( _nodes[PoseLandmarkType.LeftShoulder], @@ -172,8 +175,8 @@ private List GenerateBondsList(int[] nodes) for (var i = 0; i < nodes.Length - 1; i++) { bonds.Add(new Bond( - _nodes[nodes[i]], - _nodes[nodes[i + 1]], + _nodes[nodes[i]], + _nodes[nodes[i + 1]], Scale)); } diff --git a/Assets/Behaviours/BoneBehaviour.cs b/Assets/Behaviours/BoneBehaviour.cs index eb816ea..a96832f 100644 --- a/Assets/Behaviours/BoneBehaviour.cs +++ b/Assets/Behaviours/BoneBehaviour.cs @@ -7,14 +7,21 @@ namespace Behaviours public class BoneBehaviour : MonoBehaviour { private readonly UdpListener _listener = new UdpListener(); + private readonly GameObject[] _nodes = new GameObject[PoseLandmarkType.MaxValue]; + private readonly List _bonds = new List(); private Dictionary _rotationNodes; + private List _landmarks; private Animator _animator; + private bool _isReceived; + private const float Scale = 0.2f; private void Start() { _animator = GetComponent(); CreateRotationNodes(); + CreateNodes(); + CreateBonds(); _listener.AddHandler(OnReceive); _listener.Connect(5000); @@ -22,9 +29,41 @@ private void Start() private void Update() { - foreach (var node in _rotationNodes) + if (_isReceived) { - _animator.GetBoneTransform(node.Key).rotation = node.Value.Rotation; + _animator.GetBoneTransform(HumanBodyBones.Hips).rotation = _rotationNodes[HumanBodyBones.Hips].Rotation; + + /*var rotation = _rotationNodes[HumanBodyBones.Hips].CalculateRotate + * Quaternion.Inverse(_rotationNodes[HumanBodyBones.RightUpperArm].CalculateRotate); + + _animator.GetBoneTransform(HumanBodyBones.RightUpperArm).Rotate(rotation.eulerAngles);*/ + + var mid = (_landmarks[PoseLandmarkType.LeftHip].Position + + _landmarks[PoseLandmarkType.RightHip].Position) / 2; + + for (var i = 0; i < PoseLandmarkType.MaxValue; i++) + { + _nodes[i].transform.position = (_landmarks[i].Position - mid) * 5f; + } + + foreach (var bond in _bonds) + { + bond.UpdateBond(); + } + + _animator.GetBoneTransform(HumanBodyBones.LeftUpperArm).rotation = _bonds[9].Rotation; + _animator.GetBoneTransform(HumanBodyBones.LeftLowerArm).rotation = _bonds[10].Rotation; + + _animator.GetBoneTransform(HumanBodyBones.RightUpperArm).rotation = _bonds[15].Rotation; + _animator.GetBoneTransform(HumanBodyBones.RightLowerArm).rotation = _bonds[16].Rotation; + + _animator.GetBoneTransform(HumanBodyBones.LeftUpperLeg).rotation = _bonds[22].Rotation; + _animator.GetBoneTransform(HumanBodyBones.LeftLowerLeg).rotation = _bonds[23].Rotation; + + _animator.GetBoneTransform(HumanBodyBones.RightUpperLeg).rotation = _bonds[28].Rotation; + _animator.GetBoneTransform(HumanBodyBones.RightLowerLeg).rotation = _bonds[29].Rotation; + + _isReceived = false; } } @@ -39,12 +78,12 @@ private void OnReceive(List landmarks) { // 计算腰部 var frontLeft = Vector3.Cross( - landmarks[PoseLandmarkType.RightHip].Vector3 - landmarks[PoseLandmarkType.LeftHip].Vector3, - landmarks[PoseLandmarkType.RightShoulder].Vector3 - landmarks[PoseLandmarkType.LeftHip].Vector3); + landmarks[PoseLandmarkType.RightHip].Position - landmarks[PoseLandmarkType.LeftHip].Position, + landmarks[PoseLandmarkType.RightShoulder].Position - landmarks[PoseLandmarkType.LeftHip].Position); var frontRight = Vector3.Cross( - landmarks[PoseLandmarkType.LeftShoulder].Vector3 - landmarks[PoseLandmarkType.RightHip].Vector3, - landmarks[PoseLandmarkType.LeftHip].Vector3 - landmarks[PoseLandmarkType.LeftHip].Vector3); + landmarks[PoseLandmarkType.LeftShoulder].Position - landmarks[PoseLandmarkType.RightHip].Position, + landmarks[PoseLandmarkType.LeftHip].Position - landmarks[PoseLandmarkType.LeftHip].Position); var front = frontLeft + frontRight; front.Normalize(); @@ -54,6 +93,14 @@ private void OnReceive(List landmarks) oldRotation.UnityName, oldRotation.Rotation, Quaternion.LookRotation(front)); + + _rotationNodes[HumanBodyBones.RightUpperArm] = new RotationNode( + _rotationNodes[HumanBodyBones.RightUpperArm], + landmarks[PoseLandmarkType.RightShoulder], + landmarks[PoseLandmarkType.RightElbow]); + + _landmarks = landmarks; + _isReceived = true; } private void CreateRotationNodes() @@ -79,5 +126,132 @@ private void CreateRotationNodes() _rotationNodes.Add(bone, new RotationNode(bone, rotation, rotation)); } } + + private void CreateNodes() + { + for (var i = 0; i < PoseLandmarkType.MaxValue; i++) + { + var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere); + + ball.transform.localScale = new Vector3(Scale, Scale, Scale); + + if (i <= 10) + { + ball.GetComponent().material.color = Color.red; //给头部添加颜色 + } + else if (i <= 22) + { + ball.GetComponent().material.color = Color.blue; //给手部添加颜色 + } + else if (i <= 32) + { + ball.GetComponent().material.color = Color.green; //给脚部添加颜色 + } + + _nodes[i] = ball; + } + } + + private void CreateBonds() + { + var headBonds = new[] + { + PoseLandmarkType.RightEar, + PoseLandmarkType.RightEyeOuter, + PoseLandmarkType.RightEye, + PoseLandmarkType.RightEyeInner, + PoseLandmarkType.Nose, + PoseLandmarkType.LeftEyeInner, + PoseLandmarkType.LeftEye, + PoseLandmarkType.LeftEyeOuter, + PoseLandmarkType.LeftEar + }; + _bonds.AddRange(GenerateBondsList(headBonds)); + + var monthBonds = new[] + { + PoseLandmarkType.MouthLeft, + PoseLandmarkType.MouthRight, + }; + _bonds.AddRange(GenerateBondsList(monthBonds)); + + var leftArmBonds = new[] + { + PoseLandmarkType.LeftShoulder, + PoseLandmarkType.LeftElbow, + PoseLandmarkType.LeftWrist, + PoseLandmarkType.LeftPinky, + PoseLandmarkType.LeftIndex, + PoseLandmarkType.LeftWrist, + PoseLandmarkType.LeftThumb + }; + _bonds.AddRange(GenerateBondsList(leftArmBonds)); + + var rightArmBonds = new[] + { + PoseLandmarkType.RightShoulder, + PoseLandmarkType.RightElbow, + PoseLandmarkType.RightWrist, + PoseLandmarkType.RightPinky, + PoseLandmarkType.RightIndex, + PoseLandmarkType.RightWrist, + PoseLandmarkType.RightThumb + }; + _bonds.AddRange(GenerateBondsList(rightArmBonds)); + + var leftLegBonds = new[] + { + PoseLandmarkType.LeftShoulder, + PoseLandmarkType.LeftHip, + PoseLandmarkType.LeftKnee, + PoseLandmarkType.LeftAnkle, + PoseLandmarkType.LeftHeel, + PoseLandmarkType.LeftFootIndex, + PoseLandmarkType.LeftAnkle + }; + _bonds.AddRange(GenerateBondsList(leftLegBonds)); + + var rightLegBonds = new[] + { + PoseLandmarkType.RightShoulder, + PoseLandmarkType.RightHip, + PoseLandmarkType.RightKnee, + PoseLandmarkType.RightAnkle, + PoseLandmarkType.RightHeel, + PoseLandmarkType.RightFootIndex, + PoseLandmarkType.RightAnkle + }; + _bonds.AddRange(GenerateBondsList(rightLegBonds)); + + // 最后手动添加身体上的两条横线 + _bonds.Add(new Bond( + _nodes[PoseLandmarkType.LeftShoulder], + _nodes[PoseLandmarkType.RightShoulder], + Scale)); + _bonds.Add(new Bond( + _nodes[PoseLandmarkType.LeftHip], + _nodes[PoseLandmarkType.RightHip], + Scale)); + } + + /// + /// 创建棍子列表 + /// + /// 需要连接起来的关键点 需要按顺序设置 + /// + private List GenerateBondsList(int[] nodes) + { + var bonds = new List(); + + for (var i = 0; i < nodes.Length - 1; i++) + { + bonds.Add(new Bond( + _nodes[nodes[i]], + _nodes[nodes[i + 1]], + Scale)); + } + + return bonds; + } } } diff --git a/Assets/Models/Bond.cs b/Assets/Models/Bond.cs index a6868d1..d77f33b 100644 --- a/Assets/Models/Bond.cs +++ b/Assets/Models/Bond.cs @@ -10,6 +10,8 @@ public class Bond protected readonly GameObject Start; protected readonly GameObject End; private readonly GameObject _bond; + + public Quaternion Rotation => _bond.transform.rotation; public Bond(GameObject start,GameObject end,float scale) { diff --git a/Assets/Models/PoseLandmark.cs b/Assets/Models/PoseLandmark.cs index c0f4e2c..68f8cc5 100644 --- a/Assets/Models/PoseLandmark.cs +++ b/Assets/Models/PoseLandmark.cs @@ -40,7 +40,7 @@ public class PoseLandmark /// public float Z { get; } - public Vector3 Vector3 => new Vector3(X, Y, Z); + public Vector3 Position => new Vector3(-X, -Y, -0.2f * Z); /// /// 该坐标点估计的可见性 diff --git a/Assets/Models/RotationNode.cs b/Assets/Models/RotationNode.cs index 2eedcee..269c392 100644 --- a/Assets/Models/RotationNode.cs +++ b/Assets/Models/RotationNode.cs @@ -4,11 +4,11 @@ namespace Models { public struct RotationNode { - public HumanBodyBones UnityName; + public readonly HumanBodyBones UnityName; public Quaternion PreviousRotation; public Quaternion Rotation; - public Quaternion CalculateRotate => PreviousRotation * Quaternion.Inverse(PreviousRotation); + public Quaternion CalculateRotate => PreviousRotation * Quaternion.Inverse(Rotation); public RotationNode(HumanBodyBones humanBodyBone, Quaternion previousRotation, Quaternion rotation) { @@ -22,7 +22,7 @@ public RotationNode(RotationNode node, PoseLandmark begin, PoseLandmark end) UnityName = node.UnityName; PreviousRotation = node.Rotation; - var forward = new Vector3(end.X - begin.X, end.Y - begin.Y, end.Z - begin.Z); + var forward = end.Position - begin.Position; Rotation = Quaternion.LookRotation(forward); } } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 6646bb2..c5442c5 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -136,11 +136,11 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} propertyPath: m_LocalPosition.x - value: 0 + value: -12.91 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} propertyPath: m_LocalPosition.y - value: 0 + value: -4.98 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} propertyPath: m_LocalPosition.z @@ -178,6 +178,10 @@ PrefabInstance: propertyPath: m_IsActive value: 0 objectReference: {fileID: 0} + - target: {fileID: -464305122009946348, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} + propertyPath: m_LocalPosition.x + value: -4.32 + objectReference: {fileID: 0} - target: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} propertyPath: m_Name value: troop From 2a0047a04b5a213c4005f04793bf479f56a0fd12 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 30 Mar 2023 21:18:20 +0800 Subject: [PATCH 29/29] =?UTF-8?q?feature:=20=E5=9F=BA=E6=9C=AC=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Behaviours/BoneBehaviour.cs | 29 ++++++++++++++++++++++------- Assets/Models/Bond.cs | 2 ++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Assets/Behaviours/BoneBehaviour.cs b/Assets/Behaviours/BoneBehaviour.cs index a96832f..a187bd0 100644 --- a/Assets/Behaviours/BoneBehaviour.cs +++ b/Assets/Behaviours/BoneBehaviour.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Models; using UnityEngine; @@ -31,7 +32,8 @@ private void Update() { if (_isReceived) { - _animator.GetBoneTransform(HumanBodyBones.Hips).rotation = _rotationNodes[HumanBodyBones.Hips].Rotation; + _animator.GetBoneTransform(HumanBodyBones.Hips).rotation = + _rotationNodes[HumanBodyBones.Hips].Rotation; /*var rotation = _rotationNodes[HumanBodyBones.Hips].CalculateRotate * Quaternion.Inverse(_rotationNodes[HumanBodyBones.RightUpperArm].CalculateRotate); @@ -52,16 +54,34 @@ private void Update() } _animator.GetBoneTransform(HumanBodyBones.LeftUpperArm).rotation = _bonds[9].Rotation; + _animator.GetBoneTransform(HumanBodyBones.LeftUpperArm).Rotate(0, -180, 0, + Space.Self); _animator.GetBoneTransform(HumanBodyBones.LeftLowerArm).rotation = _bonds[10].Rotation; - + _animator.GetBoneTransform(HumanBodyBones.LeftLowerArm).Rotate(0, -180, 0, + Space.Self); + _animator.GetBoneTransform(HumanBodyBones.RightUpperArm).rotation = _bonds[15].Rotation; + _animator.GetBoneTransform(HumanBodyBones.RightUpperArm).Rotate(0, -180, 0, + Space.Self); + _animator.GetBoneTransform(HumanBodyBones.RightLowerArm).rotation = _bonds[16].Rotation; + _animator.GetBoneTransform(HumanBodyBones.RightLowerArm).Rotate(0, -180, 0, + Space.Self); _animator.GetBoneTransform(HumanBodyBones.LeftUpperLeg).rotation = _bonds[22].Rotation; + _animator.GetBoneTransform(HumanBodyBones.LeftUpperLeg).Rotate(0, -180, 0, + Space.Self); _animator.GetBoneTransform(HumanBodyBones.LeftLowerLeg).rotation = _bonds[23].Rotation; + _animator.GetBoneTransform(HumanBodyBones.LeftLowerLeg).Rotate(0, -180, 0, + Space.Self); _animator.GetBoneTransform(HumanBodyBones.RightUpperLeg).rotation = _bonds[28].Rotation; + _animator.GetBoneTransform(HumanBodyBones.RightUpperLeg).Rotate(0, -180, 0, + Space.Self); + _animator.GetBoneTransform(HumanBodyBones.RightLowerLeg).rotation = _bonds[29].Rotation; + _animator.GetBoneTransform(HumanBodyBones.RightLowerLeg).Rotate(0, -180, 0, + Space.Self); _isReceived = false; } @@ -94,11 +114,6 @@ private void OnReceive(List landmarks) oldRotation.Rotation, Quaternion.LookRotation(front)); - _rotationNodes[HumanBodyBones.RightUpperArm] = new RotationNode( - _rotationNodes[HumanBodyBones.RightUpperArm], - landmarks[PoseLandmarkType.RightShoulder], - landmarks[PoseLandmarkType.RightElbow]); - _landmarks = landmarks; _isReceived = true; } diff --git a/Assets/Models/Bond.cs b/Assets/Models/Bond.cs index d77f33b..3044302 100644 --- a/Assets/Models/Bond.cs +++ b/Assets/Models/Bond.cs @@ -1,4 +1,5 @@ using UnityEngine; +using Utils; namespace Models { @@ -12,6 +13,7 @@ public class Bond private readonly GameObject _bond; public Quaternion Rotation => _bond.transform.rotation; + public Vector3 Vector => End.transform.position - Start.transform.position; public Bond(GameObject start,GameObject end,float scale) {