-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationBlender.cpp
More file actions
556 lines (490 loc) · 19.9 KB
/
AnimationBlender.cpp
File metadata and controls
556 lines (490 loc) · 19.9 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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include "AnimationBlender.h"
#include "AnimationControlController.h"
#include "SelectionSet.h"
#include "SentryReporter.h"
#include <Ogre.h>
#include <OgreSkeletonInstance.h>
#include <OgreAnimation.h>
#include <OgreAnimationState.h>
#include <OgreKeyFrame.h>
#include <algorithm>
#include <cmath>
AnimationBlender* AnimationBlender::m_pSingleton = nullptr;
AnimationBlender* AnimationBlender::instance()
{
if (!m_pSingleton) m_pSingleton = new AnimationBlender(); // NOSONAR — see kill()
return m_pSingleton;
}
AnimationBlender* AnimationBlender::qmlInstance(QQmlEngine* engine, QJSEngine* scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
auto* inst = instance();
QQmlEngine::setObjectOwnership(inst, QQmlEngine::CppOwnership);
return inst;
}
void AnimationBlender::kill()
{
delete m_pSingleton; // NOSONAR — manual lifetime mirrors AnimationControlController
m_pSingleton = nullptr;
}
AnimationBlender::AnimationBlender()
: QObject(nullptr)
{
auto* ctrl = AnimationControlController::instance();
connect(ctrl, &AnimationControlController::selectionChanged,
this, &AnimationBlender::refreshFromSelection);
// Tell the Animation Control panel about newly baked clips so its
// animation tree picks them up without requiring a re-select. Inspector
// is wired separately in PropertiesPanelController.
connect(this, &AnimationBlender::clipBaked,
ctrl, &AnimationControlController::updateAnimationTree);
}
QString AnimationBlender::animA() const { return QString::fromStdString(m_animA); }
QString AnimationBlender::animB() const { return QString::fromStdString(m_animB); }
namespace {
// Disable every animation state on `entity`. Pre-condition: caller already
// captured a PreviewSnapshot so deactivate can restore the original flags.
void disableAllStates(Ogre::Entity* entity)
{
if (!entity) return;
auto* set = entity->getAllAnimationStates();
if (!set) return;
for (const auto& [name, state] : set->getAnimationStates()) {
state->setEnabled(false);
}
}
const char* modeName(int mode)
{
switch (mode) {
case AnimationBlender::ModeMix: return "mix";
case AnimationBlender::ModeAdditive: return "additive";
case AnimationBlender::ModeOverride: return "override";
default: return "mix";
}
}
} // namespace
void AnimationBlender::setActive(bool on)
{
if (on == m_active) return;
// Refuse activation unless A/B are both set and distinct. apply() bails
// out for the same reasons, but bailing there alone leaves the entity
// with all-states-off until the user picks valid clips. Rejecting here
// keeps the toggle truthful: "Active" only sticks when it's meaningful.
if (on && (m_animA.empty() || m_animB.empty() || m_animA == m_animB)) {
return;
}
m_active = on;
if (on) {
// Snapshot the entity's pre-blend state so deactivation can restore
// it, then turn off every animation layer immediately. apply() on
// the next frame re-enables A and B with the right weights;
// doing it here prevents a one-frame flash where a stale checked
// animation continues to play before the blend kicks in.
Ogre::Entity* entity = resolveActiveEntity();
captureSnapshot(entity);
disableAllStates(entity);
SentryReporter::addBreadcrumb(
"ui.action",
QString("Blend preview ON (A=%1, B=%2, mode=%3, weight=%4)")
.arg(QString::fromStdString(m_animA))
.arg(QString::fromStdString(m_animB))
.arg(modeName(m_mode))
.arg(m_weight, 0, 'f', 2));
} else {
// Toggling off: roll the entity's animation states back to whatever
// they looked like before the blender started writing to them.
restoreSnapshot();
SentryReporter::addBreadcrumb("ui.action", "Blend preview OFF");
}
// Inspector listens to activeChanged and refreshes its animation list,
// which keeps its per-anim Enable/Loop checkboxes in sync.
emit activeChanged();
}
void AnimationBlender::setAnimA(const QString& name)
{
const std::string s = name.toStdString();
if (s == m_animA) return;
m_animA = s;
deactivateIfInvalid();
emit selectionChanged();
}
void AnimationBlender::setAnimB(const QString& name)
{
const std::string s = name.toStdString();
if (s == m_animB) return;
m_animB = s;
deactivateIfInvalid();
emit selectionChanged();
}
void AnimationBlender::deactivateIfInvalid()
{
// Called whenever animA/animB changes. If preview is currently active and
// the new selection is unusable (empty side, or A == B), apply() would
// start returning false on the next frame and MainWindow would fall back
// to plain addTime(). The entity would still be in its blended enabled/
// weight configuration, leaving stale preview playback. Forcing a clean
// deactivation here restores the pre-blend snapshot.
if (!m_active) return;
if (m_animA.empty() || m_animB.empty() || m_animA == m_animB) {
setActive(false);
}
}
void AnimationBlender::setWeight(double w)
{
if (w < 0.0) w = 0.0;
if (w > 1.0) w = 1.0;
if (qFuzzyCompare(w + 1.0, m_weight + 1.0)) return;
m_weight = w;
emit weightChanged();
}
void AnimationBlender::setMode(int m)
{
Mode newMode = ModeMix;
if (m == ModeMix || m == ModeAdditive || m == ModeOverride) {
newMode = static_cast<Mode>(m);
}
if (newMode == m_mode) return;
m_mode = newMode;
emit modeChanged();
}
void AnimationBlender::refreshFromSelection()
{
const auto* ctrl = AnimationControlController::instance();
// Active entity changed — anything we'd cached or written to the previous
// entity's states needs to be undone before we point at the new one.
if (const std::string activeEntity = ctrl->selectedEntityName().toStdString();
activeEntity != m_activeEntityName) {
if (m_active) restoreSnapshot();
m_activeEntityName = activeEntity;
if (!m_animA.empty() || !m_animB.empty()) {
m_animA.clear();
m_animB.clear();
emit selectionChanged();
}
if (m_active) captureSnapshot(resolveActiveEntity());
}
QStringList anims;
if (const Ogre::Entity* entity = resolveActiveEntity()) {
if (const auto* set = entity->getAllAnimationStates()) {
for (const auto& [name, state] : set->getAnimationStates()) {
anims << QString::fromStdString(name);
}
}
}
if (anims != m_animations) {
m_animations = anims;
emit animationsChanged();
}
}
Ogre::Entity* AnimationBlender::resolveActiveEntity() const
{
if (m_activeEntityName.empty()) return nullptr;
// findEntityByName lives in the anonymous namespace below; forward-declare
// here to keep the function above the helper without reordering.
for (Ogre::Entity* ent : SelectionSet::getSingleton()->getResolvedEntities()) {
if (ent && ent->getName() == m_activeEntityName) return ent;
}
return nullptr;
}
// ── Snapshot helpers ──────────────────────────────────────────────────────────
void AnimationBlender::captureSnapshot(Ogre::Entity* entity)
{
m_snapshot = {};
if (!entity) return;
m_snapshot.entityName = entity->getName();
if (const auto* set = entity->getAllAnimationStates()) {
for (const auto& [name, state] : set->getAnimationStates()) {
StateSnapshot s;
s.enabled = state->getEnabled();
s.weight = state->getWeight();
m_snapshot.states[name] = s;
}
}
if (auto* skel = entity->getSkeleton()) {
m_snapshot.blendMode = skel->getBlendMode();
}
m_snapshot.valid = true;
}
namespace {
Ogre::Entity* findEntityByName(const std::string& name) {
for (Ogre::Entity* ent : SelectionSet::getSingleton()->getResolvedEntities()) {
if (ent && ent->getName() == name) return ent;
}
return nullptr;
}
} // namespace
void AnimationBlender::restoreSnapshot()
{
if (!m_snapshot.valid) return;
// Resolve by the snapshot's recorded entity name in case the active
// selection has already moved on.
Ogre::Entity* entity = findEntityByName(m_snapshot.entityName);
if (!entity) {
m_snapshot = {};
return;
}
if (auto* set = entity->getAllAnimationStates()) {
for (const auto& [name, state] : set->getAnimationStates()) {
auto it = m_snapshot.states.find(name);
if (it == m_snapshot.states.end()) continue;
state->setEnabled(it->second.enabled);
state->setWeight(it->second.weight);
}
}
if (auto* skel = entity->getSkeleton()) {
skel->setBlendMode(m_snapshot.blendMode);
}
m_snapshot = {};
}
// ── Live blend ────────────────────────────────────────────────────────────────
namespace {
// Apply weights + enabled flags for a single sample of the given mode.
// Used by both live apply() and bake() to keep the math in one place.
void configureBlend(Ogre::AnimationState* a, Ogre::AnimationState* b,
int mode, double weight)
{
if (mode == AnimationBlender::ModeOverride) {
const bool useB = (weight >= 0.5);
a->setEnabled(!useB);
b->setEnabled(useB);
a->setWeight(useB ? 0.0f : 1.0f);
b->setWeight(useB ? 1.0f : 0.0f);
} else {
a->setEnabled(true);
b->setEnabled(true);
a->setWeight(static_cast<float>(1.0 - weight));
b->setWeight(static_cast<float>(weight));
}
}
// Disable any layer that isn't A or B so the entity's pose is exactly A+B.
// Pre-blend layer enabled-flags are restored by the snapshot pathway.
void muteOtherLayers(Ogre::Entity* entity,
const std::string& nameA, const std::string& nameB)
{
auto* set = entity->getAllAnimationStates();
if (!set) return;
for (const auto& [name, state] : set->getAnimationStates()) {
if (name != nameA && name != nameB) state->setEnabled(false);
}
}
// Advance a single state, routing through advanceTime() if it's the selected
// clip (for slice-A loop region) and using speed-scaled addTime() otherwise.
void advanceState(Ogre::AnimationState* s, const std::string& name,
const std::string& activeAnim,
AnimationControlController* ctrl,
double dt, double scaledDt)
{
if (name == activeAnim) {
const auto now = static_cast<double>(s->getTimePosition());
const double next = ctrl->advanceTime(now, dt);
s->setTimePosition(static_cast<float>(next));
} else {
s->addTime(static_cast<float>(scaledDt));
}
}
} // namespace
bool AnimationBlender::apply(Ogre::Entity* entity, double dt) // NOSONAR — mutates state via entity*; not const
{
if (!m_active || !entity) return false;
if (m_animA.empty() || m_animB.empty() || m_animA == m_animB) return false;
if (!entity->hasAnimationState(m_animA) || !entity->hasAnimationState(m_animB)) {
return false;
}
Ogre::AnimationState* a = entity->getAnimationState(m_animA);
Ogre::AnimationState* b = entity->getAnimationState(m_animB);
if (!a || !b) return false;
if (auto* skel = entity->getSkeleton()) {
skel->setBlendMode(m_mode == ModeAdditive
? Ogre::ANIMBLEND_CUMULATIVE
: Ogre::ANIMBLEND_AVERAGE);
}
muteOtherLayers(entity, m_animA, m_animB);
configureBlend(a, b, m_mode, m_weight);
auto* ctrl = AnimationControlController::instance();
const std::string activeAnim = ctrl->selectedAnimation().toStdString();
const double scaledDt = dt * ctrl->playbackSpeed();
if (m_mode == ModeOverride) {
const bool useB = (m_weight >= 0.5);
advanceState(useB ? b : a, useB ? m_animB : m_animA,
activeAnim, ctrl, dt, scaledDt);
} else {
advanceState(a, m_animA, activeAnim, ctrl, dt, scaledDt);
advanceState(b, m_animB, activeAnim, ctrl, dt, scaledDt);
}
return true;
}
// ── Bake ──────────────────────────────────────────────────────────────────────
namespace {
// Per-bake snapshot of one A or B state. Smaller than PreviewSnapshot since
// bake() also restores the per-state-set's other layers via the entity-level
// snapshot from setActive().
struct StateRestore {
float time = 0.0f;
bool on = false;
float weight = 0.0f;
};
StateRestore captureState(const Ogre::AnimationState* s) {
return { s->getTimePosition(), s->getEnabled(), s->getWeight() };
}
void restoreState(Ogre::AnimationState* s, const StateRestore& r) {
s->setTimePosition(r.time);
s->setEnabled(r.on);
s->setWeight(r.weight);
}
void writeBoneKeyframe(Ogre::NodeAnimationTrack* track, float t, const Ogre::Bone* bone) {
auto* kf = track->createNodeKeyFrame(t);
kf->setTranslate(bone->getPosition() - bone->getInitialPosition());
kf->setRotation(bone->getInitialOrientation().Inverse() * bone->getOrientation());
kf->setScale(bone->getScale() / bone->getInitialScale());
}
// Set up sa/sb state for a single bake sample at time `t` according to
// `mode` and `weight`. Wraps t around each state's own clip length so a
// shorter clip loops within the bake range.
void positionForSample(Ogre::AnimationState* sa, Ogre::AnimationState* sb,
float t, int mode, double weight)
{
// Map the bake-clock `t` onto each source clip's own timeline.
// t < clipLen → straight pass-through (most common case).
// t == clipLen → clamp to clipLen (otherwise fmod returns 0, which
// writes the start pose into the closing keyframe and produces a
// visible pop on equal-length inputs).
// t > clipLen → wrap with fmod so a shorter source clip loops within
// a longer bake range.
auto mapTime = [](float t, float clipLen) -> float {
if (clipLen <= 0.0f) return 0.0f;
if (t < clipLen) return t;
if (t > clipLen) return std::fmod(t, clipLen);
return clipLen; // t == clipLen
};
sa->setTimePosition(mapTime(t, sa->getLength()));
sb->setTimePosition(mapTime(t, sb->getLength()));
if (mode == AnimationBlender::ModeOverride) {
const bool useB = (weight >= 0.5);
sa->setEnabled(!useB);
sb->setEnabled(useB);
sa->setWeight(useB ? 0.0f : 1.0f);
sb->setWeight(useB ? 1.0f : 0.0f);
} else {
sa->setEnabled(true);
sb->setEnabled(true);
sa->setWeight(static_cast<float>(1.0 - weight));
sb->setWeight(static_cast<float>(weight));
}
}
// Sample every bone's local TRS at the current skeleton state and emit a
// keyframe at time `t` on each node track owned by `anim`.
void writeAllBoneKeyframes(Ogre::Animation* anim, const Ogre::Skeleton* skel, float t)
{
const unsigned short numBones = skel->getNumBones();
for (unsigned short i = 0; i < numBones; ++i) {
Ogre::Bone* bone = skel->getBone(i);
if (!bone) continue;
if (auto* track = anim->getNodeTrack(bone->getHandle())) {
writeBoneKeyframe(track, t, bone);
}
}
}
// Capture every state's enabled/weight/time into a flat map keyed by name.
std::unordered_map<std::string, StateRestore> captureAllStates(const Ogre::Entity* entity)
{
std::unordered_map<std::string, StateRestore> out;
if (const auto* set = entity->getAllAnimationStates()) {
for (const auto& [name, state] : set->getAnimationStates()) {
out[name] = captureState(state);
}
}
return out;
}
// Inverse of captureAllStates — re-applies whatever was recorded earlier so
// the entity's animation states look identical to before bake() ran.
void restoreAllStates(Ogre::Entity* entity,
const std::unordered_map<std::string, StateRestore>& saved)
{
auto* set = entity->getAllAnimationStates();
if (!set) return;
for (const auto& [name, state] : set->getAnimationStates()) {
auto it = saved.find(name);
if (it != saved.end()) restoreState(state, it->second);
}
}
void disableNonAB(Ogre::Entity* entity, const std::string& a, const std::string& b)
{
auto* set = entity->getAllAnimationStates();
if (!set) return;
for (const auto& [name, state] : set->getAnimationStates()) {
if (name != a && name != b) state->setEnabled(false);
}
}
void createBoneTracks(Ogre::Animation* anim, Ogre::Skeleton* skel)
{
const unsigned short numBones = skel->getNumBones();
for (unsigned short i = 0; i < numBones; ++i) {
if (Ogre::Bone* bone = skel->getBone(i)) {
anim->createNodeTrack(bone->getHandle(), bone);
}
}
}
} // namespace
QString AnimationBlender::bake(const QString& clipName, int fps)
{
if (clipName.isEmpty()) return {};
if (fps <= 0) fps = 30;
if (m_animA.empty() || m_animB.empty() || m_animA == m_animB) return {};
const std::string clipStd = clipName.toStdString();
// Refuse to bake over one of the source clips — sa/sb resolve to those
// states, and removeAnimation() would invalidate them mid-bake.
if (clipStd == m_animA || clipStd == m_animB) return {};
Ogre::Entity* entity = resolveActiveEntity();
if (!entity) return {};
Ogre::SkeletonInstance* skel = entity->getSkeleton();
if (!skel) return {};
if (!entity->hasAnimationState(m_animA) || !entity->hasAnimationState(m_animB)) {
return {};
}
Ogre::AnimationState* sa = entity->getAnimationState(m_animA);
Ogre::AnimationState* sb = entity->getAnimationState(m_animB);
const float length = std::max(sa->getLength(), sb->getLength());
if (length <= 0.0f) return {};
const int sampleCount = std::max(2,
static_cast<int>(std::ceil(length * static_cast<float>(fps))) + 1);
const float step = length / static_cast<float>(sampleCount - 1);
if (skel->hasAnimation(clipStd)) skel->removeAnimation(clipStd);
Ogre::Animation* anim = skel->createAnimation(clipStd, length);
anim->setInterpolationMode(Ogre::Animation::IM_LINEAR);
createBoneTracks(anim, skel);
// Preserve every state's pre-bake config so the live preview is unaffected.
const auto live = captureAllStates(entity);
const auto prevBlend = skel->getBlendMode();
disableNonAB(entity, m_animA, m_animB);
skel->setBlendMode(m_mode == ModeAdditive
? Ogre::ANIMBLEND_CUMULATIVE
: Ogre::ANIMBLEND_AVERAGE);
for (int s = 0; s < sampleCount; ++s) {
const float t = (s == sampleCount - 1) ? length : (static_cast<float>(s) * step);
positionForSample(sa, sb, t, m_mode, m_weight);
skel->setAnimationState(*entity->getAllAnimationStates());
skel->_updateTransforms();
writeAllBoneKeyframes(anim, skel, t);
}
restoreAllStates(entity, live);
skel->setBlendMode(prevBlend);
entity->refreshAvailableAnimationState();
// Deactivate so the per-frame apply() stops re-imposing blender weights
// on top of the restored state. Without this, the next render tick would
// overwrite the pre-bake configuration we just restored. setActive(false)
// also calls restoreSnapshot(), which is a no-op if Active was off.
setActive(false);
refreshFromSelection();
SentryReporter::addBreadcrumb(
"ui.action",
QString("Bake blended animation '%1' (mode=%2, weight=%3, fps=%4, length=%5s, samples=%6)")
.arg(clipName)
.arg(modeName(m_mode))
.arg(m_weight, 0, 'f', 2)
.arg(fps)
.arg(length, 0, 'f', 3)
.arg(sampleCount));
emit clipBaked(clipName);
return clipName;
}