-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion_matching.cpp
398 lines (347 loc) · 14.5 KB
/
motion_matching.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "motion_matching.h"
#include "misc.h"
#include "profile.h"
#include <cfloat>
#define MM_MAX_FRAME_INFO_COUNT 10 * 60 * 90
const int32_t g_SkipFrameCount = 1;
// Copy the velocity for the last frame
// TODO(Lukas) make this be used by the asset pipeline
mm_controller_data*
PrecomputeRuntimeMMData(Memory::stack_allocator* TempAlloc,
array_handle<Anim::animation*> Animations, const mm_params& Params)
{
TIMED_BLOCK(BuildMotionSet);
mm_controller_data* MMData = PushAlignedStruct(TempAlloc, mm_controller_data);
memset(MMData, 0, sizeof(mm_controller_data));
MMData->Params = Params;
mm_frame_info* FrameInfoStorage =
PushAlignedArray(TempAlloc, MM_MAX_FRAME_INFO_COUNT, mm_frame_info);
// Alloc temp memory for transforms and matrices
mat4* TempMatrices = PushArray(TempAlloc, Params.FixedParams.Skeleton.BoneCount, mat4);
transform* TempTransforms =
PushArray(TempAlloc, Params.FixedParams.Skeleton.BoneCount, transform);
// Initialize the frame info stack
stack_handle<mm_frame_info> FrameInfoStack = {};
FrameInfoStack.Init(FrameInfoStorage, 0, sizeof(mm_frame_info) * MM_MAX_FRAME_INFO_COUNT);
// Loop over all animations in the set
for(int a = 0; a < Params.AnimRIDs.Count; a++)
{
const Anim::animation* Anim = Animations[a];
assert(Anim->ChannelCount == Params.FixedParams.Skeleton.BoneCount);
assert(1 < Anim->KeyframeCount);
const float AnimDuration = Anim::GetAnimDuration(Anim);
const float FrameDuration = AnimDuration / float(Anim->KeyframeCount);
const float PositionSamplingPeriod =
Params.DynamicParams.TrajectoryTimeHorizon / float(MM_POINT_COUNT);
const float AnimStartSkipTime = Anim->SampleTimes[0] + FrameDuration * float(g_SkipFrameCount);
const int32_t NewFrameInfoCount = int32_t(
MaxFloat(0.0f,
((AnimDuration - AnimStartSkipTime - Params.DynamicParams.TrajectoryTimeHorizon) *
Params.FixedParams.MetadataSamplingFrequency)));
mm_frame_info_range CurrentRange = {};
{
CurrentRange.StartTimeInAnim = AnimStartSkipTime;
CurrentRange.Start = FrameInfoStack.Count;
CurrentRange.End = FrameInfoStack.Count + NewFrameInfoCount;
}
FrameInfoStack.Expand(NewFrameInfoCount);
MMData->AnimFrameInfoRanges.Push(CurrentRange);
for(int i = 0; i < NewFrameInfoCount; i++)
{
int32_t FrameInfoIndex = CurrentRange.Start + i;
float CurrentSampleTime = CurrentRange.StartTimeInAnim +
float(i) * (1.0f / Params.FixedParams.MetadataSamplingFrequency);
Anim::LinearAnimationSample(TempTransforms, Anim, CurrentSampleTime);
Anim::ComputeBoneSpacePoses(TempMatrices, TempTransforms, Anim->ChannelCount);
ComputeModelSpacePoses(TempMatrices, TempMatrices, &Params.FixedParams.Skeleton);
ComputeFinalHierarchicalPoses(TempMatrices, TempMatrices, &Params.FixedParams.Skeleton);
mat4 InvRootMatrix;
mat4 RootMatrix;
int32_t HipIndex = 0;
mat4 HipMatrix =
Math::MulMat4(TempMatrices[HipIndex], Params.FixedParams.Skeleton.Bones[HipIndex].BindPose);
Anim::GetRootAndInvRootMatrices(&RootMatrix, &InvRootMatrix, HipMatrix);
// Fill Bone Positions
for(int b = 0; b < Params.FixedParams.ComparisonBoneIndices.Count; b++)
{
FrameInfoStack[FrameInfoIndex].BonePs[b] =
Math::MulMat4(InvRootMatrix,
Math::MulMat4(TempMatrices[Params.FixedParams.ComparisonBoneIndices[b]],
Params.FixedParams.Skeleton
.Bones[Params.FixedParams.ComparisonBoneIndices[b]]
.BindPose))
.T;
}
// Fill Bone Trajectory Positions
for(int p = 0; p < MM_POINT_COUNT; p++)
{
transform SampleHipTransform =
Anim::LinearAnimationBoneSample(Anim, HipIndex,
CurrentSampleTime + (p + 1) * PositionSamplingPeriod);
// NOTE(Lukas) this should use the root bone if animation has a dedicated one
const Anim::bone* Bone = &Params.FixedParams.Skeleton.Bones[HipIndex];
mat4 CurrentHipMatrix =
Math::MulMat4(Bone->BindPose,
Math::MulMat4(TransformToMat4(SampleHipTransform), Bone->InverseBindPose));
vec3 SamplePoint = CurrentHipMatrix.T;
vec4 SamplePointHomog = { SamplePoint, 1 };
FrameInfoStack[FrameInfoIndex].TrajectoryPs[p] =
Math::MulMat4Vec4(InvRootMatrix, SamplePointHomog).XYZ;
FrameInfoStack[FrameInfoIndex].TrajectoryPs[p].Y = 0;
vec3 CurrentZInTrajectorySpace =
Math::MulMat4Vec4(InvRootMatrix, { CurrentHipMatrix.Z, 0 }).XYZ;
FrameInfoStack[FrameInfoIndex].TrajectoryAngles[p] =
atan2f(CurrentZInTrajectorySpace.X, CurrentZInTrajectorySpace.Z);
}
}
// Compute velocity for first n-1 frames
for(int i = 0; i < NewFrameInfoCount - 1; i++)
{
for(int b = 0; b < MM_COMPARISON_BONE_COUNT; b++)
{
FrameInfoStack[i].BoneVs[b] =
(FrameInfoStack[i + 1].BonePs[b] - FrameInfoStack[i].BonePs[b]) *
MMData->Params.FixedParams.MetadataSamplingFrequency;
}
for(int p = 0; p < MM_POINT_COUNT; p++)
{
FrameInfoStack[i].TrajectoryVs[p] =
Math::Length(FrameInfoStack[i + 1].TrajectoryPs[p] - FrameInfoStack[i].TrajectoryPs[p]) /
PositionSamplingPeriod;
}
}
if(0 < NewFrameInfoCount)
{
int32_t LastFrameIndex = NewFrameInfoCount - 1;
// Copy the velocity for the last frame
// Last frames data
for(int b = 0; b < MM_COMPARISON_BONE_COUNT; b++)
{
FrameInfoStack[LastFrameIndex].BoneVs[b] =
(0 < LastFrameIndex) ? FrameInfoStack[LastFrameIndex - 1].BoneVs[b] : vec3{ 0, 0, 0 };
}
/*for(int p = 0; p < MM_POINT_COUNT; p++)
{
FrameInfoStack[LastFrameIndex].TrajectoryVs[p] =
(0 < LastFrameIndex) ? FrameInfoStack[LastFrameIndex - 1].TrajectoryVs[p] : 0.0f;
}*/
}
}
MMData->FrameInfos = FrameInfoStack.GetArrayHandle();
Memory::marker AssetEndMarker = {};
{
AssetEndMarker.Address = (uint8_t*)(MMData->FrameInfos.Elements + MMData->FrameInfos.Count);
}
// Set up the mirroring info for goal generation
MMData->Params.FixedParams.MirrorBoneIndices.HardClear();
if(MMData->Params.DynamicParams.MatchMirroredAnimations)
{
for(int i = 0; i < MMData->Params.FixedParams.ComparisonBoneIndices.Count; i++)
{
int BoneA = MMData->Params.FixedParams.ComparisonBoneIndices[i];
int BoneB = -1;
for(int p = 0; p < MMData->Params.DynamicParams.MirrorInfo.PairCount; p++)
{
int CandidateA = MMData->Params.DynamicParams.MirrorInfo.BoneMirrorIndices[p].a;
int CandidateB = MMData->Params.DynamicParams.MirrorInfo.BoneMirrorIndices[p].b;
if(BoneA == CandidateA)
{
BoneB = CandidateB;
break;
}
if(BoneA == CandidateB)
{
BoneB = CandidateA;
break;
}
}
// TODO(Lukas) Make sura that the bones selected are always in the mirror info stack
assert(BoneB != -1 && "Search bone does not have a mirror");
MMData->Params.FixedParams.MirrorBoneIndices.Push(BoneB);
}
}
TempAlloc->FreeToMarker(AssetEndMarker);
return MMData;
}
float
ComputeCost(const mm_frame_info& A, const mm_frame_info& B, float PosCoef, float VelCoef,
float TrajCoef, float TrajVCoef, float TrajAngleCoef, const float* TrajectoryWeights)
{
float PosDiffSum = 0.0f;
for(int b = 0; b < MM_COMPARISON_BONE_COUNT; b++)
{
vec3 Diff = A.BonePs[b] - B.BonePs[b];
PosDiffSum += Math::Length(Diff);
}
float VelDiffSum = 0.0f;
for(int b = 0; b < MM_COMPARISON_BONE_COUNT; b++)
{
vec3 VelDiff = A.BoneVs[b] - B.BoneVs[b];
VelDiffSum += Math::Length(VelDiff);
}
float TrajDiffSum = 0.0f;
//float TrajVDiffSum = 0.0f;
for(int p = 0; p < MM_POINT_COUNT; p++)
{
vec3 Diff = A.TrajectoryPs[p] - B.TrajectoryPs[p];
TrajDiffSum += TrajectoryWeights[p] * Math::Length(Diff);
/*float VDiff = TrajectoryWeights[p] * fabs(A.TrajectoryVs[p] - B.TrajectoryVs[p]);
TrajVDiffSum += VDiff;*/
}
float TrajDirDiffSum = 0.0f;
for(int p = 0; p < MM_POINT_COUNT; p++)
{
vec2 DirA = { sinf(A.TrajectoryAngles[p]), cosf(A.TrajectoryAngles[p]) };
vec2 DirB = { sinf(B.TrajectoryAngles[p]), cosf(B.TrajectoryAngles[p]) };
vec2 Diff = DirA - DirB;
TrajDirDiffSum += TrajectoryWeights[p] * Math::Length(Diff);
}
float Cost = PosCoef * PosDiffSum + VelCoef * VelDiffSum + TrajCoef * TrajDiffSum +
/*TrajVCoef * TrajVDiffSum +*/ TrajAngleCoef * TrajDirDiffSum;
return Cost;
}
float
ComputeCostComponents(float* BonePCost, float* BoneVCost, float* TrajPCost, float* TrajVCost,
float* TrajACost, const mm_frame_info& A, const mm_frame_info& B,
float PosCoef, float VelCoef, float TrajCoef, float TrajVCoef,
float TrajAngleCoef, const float* TrajectoryWeights)
{
float PosDiffSum = 0.0f;
for(int b = 0; b < MM_COMPARISON_BONE_COUNT; b++)
{
vec3 Diff = A.BonePs[b] - B.BonePs[b];
PosDiffSum += Math::Length(Diff);
}
float VelDiffSum = 0.0f;
for(int b = 0; b < MM_COMPARISON_BONE_COUNT; b++)
{
vec3 VelDiff = A.BoneVs[b] - B.BoneVs[b];
VelDiffSum += Math::Length(VelDiff);
}
float TrajDiffSum = 0.0f;
//float TrajVDiffSum = 0.0f;
for(int p = 0; p < MM_POINT_COUNT; p++)
{
vec3 Diff = A.TrajectoryPs[p] - B.TrajectoryPs[p];
TrajDiffSum += TrajectoryWeights[p] * Math::Length(Diff);
/*float VDiff = TrajectoryWeights[p] * fabs(A.TrajectoryVs[p] - B.TrajectoryVs[p]);
TrajVDiffSum += VDiff;*/
}
float TrajDirDiffSum = 0.0f;
for(int p = 0; p < MM_POINT_COUNT; p++)
{
vec2 DirA = { sinf(A.TrajectoryAngles[p]), cosf(A.TrajectoryAngles[p]) };
vec2 DirB = { sinf(B.TrajectoryAngles[p]), cosf(B.TrajectoryAngles[p]) };
vec2 Diff = DirA - DirB;
TrajDirDiffSum += TrajectoryWeights[p] * Math::Length(Diff);
}
*BonePCost = PosDiffSum * PosCoef;
*BoneVCost = VelDiffSum * VelCoef;
*TrajPCost = TrajDiffSum * TrajCoef;
*TrajVCost = 0;//TrajVDiffSum * TrajVCoef;
*TrajACost = TrajDirDiffSum * TrajAngleCoef;
float Cost = PosCoef * PosDiffSum + VelCoef * VelDiffSum + TrajCoef * TrajDiffSum +
/*TrajVCoef * TrajVDiffSum +*/ TrajAngleCoef * TrajDirDiffSum;
return Cost;
}
float
MotionMatch(int32_t* OutAnimIndex, float* OutLocalStartTime, mm_frame_info* OutBestMatch,
const mm_controller_data* MMData, mm_frame_info Goal)
{
TIMED_BLOCK(MotionMatch);
assert(OutAnimIndex && OutLocalStartTime);
assert(MMData);
assert(MMData->FrameInfos.IsValid());
float SmallestCost = FLT_MAX;
int32_t BestFrameInfoIndex = -1;
for(int i = 0; i < MMData->FrameInfos.Count; i++)
{
{
float CurrentCost =
ComputeCost(Goal, MMData->FrameInfos[i], MMData->Params.DynamicParams.BonePCoefficient,
MMData->Params.DynamicParams.BoneVCoefficient,
MMData->Params.DynamicParams.TrajPCoefficient,
MMData->Params.DynamicParams.TrajVCoefficient,
MMData->Params.DynamicParams.TrajAngleCoefficient,
MMData->Params.DynamicParams.TrajectoryWeights);
if(CurrentCost < SmallestCost)
{
SmallestCost = CurrentCost;
BestFrameInfoIndex = i;
}
}
}
assert(BestFrameInfoIndex != -1);
for(int a = 0; a < MMData->AnimFrameInfoRanges.Count; a++)
{
mm_frame_info_range CurrentRange = MMData->AnimFrameInfoRanges[a];
if(CurrentRange.Start <= BestFrameInfoIndex && BestFrameInfoIndex < CurrentRange.End)
{
*OutAnimIndex = a;
*OutLocalStartTime =
CurrentRange.StartTimeInAnim + (BestFrameInfoIndex - CurrentRange.Start) /
MMData->Params.FixedParams.MetadataSamplingFrequency;
*OutBestMatch = MMData->FrameInfos[BestFrameInfoIndex];
}
}
return SmallestCost;
}
float
MotionMatchWithMirrors(int32_t* OutAnimIndex, float* OutLocalStartTime, mm_frame_info* OutBestMatch,
bool* OutMatchedMirrored, const mm_controller_data* MMData,
mm_frame_info Goal, mm_frame_info MirroredGoal)
{
TIMED_BLOCK(MotionMatch);
assert(MMData->FrameInfos.IsValid());
float SmallestCost = FLT_MAX;
int32_t BestFrameInfoIndex = -1;
bool MatchIsMirrored = false;
for(int i = 0; i < MMData->FrameInfos.Count; i++)
{
{
float CurrentCost =
ComputeCost(Goal, MMData->FrameInfos[i], MMData->Params.DynamicParams.BonePCoefficient,
MMData->Params.DynamicParams.BoneVCoefficient,
MMData->Params.DynamicParams.TrajPCoefficient,
MMData->Params.DynamicParams.TrajVCoefficient,
MMData->Params.DynamicParams.TrajAngleCoefficient,
MMData->Params.DynamicParams.TrajectoryWeights);
if(CurrentCost < SmallestCost)
{
SmallestCost = CurrentCost;
BestFrameInfoIndex = i;
MatchIsMirrored = false;
}
}
{
float MirroredCost = ComputeCost(MirroredGoal, MMData->FrameInfos[i],
MMData->Params.DynamicParams.BonePCoefficient,
MMData->Params.DynamicParams.BoneVCoefficient,
MMData->Params.DynamicParams.TrajPCoefficient,
MMData->Params.DynamicParams.TrajVCoefficient,
MMData->Params.DynamicParams.TrajAngleCoefficient,
MMData->Params.DynamicParams.TrajectoryWeights);
if(MirroredCost < SmallestCost)
{
SmallestCost = MirroredCost;
BestFrameInfoIndex = i;
MatchIsMirrored = true;
}
}
}
assert(BestFrameInfoIndex != -1);
for(int a = 0; a < MMData->AnimFrameInfoRanges.Count; a++)
{
mm_frame_info_range CurrentRange = MMData->AnimFrameInfoRanges[a];
if(CurrentRange.Start <= BestFrameInfoIndex && BestFrameInfoIndex < CurrentRange.End)
{
*OutAnimIndex = a;
*OutLocalStartTime =
CurrentRange.StartTimeInAnim + (BestFrameInfoIndex - CurrentRange.Start) /
MMData->Params.FixedParams.MetadataSamplingFrequency;
*OutBestMatch = MMData->FrameInfos[BestFrameInfoIndex];
}
}
*OutMatchedMirrored = MatchIsMirrored;
return SmallestCost;
}