2023-03-05 19:55:37 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using Models;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Behaviours
|
|
|
|
{
|
|
|
|
public class BoneBehaviour : MonoBehaviour
|
|
|
|
{
|
|
|
|
private readonly UdpListener _listener = new UdpListener();
|
2023-03-29 22:58:07 +08:00
|
|
|
private Dictionary<HumanBodyBones, RotationNode> _rotationNodes;
|
|
|
|
|
2023-03-05 19:55:37 +08:00
|
|
|
private Animator _animator;
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
_animator = GetComponent<Animator>();
|
2023-03-29 22:58:07 +08:00
|
|
|
CreateRotationNodes();
|
2023-03-05 19:55:37 +08:00
|
|
|
|
|
|
|
_listener.AddHandler(OnReceive);
|
|
|
|
_listener.Connect(5000);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Update()
|
2023-03-25 11:43:42 +08:00
|
|
|
{
|
2023-03-29 22:58:07 +08:00
|
|
|
foreach (var node in _rotationNodes)
|
|
|
|
{
|
|
|
|
_animator.GetBoneTransform(node.Key).rotation = node.Value.Rotation;
|
|
|
|
}
|
2023-03-25 11:43:42 +08:00
|
|
|
}
|
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
|
|
|
|
|
2023-03-25 11:43:42 +08:00
|
|
|
private void OnDisable()
|
|
|
|
{
|
|
|
|
_listener.DisConnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnReceive(List<PoseLandmark> landmarks)
|
|
|
|
{
|
2023-03-29 22:58:07 +08:00
|
|
|
// 计算腰部
|
2023-03-05 19:55:37 +08:00
|
|
|
var frontLeft = Vector3.Cross(
|
2023-03-29 22:58:07 +08:00
|
|
|
landmarks[PoseLandmarkType.RightHip].Vector3 - landmarks[PoseLandmarkType.LeftHip].Vector3,
|
|
|
|
landmarks[PoseLandmarkType.RightShoulder].Vector3 - landmarks[PoseLandmarkType.LeftHip].Vector3);
|
2023-03-05 19:55:37 +08:00
|
|
|
|
|
|
|
var frontRight = Vector3.Cross(
|
2023-03-29 22:58:07 +08:00
|
|
|
landmarks[PoseLandmarkType.LeftShoulder].Vector3 - landmarks[PoseLandmarkType.RightHip].Vector3,
|
|
|
|
landmarks[PoseLandmarkType.LeftHip].Vector3 - landmarks[PoseLandmarkType.LeftHip].Vector3);
|
2023-03-05 19:55:37 +08:00
|
|
|
|
|
|
|
var front = frontLeft + frontRight;
|
|
|
|
front.Normalize();
|
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
var oldRotation = _rotationNodes[HumanBodyBones.Hips];
|
|
|
|
_rotationNodes[HumanBodyBones.Hips] = new RotationNode(
|
|
|
|
oldRotation.UnityName,
|
|
|
|
oldRotation.Rotation,
|
|
|
|
Quaternion.LookRotation(front));
|
2023-03-05 19:55:37 +08:00
|
|
|
}
|
|
|
|
|
2023-03-29 22:58:07 +08:00
|
|
|
private void CreateRotationNodes()
|
2023-03-05 19:55:37 +08:00
|
|
|
{
|
2023-03-29 22:58:07 +08:00
|
|
|
_rotationNodes = new Dictionary<HumanBodyBones, RotationNode>();
|
|
|
|
|
|
|
|
var bonesArray = new[]
|
|
|
|
{
|
|
|
|
HumanBodyBones.Hips,
|
|
|
|
HumanBodyBones.LeftUpperArm,
|
|
|
|
HumanBodyBones.LeftLowerArm,
|
|
|
|
HumanBodyBones.RightUpperArm,
|
|
|
|
HumanBodyBones.RightLowerArm,
|
|
|
|
HumanBodyBones.LeftUpperLeg,
|
|
|
|
HumanBodyBones.LeftLowerLeg,
|
|
|
|
HumanBodyBones.RightUpperLeg,
|
|
|
|
HumanBodyBones.RightLowerLeg
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (var bone in bonesArray)
|
|
|
|
{
|
|
|
|
var rotation = _animator.GetBoneTransform(bone).rotation;
|
|
|
|
_rotationNodes.Add(bone, new RotationNode(bone, rotation, rotation));
|
|
|
|
}
|
2023-03-05 19:55:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|