2023-02-20 11:34:21 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2023-03-01 22:07:39 +08:00
|
|
|
using Utils;
|
2023-02-20 11:34:21 +08:00
|
|
|
|
|
|
|
namespace Models
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 捕捉骨骼的动作转换
|
|
|
|
/// </summary>
|
|
|
|
public struct PoseTransform
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// MediaPipe中动捕节点标识
|
|
|
|
/// </summary>
|
|
|
|
public readonly PoseLandmarkType MediaPipeName;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Unity中绑定骨骼名称
|
|
|
|
/// </summary>
|
|
|
|
public readonly HumanBodyBones UnityName;
|
|
|
|
|
|
|
|
/// <summary>
|
2023-02-28 12:23:15 +08:00
|
|
|
/// 取平均的长度
|
2023-02-20 11:34:21 +08:00
|
|
|
/// </summary>
|
2023-03-01 22:07:39 +08:00
|
|
|
public static int AverageLength = 3;
|
2023-02-28 12:23:15 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 取平均之后的结果
|
|
|
|
/// </summary>
|
2023-03-01 22:07:39 +08:00
|
|
|
public Vector3 ResultPosition => _transformHandler.GetResultPosition();
|
2023-02-20 11:34:21 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 节点的父节点列表
|
|
|
|
/// </summary>
|
2023-02-28 22:59:38 +08:00
|
|
|
public readonly List<PoseLandmarkType> Parent;
|
2023-02-20 11:34:21 +08:00
|
|
|
|
2023-03-01 22:07:39 +08:00
|
|
|
private readonly IPoseTransformHandler _transformHandler;
|
2023-02-28 12:23:15 +08:00
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
public PoseTransform(
|
2023-02-28 23:35:49 +08:00
|
|
|
int type,
|
2023-03-01 22:07:39 +08:00
|
|
|
IPoseTransformHandler handler
|
2023-02-20 11:34:21 +08:00
|
|
|
)
|
|
|
|
{
|
2023-02-28 23:35:49 +08:00
|
|
|
MediaPipeName = new PoseLandmarkType(type);
|
|
|
|
UnityName = GetRelatedBone(MediaPipeName);
|
2023-02-28 22:59:38 +08:00
|
|
|
Parent = new List<PoseLandmarkType>();
|
2023-02-28 12:23:15 +08:00
|
|
|
|
2023-03-01 22:07:39 +08:00
|
|
|
_transformHandler = handler;
|
2023-02-20 11:34:21 +08:00
|
|
|
}
|
|
|
|
|
2023-02-28 12:23:15 +08:00
|
|
|
public static void UpdatePosition(ref PoseTransform pose, PoseLandmark landmark)
|
|
|
|
{
|
2023-03-01 22:07:39 +08:00
|
|
|
pose._transformHandler.ReceivePoseLandmark(landmark);
|
2023-02-28 12:23:15 +08:00
|
|
|
}
|
|
|
|
|
2023-03-02 19:06:32 +08:00
|
|
|
public static Vector3 operator +(PoseTransform a) => a.ResultPosition;
|
|
|
|
|
|
|
|
public static Vector3 operator -(PoseTransform a) => -a.ResultPosition;
|
|
|
|
|
|
|
|
public static Vector3 operator +(PoseTransform a, PoseTransform b) =>
|
|
|
|
a.ResultPosition + b.ResultPosition;
|
|
|
|
|
|
|
|
public static Vector3 operator -(PoseTransform a, PoseTransform b) =>
|
|
|
|
a.ResultPosition - b.ResultPosition;
|
|
|
|
|
|
|
|
public static Vector3 operator *(PoseTransform a, int b) =>
|
|
|
|
a.ResultPosition * b;
|
|
|
|
|
|
|
|
public static Vector3 operator *(int a, PoseTransform b) =>
|
|
|
|
a * b.ResultPosition;
|
|
|
|
|
|
|
|
public static Vector3 operator /(PoseTransform a, int b) =>
|
|
|
|
a.ResultPosition / b;
|
|
|
|
|
2023-02-20 11:34:21 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 获得同相关捕捉点关联的骨骼
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">捕捉点的种类</param>
|
|
|
|
/// <returns>关联骨骼的种类</returns>
|
|
|
|
private static HumanBodyBones GetRelatedBone(PoseLandmarkType type)
|
|
|
|
{
|
2023-02-28 23:35:49 +08:00
|
|
|
switch (type.Value)
|
2023-02-20 11:34:21 +08:00
|
|
|
{
|
|
|
|
case PoseLandmarkType.LeftHip:
|
|
|
|
return HumanBodyBones.LeftUpperLeg;
|
|
|
|
case PoseLandmarkType.RightHip:
|
|
|
|
return HumanBodyBones.RightUpperLeg;
|
|
|
|
case PoseLandmarkType.LeftShoulder:
|
|
|
|
return HumanBodyBones.LeftUpperArm;
|
|
|
|
case PoseLandmarkType.RightShoulder:
|
|
|
|
return HumanBodyBones.RightUpperArm;
|
|
|
|
case PoseLandmarkType.Nose:
|
|
|
|
return HumanBodyBones.Head;
|
|
|
|
case PoseLandmarkType.LeftElbow:
|
|
|
|
return HumanBodyBones.LeftLowerArm;
|
|
|
|
case PoseLandmarkType.RightElbow:
|
|
|
|
return HumanBodyBones.RightLowerArm;
|
|
|
|
case PoseLandmarkType.LeftWrist:
|
|
|
|
return HumanBodyBones.LeftHand;
|
|
|
|
case PoseLandmarkType.RightWrist:
|
|
|
|
return HumanBodyBones.RightHand;
|
|
|
|
case PoseLandmarkType.LeftKnee:
|
|
|
|
return HumanBodyBones.LeftLowerLeg;
|
|
|
|
case PoseLandmarkType.RightKnee:
|
|
|
|
return HumanBodyBones.RightLowerLeg;
|
|
|
|
case PoseLandmarkType.LeftAnkle:
|
|
|
|
return HumanBodyBones.LeftFoot;
|
|
|
|
case PoseLandmarkType.RightAnkle:
|
|
|
|
return HumanBodyBones.RightFoot;
|
|
|
|
case PoseLandmarkType.LeftFootIndex:
|
|
|
|
return HumanBodyBones.LeftToes;
|
|
|
|
case PoseLandmarkType.RightFootIndex:
|
|
|
|
return HumanBodyBones.RightToes;
|
|
|
|
default:
|
|
|
|
return HumanBodyBones.LastBone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|