feature: 船新的基于过程的动作绑定

This commit is contained in:
jackfiled 2023-03-29 22:58:07 +08:00
parent 479e64907f
commit 77bf523001
6 changed files with 226 additions and 194 deletions

View File

@ -3,7 +3,9 @@
using UnityEngine; using UnityEngine;
using Utils.PoseTransformHandlers; using Utils.PoseTransformHandlers;
public class MainBehaviour : MonoBehaviour namespace Behaviours
{
public class BallStickBehaviour : MonoBehaviour
{ {
private readonly GameObject[] _nodes = new GameObject[PoseLandmarkType.MaxValue]; private readonly GameObject[] _nodes = new GameObject[PoseLandmarkType.MaxValue];
private readonly List<Bond> _bonds = new List<Bond>(); private readonly List<Bond> _bonds = new List<Bond>();
@ -178,3 +180,4 @@ private List<Bond> GenerateBondsList(int[] nodes)
return bonds; return bonds;
} }
} }
}

View File

@ -1,26 +1,20 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Models; using Models;
using UnityEngine; using UnityEngine;
using Utils.PoseTransformHandlers;
namespace Behaviours namespace Behaviours
{ {
public class BoneBehaviour : MonoBehaviour public class BoneBehaviour : MonoBehaviour
{ {
private readonly UdpListener _listener = new UdpListener(); private readonly UdpListener _listener = new UdpListener();
private readonly PoseTransform[] _transforms = new PoseTransform[PoseLandmarkType.MaxValue]; private Dictionary<HumanBodyBones, RotationNode> _rotationNodes;
private Animator _animator; private Animator _animator;
private void Start() private void Start()
{ {
_animator = GetComponent<Animator>(); _animator = GetComponent<Animator>();
CreateRotationNodes();
for (var i = 0; i < PoseLandmarkType.MaxValue; i++)
{
_transforms[i] = new PoseTransform(i, new ZAxisHandler());
}
_listener.AddHandler(OnReceive); _listener.AddHandler(OnReceive);
_listener.Connect(5000); _listener.Connect(5000);
@ -28,10 +22,13 @@ private void Start()
private void Update() private void Update()
{ {
HipUpdate(); foreach (var node in _rotationNodes)
{
RShoulderUpdate(); _animator.GetBoneTransform(node.Key).rotation = node.Value.Rotation;
} }
}
private void OnDisable() private void OnDisable()
{ {
@ -40,51 +37,47 @@ private void OnDisable()
private void OnReceive(List<PoseLandmark> landmarks) private void OnReceive(List<PoseLandmark> landmarks)
{ {
foreach (var landmark in landmarks) // 计算腰部
{
PoseTransform.UpdatePosition(ref _transforms[landmark.Type.Value], landmark);
}
}
private void HipUpdate()
{
var frontLeft = Vector3.Cross( var frontLeft = Vector3.Cross(
_transforms[PoseLandmarkType.RightHip] - _transforms[PoseLandmarkType.LeftHip], landmarks[PoseLandmarkType.RightHip].Vector3 - landmarks[PoseLandmarkType.LeftHip].Vector3,
_transforms[PoseLandmarkType.RightShoulder] - _transforms[PoseLandmarkType.LeftHip]); landmarks[PoseLandmarkType.RightShoulder].Vector3 - landmarks[PoseLandmarkType.LeftHip].Vector3);
var frontRight = Vector3.Cross( var frontRight = Vector3.Cross(
_transforms[PoseLandmarkType.LeftShoulder] - _transforms[PoseLandmarkType.RightHip], landmarks[PoseLandmarkType.LeftShoulder].Vector3 - landmarks[PoseLandmarkType.RightHip].Vector3,
_transforms[PoseLandmarkType.LeftHip] - _transforms[PoseLandmarkType.RightHip]); landmarks[PoseLandmarkType.LeftHip].Vector3 - landmarks[PoseLandmarkType.LeftHip].Vector3);
var front = frontLeft + frontRight; var front = frontLeft + frontRight;
front.Normalize(); front.Normalize();
_animator.GetBoneTransform(HumanBodyBones.Hips).rotation = Quaternion.LookRotation(front); var oldRotation = _rotationNodes[HumanBodyBones.Hips];
_rotationNodes[HumanBodyBones.Hips] = new RotationNode(
oldRotation.UnityName,
oldRotation.Rotation,
Quaternion.LookRotation(front));
} }
private void RShoulderUpdate() private void CreateRotationNodes()
{ {
var direction = _transforms[PoseLandmarkType.RightElbow] _rotationNodes = new Dictionary<HumanBodyBones, RotationNode>();
- _transforms[PoseLandmarkType.RightShoulder];
var absolutelyRotation = Quaternion.LookRotation(direction);
_animator.GetBoneTransform(HumanBodyBones.RightUpperArm).rotation var bonesArray = new[]
= absolutelyRotation * Quaternion.Inverse(_animator.GetBoneTransform(HumanBodyBones.Hips).rotation);
}
//计算向量a到向量b的旋转角
//参数 a起始向量; b目标向量; n旋转方向 (0, 1, 0)顺时针 (0, -1, 0)逆时针
private static float SignedAngleBetween(Vector3 a, Vector3 b, Vector3 n)
{ {
var angle = Vector3.Angle(a,b); HumanBodyBones.Hips,
var sign = Mathf.Sign(Vector3.Dot(n,Vector3.Cross(a,b))); HumanBodyBones.LeftUpperArm,
var signedAngle = angle * sign; HumanBodyBones.LeftLowerArm,
return (signedAngle <= 0) ? 360 + signedAngle : signedAngle; 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));
}
} }
} }
} }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using UnityEngine;
namespace Models namespace Models
{ {
@ -39,6 +40,8 @@ public class PoseLandmark
/// </summary> /// </summary>
public float Z { get; } public float Z { get; }
public Vector3 Vector3 => new Vector3(X, Y, Z);
/// <summary> /// <summary>
/// 该坐标点估计的可见性 /// 该坐标点估计的可见性
/// [0,1] /// [0,1]

View File

@ -0,0 +1,29 @@
using UnityEngine;
namespace Models
{
public struct RotationNode
{
public HumanBodyBones UnityName;
public Quaternion PreviousRotation;
public Quaternion Rotation;
public Quaternion CalculateRotate => PreviousRotation * Quaternion.Inverse(PreviousRotation);
public RotationNode(HumanBodyBones humanBodyBone, Quaternion previousRotation, Quaternion rotation)
{
UnityName = humanBodyBone;
PreviousRotation = previousRotation;
Rotation = rotation;
}
public RotationNode(RotationNode node, PoseLandmark begin, PoseLandmark end)
{
UnityName = node.UnityName;
PreviousRotation = node.Rotation;
var forward = new Vector3(end.X - begin.X, end.Y - begin.Y, end.Z - begin.Z);
Rotation = Quaternion.LookRotation(forward);
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 87e238cb51f34339a39945af13f43248
timeCreated: 1680090845

View File

@ -9,7 +9,8 @@ public class ZAxisHandler : IPoseTransformHandler
public void ReceivePoseLandmark(PoseLandmark landmark) public void ReceivePoseLandmark(PoseLandmark landmark)
{ {
_result = new Vector3(landmark.X, landmark.Y, landmark.Z * 0.5f); _result = new Vector3(landmark.X, landmark.Y, landmark.Z * 0.2f);
_result = _result * -1;
} }
public Vector3 GetResultPosition() public Vector3 GetResultPosition()