using System.Collections.Generic; using Models; using UnityEngine; public class MainBehaviour : MonoBehaviour { private readonly GameObject[] _nodes = new GameObject[(int)PoseLandmarkType.MaxValue]; private readonly List _bonds = 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(); _listener.AddHandler(OnReceive); _listener.Connect(5000); } // Update is called once per frame private void Update() { for (var i = 0; i < (int)PoseLandmarkType.MaxValue; i++) { _nodes[i].transform.position = _poseTransforms[i].AveragePos * -5; } foreach (var bond in _bonds) { bond.UpdateBond(); } } 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 < (int)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; _poseTransforms[i] = new PoseTransform((PoseLandmarkType)i); } } 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[(int)PoseLandmarkType.LeftShoulder], _nodes[(int)PoseLandmarkType.RightShoulder], Scale)); _bonds.Add(new Bond( _nodes[(int)PoseLandmarkType.LeftHip], _nodes[(int)PoseLandmarkType.RightHip], Scale)); } /// /// 创建棍子列表 /// /// 需要连接起来的关键点 需要按顺序设置 /// 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; } }