2023-02-21 21:40:12 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using Models;
|
|
|
|
using UnityEngine;
|
2023-03-01 22:07:39 +08:00
|
|
|
using Utils.PoseTransformHandlers;
|
2023-02-21 21:40:12 +08:00
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
namespace Behaviours
|
2023-02-21 21:40:12 +08:00
|
|
|
{
|
2023-03-29 22:58:07 +08:00
|
|
|
public class BallStickBehaviour : MonoBehaviour
|
2023-02-21 21:40:12 +08:00
|
|
|
{
|
2023-03-29 22:58:07 +08:00
|
|
|
private readonly GameObject[] _nodes = new GameObject[PoseLandmarkType.MaxValue];
|
|
|
|
private readonly List<Bond> _bonds = new List<Bond>();
|
|
|
|
private readonly PoseTransform[] _poseTransforms = new PoseTransform[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;
|
2023-02-28 12:23:15 +08:00
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
CreateNodes();
|
|
|
|
CreateBonds();
|
2023-02-27 13:34:19 +08:00
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
_listener.AddHandler(OnReceive);
|
|
|
|
_listener.Connect(5000);
|
|
|
|
}
|
2023-02-21 21:40:12 +08:00
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
// Update is called once per frame
|
|
|
|
private void Update()
|
2023-02-21 21:40:12 +08:00
|
|
|
{
|
2023-03-30 14:26:38 +08:00
|
|
|
var mid = _poseTransforms[PoseLandmarkType.LeftHip] + _poseTransforms[PoseLandmarkType.RightHip];
|
|
|
|
mid = mid / 2;
|
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
for (var i = 0; i < PoseLandmarkType.MaxValue; i++)
|
|
|
|
{
|
2023-03-30 14:26:38 +08:00
|
|
|
_nodes[i].transform.position = (_poseTransforms[i].ResultPosition - mid) * -5f;
|
2023-03-29 22:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var bond in _bonds)
|
|
|
|
{
|
|
|
|
bond.UpdateBond();
|
|
|
|
}
|
2023-02-21 21:40:12 +08:00
|
|
|
}
|
2023-02-24 18:18:56 +08:00
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
private void OnDisable()
|
2023-02-24 18:18:56 +08:00
|
|
|
{
|
2023-03-29 22:58:07 +08:00
|
|
|
_listener.DisConnect();
|
2023-02-24 18:18:56 +08:00
|
|
|
}
|
2023-03-30 14:26:38 +08:00
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
private void OnReceive(List<PoseLandmark> landmarks)
|
2023-02-28 12:23:15 +08:00
|
|
|
{
|
2023-03-29 22:58:07 +08:00
|
|
|
foreach (var landmark in landmarks)
|
|
|
|
{
|
|
|
|
PoseTransform.UpdatePosition(ref _poseTransforms[landmark.Type.Value], landmark);
|
|
|
|
}
|
2023-02-28 12:23:15 +08:00
|
|
|
}
|
2023-02-21 21:40:12 +08:00
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
private void CreateNodes()
|
2023-02-21 21:40:12 +08:00
|
|
|
{
|
2023-03-29 22:58:07 +08:00
|
|
|
for (var i = 0; i < PoseLandmarkType.MaxValue; i++)
|
2023-02-22 19:51:22 +08:00
|
|
|
{
|
2023-03-29 22:58:07 +08:00
|
|
|
var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
|
|
|
|
|
|
|
ball.transform.localScale = new Vector3(Scale, Scale, Scale);
|
|
|
|
|
|
|
|
if (i <= 10)
|
|
|
|
{
|
2023-03-30 14:26:38 +08:00
|
|
|
ball.GetComponent<Renderer>().material.color = Color.red; //给头部添加颜色
|
2023-03-29 22:58:07 +08:00
|
|
|
}
|
|
|
|
else if (i <= 22)
|
|
|
|
{
|
2023-03-30 14:26:38 +08:00
|
|
|
ball.GetComponent<Renderer>().material.color = Color.blue; //给手部添加颜色
|
2023-03-29 22:58:07 +08:00
|
|
|
}
|
|
|
|
else if (i <= 32)
|
|
|
|
{
|
2023-03-30 14:26:38 +08:00
|
|
|
ball.GetComponent<Renderer>().material.color = Color.green; //给脚部添加颜色
|
2023-03-29 22:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_nodes[i] = ball;
|
|
|
|
_poseTransforms[i] = new PoseTransform(i, new AverageHandler());
|
2023-02-22 19:51:22 +08:00
|
|
|
}
|
2023-03-29 22:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void CreateBonds()
|
|
|
|
{
|
|
|
|
var headBonds = new[]
|
2023-02-22 19:51:22 +08:00
|
|
|
{
|
2023-03-29 22:58:07 +08:00
|
|
|
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[]
|
2023-02-22 19:51:22 +08:00
|
|
|
{
|
2023-03-29 22:58:07 +08:00
|
|
|
PoseLandmarkType.MouthLeft,
|
|
|
|
PoseLandmarkType.MouthRight,
|
|
|
|
};
|
|
|
|
_bonds.AddRange(GenerateBondsList(monthBonds));
|
2023-02-21 21:40:12 +08:00
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
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));
|
2023-03-30 14:26:38 +08:00
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
// 最后手动添加身体上的两条横线
|
|
|
|
_bonds.Add(new Bond(
|
|
|
|
_nodes[PoseLandmarkType.LeftShoulder],
|
|
|
|
_nodes[PoseLandmarkType.RightShoulder],
|
|
|
|
Scale));
|
|
|
|
_bonds.Add(new Bond(
|
|
|
|
_nodes[PoseLandmarkType.LeftHip],
|
|
|
|
_nodes[PoseLandmarkType.RightHip],
|
|
|
|
Scale));
|
2023-02-21 21:40:12 +08:00
|
|
|
}
|
2023-02-27 11:07:44 +08:00
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 创建棍子列表
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="nodes">需要连接起来的关键点 需要按顺序设置</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
private List<Bond> GenerateBondsList(int[] nodes)
|
2023-02-27 11:07:44 +08:00
|
|
|
{
|
2023-03-29 22:58:07 +08:00
|
|
|
var bonds = new List<Bond>();
|
2023-02-27 11:07:44 +08:00
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
for (var i = 0; i < nodes.Length - 1; i++)
|
|
|
|
{
|
|
|
|
bonds.Add(new Bond(
|
2023-03-30 14:26:38 +08:00
|
|
|
_nodes[nodes[i]],
|
|
|
|
_nodes[nodes[i + 1]],
|
2023-03-29 22:58:07 +08:00
|
|
|
Scale));
|
|
|
|
}
|
2023-02-27 11:07:44 +08:00
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
return bonds;
|
2023-02-27 11:07:44 +08:00
|
|
|
}
|
|
|
|
}
|
2023-02-21 21:40:12 +08:00
|
|
|
}
|