add ModelControler.cs and change model to humanoid type

This commit is contained in:
Ichirinko 2023-02-15 23:24:47 +08:00
parent ce5d68721d
commit b0a695bb3b
8 changed files with 310 additions and 36 deletions

View File

@ -39,7 +39,7 @@ ModelImporter:
animationDoRetargetingWarnings: 0 animationDoRetargetingWarnings: 0
importAnimatedCustomProperties: 0 importAnimatedCustomProperties: 0
importConstraints: 0 importConstraints: 0
animationCompression: 1 animationCompression: 3
animationRotationError: 0.5 animationRotationError: 0.5
animationPositionError: 0.5 animationPositionError: 0.5
animationScaleError: 0.5 animationScaleError: 0.5
@ -106,11 +106,11 @@ ModelImporter:
globalScale: 1 globalScale: 1
rootMotionBoneName: mixamorig:Hips rootMotionBoneName: mixamorig:Hips
hasTranslationDoF: 0 hasTranslationDoF: 0
hasExtraRoot: 0 hasExtraRoot: 1
skeletonHasParents: 1 skeletonHasParents: 1
lastHumanDescriptionAvatarSource: {instanceID: 0} lastHumanDescriptionAvatarSource: {instanceID: 0}
autoGenerateAvatarMappingIfUnspecified: 1 autoGenerateAvatarMappingIfUnspecified: 1
animationType: 2 animationType: 3
humanoidOversampling: 1 humanoidOversampling: 1
avatarSetup: 1 avatarSetup: 1
addHumanoidExtraRootOnlyWhenUsingAvatar: 1 addHumanoidExtraRootOnlyWhenUsingAvatar: 1

View File

@ -34,8 +34,8 @@ private void Start()
_eulerX = angles.y; _eulerX = angles.y;
_eulerY = angles.x; _eulerY = angles.x;
_targetPosition = target.position; _targetPosition = target.position;
_cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0); //_cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0);
_cameraRotation = Quaternion.Euler(_eulerY, _eulerX, 0);
// 引入中间变量 提高代码效率 // 引入中间变量 提高代码效率
var transform1 = transform; var transform1 = transform;
//设置相机姿态 //设置相机姿态
@ -57,7 +57,8 @@ private void Update()
_eulerY = ClampAngle(_eulerY, YMinLimit, YMaxLimit); _eulerY = ClampAngle(_eulerY, YMinLimit, YMaxLimit);
_cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0); //_cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0);
_cameraRotation = Quaternion.Euler(_eulerY, _eulerX, 0);
var position = _cameraRotation * new Vector3(0.0f, 0.0f, -_distance) + _targetPosition; var position = _cameraRotation * new Vector3(0.0f, 0.0f, -_distance) + _targetPosition;
// 引入中间变量 提高代码效率 // 引入中间变量 提高代码效率

View 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++;
}
}
}

