AnimGraphTransitionConditionFixture.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "AnimGraphTransitionConditionFixture.h"
  9. #include <EMotionFX/Source/Actor.h>
  10. #include <EMotionFX/Source/ActorInstance.h>
  11. #include <EMotionFX/Source/AnimGraph.h>
  12. #include <EMotionFX/Source/AnimGraphInstance.h>
  13. #include <EMotionFX/Source/AnimGraphMotionCondition.h>
  14. #include <EMotionFX/Source/AnimGraphMotionNode.h>
  15. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  16. #include <EMotionFX/Source/AnimGraphStateTransition.h>
  17. #include <EMotionFX/Source/MotionInstance.h>
  18. #include <EMotionFX/Source/MotionEventTable.h>
  19. #include <EMotionFX/Source/MotionEventTrack.h>
  20. #include <EMotionFX/Source/MotionSet.h>
  21. #include <EMotionFX/Source/Node.h>
  22. #include <EMotionFX/Source/Motion.h>
  23. #include <EMotionFX/Source/MotionData/NonUniformMotionData.h>
  24. #include <EMotionFX/Source/TwoStringEventData.h>
  25. #include <Tests/TestAssetCode/AnimGraphFactory.h>
  26. #include <Tests/TestAssetCode/ActorFactory.h>
  27. #include <Tests/TestAssetCode/SimpleActors.h>
  28. namespace EMotionFX
  29. {
  30. void AnimGraphTransitionConditionFixture::SetUp()
  31. {
  32. SystemComponentFixture::SetUp();
  33. // This test sets up an anim graph with 2 motions, each of which is 1
  34. // second long. There is a transition from the first to the second that
  35. // triggers when the first is complete, and takes 0.5 seconds to
  36. // transition, and an identical one going the other way. During the
  37. // transition, the weights of the motion states should add up to 1.
  38. m_actor = ActorFactory::CreateAndInit<SimpleJointChainActor>(1);
  39. m_animGraph = AnimGraphFactory::Create<TwoMotionNodeAnimGraph>();
  40. m_stateMachine = m_animGraph->GetRootStateMachine();
  41. m_motionNodeA = m_animGraph->GetMotionNodeA();
  42. m_motionNodeB = m_animGraph->GetMotionNodeB();
  43. m_transition = aznew AnimGraphStateTransition();
  44. m_transition->SetSourceNode(m_motionNodeA);
  45. m_transition->SetTargetNode(m_motionNodeB);
  46. m_stateMachine->AddTransition(m_transition);
  47. m_motionSet = aznew MotionSet("testMotionSet");
  48. for (int nodeIndex = 0; nodeIndex < 2; ++nodeIndex)
  49. {
  50. // The motion set keeps track of motions by their name. Each motion
  51. // within the motion set must have a unique name.
  52. AZStd::string motionId = AZStd::string::format("testSkeletalMotion%i", nodeIndex);
  53. Motion* motion = aznew Motion(motionId.c_str());
  54. motion->SetMotionData(aznew NonUniformMotionData());
  55. motion->GetMotionData()->SetDuration(1.0f);
  56. // 0.73 seconds would trigger frame 44 when sampling at 60 fps. The
  57. // event will be seen as triggered inside a motion condition, but a
  58. // frame later, at frame 45.
  59. motion->GetEventTable()->AddTrack(MotionEventTrack::Create("TestEventTrack", motion));
  60. AZStd::shared_ptr<const TwoStringEventData> data = EMotionFX::GetEventManager().FindOrCreateEventData<TwoStringEventData>("TestEvent", "TestParameter");
  61. AZStd::shared_ptr<const TwoStringEventData> rangeData = EMotionFX::GetEventManager().FindOrCreateEventData<TwoStringEventData>("TestRangeEvent", "TestParameter");
  62. motion->GetEventTable()->FindTrackByName("TestEventTrack")->AddEvent(0.73f, data);
  63. motion->GetEventTable()->FindTrackByName("TestEventTrack")->AddEvent(0.65f, 0.95f, rangeData);
  64. MotionSet::MotionEntry* motionEntry = aznew MotionSet::MotionEntry(motion->GetName(), motion->GetName(), motion);
  65. m_motionSet->AddMotionEntry(motionEntry);
  66. AnimGraphMotionNode* motionNode = (nodeIndex == 0) ? m_motionNodeA : m_motionNodeB;
  67. motionNode->SetName(motionId.c_str());
  68. motionNode->AddMotionId(motionId.c_str());
  69. // Disable looping of the motion nodes.
  70. motionNode->SetLoop(false);
  71. }
  72. // Allow subclasses to create any additional nodes before the animgraph is activated.
  73. AddNodesToAnimGraph();
  74. m_animGraph->InitAfterLoading();
  75. m_actorInstance = ActorInstance::Create(m_actor.get());
  76. m_animGraphInstance = AnimGraphInstance::Create(m_animGraph.get(), m_actorInstance, m_motionSet);
  77. m_actorInstance->SetAnimGraphInstance(m_animGraphInstance);
  78. }
  79. void AnimGraphTransitionConditionFixture::TearDown()
  80. {
  81. delete m_motionSet;
  82. m_motionSet = nullptr;
  83. if (m_actorInstance)
  84. {
  85. m_actorInstance->Destroy();
  86. m_actorInstance = nullptr;
  87. }
  88. m_actor.reset();
  89. m_animGraph.reset();
  90. SystemComponentFixture::TearDown();
  91. }
  92. } // namespace EMotionFX