add ModelControler.cs and change model to humanoid type
This commit is contained in:
179
Assets/Models/ModelControler.cs
Normal file
179
Assets/Models/ModelControler.cs
Normal file
@@ -0,0 +1,179 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Models;
|
||||
using UnityEngine;
|
||||
|
||||
public class ModelControler : MonoBehaviour
|
||||
{
|
||||
Animator animator;
|
||||
//float deltaz = 0f;
|
||||
//Vector3 upperArm = new Vector3(0f, 0f, 0f);
|
||||
private Quaternion[] prevQ = new Quaternion[15];
|
||||
|
||||
private static Quaternion[] uniRotation = new Quaternion[15]; //全局两坐标系通用四元数
|
||||
|
||||
private readonly UdpListener _listener = new UdpListener();
|
||||
|
||||
private static Dictionary<string,(HumanBodyBones,int)> Landmarks_Mapping= new Dictionary<string,HumanBodyBones>();
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
_listener.AddHandler(LogLandmarks);
|
||||
_listener.AddHandler(RigPoint);
|
||||
_listener.Connect(5000);
|
||||
|
||||
|
||||
DictProcess(); //匹配字典初始化
|
||||
|
||||
|
||||
// 获取动画控件
|
||||
animator = this.GetComponent<Animator>();
|
||||
|
||||
GetOriginStatus(); // 获取骨骼的原始旋转角
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
//deltaz += 1f;
|
||||
//upperArm.z = deltaz;
|
||||
|
||||
//animator.GetBoneTransform(HumanBodyBones.RightLowerArm).rotation = uniRotation * prevQ ;
|
||||
int pos=0;
|
||||
foreach(var landmark in Landmarks_Mapping.Values)
|
||||
{
|
||||
animator.GetBoneTransform(landmark).rotation = uniRotation[pos];
|
||||
pos++;
|
||||
}
|
||||
//animator.GetBoneTransform(HumanBodyBones.RightLowerArm).rotation = uniRotation[14];
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
int pos = 0;
|
||||
|
||||
foreach (var landmark in landmarks)
|
||||
{
|
||||
|
||||
if(Landmarks_Mapping.ContainsKey(landmark.Type.ToString()))
|
||||
{
|
||||
|
||||
Vector3 realPosition = new Vector3(landmark.X,landmark.Y,landmark.Z);
|
||||
|
||||
if(landmark.Visibility<0.8){
|
||||
|
||||
//uniRotation=(0f,0f);
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
|
||||
uniRotation[pos] = Quaternion.LookRotation(realPosition);
|
||||
pos++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Vector3 realPosition = new Vector3(landmarks[14].X,landmarks[14].Y,landmarks[14].Z);
|
||||
|
||||
if(landmarks[14].Visibility<0.8){
|
||||
|
||||
//uniRotation=(0f,0f);
|
||||
|
||||
}
|
||||
else{
|
||||
|
||||
uniRotation[14] = Quaternion.LookRotation(realPosition);
|
||||
|
||||
}
|
||||
|
||||
//Debug.Log(uniRotation);
|
||||
*/
|
||||
}
|
||||
|
||||
private void DictProcess()
|
||||
{
|
||||
|
||||
/*下面的语句的顺序最好别调换,不然可能会发生难以预料的事情*/
|
||||
|
||||
Landmarks_Mapping.Add("Nose",HumanBodyBones.Head); //0 10
|
||||
|
||||
Landmarks_Mapping.Add("LeftShoulder",HumanBodyBones.LeftUpperArm); //11
|
||||
|
||||
Landmarks_Mapping.Add("RightShoulder",HumanBodyBones.RightUpperArm); //12
|
||||
|
||||
Landmarks_Mapping.Add("LeftElbow",HumanBodyBones.LeftLowerArm); //13
|
||||
|
||||
Landmarks_Mapping.Add("RightElbow",HumanBodyBones.RightLowerArm); //14
|
||||
|
||||
Landmarks_Mapping.Add("LeftWrist",HumanBodyBones.LeftHand); //15
|
||||
|
||||
Landmarks_Mapping.Add("RightWrist",HumanBodyBones.RightHand); //16
|
||||
|
||||
Landmarks_Mapping.Add("LeftHip",HumanBodyBones.LeftUpperLeg); //23
|
||||
|
||||
Landmarks_Mapping.Add("RightHip",HumanBodyBones.RightUpperLeg); //24
|
||||
|
||||
Landmarks_Mapping.Add("LeftKnee",HumanBodyBones.LeftLowerLeg); //25
|
||||
|
||||
Landmarks_Mapping.Add("RightKnee",HumanBodyBones.RightLowerLeg); //26
|
||||
|
||||
Landmarks_Mapping.Add("LeftAnkle",HumanBodyBones.LeftFoot); //27
|
||||
|
||||
Landmarks_Mapping.Add("RightAnkle",HumanBodyBones.RightFoot); //28
|
||||
|
||||
Landmarks_Mapping.Add("LeftFootIndex",HumanBodyBones.LeftToes); //31
|
||||
|
||||
Landmarks_Mapping.Add("RightFootIndex",HumanBodyBones.RightToes); //32
|
||||
|
||||
}
|
||||
|
||||
private void GetOriginStatus()
|
||||
{
|
||||
|
||||
int pos=0;
|
||||
|
||||
foreach(var landmarks in Landmarks_Mapping.Values)
|
||||
{
|
||||
|
||||
|
||||
prevQ[pos] = animator.GetBoneTransform(landmarks).rotation;
|
||||
//Quaternion currentQ = Quaternion.Euler(upperArm.x, upperArm.y, upperArm.z);
|
||||
//animator.GetBoneTransform(HumanBodyBones.LeftUpperArm).rotation = currentQ * prevQ;
|
||||
uniRotation[pos] = prevQ[pos];
|
||||
|
||||
pos++;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
11
Assets/Models/ModelControler.cs.meta
Normal file
11
Assets/Models/ModelControler.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f3dc31402d1b12d4faa2846675feff35
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user