CanDeleteMotionWhenMotionIsBeingBlended.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/std/smart_ptr/unique_ptr.h>
  9. #include <EMotionFX/CommandSystem/Source/CommandManager.h>
  10. #include <EMotionFX/Source/Actor.h>
  11. #include <EMotionFX/Source/AnimGraph.h>
  12. #include <EMotionFX/Source/AnimGraphMotionNode.h>
  13. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  14. #include <EMotionFX/Source/AnimGraphStateTransition.h>
  15. #include <EMotionFX/Source/AnimGraphTimeCondition.h>
  16. #include <EMotionFX/Source/Mesh.h>
  17. #include <EMotionFX/Source/Motion.h>
  18. #include <EMotionFX/Source/MotionEventTable.h>
  19. #include <EMotionFX/Source/MotionSet.h>
  20. #include <EMotionFX/Source/Node.h>
  21. #include <EMotionFX/Source/MotionData/NonUniformMotionData.h>
  22. #include <EMotionFX/Source/TwoStringEventData.h>
  23. #include <Tests/Printers.h>
  24. #include <Tests/UI/UIFixture.h>
  25. #include <Tests/TestAssetCode/SimpleActors.h>
  26. #include <Tests/TestAssetCode/ActorFactory.h>
  27. namespace EMotionFX
  28. {
  29. TEST_F(UIFixture, CanDeleteMotionWhenMotionIsBeingBlended)
  30. {
  31. AZStd::unique_ptr<Actor> actor = ActorFactory::CreateAndInit<SimpleJointChainActor>(1);
  32. Motion* xmotion = aznew Motion("xmotion");
  33. auto xMotionData = aznew NonUniformMotionData();
  34. xmotion->SetMotionData(xMotionData);
  35. xmotion->SetFileName("xmotion.motion");
  36. const size_t xJointIndex = xMotionData->AddJoint("Joint", Transform::CreateIdentity(), Transform::CreateIdentity());
  37. xMotionData->AllocateJointPositionSamples(xJointIndex, 2);
  38. xMotionData->SetJointPositionSample(xJointIndex, 0, {0.0f, AZ::Vector3(0.0f, 0.0f, 0.0f)});
  39. xMotionData->SetJointPositionSample(xJointIndex, 1, {1.0f, AZ::Vector3(1.0f, 0.0f, 0.0f)});
  40. xMotionData->UpdateDuration();
  41. xmotion->GetEventTable()->AutoCreateSyncTrack(xmotion);
  42. xmotion->GetEventTable()->GetSyncTrack()->AddEvent(0.0f, GetEventManager().FindOrCreateEventData<TwoStringEventData>("leftFoot", ""));
  43. xmotion->GetEventTable()->GetSyncTrack()->AddEvent(0.5f, GetEventManager().FindOrCreateEventData<TwoStringEventData>("rightFoot", ""));
  44. Motion* ymotion = aznew Motion("ymotion");
  45. ymotion->SetFileName("ymotion.motion");
  46. auto yMotionData = aznew NonUniformMotionData();
  47. ymotion->SetMotionData(yMotionData);
  48. const size_t yJointIndex = yMotionData->AddJoint("Joint", Transform::CreateIdentity(), Transform::CreateIdentity());
  49. yMotionData->AllocateJointPositionSamples(yJointIndex, 2);
  50. yMotionData->SetJointPositionSample(yJointIndex, 0, {0.0f, AZ::Vector3(0.0f, 0.0f, 0.0f)});
  51. yMotionData->SetJointPositionSample(yJointIndex, 1, {1.0f, AZ::Vector3(0.0f, 1.0f, 0.0f)});
  52. yMotionData->UpdateDuration();
  53. ymotion->GetEventTable()->AutoCreateSyncTrack(ymotion);
  54. ymotion->GetEventTable()->GetSyncTrack()->AddEvent(0.25f, GetEventManager().FindOrCreateEventData<TwoStringEventData>("leftFoot", ""));
  55. ymotion->GetEventTable()->GetSyncTrack()->AddEvent(0.75f, GetEventManager().FindOrCreateEventData<TwoStringEventData>("rightFoot", ""));
  56. // Create anim graph
  57. AnimGraphMotionNode* motionX = aznew AnimGraphMotionNode();
  58. motionX->SetName("xmotion");
  59. motionX->SetMotionIds({"xmotion"});
  60. AnimGraphMotionNode* motionY = aznew AnimGraphMotionNode();
  61. motionY->SetName("ymotion");
  62. motionY->SetMotionIds({"ymotion"});
  63. AnimGraphTimeCondition* condition = aznew AnimGraphTimeCondition();
  64. condition->SetCountDownTime(0.0f);
  65. AnimGraphStateTransition* transition = aznew AnimGraphStateTransition();
  66. transition->SetSourceNode(motionX);
  67. transition->SetTargetNode(motionY);
  68. transition->SetBlendTime(5.0f);
  69. transition->AddCondition(condition);
  70. transition->SetSyncMode(AnimGraphStateTransition::SYNCMODE_TRACKBASED);
  71. AnimGraphStateMachine* rootState = aznew AnimGraphStateMachine();
  72. rootState->SetEntryState(motionX);
  73. rootState->AddChildNode(motionX);
  74. rootState->AddChildNode(motionY);
  75. rootState->AddTransition(transition);
  76. AZStd::unique_ptr<AnimGraph> animGraph = AZStd::make_unique<AnimGraph>();
  77. animGraph->SetRootStateMachine(rootState);
  78. animGraph->InitAfterLoading();
  79. // Create Motion Set
  80. MotionSet::MotionEntry* motionEntryX = aznew MotionSet::MotionEntry(xmotion->GetName(), xmotion->GetName(), xmotion);
  81. MotionSet::MotionEntry* motionEntryY = aznew MotionSet::MotionEntry(ymotion->GetName(), ymotion->GetName(), ymotion);
  82. AZStd::unique_ptr<MotionSet> motionSet = AZStd::make_unique<MotionSet>();
  83. const AZ::u32 motionSetId = 100;
  84. motionSet->SetID(motionSetId);
  85. motionSet->AddMotionEntry(motionEntryX);
  86. motionSet->AddMotionEntry(motionEntryY);
  87. // Create ActorInstance
  88. ActorInstance* actorInstance = ActorInstance::Create(actor.get());
  89. // Create AnimGraphInstance
  90. AnimGraphInstance* animGraphInstance = AnimGraphInstance::Create(animGraph.get(), actorInstance, motionSet.get());
  91. actorInstance->SetAnimGraphInstance(animGraphInstance);
  92. GetEMotionFX().Update(0.0f);
  93. GetEMotionFX().Update(0.5f);
  94. GetEMotionFX().Update(2.0f);
  95. EXPECT_THAT(
  96. rootState->GetActiveStates(animGraphInstance),
  97. ::testing::Pointwise(::testing::Eq(), AZStd::vector<AnimGraphNode*>{motionX, motionY})
  98. );
  99. AZStd::string result;
  100. EXPECT_TRUE(CommandSystem::GetCommandManager()->ExecuteCommand({"MotionSetRemoveMotion -motionSetID " + AZStd::to_string(motionSetId) + " -motionIds xmotion"}, result)) << result.c_str();
  101. EXPECT_TRUE(CommandSystem::GetCommandManager()->ExecuteCommand("RemoveMotion -filename xmotion.motion", result)) << result.c_str();
  102. GetEMotionFX().Update(0.5f);
  103. EXPECT_THAT(
  104. rootState->GetActiveStates(animGraphInstance),
  105. ::testing::Pointwise(::testing::Eq(), AZStd::vector<AnimGraphNode*>{motionX, motionY})
  106. );
  107. actorInstance->Destroy();
  108. }
  109. } // namespace EMotionFX