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] 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: []