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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
static std::vector<Channel> ImportChannels(aiAnimation* anim, const Skeleton& skeleton, const bool isMaskedRootMotion, const glm::vec3& rootTranslationMask, float rootRotationMask)
{
std::vector<Channel> channels;
std::unordered_map<std::string_view, uint32_t> boneIndices;
std::unordered_set<uint32_t> rootBoneIndices;
for (uint32_t i = 0; i < skeleton.GetNumBones(); ++i)
{
boneIndices.emplace(skeleton.GetBoneName(i), i + 1); // 0 is reserved for root motion channel boneIndices are base=1
if (skeleton.GetParentBoneIndex(i) == Skeleton::NullIndex)
rootBoneIndices.emplace(i + 1);
}
std::map<uint32_t, aiNodeAnim*> validChannels;
for (uint32_t channelIndex = 0; channelIndex < anim->mNumChannels; ++channelIndex)
{
aiNodeAnim* nodeAnim = anim->mChannels[channelIndex];
auto it = boneIndices.find(nodeAnim->mNodeName.C_Str());
if (it != boneIndices.end())
{
validChannels.emplace(it->second, nodeAnim); // validChannels.first is base=1, .second is node pointer
}
}
channels.resize(skeleton.GetNumBones() + 1); // channels is base=1
// channels don't necessarily have first frame at time zero.
// We can just generate a dummy key frame, but that can end up looking a bit odd (in particular,
// looping animations appear to pause for a split second each time they loop and encounter the
// dummy key frame.
// So instead, we clip the animation time horizon.
double firstFrameDelta = DBL_MAX;
double animationDuration = anim->mDuration;
for (uint32_t boneIndex = 1; boneIndex < channels.size(); ++boneIndex)
{
if (auto validChannel = validChannels.find(boneIndex); validChannel != validChannels.end())
{
auto nodeAnim = validChannel->second;
if (nodeAnim->mNumPositionKeys > 0)
firstFrameDelta = std::min(firstFrameDelta, nodeAnim->mPositionKeys[0].mTime);
if (nodeAnim->mNumRotationKeys > 0)
firstFrameDelta = std::min(firstFrameDelta, nodeAnim->mRotationKeys[0].mTime);
if (nodeAnim->mNumScalingKeys > 0)
firstFrameDelta = std::min(firstFrameDelta, nodeAnim->mScalingKeys[0].mTime);
}
}
anim->mDuration -= firstFrameDelta;
// The rest of the code assumes non-zero animation duration.
// Enforce that here.
if (anim->mDuration <= 0.0) {
anim->mDuration = 1.0;
}
for (uint32_t boneIndex = 1; boneIndex < channels.size(); ++boneIndex)
{
Channel& channel = channels[boneIndex];
channel.Index = boneIndex;
if (auto validChannel = validChannels.find(boneIndex); validChannel != validChannels.end())
{
auto nodeAnim = validChannel->second;
channel.Translations.reserve(nodeAnim->mNumPositionKeys + 2); // +2 because worst case we insert two more keys
channel.Rotations.reserve(nodeAnim->mNumRotationKeys + 2);
channel.Scales.reserve(nodeAnim->mNumScalingKeys + 2);
// Note: There is no need to check for duplicate keys (i.e. multiple keys all at same frame time)
// because Assimp throws these out for us
for (uint32_t keyIndex = 0; keyIndex < nodeAnim->mNumPositionKeys; ++keyIndex)
{
aiVectorKey key = nodeAnim->mPositionKeys[keyIndex];
float frameTime = std::clamp(static_cast<float>((key.mTime - firstFrameDelta) / anim->mDuration), 0.0f, 1.0f);
if ((keyIndex == 0) && (frameTime > 0.0f))
{
channels[boneIndex].Translations.emplace_back(0.0f, glm::vec3{ static_cast<float>(key.mValue.x), static_cast<float>(key.mValue.y), static_cast<float>(key.mValue.z) });
}
channel.Translations.emplace_back(frameTime, glm::vec3{ static_cast<float>(key.mValue.x), static_cast<float>(key.mValue.y), static_cast<float>(key.mValue.z) });
}
if (channel.Translations.empty())
{
HZ_CORE_WARN_TAG("Animation", "No translation track found for bone '{}'", skeleton.GetBoneName(boneIndex - 1));
channel.Translations = { {0.0f, glm::vec3{0.0f}}, {1.0f, glm::vec3{0.0f}} };
}
else if (channel.Translations.back().FrameTime < 1.0f)
{
channel.Translations.emplace_back(1.0f, channel.Translations.back().Value);
}
for (uint32_t keyIndex = 0; keyIndex < nodeAnim->mNumRotationKeys; ++keyIndex)
{
aiQuatKey key = nodeAnim->mRotationKeys[keyIndex];
float frameTime = std::clamp(static_cast<float>((key.mTime - firstFrameDelta) / anim->mDuration), 0.0f, 1.0f);
// WARNING: constructor parameter order for a quat is still WXYZ even if you have defined GLM_FORCE_QUAT_DATA_XYZW
if ((keyIndex == 0) && (frameTime > 0.0f))
{
channel.Rotations.emplace_back(0.0f, glm::quat{ static_cast<float>(key.mValue.w), static_cast<float>(key.mValue.x), static_cast<float>(key.mValue.y), static_cast<float>(key.mValue.z) });
}
channel.Rotations.emplace_back(frameTime, glm::quat{ static_cast<float>(key.mValue.w), static_cast<float>(key.mValue.x), static_cast<float>(key.mValue.y), static_cast<float>(key.mValue.z) });
HZ_CORE_ASSERT(fabs(glm::length(channels[boneIndex].Rotations.back().Value) - 1.0f) < 0.00001f); // check rotations are normalized (I think assimp ensures this, but not 100% sure)
}
if (channel.Rotations.empty())
{
HZ_CORE_WARN_TAG("Animation", "No rotation track found for bone '{}'", skeleton.GetBoneName(boneIndex - 1));
channel.Rotations = { {0.0f, glm::quat{1.0f, 0.0f, 0.0f, 0.0f}}, {1.0f, glm::quat{1.0f, 0.0f, 0.0f, 0.0f}} };
}
else if (channel.Rotations.back().FrameTime < 1.0f)
{
channel.Rotations.emplace_back(1.0f, channel.Rotations.back().Value);
}
for (uint32_t keyIndex = 0; keyIndex < nodeAnim->mNumScalingKeys; ++keyIndex)
{
aiVectorKey key = nodeAnim->mScalingKeys[keyIndex];
float frameTime = std::clamp(static_cast<float>((key.mTime - firstFrameDelta) / anim->mDuration), 0.0f, 1.0f);
if (keyIndex == 0 && frameTime > 0.0f)
{
channel.Scales.emplace_back(0.0f, glm::vec3{ static_cast<float>(key.mValue.x), static_cast<float>(key.mValue.y), static_cast<float>(key.mValue.z) });
}
channel.Scales.emplace_back(frameTime, glm::vec3{ static_cast<float>(key.mValue.x), static_cast<float>(key.mValue.y), static_cast<float>(key.mValue.z) });
}
if (channel.Scales.empty())
{
HZ_CORE_WARN_TAG("Animation", "No scale track found for bone '{}'", skeleton.GetBoneName(boneIndex - 1));
channel.Scales = { {0.0f, glm::vec3{1.0f}}, {1.0f, glm::vec3{1.0f}} };
}
else if (channel.Scales.back().FrameTime < 1.0f)
{
channel.Scales.emplace_back(1.0f, channels[boneIndex].Scales.back().Value);
}
}
else
{
HZ_CORE_WARN_TAG("Animation", "No animation tracks found for bone '{}'", skeleton.GetBoneName(boneIndex - 1));
auto translation = skeleton.GetBoneTranslations().at(boneIndex - 1);
auto rotation = skeleton.GetBoneRotations().at(boneIndex - 1);
auto scale = skeleton.GetBoneScales().at(boneIndex - 1);
channel.Translations = { {0.0f, translation}, {1.0f, translation} };
channel.Rotations = { {0.0f, rotation}, {1.0f, rotation} };
channel.Scales = { {0.0f, scale}, {1.0f, scale} };
}
}
// Create root motion channel.
// If isMaskedRootMotion is true, then root motion channel is created by filtering components of the first channel.
// Otherwise root motion channel is copied as-is from the first channel.
//
// Root motion is then removed from all "root" channels (so it doesn't get applied twice)
HZ_CORE_ASSERT(!rootBoneIndices.empty()); // Can't see how this would ever be false!
HZ_CORE_ASSERT(rootBoneIndices.find(1) != rootBoneIndices.end()); // First bone must be a root!
Channel& root = channels[0];
root.Index = 0;
if (isMaskedRootMotion)
{
for (auto& translation : channels[1].Translations)
{
root.Translations.emplace_back(translation.FrameTime, translation.Value * rootTranslationMask);
translation.Value *= (glm::vec3(1.0f) - rootTranslationMask);
translation.Value += root.Translations.front().Value;
}
for (auto& rotation : channels[1].Rotations)
{
if (rootRotationMask > 0.0f)
{
auto angleY = Utils::AngleAroundYAxis(rotation.Value);
root.Rotations.emplace_back(rotation.FrameTime, glm::quat{ glm::cos(angleY * 0.5f), glm::vec3{0.0f, 1.0f, 0.0f} *glm::sin(angleY * 0.5f) });
rotation.Value = glm::conjugate(glm::quat(glm::cos(angleY * 0.5f), glm::vec3{ 0.0f, 1.0f, 0.0f } *glm::sin(angleY * 0.5f))) * rotation.Value;
rotation.Value *= root.Rotations.front().Value;
}
else
{
root.Rotations.emplace_back(rotation.FrameTime, glm::quat{ 1.0f, 0.0f, 0.0f, 0.0f });
}
}
}
else
{
root.Translations = channels[1].Translations;
root.Rotations = channels[1].Rotations;
channels[1].Translations = { {0.0f, root.Translations.front().Value}, {1.0f, root.Translations.front().Value} };
channels[1].Rotations = { {0.0f, root.Rotations.front().Value}, {1.0f, root.Rotations.front().Value} };
}
root.Scales = { {0.0f, glm::vec3{1.0f}}, {1.0f, glm::vec3{1.0f}} };
// It is possible that there is more than one "root" bone in the asset.
// We need to remove the root motion from all of them (otherwise those bones will move twice as fast when root motion is applied)
for (const auto rootBoneIndex : rootBoneIndices)
{
// we already removed root motion from the first bone, above
if (rootBoneIndex != 1)
{
for (auto& translation : channels[rootBoneIndex].Translations)
{
// sample root channel at this translation's frametime
for (size_t rootFrame = 0; rootFrame < root.Translations.size() - 1; ++rootFrame)
{
if (root.Translations[rootFrame + 1].FrameTime >= translation.FrameTime)
{
const float alpha = (translation.FrameTime - root.Translations[rootFrame].FrameTime) / (root.Translations[rootFrame + 1].FrameTime - root.Translations[rootFrame].FrameTime);
translation.Value -= glm::mix(root.Translations[rootFrame].Value, root.Translations[rootFrame + 1].Value, alpha);
translation.Value += root.Translations.front().Value;
break;
}
}
}
for (auto& rotation : channels[rootBoneIndex].Rotations)
{
// sample root channel at the this rotation's frametime
for (size_t rootFrame = 0; rootFrame < root.Rotations.size() - 1; ++rootFrame)
{
if (root.Rotations[rootFrame + 1].FrameTime >= rotation.FrameTime)
{
const float alpha = (rotation.FrameTime - root.Rotations[rootFrame].FrameTime) / (root.Rotations[rootFrame + 1].FrameTime - root.Rotations[rootFrame].FrameTime);
rotation.Value = glm::normalize(glm::conjugate(glm::slerp(root.Rotations[rootFrame].Value, root.Rotations[rootFrame + 1].Value, alpha)) * rotation.Value);
rotation.Value *= root.Rotations.front().Value;
break;
}
}
}
}
}
return channels;
}
|