AnimationClipUtility.cs
5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System.Collections.Generic;
using UnityEngine;
namespace UniHumanoid
{
public static class AnimationClipUtility
{
static Dictionary<string, string> TraitPropMap = new Dictionary<string, string>
{
{"Left Thumb 1 Stretched", "LeftHand.Thumb.1 Stretched"},
{"Left Thumb Spread", "LeftHand.Thumb Spread"},
{"Left Thumb 2 Stretched", "LeftHand.Thumb.2 Stretched"},
{"Left Thumb 3 Stretched", "LeftHand.Thumb.3 Stretched"},
{"Left Index 1 Stretched", "LeftHand.Index.1 Stretched"},
{"Left Index Spread", "LeftHand.Index Spread"},
{"Left Index 2 Stretched", "LeftHand.Index.2 Stretched"},
{"Left Index 3 Stretched", "LeftHand.Index.3 Stretched"},
{"Left Middle 1 Stretched", "LeftHand.Middle.1 Stretched"},
{"Left Middle Spread", "LeftHand.Middle Spread"},
{"Left Middle 2 Stretched", "LeftHand.Middle.2 Stretched"},
{"Left Middle 3 Stretched", "LeftHand.Middle.3 Stretched"},
{"Left Ring 1 Stretched", "LeftHand.Ring.1 Stretched"},
{"Left Ring Spread", "LeftHand.Ring Spread"},
{"Left Ring 2 Stretched", "LeftHand.Ring.2 Stretched"},
{"Left Ring 3 Stretched", "LeftHand.Ring.3 Stretched"},
{"Left Little 1 Stretched", "LeftHand.Little.1 Stretched"},
{"Left Little Spread", "LeftHand.Little Spread"},
{"Left Little 2 Stretched", "LeftHand.Little.2 Stretched"},
{"Left Little 3 Stretched", "LeftHand.Little.3 Stretched"},
{"Right Thumb 1 Stretched", "RightHand.Thumb.1 Stretched"},
{"Right Thumb Spread", "RightHand.Thumb Spread"},
{"Right Thumb 2 Stretched", "RightHand.Thumb.2 Stretched"},
{"Right Thumb 3 Stretched", "RightHand.Thumb.3 Stretched"},
{"Right Index 1 Stretched", "RightHand.Index.1 Stretched"},
{"Right Index Spread", "RightHand.Index Spread"},
{"Right Index 2 Stretched", "RightHand.Index.2 Stretched"},
{"Right Index 3 Stretched", "RightHand.Index.3 Stretched"},
{"Right Middle 1 Stretched", "RightHand.Middle.1 Stretched"},
{"Right Middle Spread", "RightHand.Middle Spread"},
{"Right Middle 2 Stretched", "RightHand.Middle.2 Stretched"},
{"Right Middle 3 Stretched", "RightHand.Middle.3 Stretched"},
{"Right Ring 1 Stretched", "RightHand.Ring.1 Stretched"},
{"Right Ring Spread", "RightHand.Ring Spread"},
{"Right Ring 2 Stretched", "RightHand.Ring.2 Stretched"},
{"Right Ring 3 Stretched", "RightHand.Ring.3 Stretched"},
{"Right Little 1 Stretched", "RightHand.Little.1 Stretched"},
{"Right Little Spread", "RightHand.Little Spread"},
{"Right Little 2 Stretched", "RightHand.Little.2 Stretched"},
{"Right Little 3 Stretched", "RightHand.Little.3 Stretched"},
};
public static AnimationClip CreateAnimationClipFromHumanPose(HumanPose pose)
{
var clip = new AnimationClip();
// pos
{
var curve = new AnimationCurve(new Keyframe[]
{
new Keyframe(0, pose.bodyPosition.x),
});
var muscle = "RootT.x";
clip.SetCurve(null, typeof(Animator), muscle, curve);
}
{
var curve = new AnimationCurve(new Keyframe[]
{
new Keyframe(0, pose.bodyPosition.y),
});
var muscle = "RootT.y";
clip.SetCurve(null, typeof(Animator), muscle, curve);
}
{
var curve = new AnimationCurve(new Keyframe[]
{
new Keyframe(0, pose.bodyPosition.z),
});
var muscle = "RootT.z";
clip.SetCurve(null, typeof(Animator), muscle, curve);
}
// rot
{
var curve = new AnimationCurve(new Keyframe[]
{
new Keyframe(0, pose.bodyRotation.x),
});
var muscle = "RootQ.x";
clip.SetCurve(null, typeof(Animator), muscle, curve);
}
{
var curve = new AnimationCurve(new Keyframe[]
{
new Keyframe(0, pose.bodyRotation.y),
});
var muscle = "RootQ.y";
clip.SetCurve(null, typeof(Animator), muscle, curve);
}
{
var curve = new AnimationCurve(new Keyframe[]
{
new Keyframe(0, pose.bodyRotation.z),
});
var muscle = "RootQ.z";
clip.SetCurve(null, typeof(Animator), muscle, curve);
}
{
var curve = new AnimationCurve(new Keyframe[]
{
new Keyframe(0, pose.bodyRotation.w),
});
var muscle = "RootQ.w";
clip.SetCurve(null, typeof(Animator), muscle, curve);
}
// muscles
for (int i = 0; i < HumanTrait.MuscleCount; ++i)
{
var curve = new AnimationCurve(new Keyframe[]
{
new Keyframe(0, pose.muscles[i]),
});
var muscle = HumanTrait.MuscleName[i];
if (TraitPropMap.ContainsKey(muscle))
{
muscle = TraitPropMap[muscle];
}
clip.SetCurve(null, typeof(Animator), muscle, curve);
}
return clip;
}
}
}