-
Notifications
You must be signed in to change notification settings - Fork 63
/
SkinnedData.cs
166 lines (138 loc) · 5.86 KB
/
SkinnedData.cs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System.Collections.Generic;
using System.Linq;
using SharpDX;
namespace DX12GameProgramming
{
///<summary>
/// A Keyframe defines the bone transformation at an instant in time.
///</summary>
internal class Keyframe
{
public float Time { get; set; }
public Vector3 Translation { get; set; }
public float Scale { get; set; } = 1.0f;
public Quaternion Rotation { get; set; } = Quaternion.Identity;
}
///<summary>
/// A BoneAnimation is defined by a list of keyframes. For time
/// values inbetween two keyframes, we interpolate between the
/// two nearest keyframes that bound the time.
///
/// We assume an animation always has two keyframes.
///</summary>
internal class BoneAnimation
{
public List<Keyframe> Keyframes { get; } = new List<Keyframe>();
// Keyframes are sorted by time, so first keyframe gives start time.
public float StartTime => Keyframes.First().Time;
// Keyframes are sorted by time, so last keyframe gives end time.
public float EndTime => Keyframes.Last().Time;
public Matrix Interpolate(float t)
{
float scale = 1.0f;
Quaternion rotation = Quaternion.Identity;
Vector3 translation = Vector3.Zero;
if (t <= StartTime)
{
Keyframe first = Keyframes.First();
scale = first.Scale;
rotation = first.Rotation;
translation = first.Translation;
}
if (t >= EndTime)
{
Keyframe last = Keyframes.Last();
scale = last.Scale;
rotation = last.Rotation;
translation = last.Translation;
}
else
{
for (int i = 0; i < Keyframes.Count - 1; i++)
{
Keyframe current = Keyframes[i];
Keyframe next = Keyframes[i + 1];
if (t >= current.Time && t <= next.Time)
{
float lerpPercent = (t - current.Time) / (next.Time - current.Time);
scale = MathUtil.Lerp(current.Scale, next.Scale, lerpPercent);
translation = Vector3.Lerp(current.Translation, next.Translation, lerpPercent);
rotation = Quaternion.Lerp(current.Rotation, next.Rotation, lerpPercent);
break;
}
}
}
return Matrix.AffineTransformation(scale, rotation, translation);
}
}
///<summary>
/// Examples of AnimationClips are "Walk", "Run", "Attack", "Defend".
/// An AnimationClip requires a BoneAnimation for every bone to form
/// the animation clip.
///</summary>
internal class AnimationClip
{
public List<BoneAnimation> BoneAnimations { get; } = new List<BoneAnimation>();
public float ClipStartTime => BoneAnimations.Min(x => x.StartTime);
public float ClipEndTime => BoneAnimations.Max(x => x.EndTime);
public void Interpolate(float t, Matrix[] boneTransforms)
{
for (int i = 0; i < BoneAnimations.Count; i++)
boneTransforms[i] = BoneAnimations[i].Interpolate(t);
}
}
internal class SkinnedData
{
// Gives parentIndex of ith bone.
private readonly List<int> _boneHierarchy;
private readonly List<Matrix> _boneOffsets;
private readonly Dictionary<string, AnimationClip> _animations;
private readonly Matrix[] _toParentTransforms;
private readonly Matrix[] _toRootTransforms;
public SkinnedData(
List<int> boneHierarchy,
List<Matrix> boneOffsets,
Dictionary<string, AnimationClip> animations)
{
_boneHierarchy = boneHierarchy;
_boneOffsets = boneOffsets;
_animations = animations;
_toParentTransforms = new Matrix[BoneCount];
_toRootTransforms = new Matrix[BoneCount];
}
public int BoneCount => _boneHierarchy.Count;
public float GetClipStartTime(string clipName) => _animations[clipName].ClipStartTime;
public float GetClipEndTime(string clipName) => _animations[clipName].ClipEndTime;
// In a real project, you'd want to cache the result if there was a chance
// that you were calling this several times with the same clipName at
// the same timePos.
public void GetFinalTransforms(string clipName, float time, List<Matrix> finalTransforms)
{
finalTransforms.Clear();
// Interpolate all the bones of this clip at the given time instance.
AnimationClip clip = _animations[clipName];
clip.Interpolate(time, _toParentTransforms);
//
// Traverse the hierarchy and transform all the bones to the root space.
//
// The root bone has index 0. The root bone has no parent, so its toRootTransform
// is just its local bone transform.
_toRootTransforms[0] = _toParentTransforms[0];
// Now find the toRootTransform of the children.
for (int i = 1; i < BoneCount; i++)
{
Matrix toParent = _toParentTransforms[i];
int parentIndex = _boneHierarchy[i];
Matrix parentToRoot = _toRootTransforms[parentIndex];
Matrix toRoot = toParent * parentToRoot;
_toRootTransforms[i] = toRoot;
}
// Premultiply by the bone offset transform to get the final transform.
for (int i = 0; i < BoneCount; i++)
{
Matrix finalTransform = _boneOffsets[i] * _toRootTransforms[i];
finalTransforms.Add(Matrix.Transpose(finalTransform));
}
}
}
}