-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImportAnimation.cpp
472 lines (365 loc) · 16.2 KB
/
ImportAnimation.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include "ImportAnimation.hpp"
#include "ImportPath.hpp"
#include "ImportSkeletalMesh.hpp"
#include "ResourceManifest.hpp"
#include <Animation/BsAnimationClip.h>
#include <Animation/BsAnimationUtility.h>
#include <FileSystem/BsFileSystem.h>
#include <Resources/BsResources.h>
#include <vdfs/fileIndex.h>
#include <zenload/modelAnimationParser.h>
#include <zenload/modelScriptParser.h>
#include <zenload/zCModelAni.h>
#include <zenload/zCModelMeshLib.h>
#include <zenload/zenParser.h>
using namespace bs;
using namespace BsZenLib;
using namespace BsZenLib::Res;
struct AnimationToImport
{
String fullAnimationName;
ZenLoad::zCModelScriptAni animation;
Vector<ZenLoad::zCModelScriptEventSfx> eventsSfx;
Vector<ZenLoad::zCModelScriptEventSfx> eventsSfxGround;
Vector<ZenLoad::zCModelScriptEventPfx> eventsPfx;
Vector<ZenLoad::zCModelScriptEventPfxStop> eventsPfxStop;
Vector<ZenLoad::zCModelScriptEventTag> eventsTag;
};
static Matrix3 createMatrixFromQuaternion(const Quaternion& quat);
static Matrix4 convertMatrix(const ZMath::Matrix& m);
static int32_t scaleFrameToHeaderFrameRate(const ZenLoad::zCModelScriptAni& ani, size_t frame,
size_t numFramesTotal);
struct AnimationCurvesWithRootMotion
{
bs::AnimationCurves curves;
bs::RootMotion rootMotion;
};
static AnimationCurvesWithRootMotion importAnimationSamples(const String& virtualFilePath,
const ZenLoad::zCModelMeshLib& meshLib,
const ZenLoad::zCModelScriptAni& def,
const VDFS::FileIndex& vdfs);
static AnimationCurvesWithRootMotion convertSamples(const std::vector<ZenLoad::ModelNode>& nodes,
const ZenLoad::zCModelScriptAni& def,
const ZenLoad::ModelAnimationParser& parser);
bool BsZenLib::HasCachedMAN(const bs::String& fullAnimationName)
{
return HasCachedResource(GothicPathToCachedZAnimation(fullAnimationName));
}
Res::HZAnimation BsZenLib::LoadCachedAnimation(const bs::String& fullAnimationName)
{
return bs::gResources().load<ZAnimationClip>(GothicPathToCachedZAnimation(fullAnimationName));
}
Res::HZAnimation BsZenLib::ImportMAN(const ZenLoad::zCModelMeshLib& meshLib,
const AnimationToImport& def, const VDFS::FileIndex& vdfs)
{
BS_LOG(Info, Uncategorized, "Caching Animation: {0} ({1})", def.fullAnimationName,
def.manFileName);
// Import keyframes and convert them to curves (From .MAN-file)
AnimationCurvesWithRootMotion samples =
importAnimationSamples(def.manFileName + ".MAN", meshLib, def.animation, vdfs);
SPtr<RootMotion> pRootMotion = bs::bs_shared_ptr_new<RootMotion>(samples.rootMotion);
SPtr<AnimationCurves> pCurves = bs::bs_shared_ptr_new<AnimationCurves>(samples.curves);
HAnimationClip clip = AnimationClip::create(pCurves, false, 1, pRootMotion);
HZAnimation anim = ZAnimationClip::create();
anim->mClip = clip;
anim->mNext = def.animation.m_Next.c_str();
anim->mShouldQueueIntoLayer = (def.animation.m_Flags & ZenLoad::MSB_QUEUE_ANI) != 0;
anim->mShouldRotateModel = (def.animation.m_Flags & ZenLoad::MSB_ROTATE_MODEL) != 0;
anim->mIsFlyingAnimation = (def.animation.m_Flags & ZenLoad::MSB_FLY) != 0;
anim->mShouldMoveModel = (def.animation.m_Flags & ZenLoad::MSB_MOVE_MODEL) != 0;
anim->mIsIdleAnimation = (def.animation.m_Flags & ZenLoad::MSB_IDLE) != 0;
anim->mIsLooping = def.animation.m_Next == def.animation.m_Name;
// Gothics default layer is 1, while bsf uses 0. Therefore, subtract 1 here.
anim->mLayer = def.animation.m_Layer - 1;
switch (def.animation.m_Dir)
{
case ZenLoad::MSB_BACKWARD:
anim->mDirection = ZAnimationClip::Direction::Reverse;
break;
default:
case ZenLoad::MSB_FORWARD:
anim->mDirection = ZAnimationClip::Direction::Forward;
break;
}
Vector<AnimationEvent> events;
// Notify about the next animation if needed
if (!anim->mIsLooping)
{
if (def.animation.m_Next.empty())
{
bs::String command = "STOP";
float endOfAnimation = clip->getLength();
events.emplace_back(command, endOfAnimation);
}
else
{
bs::String command = "PLAYCLIP:" + bs::String(def.animation.m_Next.c_str());
float endOfAnimation = clip->getLength();
events.emplace_back(command, endOfAnimation);
}
}
clip->setEvents(events);
// TODO: clip->setEvents(...) for sounds/particles/other
clip->setName(def.fullAnimationName);
anim->setName(def.fullAnimationName);
const bool overwrite = true;
gResources().save(clip, GothicPathToCachedAnimationClip(def.fullAnimationName), overwrite);
AddToResourceManifest(clip, GothicPathToCachedAnimationClip(def.fullAnimationName));
gResources().save(anim, GothicPathToCachedZAnimation(def.fullAnimationName), overwrite);
AddToResourceManifest(anim, GothicPathToCachedZAnimation(def.fullAnimationName));
return anim;
}
BsZenLib::Res::HZAnimation BsZenLib::AliasAnimation(const ZenLoad::zCModelMeshLib& meshLib,
const AnimationToAlias& def,
const VDFS::FileIndex& vdfs)
{
BS_LOG(Info, Uncategorized,
"Aliasing Animation: " + def.fullAnimationName + " to " + def.fullAnimationNameOfAlias);
AnimationToImport a;
// Patch the existing animation with the settings of the alias
a.animation = def.animationSource;
a.animation.m_Layer = def.animation.m_Layer;
a.animation.m_Next = def.animation.m_Next;
a.animation.m_BlendIn = def.animation.m_BlendIn;
a.animation.m_BlendOut = def.animation.m_BlendOut;
a.animation.m_Flags = def.animation.m_Flags;
a.animation.m_Dir = def.animation.m_Dir;
a.fullAnimationName = def.fullAnimationName;
a.manFileName = def.fullAnimationNameOfAlias;
// TODO: Pass events
return ImportMAN(meshLib, a, vdfs);
}
BsZenLib::Res::HZAnimation BsZenLib::BlendAnimation(const AnimationToBlend& def)
{
BS_LOG(Info, Uncategorized,
"Blending Animation: " + def.fullAnimationName + " to " + def.fullAnimationNameOfBlend);
HZAnimation anim = ZAnimationClip::create();
HZAnimation toAlias = LoadCachedAnimation(def.fullAnimationNameOfBlend);
if (!toAlias)
{
// BS_EXCEPT(InvalidStateException, "Animation to blend has to have been cached before!");
return {};
}
// FIXME: Blending doesn't really work. If we were putting in the animation data of the
// animation to blend to in here, we would never end at a looping animation.
HAnimationClip clip = AnimationClip::create();
anim->mClip = {};
anim->mShouldQueueIntoLayer = toAlias->mShouldQueueIntoLayer;
anim->mShouldRotateModel = toAlias->mShouldRotateModel;
anim->mIsFlyingAnimation = toAlias->mIsFlyingAnimation;
anim->mShouldMoveModel = toAlias->mShouldMoveModel;
anim->mIsIdleAnimation = toAlias->mIsIdleAnimation;
anim->mIsLooping = false; // Blends cannot loop
anim->mDirection = toAlias->mDirection;
// Gothics default layer is 1, while bsf uses 0. Therefore, subtract 1 here.
anim->mLayer = def.animation.m_Layer - 1;
Vector<AnimationEvent> events;
// Notify about the next animation if needed
if (!anim->mIsLooping)
{
if (def.animation.m_Next.empty())
{
bs::String command = "STOP";
float endOfAnimation = clip->getLength();
events.emplace_back(command, endOfAnimation);
}
else
{
bs::String command = "PLAYCLIP:" + bs::String(def.animation.m_Next.c_str());
float endOfAnimation = clip->getLength();
events.emplace_back(command, endOfAnimation);
}
}
clip->setEvents(events);
// TODO: clip->setEvents(...) for sounds/particles/other
clip->setName(def.fullAnimationName);
anim->setName(def.fullAnimationName);
const bool overwrite = true;
gResources().save(clip, GothicPathToCachedAnimationClip(def.fullAnimationName), overwrite);
AddToResourceManifest(clip, GothicPathToCachedAnimationClip(def.fullAnimationName));
gResources().save(anim, GothicPathToCachedZAnimation(def.fullAnimationName), overwrite);
AddToResourceManifest(anim, GothicPathToCachedZAnimation(def.fullAnimationName));
return anim;
}
static AnimationCurvesWithRootMotion importAnimationSamples(const String& manFile,
const ZenLoad::zCModelMeshLib& meshLib,
const ZenLoad::zCModelScriptAni& def,
const VDFS::FileIndex& vdfs)
{
ZenLoad::ZenParser zen(manFile.c_str(), vdfs);
ZenLoad::ModelAnimationParser parser(zen);
parser.setScale(1.0f / 100.0f); // Centimeters -> Meters
ZenLoad::zCModelAniHeader header;
std::vector<ZenLoad::zCModelAniSample> samples;
std::vector<uint32_t> nodeIndexList;
ZenLoad::ModelAnimationParser::EChunkType type;
while ((type = parser.parse()) != ZenLoad::ModelAnimationParser::CHUNK_EOF)
{
switch (type)
{
case ZenLoad::ModelAnimationParser::CHUNK_ERROR:
return {};
default:
// Continue
break;
}
}
return convertSamples(meshLib.getNodes(), def, parser);
}
static AnimationCurvesWithRootMotion convertSamples(const std::vector<ZenLoad::ModelNode>& nodes,
const ZenLoad::zCModelScriptAni& def,
const ZenLoad::ModelAnimationParser& parser)
{
AnimationCurvesWithRootMotion result = {};
size_t numFrames = parser.getHeader().numFrames;
size_t numNodesInAnimation = parser.getNodeIndex().size();
size_t startFrame = 0;
size_t lastFrame = numFrames - 1;
float speed = def.m_Speed;
// Some animations seem to be empty? Is this a bug or are they supposed to be empty?
if (numFrames == 0)
{
return {};
}
Vector<bool> animatedNodes(nodes.size(), false);
for (size_t nodeIdx = 0; nodeIdx < numNodesInAnimation; nodeIdx++)
{
size_t realNodeIdx = parser.getNodeIndex()[nodeIdx];
// if (realNodeIdx != 0)
// continue;
assert(realNodeIdx < nodes.size());
if (realNodeIdx >= nodes.size()) return {};
const ZenLoad::ModelNode& node = nodes[realNodeIdx];
Vector<TKeyframe<Vector3>> positionKeyframes(numFrames);
Vector<TKeyframe<Quaternion>> rotationKeyframes(numFrames);
for (size_t frame = 0; frame < numFrames; frame++)
{
size_t frameIdx = [&]() {
if (def.m_Dir == ZenLoad::EModelScriptAniDir::MSB_BACKWARD)
{
return numFrames - frame - 1;
}
else
{
return frame;
}
}();
// One Frame is stored as a continuous array of samples for each node.
// That means, that for each frame, there are as many samples as there are
// nodes. To get all samples for only one node, you have to skip all the others
// to get to the next frame.
size_t realFrame = frameIdx + startFrame;
size_t sampleIdx = numNodesInAnimation * realFrame + nodeIdx;
const ZenLoad::zCModelAniSample& sample = parser.getSamples()[sampleIdx];
Vector4 sdfsd;
Vector3 position = Vector3(sample.position.x, // ...
sample.position.y, // ...
sample.position.z); // ...
Quaternion rotation = Quaternion(sample.rotation.w, // ...
-sample.rotation.x, // ...
-sample.rotation.y, // ...
-sample.rotation.z); // ...
// Matrix3 workaround = createMatrixFromQuaternion(rotation);
// rotation.fromRotationMatrix(workaround);
// TODO: Find out whether these are seconds, milliseconds, ...?
float keyframeTime = frame / parser.getHeader().fpsRate;
positionKeyframes[frame].inTangent = Vector3(BsZero);
positionKeyframes[frame].outTangent = Vector3(BsZero);
positionKeyframes[frame].value = position;
positionKeyframes[frame].time = keyframeTime;
rotationKeyframes[frame].inTangent = Quaternion(BsZero);
rotationKeyframes[frame].outTangent = Quaternion(BsZero);
rotationKeyframes[frame].value = rotation;
rotationKeyframes[frame].time = keyframeTime;
}
AnimationUtility::calculateTangents(positionKeyframes);
AnimationUtility::calculateTangents(rotationKeyframes);
TAnimationCurve<Vector3> positionCurve(positionKeyframes);
TAnimationCurve<Quaternion> rotationCurve(rotationKeyframes);
if (realNodeIdx == 0)
{
result.rootMotion = RootMotion(positionCurve, rotationCurve);
// It's important that this is not set as 'animated node' so the node
// will be filled with dummy data later on
}
else
{
result.curves.addPositionCurve(node.name.c_str(), positionCurve);
result.curves.addRotationCurve(node.name.c_str(), rotationCurve);
animatedNodes[realNodeIdx] = true;
}
}
// Add all non-animated nodes
for (size_t i = 0; i < nodes.size(); i++)
{
// Does this node already have animation data?
if (animatedNodes[i]) continue;
const ZenLoad::ModelNode& node = nodes[i];
Vector3 position;
Quaternion rotation;
Vector3 scale;
convertMatrix(node.transformLocal).decomposition(position, rotation, scale);
Vector<TKeyframe<Vector3>> positionKeyframes(1);
Vector<TKeyframe<Quaternion>> rotationKeyframes(1);
positionKeyframes[0].inTangent = Vector3(BsZero);
positionKeyframes[0].outTangent = Vector3(BsZero);
positionKeyframes[0].value = position;
positionKeyframes[0].time = 0.0f;
rotationKeyframes[0].inTangent = Quaternion(BsZero);
rotationKeyframes[0].outTangent = Quaternion(BsZero);
rotationKeyframes[0].value = rotation;
rotationKeyframes[0].time = 0.0f;
AnimationUtility::calculateTangents(positionKeyframes);
AnimationUtility::calculateTangents(rotationKeyframes);
TAnimationCurve<Vector3> positionCurve(positionKeyframes);
TAnimationCurve<Quaternion> rotationCurve(rotationKeyframes);
result.curves.addPositionCurve(node.name.c_str(), positionCurve);
result.curves.addRotationCurve(node.name.c_str(), rotationCurve);
}
return result;
}
/**
* The quaternions supplied by Gothic seem to be a little bit different...
*/
static Matrix3 createMatrixFromQuaternion(const Quaternion& quat)
{
Matrix3 m = Matrix3::IDENTITY;
Vector3 cols[3];
cols[0].x = quat.w * quat.w + quat.x * quat.x - quat.y * quat.y - quat.z * quat.z;
cols[0].y = 2.0f * (quat.x * quat.y + quat.w * quat.z);
cols[0].z = 2.0f * (quat.x * quat.z - quat.w * quat.y);
cols[1].x = 2.0f * (quat.x * quat.y - quat.w * quat.z);
cols[1].y = quat.w * quat.w - quat.x * quat.x + quat.y * quat.y - quat.z * quat.z;
cols[1].z = 2.0f * (quat.y * quat.z + quat.w * quat.x);
cols[2].x = 2.0f * (quat.x * quat.z + quat.w * quat.y);
cols[2].y = 2.0f * (quat.y * quat.z - quat.w * quat.x);
cols[2].z = quat.w * quat.w - quat.x * quat.x - quat.y * quat.y + quat.z * quat.z;
m.setColumn(0, cols[0]);
m.setColumn(1, cols[1]);
m.setColumn(2, cols[2]);
return m;
}
static Matrix4 convertMatrix(const ZMath::Matrix& m)
{
Matrix4 bs = {m.mv[0], m.mv[1], m.mv[2], m.mv[3], m.mv[4], m.mv[5], m.mv[6], m.mv[7],
m.mv[8], m.mv[9], m.mv[10], m.mv[11], m.mv[12], m.mv[13], m.mv[14], m.mv[15]};
return bs.transpose();
}
/**
* This weird function scales the frame when an event occurs according to m_FrameCount (header)
* FIXME there are cases when m_LastFrame, m_FirstFrame and other values are obviously incorrect
* (high values) I checked for overflows/weird arithmetic with unsigned/signed stuff but the numbers
* are not even close to maximum/minimum numbers. m_FrameCount doesn't have this Problem, thats why
* animations are played "correctly" ingame. In many cases m_LastFrame, m_FirstFrame and m_FrameCount
* don't match.
*/
static int32_t scaleFrameToHeaderFrameRate(const ZenLoad::zCModelScriptAni& ani, size_t frame,
size_t numFramesTotal)
{
if (ani.m_LastFrame < ani.m_FirstFrame) return frame;
auto diff = ani.m_LastFrame - ani.m_FirstFrame;
float pos = 0.0f;
if (diff == 0) return numFramesTotal;
pos = frame / (float)diff;
return static_cast<int32_t>(pos * numFramesTotal);
}