add:添加了各节点之间的连接构架

This commit is contained in:
Ichirinko 2023-02-24 18:18:56 +08:00
parent 7de82ae644
commit d13b0b0b2b
3 changed files with 172 additions and 2 deletions

49
Assets/Bonds.cs Normal file
View File

@ -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<Renderer>().material.color = Color.cyan;
}
}
}

3
Assets/Bonds.cs.meta Normal file
View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 94a8f8c83e58421d9447173d38482d74
timeCreated: 1677230676

View File

@ -6,6 +6,7 @@
public class MainBehaviour : MonoBehaviour public class MainBehaviour : MonoBehaviour
{ {
private readonly List<GameObject> _nodes = new List<GameObject>(); private readonly List<GameObject> _nodes = new List<GameObject>();
private readonly List<Bonds> _bonds = new List<Bonds>();
private readonly List<PoseTransform> _poseTransforms = new List<PoseTransform>(); private readonly List<PoseTransform> _poseTransforms = new List<PoseTransform>();
private readonly UdpListener _listener = new UdpListener(); private readonly UdpListener _listener = new UdpListener();
private const float _scale = 0.2f; private const float _scale = 0.2f;
@ -15,7 +16,7 @@ public class MainBehaviour : MonoBehaviour
private void Start() private void Start()
{ {
CreateNodes(); CreateNodes();
CreateBonds();
_listener.AddHandler(OnReceive); _listener.AddHandler(OnReceive);
_listener.Connect(5000); _listener.Connect(5000);
} }
@ -30,12 +31,17 @@ private void Update()
var temp = new Vector4(_poseTransforms[index].MediaPipePos.x, var temp = new Vector4(_poseTransforms[index].MediaPipePos.x,
_poseTransforms[index].MediaPipePos.y, _poseTransforms[index].MediaPipePos.y,
_poseTransforms[index].MediaPipePos.z,1); _poseTransforms[index].MediaPipePos.z,1);
temp = RotateAxisX(-45) * temp; //temp = RotateAxisX(-45) * temp;
//warning:乘法的顺序不能改! //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; //_nodes[index].transform.position = _poseTransforms[index].MediaPipePos * 5;
} }
foreach (var bond in _bonds)
{
bond.UpdateBond();
}
} }
private void OnDisable() 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<PoseLandmark> landmarks) private void OnReceive(List<PoseLandmark> landmarks)
{ {
foreach (var landmark in landmarks) foreach (var landmark in landmarks)