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