refactor: 重新规划了项目结构

删除了冗余的控制类
This commit is contained in:
jackfiled 2023-03-27 13:10:42 +08:00
parent 4cd3bad951
commit d901880eea
9 changed files with 139 additions and 303 deletions

View File

@ -1,135 +0,0 @@
using System.Collections.Generic;
using Models;
using UnityEngine;
using Utils.PoseTransformHandlers;
public class ModelBehaviour : MonoBehaviour
{
private Animator _animator;
private readonly UdpListener _listener = new UdpListener();
private static readonly PoseTransform[] PoseTransforms = new PoseTransform[PoseLandmarkType.MaxValue];
private void Start()
{
_animator = GetComponent<Animator>(); // 获取动画控件
InitPoseTransformList();
//_listener.AddHandler(LogLandmarks);
_listener.AddHandler(RigPoint);
_listener.Connect(5000);
}
// Update is called once per frame
private void Update()
{
foreach (var landmark in PoseTransforms)
{
if (landmark.UnityName != HumanBodyBones.LastBone)
{
var transfrom = _animator.GetBoneTransform(landmark.UnityName);
var parentsum = new Vector3(0, 0, 0);
foreach (var parent in landmark.Parent)
{
parentsum += PoseTransforms[parent.Value].ResultPosition;
}
parentsum /= landmark.Parent.Count;
transfrom.rotation = Quaternion.FromToRotation(transfrom.rotation.eulerAngles,landmark.ResultPosition-parentsum);
}
}
}
private void OnDisable()
{
_listener.DisConnect();
}
private static void LogLandmarks(List<PoseLandmark> landmarks)
{
foreach (var landmark in landmarks)
{
Debug.Log(landmark.ToString());
}
}
//获取传上来的数据点坐标转换成四元数的回调函数
private static void RigPoint(List<PoseLandmark> landmarks)
{
foreach (var landmark in landmarks)
{
PoseTransform.UpdatePosition(ref PoseTransforms[landmark.Type.Value], landmark);
Debug.Log(PoseTransforms[landmark.Type.Value].UnityName+":"+PoseTransforms[landmark.Type.Value].ResultPosition);
}
}
/// <summary>
/// 初始化骨骼变化结构体列表
/// </summary>
private void InitPoseTransformList()
{
for (var type = 0; type <= PoseLandmarkType.RightFootIndex; type++)
{
var item = new PoseTransform(type, new NormalHandler());
if (item.UnityName != HumanBodyBones.LastBone)
{
item.PreviousQuaternion = _animator.GetBoneTransform(item.UnityName).rotation;
item.CurrentQuaternion = item.PreviousQuaternion;
}
PoseTransforms[type] = item;
}
// 在这里添加父节点
AddParentTransform(PoseLandmarkType.LeftShoulder, PoseLandmarkType.LeftHip);
AddParentTransform(PoseLandmarkType.LeftShoulder, PoseLandmarkType.RightHip);
AddParentTransform(PoseLandmarkType.RightShoulder, PoseLandmarkType.LeftHip);
AddParentTransform(PoseLandmarkType.RightShoulder, PoseLandmarkType.RightHip);
AddParentTransform(PoseLandmarkType.Nose, PoseLandmarkType.LeftShoulder);
AddParentTransform(PoseLandmarkType.Nose, PoseLandmarkType.RightShoulder);
AddParentTransform(PoseLandmarkType.LeftElbow, PoseLandmarkType.LeftShoulder);
AddParentTransform(PoseLandmarkType.RightElbow, PoseLandmarkType.RightShoulder);
AddParentTransform(PoseLandmarkType.LeftWrist, PoseLandmarkType.LeftElbow);
AddParentTransform(PoseLandmarkType.RightWrist, PoseLandmarkType.RightElbow);
AddParentTransform(PoseLandmarkType.LeftKnee, PoseLandmarkType.LeftHip);
AddParentTransform(PoseLandmarkType.RightKnee, PoseLandmarkType.RightHip);
AddParentTransform(PoseLandmarkType.LeftAnkle, PoseLandmarkType.LeftKnee);
AddParentTransform(PoseLandmarkType.RightAnkle, PoseLandmarkType.RightKnee);
AddParentTransform(PoseLandmarkType.LeftFootIndex, PoseLandmarkType.LeftAnkle);
AddParentTransform(PoseLandmarkType.RightFootIndex, PoseLandmarkType.RightAnkle);
}
/// <summary>
/// 给指定的骨骼变化结构体添加父节点
/// </summary>
/// <param name="target">需要添加的节点</param>
/// <param name="parent">添加的父节点</param>
private void AddParentTransform(int target, int parent)
{
var parents = PoseTransforms[target].Parent;
parents.Add(PoseTransforms[parent].MediaPipeName);
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: a2b30b6aa6e08c4468a02e1fcabcd485
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -28,22 +28,11 @@ public struct PoseTransform
/// 取平均之后的结果 /// 取平均之后的结果
/// </summary> /// </summary>
public Vector3 ResultPosition => _transformHandler.GetResultPosition(); public Vector3 ResultPosition => _transformHandler.GetResultPosition();
/// <summary> /// <summary>
/// 节点的父节点列表 /// 节点的父节点列表
/// </summary> /// </summary>
public readonly List<PoseLandmarkType> Parent; public readonly List<PoseLandmarkType> Parent;
/// <summary>
/// 骨骼节点的初始角度
/// </summary>
public Quaternion PreviousQuaternion;
/// <summary>
/// 骨骼节点的当前角度
/// </summary>
public Quaternion CurrentQuaternion;
private readonly IPoseTransformHandler _transformHandler; private readonly IPoseTransformHandler _transformHandler;
public PoseTransform( public PoseTransform(
@ -54,8 +43,6 @@ IPoseTransformHandler handler
MediaPipeName = new PoseLandmarkType(type); MediaPipeName = new PoseLandmarkType(type);
UnityName = GetRelatedBone(MediaPipeName); UnityName = GetRelatedBone(MediaPipeName);
Parent = new List<PoseLandmarkType>(); Parent = new List<PoseLandmarkType>();
PreviousQuaternion = new Quaternion();
CurrentQuaternion = new Quaternion();
_transformHandler = handler; _transformHandler = handler;
} }
@ -65,11 +52,6 @@ public static void UpdatePosition(ref PoseTransform pose, PoseLandmark landmark)
pose._transformHandler.ReceivePoseLandmark(landmark); pose._transformHandler.ReceivePoseLandmark(landmark);
} }
public static void CalculateRotation(ref PoseTransform poseTransform, PoseLandmark landmark)
{
}
public static Vector3 operator +(PoseTransform a) => a.ResultPosition; public static Vector3 operator +(PoseTransform a) => a.ResultPosition;
public static Vector3 operator -(PoseTransform a) => -a.ResultPosition; public static Vector3 operator -(PoseTransform a) => -a.ResultPosition;

