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)