-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnimator.cpp
More file actions
677 lines (594 loc) · 18 KB
/
Copy pathAnimator.cpp
File metadata and controls
677 lines (594 loc) · 18 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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
#include "Animator.h"
#include "Interfaces/AssetAuthoringPort.h"
#include "AnimatorSystem.h"
#include "Model.h"
#include "TransCondition.h"
#include "AniTransition.h"
#include "AnimationState.h"
#include "AvatarMask.h"
#include "ConditionParameter.h"
#include "ReflectionYml.h"
#include "DataSystem.h"
#include "AnimationController.h"
#include "RenderScene.h"
#include "../RenderEngine/Skeleton.h"
#include "SceneManager.h"
#include "Socket.h"
#include <nlohmann/json.hpp>
void Animator::OnInitialized()
{
auto renderScene = SceneManagers->GetRenderScene();
if (renderScene)
{
renderScene->RegisterAnimator(this);
}
}
void Animator::OnUninitializing()
{
auto scene = GetOwner()->m_ownerScene;
auto renderScene = SceneManagers->GetRenderScene();
if (renderScene)
{
renderScene->UnregisterAnimator(this);
}
}
// ?�랙 C3 ??AnimatorSystem ?�록/?��?. Awake/OnDestroy(컴포?�트??1??게이??가
// ?�니?????�입/?�탈 ?�을 ?�는 ?�유??AnimatorSystem.h ?�단 주석 참조 ??DDOL
// ?�브?�트가 ?�을 건널 ?�도 매번 ?�시 불려???�기 ?�문?�다. ?�제 ?�괴 경로
// (Scene::FlushPendingDestroy)??OnUninitializing(??OnDestroy 브리지) 직전??
// OnRemovingFromScene??먼�? 부르�?�? ???�스?�에??빠�????�점????�� ??
// ?�괴보다 먼�???
void Animator::OnAddedToScene()
{
AnimatorSystems->Register(this);
}
void Animator::OnRemovingFromScene()
{
AnimatorSystems->Unregister(this);
}
void Animator::SetAnimation(int index)
{
m_AnimIndex = index;
UpdateAnimation();
}
void Animator::UpdateAnimation()
{
if (m_AnimIndex <= 0)
m_AnimIndex = 0;
if (m_AnimIndex >= m_Skeleton->m_animations.size())
m_AnimIndex = m_Skeleton->m_animations.size() - 1;
m_AnimIndexChosen = m_AnimIndex;
m_TimeElapsed = 0;
}
void Animator::CreateController(std::string name)
{
std::shared_ptr<AnimationController> animationController = std::make_shared<AnimationController>();
//AnimationController* animationController = new AnimationController();
animationController->m_owner = this;
animationController->name = name;
animationController->CreateState("Ani State", -1, true);
m_animationControllers.push_back(animationController);
}
std::shared_ptr<AnimationController> Animator::CreateController_UI()
{
std::shared_ptr<AnimationController> animationController = std::make_shared<AnimationController>();
//AnimationController* animationController = new AnimationController();
animationController->m_owner = this;
animationController->name = "NewLayer" + std::to_string(m_animationControllers.size());
animationController->CreateState("Ani State",-1,true);
m_animationControllers.push_back(animationController);
return animationController;
}
std::shared_ptr<AnimationController> Animator::CreateController_UINoAni()
{
std::shared_ptr<AnimationController> animationController = std::make_shared<AnimationController>();
//AnimationController* animationController = new AnimationController();
animationController->m_owner = this;
animationController->name = "NewLayer" + std::to_string(m_animationControllers.size());
//animationController->CreateState("Ani State", -1, true);
m_animationControllers.push_back(animationController);
return animationController;
}
void Animator::DeleteController(int index)
{
m_animationControllers.erase(m_animationControllers.begin() + index);
}
void Animator::DeleteController(std::string controllerName)
{
auto it = std::remove_if(m_animationControllers.begin(), m_animationControllers.end(),
[&](std::shared_ptr<AnimationController> controller)
{
return controller->name == controllerName;
});
m_animationControllers.erase(it, m_animationControllers.end());
}
AnimationController* Animator::GetController(std::string name)
{
for (auto& Controller : m_animationControllers)
{
if (Controller->name == name)
return Controller.get();
}
return nullptr;
}
void Animator::SetUseLayer(int layerindex, bool _useLayer)
{
if (layerindex >= 0 && layerindex < m_animationControllers.size())
{
m_animationControllers[layerindex]->SetUseLayer(_useLayer);
}
}
Entity* Animator::FindBoneRecursive(Entity* parent, const std::string& boneName)
{
if (!parent) return nullptr;
for (Entity::Index childIndex : parent->GetChildrenIndices())
{
Entity* child = parent->OwnerSceneFindIndex(childIndex);
if (!child) continue;
if (child->m_name == boneName)
return child;
// �ڽ��� �ڽĵ鵵 Ž��
if (Entity* result = FindBoneRecursive(child, boneName))
return result;
}
return nullptr;
}
Socket* Animator::MakeSocket(std::string_view socketName, std::string_view boneName, Entity* object)
{
if (Socket* socket = FindSocket(socketName); socket)
return socket;
// ���� �ڽ� ���� ��ü���� boneName�� ã�´� (����� Ž��)
std::string realBoneName = boneName.data();
Entity* socketBone = FindBoneRecursive(object, realBoneName);
// ������ (1)~(100)���� �̸� �ٿ��� ã�´�
int index = 1;
while (!socketBone && index <= 10)
{
std::string indexedName = realBoneName + " (" + std::to_string(index) + ")";
socketBone = FindBoneRecursive(object, indexedName);
if (socketBone)
{
realBoneName = indexedName; // ���� �� �̸� ������Ʈ
break;
}
++index;
}
// ã������ ���� ���� �� ��ȯ
if (socketBone)
{
Socket* newSocket = new Socket();
newSocket->m_name = socketName;
newSocket->GameObjectIndex = 9999 + index; // ������ ���
newSocket->m_ObjectName = boneName;
socketvec.push_back(newSocket);
return newSocket;
}
return nullptr;
}
Socket* Animator::FindSocket(std::string_view socketName)
{
for (auto& socket : socketvec)
{
if (socket->m_name == socketName)
return socket;
}
return nullptr;
}
void Animator::ClearControllersAndParams()
{
m_animationControllers.clear();
std::unique_lock lock(m_paramMutex);
for (auto* p : Parameters)
{
delete p;
}
Parameters.clear();
}
void Animator::DeleteParameter(int index)
{
std::unique_lock lock(m_paramMutex);
if (index >= 0 && index < Parameters.size())
{
for (auto& controller : m_animationControllers)
{
for (auto& state : controller->StateVec)
{
for (auto& transition : state->Transitions)
{
for (auto& condition : transition->conditions)
{
if (condition.valueParameter == Parameters[index])
{
condition.valueParameter = nullptr;
}
}
}
}
}
delete Parameters[index];
Parameters.erase(Parameters.begin() + index);
}
}
ConditionParameter* Animator::AddDefaultParameter(ValueType vType)
{
std::string baseName;
switch (vType)
{
case ValueType::Float:
baseName = "NewFloat";
break;
case ValueType::Int:
baseName = "NewInt";
break;
case ValueType::Bool:
baseName = "NewBool";
break;
case ValueType::Trigger:
baseName = "NewTrigger";
break;
}
std::string valueName = baseName;
int index = 0;
bool isDuplicate = true;
{
std::unique_lock lock(m_paramMutex);
while (isDuplicate)
{
isDuplicate = false;
for (auto& parm : Parameters)
{
if (parm->name == valueName)
{
isDuplicate = true;
valueName = baseName + std::to_string(++index);
break;
}
}
}
}
ConditionParameter* newParameter = new ConditionParameter(0, vType, valueName);
{
std::unique_lock lock(m_paramMutex);
Parameters.push_back(newParameter);
}
return newParameter;
}
ConditionParameter* Animator::FindParameter(std::string valueName)
{
std::unique_lock lock(m_paramMutex);
for (auto& parameter : Parameters)
{
if (parameter->name == valueName)
{
return parameter;
}
}
return nullptr;
}
bool Animator::SerializeControllers(std::string _jsonName)
{
if (_jsonName.empty())
{
Debug->LogError("Animator controller save requires a non-empty name");
return false;
}
nlohmann::json json;
nlohmann::json controllerArray = nlohmann::json::array();
for (auto& Controller : m_animationControllers)
{
controllerArray.push_back(Controller->Serialize());
}
json["Controllers"] = controllerArray;
nlohmann::json paramArray = nlohmann::json::array();
{
std::unique_lock lock(m_paramMutex);
for (auto& param : Parameters)
{
paramArray.push_back(param->Serialize());
}
}
json["Parameters"] = paramArray;
// replace_extension은 이름에 '.'이 있으면 그 뒤를 통째로 잘라낸다("v1.2" →
// "v1.json"). 문자열로 붙여 이름을 보존한다.
UncatalogedAuthoringRequest request{};
request.destinationPath = PathFinder::AnimatorjsonPath(_jsonName + ".json");
request.payload = json.dump(4);
if (!AssetAuthoringPort::WriteAnimatorController(request))
{
Debug->LogError(
"Animator controller save requires a complete Editor authoring "
"transaction: " + _jsonName);
return false;
}
return true;
}
void Animator::DeserializeControllers(std::string _filename)
{
//������� json ����
/*namespace fs = std::filesystem;
fs::path dirPath = PathFinder::AnimatorjsonPath();
if (!fs::exists(dirPath) || !fs::is_directory(dirPath))
{
std::cerr << "Directory does not exist: " << dirPath << std::endl;
return;
}
for (const auto& entry : fs::directory_iterator(dirPath))
{
if (entry.is_regular_file() && entry.path().extension() == ".json")
{
std::string filePath = entry.path().string();
}
}*/
std::ifstream file(_filename);
if (!file.is_open())
{
std::cerr << "Failed to open: " << _filename << std::endl;
}
nlohmann::json json;
try
{
file >> json;
}
catch (std::exception& e)
{
std::cerr << "JSON parsing error: " << e.what() << std::endl;
}
ClearControllersAndParams();
for (auto& parameterJson : json["Parameters"])
{
int param_vType = parameterJson["param_vType"];
ValueType paramvType = ValueType::Float;
switch (param_vType)
{
case 0:
paramvType = ValueType::Float;
break;
case 1:
paramvType = ValueType::Int;
break;
case 2:
paramvType = ValueType::Bool;
break;
case 3:
paramvType = ValueType::Trigger;
break;
}
ConditionParameter* param = AddDefaultParameter(paramvType);
param->name = parameterJson["param_name"];
}
for (auto& contorllerJson : json["Controllers"])
{
std::shared_ptr<AnimationController> curController = CreateController_UINoAni();
curController->name = contorllerJson["controller_name"];
bool usecontroller = false;
bool usemask = false;
if (contorllerJson["useController"] == 1)
usecontroller = true;
curController->useController = usecontroller;
/*if (contorllerJson["useMask"] == 1)
usemask = true;*/
//curController->useMask = usemask;
for (auto& stateJson : contorllerJson["StateVec"])
{
std::shared_ptr<AnimationState> curState = curController->CreateState_UI();
curState->AnimationIndex = stateJson["animationIndex"];
curState->animationSpeed = stateJson["animationSpeed"];
curState->animationSpeedParameterName = stateJson["animationSpeedParameterName"];
curState->behaviourName = stateJson["behaviourName"];
curState->SetBehaviour(curState->behaviourName);
curState->multiplerAnimationSpeed = stateJson["multiplerAnimationSpeed"];
curState->m_name = stateJson["state_name"];
curState->m_isAny = stateJson["m_isAny"];
bool usemultipler = false;
if (stateJson["useMultipler"] == 1)
{
usemultipler = true;
}
curState->useMultipler = usemultipler;
}
for (auto& stateJson : contorllerJson["StateVec"])
{
for (auto& transionJson : stateJson["transitions"])
{
std::string curstateName = transionJson["curStateName"];
std::string nextStateName = transionJson["nextStateName"];
AniTransition* curtrans = curController->CreateTransition(curstateName, nextStateName);
bool hasexitTime = false;
if (transionJson["hasExitTime"] == 1)
hasexitTime = true;
curtrans->hasExitTime = hasexitTime;
curtrans->exitTime = transionJson["exitTime"];
curtrans->blendTime = transionJson["blendTime"];
for (auto& condionJson : transionJson["conditions"])
{
auto firstParam = Parameters[0];
TransCondition* curcodition = curtrans->AddConditionDefault(firstParam->name, ConditionType::None, firstParam->vType);
curcodition->SetCondition(condionJson["valueName"]);
ConditionType ctype = ConditionType::Greater;
int type = condionJson["cType"].get<int>();
switch (type)
{
case 1:
ctype = ConditionType::Less;
break;
case 2:
ctype = ConditionType::Equal;
break;
case 3:
ctype = ConditionType::NotEqual;
break;
case 4:
ctype = ConditionType::True;
break;
case 5:
ctype = ConditionType::False;
break;
default:
ctype = ConditionType::Less;
break;
}
curcodition->cType = ctype;
curcodition->CompareParameter.fValue = condionJson["fValue"];
curcodition->CompareParameter.iValue = condionJson["iValue"];
curcodition->CompareParameter.bValue = condionJson["bValue"];
curcodition->CompareParameter.tValue = condionJson["tValue"];
}
}
}
curController->SetCurState(contorllerJson["m_curState"]);
}
}
void Animator::OnDeserialized(const YAML::Node& node)
{
// CT6-d: �?ComponentFactory Animator 분기 ?�동(?�작·?�서 보존).
// Parameters·m_animationControllers???�인???�소 벡터??typed ??��?�화가
// 건드리�? ?�는?????�기???�동 복원???�채?�?�다.
Model* model = nullptr;
std::vector<bool> animationBools;
std::unordered_map<int, std::vector<KeyFrameEvent>> animationKeyFrameMap;
int aniIndex = 0;
if (node["m_Skeleton"])
{
auto& skel = node["m_Skeleton"];
if (skel["m_animations"])
{
auto& animations = skel["m_animations"];
for (auto& animation : animations)
{
bool _aniBool = animation["m_isLoop"].as<bool>();
animationBools.push_back(_aniBool);
auto& keyFrameEvents = animation["m_keyFrameEvent"];
std::vector<KeyFrameEvent> KeyFrameEventVec;
for (auto& keyFrameEvent : keyFrameEvents)
{
KeyFrameEvent newEvent;
Meta::Deserialize(&newEvent, keyFrameEvent);
KeyFrameEventVec.push_back(newEvent);
}
if (!KeyFrameEventVec.empty())
{
animationKeyFrameMap[aniIndex] = KeyFrameEventVec;
}
aniIndex++;
}
}
}
if (node["m_Motion"])
{
FileGuid guid = node["m_Motion"].as<std::string>();
if (guid != nullFileGuid)
{
m_Motion = guid;
auto model = DataSystems->LoadModelGUID(guid);
if (model)
{
m_Skeleton = model->m_Skeleton;
}
for (int i = 0; i < m_Skeleton->m_animations.size(); ++i)
{
if (animationBools.empty()) break;
m_Skeleton->m_animations[i].m_isLoop = animationBools[i];
for (auto& event : animationKeyFrameMap[i])
m_Skeleton->m_animations[i].AddEvent(event);
}
}
}
if (node["Parameters"])
{
auto& paramNode = node["Parameters"];
for (auto& param : paramNode)
{
ConditionParameter* aniParam = new ConditionParameter();
Meta::Deserialize(aniParam, param);
Parameters.push_back(aniParam);
}
}
if (node["m_animationControllers"])
{
auto& animationControllerNode = node["m_animationControllers"];
for (auto& layer : animationControllerNode)
{
std::shared_ptr<AnimationController> animationController = std::make_shared<AnimationController>();
Meta::Deserialize(animationController.get(), layer);
animationController->m_owner = this;
if (animationController->useMask == true)
{
if (layer["m_avatarMask"])
{
auto& MaskNode = layer["m_avatarMask"];
AvatarMask avatarMask;
Meta::Deserialize(&avatarMask, MaskNode);
avatarMask.RootMask = avatarMask.MakeBoneMask(animationController->m_owner->m_Skeleton->m_rootBone);
if (MaskNode["m_BoneMasks"])
{
auto& boneMaskNode = MaskNode["m_BoneMasks"];
int i = 0;
for (auto& boneMask : boneMaskNode)
{
BoneMask* newboneMask = new BoneMask();
Meta::Deserialize(newboneMask, boneMask);
avatarMask.m_BoneMasks[i]->isEnabled = newboneMask->isEnabled;
i++;
}
}
animationController->ReCreateMask(&avatarMask);
}
}
if (layer["StateVec"])
{
auto& StatesNode = layer["StateVec"];
for (auto& state : StatesNode)
{
std::shared_ptr<AnimationState> sharedState = std::make_shared<AnimationState>();
Meta::Deserialize(sharedState.get(), state);
animationController->StateVec.push_back(sharedState);
animationController->StateNameSet.insert(sharedState->m_name);
animationController->m_nameToState.insert(std::make_pair(sharedState->m_name, sharedState));
sharedState->m_ownerController = animationController.get();
sharedState->SetBehaviour(sharedState->behaviourName);
if (state["Transitions"])
{
auto& transitionNode = state["Transitions"];
for (auto& transition : transitionNode)
{
std::shared_ptr<AniTransition> sharedTransition = std::make_shared<AniTransition>();
Meta::Deserialize(sharedTransition.get(), transition);
sharedState->Transitions.push_back(sharedTransition);
sharedTransition->m_ownerController = animationController.get();
if (transition["conditions"])
{
auto& conditionNode = transition["conditions"];
for (auto& condition : conditionNode)
{
TransCondition newcondition;
Meta::Deserialize(&newcondition, condition);
newcondition.m_ownerController = animationController.get();
newcondition.SetValue(newcondition.valueName);
sharedTransition->conditions.push_back(newcondition);
}
}
}
}
}
}
if (layer["m_curState"])
{
auto& curNode = layer["m_curState"];
if (curNode.IsNull() == false)
{
std::string name = curNode["m_name"].as<std::string>();
animationController->SetCurState(name);
}
}
for (auto& state : animationController->StateVec)
{
for (auto& transition : state->Transitions)
{
transition->SetCurState(transition->curStateName);
transition->SetNextState(transition->nextStateName);
}
}
m_animationControllers.push_back(animationController);
}
}
}