View File

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

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.18029127, g: 0.22572401, b: 0.3069303, a: 1} m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1}
m_UseRadianceAmbientProbe: 0 m_UseRadianceAmbientProbe: 0
--- !u!157 &3 --- !u!157 &3
LightmapSettings: LightmapSettings:
@ -150,7 +150,7 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 320693305} m_GameObject: {fileID: 320693305}
m_Enabled: 1 m_Enabled: 0
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 11e8a976fe328b1c3bdf9f44ef9f6fa1, type: 3} m_Script: {fileID: 11500000, guid: 11e8a976fe328b1c3bdf9f44ef9f6fa1, type: 3}
m_Name: m_Name:
@ -232,8 +232,91 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 2 m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &421937415
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_RootOrder
value: 1
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_LocalPosition.z
value: 5
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: -2845779275431937956, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
- target: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
propertyPath: m_Name
value: troop
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
--- !u!1 &437000000 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
m_PrefabInstance: {fileID: 421937415}
m_PrefabAsset: {fileID: 0}
--- !u!114 &437000001
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 437000000}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f3dc31402d1b12d4faa2846675feff35, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &437000003 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: c5470c9a1aff50643b6a9ff0bab4297d, type: 3}
m_PrefabInstance: {fileID: 421937415}
m_PrefabAsset: {fileID: 0}
--- !u!1 &705507993 --- !u!1 &705507993
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -326,7 +409,7 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 1 m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1 &963194225 --- !u!1 &963194225
GameObject: GameObject:
@ -405,14 +488,14 @@ Transform:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 963194225} m_GameObject: {fileID: 963194225}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 1, z: 0, w: 0}
m_LocalPosition: {x: 0, y: 1, z: -10} m_LocalPosition: {x: 0, y: 9.29, z: 20}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 180, z: 0}
--- !u!114 &963194229 --- !u!114 &963194229
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -420,12 +503,12 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 963194225} m_GameObject: {fileID: 963194225}
m_Enabled: 1 m_Enabled: 0
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9189b565e8ba9bfc3bdaa38149e3e13f, type: 3} m_Script: {fileID: 11500000, guid: 9189b565e8ba9bfc3bdaa38149e3e13f, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
target: {fileID: 320693310} target: {fileID: 437000003}
--- !u!1 &1880773849 --- !u!1 &1880773849
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -492,5 +575,5 @@ Transform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 3 m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

View File

@ -7,21 +7,21 @@
"dependencies": { "dependencies": {
"com.unity.services.core": "1.0.1" "com.unity.services.core": "1.0.1"
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.editorcoroutines": { "com.unity.editorcoroutines": {
"version": "1.0.0", "version": "1.0.0",
"depth": 1, "depth": 1,
"source": "registry", "source": "registry",
"dependencies": {}, "dependencies": {},
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.ext.nunit": { "com.unity.ext.nunit": {
"version": "1.0.6", "version": "1.0.6",
"depth": 1, "depth": 1,
"source": "registry", "source": "registry",
"dependencies": {}, "dependencies": {},
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.feature.development": { "com.unity.feature.development": {
"version": "1.0.1", "version": "1.0.1",
@ -44,7 +44,7 @@
"dependencies": { "dependencies": {
"com.unity.ext.nunit": "1.0.6" "com.unity.ext.nunit": "1.0.6"
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.ide.visualstudio": { "com.unity.ide.visualstudio": {
"version": "2.0.16", "version": "2.0.16",
@ -53,28 +53,28 @@
"dependencies": { "dependencies": {
"com.unity.test-framework": "1.1.9" "com.unity.test-framework": "1.1.9"
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.ide.vscode": { "com.unity.ide.vscode": {
"version": "1.2.5", "version": "1.2.5",
"depth": 0, "depth": 0,
"source": "registry", "source": "registry",
"dependencies": {}, "dependencies": {},
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.nuget.newtonsoft-json": { "com.unity.nuget.newtonsoft-json": {
"version": "3.0.2", "version": "3.0.2",
"depth": 2, "depth": 2,
"source": "registry", "source": "registry",
"dependencies": {}, "dependencies": {},
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.performance.profile-analyzer": { "com.unity.performance.profile-analyzer": {
"version": "1.1.1", "version": "1.1.1",
"depth": 1, "depth": 1,
"source": "registry", "source": "registry",
"dependencies": {}, "dependencies": {},
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.services.core": { "com.unity.services.core": {
"version": "1.6.0", "version": "1.6.0",
@ -85,14 +85,14 @@
"com.unity.nuget.newtonsoft-json": "3.0.2", "com.unity.nuget.newtonsoft-json": "3.0.2",
"com.unity.modules.androidjni": "1.0.0" "com.unity.modules.androidjni": "1.0.0"
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.settings-manager": { "com.unity.settings-manager": {
"version": "1.0.3", "version": "1.0.3",
"depth": 2, "depth": 2,
"source": "registry", "source": "registry",
"dependencies": {}, "dependencies": {},
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.test-framework": { "com.unity.test-framework": {
"version": "1.1.31", "version": "1.1.31",
@ -103,7 +103,7 @@
"com.unity.modules.imgui": "1.0.0", "com.unity.modules.imgui": "1.0.0",
"com.unity.modules.jsonserialize": "1.0.0" "com.unity.modules.jsonserialize": "1.0.0"
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.testtools.codecoverage": { "com.unity.testtools.codecoverage": {
"version": "1.2.2", "version": "1.2.2",
@ -113,7 +113,7 @@
"com.unity.test-framework": "1.0.16", "com.unity.test-framework": "1.0.16",
"com.unity.settings-manager": "1.0.1" "com.unity.settings-manager": "1.0.1"
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.textmeshpro": { "com.unity.textmeshpro": {
"version": "3.0.6", "version": "3.0.6",
@ -122,7 +122,7 @@
"dependencies": { "dependencies": {
"com.unity.ugui": "1.0.0" "com.unity.ugui": "1.0.0"
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.timeline": { "com.unity.timeline": {
"version": "1.6.4", "version": "1.6.4",
@ -134,7 +134,7 @@
"com.unity.modules.audio": "1.0.0", "com.unity.modules.audio": "1.0.0",
"com.unity.modules.particlesystem": "1.0.0" "com.unity.modules.particlesystem": "1.0.0"
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.ugui": { "com.unity.ugui": {
"version": "1.0.0", "version": "1.0.0",
@ -153,7 +153,7 @@
"com.unity.ugui": "1.0.0", "com.unity.ugui": "1.0.0",
"com.unity.modules.jsonserialize": "1.0.0" "com.unity.modules.jsonserialize": "1.0.0"
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.cn"
}, },
"com.unity.modules.ai": { "com.unity.modules.ai": {
"version": "1.0.0", "version": "1.0.0",

View File

@ -21,7 +21,7 @@ MonoBehaviour:
m_Registries: m_Registries:
- m_Id: main - m_Id: main
m_Name: m_Name:
m_Url: https://packages.unity.com m_Url: https://packages.unity.cn
m_Scopes: [] m_Scopes: []
m_IsDefault: 1 m_IsDefault: 1
m_Capabilities: 7 m_Capabilities: 7
@ -31,6 +31,6 @@ MonoBehaviour:
m_RegistryInfoDraft: m_RegistryInfoDraft:
m_Modified: 0 m_Modified: 0
m_ErrorMessage: m_ErrorMessage:
m_UserModificationsInstanceId: -824 m_UserModificationsInstanceId: -846
m_OriginalInstanceId: -826 m_OriginalInstanceId: -848
m_LoadAssets: 0 m_LoadAssets: 0

View File

@ -1,2 +1,2 @@
m_EditorVersion: 2021.3.16f1 m_EditorVersion: 2021.3.16f1c1
m_EditorVersionWithRevision: 2021.3.16f1 (4016570cf34f) m_EditorVersionWithRevision: 2021.3.16f1c1 (56dbfdd6697f)