MotionCapture/Assets/MainBehaviour.cs
jackfiled 6eba30e268 fix: 修复了foreach遍历警告的问题
refact: 移动UdpListener到Utils文件夹
2023-02-27 13:34:19 +08:00

197 lines
5.8 KiB
C#

using System.Collections.Generic;
using Models;
using UnityEngine;
public class MainBehaviour : MonoBehaviour
{
private readonly List<GameObject> _nodes = new List<GameObject>();
private readonly List<Bond> _bonds = new List<Bond>();
private readonly List<VirtualSkeleton> _virtualSkeletons = new List<VirtualSkeleton>();
private readonly List<PoseTransform> _poseTransforms = new List<PoseTransform>();
private readonly UdpListener _listener = new UdpListener();
private const float Scale = 0.2f;
// Start is called before the first frame update
private void Start()
{
CreateNodes();
CreateBonds();
_listener.AddHandler(OnReceive);
_listener.Connect(5000);
}
// Update is called once per frame
private void Update()
{
for (var i = 0; i < 33; i++)
{
var temp = new Vector4(_poseTransforms[i].MediaPipePos.x,
_poseTransforms[i].MediaPipePos.y,
_poseTransforms[i].MediaPipePos.z,
1);
_nodes[i].transform.position = new Vector3(-temp.x, -temp.y, -temp.z) * 5;
}
foreach (var bond in _bonds)
{
bond.UpdateBond();
}
/*foreach (var vs in _virtualSkeletons)
{
vs.UpdateVS();
}*/
}
private void OnDisable()
{
_listener.DisConnect();
}
private void CreateNodes()
{
for (var i = 0; i < 33; i++)
{
var ball = GameObject.CreatePrimitive(PrimitiveType.Sphere);
ball.transform.localScale = new Vector3(Scale, Scale, Scale);
if (i <= 10)
{
ball.GetComponent<Renderer>().material.color = Color.red;//给头部添加颜色
}
else if (i <= 22)
{
ball.GetComponent<Renderer>().material.color = Color.blue;//给手部添加颜色
}
else if (i <= 32)
{
ball.GetComponent<Renderer>().material.color = Color.green;//给脚部添加颜色
}
_nodes.Add(ball);
var poseTransform = new PoseTransform((PoseLandmarkType)i);
_poseTransforms.Add(poseTransform);
}
}
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 void OnReceive(List<PoseLandmark> landmarks)
{
foreach (var landmark in landmarks)
{
var poseTransform = new PoseTransform(landmark.Type)
{
MediaPipePos = new Vector3(landmark.X, landmark.Y, landmark.Z)
};
_poseTransforms[(int)landmark.Type] = poseTransform;
}
}
/// <summary>
/// 创建棍子列表
/// </summary>
/// <param name="nodes">需要连接起来的关键点 需要按顺序设置</param>
/// <returns></returns>
private List<Bond> GenerateBondsList(PoseLandmarkType[] nodes)
{
var bonds = new List<Bond>();
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;
}
}