2023-02-20 11:34:21 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Models
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 捕捉骨骼的动作转换
|
|
|
|
/// </summary>
|
|
|
|
public struct PoseTransform
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// MediaPipe中动捕节点标识
|
|
|
|
/// </summary>
|
|
|
|
public readonly PoseLandmarkType MediaPipeName;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Unity中绑定骨骼名称
|
|
|
|
/// </summary>
|
|
|
|
public readonly HumanBodyBones UnityName;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// MediaPipe捕捉空间坐标
|
|
|
|
/// </summary>
|
|
|
|
public Vector3 MediaPipePos;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 节点的父节点列表
|
|
|
|
/// </summary>
|
|
|
|
public readonly List<PoseTransform> Parent;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 骨骼节点的初始角度
|
|
|
|
/// </summary>
|
|
|
|
public Quaternion PreviousQuaternion;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 骨骼节点的当前角度
|
|
|
|
/// </summary>
|
|
|
|
public Quaternion CurrentQuaternion;
|
|
|
|
|
|
|
|
public PoseTransform(
|
|
|
|
PoseLandmarkType type
|
|
|
|
)
|
|
|
|
{
|
|
|
|
MediaPipeName = type;
|
|
|
|
UnityName = GetRelatedBone(type);
|
|
|
|
MediaPipePos = new Vector3();
|
|
|
|
Parent = new List<PoseTransform>();
|
|
|
|
PreviousQuaternion = new Quaternion();
|
|
|
|
CurrentQuaternion = new Quaternion();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 计算当前的旋转四元数
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="landmark"></param>
|
|
|
|
public void CalculateCurrentQuaternion(PoseLandmark landmark)
|
|
|
|
{
|
2023-02-21 21:40:12 +08:00
|
|
|
MediaPipePos.x = landmark.X;
|
|
|
|
MediaPipePos.y = landmark.Y;
|
|
|
|
MediaPipePos.z = landmark.Z;
|
2023-02-20 11:34:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获得同相关捕捉点关联的骨骼
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">捕捉点的种类</param>
|
|
|
|
/// <returns>关联骨骼的种类</returns>
|
|
|
|
private static HumanBodyBones GetRelatedBone(PoseLandmarkType type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|