Merge branch 'feature-ball' into dev
# Conflicts: # Assets/ModelBehaviour.cs # Assets/Models/PoseTransform.cs
This commit is contained in:
44
Assets/Models/Bond.cs
Normal file
44
Assets/Models/Bond.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Models
|
||||
{
|
||||
/// <summary>
|
||||
/// 捕捉点之间的连接线
|
||||
/// </summary>
|
||||
public class Bond
|
||||
{
|
||||
protected readonly GameObject Start;
|
||||
protected readonly GameObject End;
|
||||
private readonly GameObject _bond;
|
||||
|
||||
public Bond(GameObject start,GameObject end,float scale)
|
||||
{
|
||||
Start = start;
|
||||
End = end;
|
||||
|
||||
_bond = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
|
||||
|
||||
_bond.transform.localScale = new Vector3(scale/2, scale/2, scale/2);
|
||||
|
||||
//这里可以设置材质,具体自己设置
|
||||
_bond.GetComponent<Renderer>().material.color = Color.cyan;
|
||||
}
|
||||
|
||||
|
||||
public void UpdateBond()
|
||||
{
|
||||
var startPos = Start.transform.position;
|
||||
var endPos = End.transform.position;
|
||||
var rightPosition = (startPos + endPos) / 2;
|
||||
var rightRotation = endPos - startPos;
|
||||
var halfLength = Vector3.Distance(startPos, endPos) / 2;
|
||||
var thickness = 0.1f;//线的粗细
|
||||
|
||||
//创建圆柱体
|
||||
//bond.gameObject.transform.parent = transform;
|
||||
_bond.transform.position = rightPosition;
|
||||
_bond.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation);
|
||||
_bond.transform.localScale = new Vector3(thickness, halfLength, thickness);
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Models/Bond.cs.meta
Normal file
3
Assets/Models/Bond.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94a8f8c83e58421d9447173d38482d74
|
||||
timeCreated: 1677230676
|
@@ -35,6 +35,7 @@ namespace Models
|
||||
LeftHeel,
|
||||
RightHeel,
|
||||
LeftFootIndex,
|
||||
RightFootIndex
|
||||
RightFootIndex,
|
||||
MaxValue
|
||||
}
|
||||
}
|
@@ -20,9 +20,14 @@ namespace Models
|
||||
public readonly HumanBodyBones UnityName;
|
||||
|
||||
/// <summary>
|
||||
/// MediaPipe捕捉空间坐标
|
||||
/// 取平均的长度
|
||||
/// </summary>
|
||||
public Vector3 MediaPipePos;
|
||||
public static int AverageLength;
|
||||
|
||||
/// <summary>
|
||||
/// 取平均之后的结果
|
||||
/// </summary>
|
||||
public Vector3 AveragePos;
|
||||
|
||||
/// <summary>
|
||||
/// 节点的父节点列表
|
||||
@@ -39,23 +44,50 @@ namespace Models
|
||||
/// </summary>
|
||||
public Quaternion CurrentQuaternion;
|
||||
|
||||
private int _count;
|
||||
private readonly Queue<PoseLandmark> _landmarkQueue;
|
||||
|
||||
public PoseTransform(
|
||||
PoseLandmarkType type
|
||||
PoseLandmarkType type,
|
||||
int averageLength = 5
|
||||
)
|
||||
{
|
||||
MediaPipeName = type;
|
||||
UnityName = GetRelatedBone(type);
|
||||
MediaPipePos = new Vector3();
|
||||
AveragePos = new Vector3();
|
||||
Parent = new List<PoseTransform>();
|
||||
PreviousQuaternion = new Quaternion();
|
||||
CurrentQuaternion = new Quaternion();
|
||||
AverageLength = averageLength;
|
||||
|
||||
_count = 0;
|
||||
_landmarkQueue = new Queue<PoseLandmark>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算当前的旋转四元数
|
||||
/// </summary>
|
||||
/// <param name="landmark"></param>
|
||||
public void CalculateCurrentQuaternion(PoseLandmark landmark)
|
||||
public static void UpdatePosition(ref PoseTransform pose, PoseLandmark landmark)
|
||||
{
|
||||
if (pose._count < AverageLength)
|
||||
{
|
||||
pose._landmarkQueue.Enqueue(landmark);
|
||||
pose._count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
pose._landmarkQueue.Dequeue();
|
||||
pose._landmarkQueue.Enqueue(landmark);
|
||||
|
||||
var sum = new Vector3();
|
||||
|
||||
foreach (var poseLandmark in pose._landmarkQueue)
|
||||
{
|
||||
sum += new Vector3(poseLandmark.X, poseLandmark.Y, poseLandmark.Z);
|
||||
}
|
||||
|
||||
pose.AveragePos = sum / AverageLength;
|
||||
}
|
||||
}
|
||||
|
||||
public static void CalculateRotation(PoseTransform poseTransform)
|
||||
{
|
||||
|
||||
//var temp = _posLandmarks.Find(t=>t.mediaPipeName == landmark.Type.ToString());
|
||||
@@ -134,51 +166,5 @@ namespace Models
|
||||
}
|
||||
|
||||
|
||||
//绕X轴旋转
|
||||
private Matrix4x4 RotateAxisX(float rot)
|
||||
{
|
||||
var matrix = new Matrix4x4();
|
||||
float cosrot = MathF.Cos(rot);
|
||||
float sinrot = MathF.Sin(rot);
|
||||
|
||||
matrix.SetRow(0, new Vector4(1,0,0,0));
|
||||
matrix.SetRow(1, new Vector4(0,cosrot,-sinrot,0));
|
||||
matrix.SetRow(2, new Vector4(0,sinrot,cosrot,0));
|
||||
matrix.SetRow(3, new Vector4(0,0,0,1));
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
//绕Y轴旋转
|
||||
private Matrix4x4 RotateAxisY(float rot)
|
||||
{
|
||||
var matrix = new Matrix4x4();
|
||||
float cosrot = MathF.Cos(rot);
|
||||
float sinrot = MathF.Sin(rot);
|
||||
|
||||
matrix.SetRow(0, new Vector4(cosrot,0,sinrot,0));
|
||||
matrix.SetRow(1, new Vector4(0,1,0,0));
|
||||
matrix.SetRow(2, new Vector4(-sinrot,0,cosrot,0));
|
||||
matrix.SetRow(3, new Vector4(0,0,0,1));
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
//绕Z轴旋转
|
||||
private Matrix4x4 RotateAxisZ(float rot)
|
||||
{
|
||||
var matrix = new Matrix4x4();
|
||||
float cosrot = MathF.Cos(rot);
|
||||
float sinrot = MathF.Sin(rot);
|
||||
|
||||
matrix.SetRow(0, new Vector4(cosrot,-sinrot,0,0));
|
||||
matrix.SetRow(1, new Vector4(sinrot,cosrot,0,0));
|
||||
matrix.SetRow(2, new Vector4(0,0,1,0));
|
||||
matrix.SetRow(3, new Vector4(0,0,0,1));
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
35
Assets/Models/VirtualSkeleton.cs
Normal file
35
Assets/Models/VirtualSkeleton.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Models
|
||||
{
|
||||
public class VirtualSkeleton : Bond
|
||||
{
|
||||
private readonly GameObject _virtualSkeleton;
|
||||
|
||||
public VirtualSkeleton(GameObject start,GameObject end,float scale) : base(start,end,scale)
|
||||
{
|
||||
_virtualSkeleton = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
|
||||
|
||||
_virtualSkeleton.transform.localScale = new Vector3(scale/2, scale/2, scale/2);
|
||||
|
||||
_virtualSkeleton.GetComponent<Renderer>().material.color = Color.white;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 覆盖基类的更新方法
|
||||
/// </summary>
|
||||
public new void UpdateBond()
|
||||
{
|
||||
var startPos = Start.transform.position;
|
||||
var endPos = End.transform.position;
|
||||
|
||||
var rightPosition = (startPos + endPos) / 2;
|
||||
var rightRotation = endPos - startPos;
|
||||
var lThickness = 0.2f;
|
||||
|
||||
_virtualSkeleton.transform.position = rightPosition;
|
||||
_virtualSkeleton.transform.rotation = Quaternion.FromToRotation(Vector3.up, rightRotation);
|
||||
_virtualSkeleton.transform.localScale = new Vector3(lThickness, lThickness, lThickness);
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Models/VirtualSkeleton.cs.meta
Normal file
3
Assets/Models/VirtualSkeleton.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5f725336fc6a4481a4c7d74ec0e3b17b
|
||||
timeCreated: 1677413910
|
Reference in New Issue
Block a user