58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
using System.Collections.Generic;
|
|
using Models;
|
|
using UnityEngine;
|
|
using Utils.PoseTransformHandlers;
|
|
|
|
namespace Behaviours
|
|
{
|
|
public class BoneBehaviour : MonoBehaviour
|
|
{
|
|
private readonly UdpListener _listener = new UdpListener();
|
|
private PoseTransform[] _transforms = new PoseTransform[PoseLandmarkType.MaxValue];
|
|
|
|
private Animator _animator;
|
|
|
|
private void Start()
|
|
{
|
|
_animator = GetComponent<Animator>();
|
|
|
|
for (var i = 0; i < PoseLandmarkType.MaxValue; i++)
|
|
{
|
|
_transforms[i] = new PoseTransform(i, new NormalHandler());
|
|
}
|
|
|
|
_listener.AddHandler(OnReceive);
|
|
_listener.Connect(5000);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
var frontLeft = Vector3.Cross(
|
|
_transforms[PoseLandmarkType.RightHip] - _transforms[PoseLandmarkType.LeftHip],
|
|
_transforms[PoseLandmarkType.RightShoulder] - _transforms[PoseLandmarkType.LeftHip]);
|
|
|
|
var frontRight = Vector3.Cross(
|
|
_transforms[PoseLandmarkType.LeftShoulder] - _transforms[PoseLandmarkType.RightHip],
|
|
_transforms[PoseLandmarkType.LeftHip] - _transforms[PoseLandmarkType.RightHip]);
|
|
|
|
var front = frontLeft + frontRight;
|
|
front.Normalize();
|
|
|
|
_animator.GetBoneTransform(HumanBodyBones.Hips).rotation = Quaternion.LookRotation(front);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
_listener.DisConnect();
|
|
}
|
|
|
|
private void OnReceive(List<PoseLandmark> landmarks)
|
|
{
|
|
foreach (var landmark in landmarks)
|
|
{
|
|
PoseTransform.UpdatePosition(ref _transforms[landmark.Type.Value], landmark);
|
|
}
|
|
}
|
|
}
|
|
}
|