View File

@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1 m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0} m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 705507994} m_Sun: {fileID: 705507994}
m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1} m_IndirectSpecularColor: {r: 0.18029127, g: 0.22572401, b: 0.3069303, a: 1}
m_UseRadianceAmbientProbe: 0 m_UseRadianceAmbientProbe: 0
--- !u!157 &3 --- !u!157 &3
LightmapSettings: LightmapSettings:
@ -189,7 +189,12 @@ GameObject:
m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3} m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
m_PrefabInstance: {fileID: 404755305} m_PrefabInstance: {fileID: 404755305}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
--- !u!114 &404755307 --- !u!4 &404755309 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
m_PrefabInstance: {fileID: 404755305}
m_PrefabAsset: {fileID: 0}
--- !u!114 &404755310
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0} m_CorrespondingSourceObject: {fileID: 0}
@ -198,14 +203,9 @@ MonoBehaviour:
m_GameObject: {fileID: 404755306} m_GameObject: {fileID: 404755306}
m_Enabled: 1 m_Enabled: 1
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a2b30b6aa6e08c4468a02e1fcabcd485, type: 3} m_Script: {fileID: 11500000, guid: a2b060336532a01bfb4593b385955175, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
--- !u!4 &404755309 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
m_PrefabInstance: {fileID: 404755305}
m_PrefabAsset: {fileID: 0}
--- !u!1 &705507993 --- !u!1 &705507993
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0