MotionCapture/Assets/MainBehaviour.cs

67 lines
1.7 KiB
C#
Raw Normal View History

2023-02-21 21:40:12 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using Models;
using Unity.VisualScripting;
using UnityEngine;
public class MainBehaviour : MonoBehaviour
{
private readonly List<GameObject> _nodes = new List<GameObject>();
private readonly List<PoseTransform> _poseTransforms = new List<PoseTransform>();
private readonly UdpListener _listener = new UdpListener();
// Start is called before the first frame update
private void Start()
{
CreateNodes();
_listener.AddHandler(OnReceive);
_listener.Connect(5000);
}
// Update is called once per frame
private void Update()
{
foreach (var poseTransform in _poseTransforms)
{
var index = (int)poseTransform.MediaPipeName;
_nodes[index].transform.position = _poseTransforms[index].MediaPipePos * 5;
}
}
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(0.3f, 0.3f, 0.3f);
_nodes.Add(ball);
var poseTransform = new PoseTransform((PoseLandmarkType)i);
_poseTransforms.Add(poseTransform);
}
}
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;
}
}
}