AnimGraphEventTests.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <EMotionFX/Source/AnimGraphMotionNode.h>
  9. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  10. #include <EMotionFX/Source/MotionSet.h>
  11. #include <Tests/AnimGraphEventHandlerCounter.h>
  12. #include <Tests/AnimGraphFixture.h>
  13. namespace EMotionFX
  14. {
  15. struct AnimGraphEventTestParm
  16. {
  17. int m_numStates;
  18. float m_transitionBlendTime;
  19. float m_conditionCountDownTime;
  20. float m_simulationTime;
  21. float m_expectedFps;
  22. float m_fpsVariance;
  23. };
  24. class AnimGraphEventTestFixture : public AnimGraphFixture
  25. , public ::testing::WithParamInterface<AnimGraphEventTestParm>
  26. {
  27. public:
  28. void ConstructGraph() override
  29. {
  30. const AnimGraphEventTestParm& params = GetParam();
  31. AnimGraphFixture::ConstructGraph();
  32. /*
  33. +-------+ +---+ +---+ +---+
  34. | Start |--->| A |--->| B |---> ... --->| N |
  35. +-------+ +---+ +---+ +---+
  36. */
  37. AnimGraphNode* stateStart = aznew AnimGraphMotionNode();
  38. stateStart->SetName("Start");
  39. m_rootStateMachine->AddChildNode(stateStart);
  40. m_rootStateMachine->SetEntryState(stateStart);
  41. const char startChar = 'A';
  42. AnimGraphNode* prevState = stateStart;
  43. for (int i = 0; i < params.m_numStates; ++i)
  44. {
  45. AnimGraphNode* state = aznew AnimGraphMotionNode();
  46. state->SetName(AZStd::string(1, static_cast<char>(startChar + i)).c_str());
  47. m_rootStateMachine->AddChildNode(state);
  48. AddTransitionWithTimeCondition(prevState, state, /*blendTime*/params.m_transitionBlendTime, /*countDownTime*/params.m_conditionCountDownTime);
  49. prevState = state;
  50. }
  51. }
  52. void SetUp() override
  53. {
  54. AnimGraphFixture::SetUp();
  55. MotionSet::MotionEntry* motionEntry = AddMotionEntry("testMotion", 1.0);
  56. // Assign a motion to all our motion nodes
  57. const size_t numStates = m_rootStateMachine->GetNumChildNodes();
  58. for (size_t i = 0; i < numStates; ++i)
  59. {
  60. AnimGraphMotionNode* motionNode = azdynamic_cast<AnimGraphMotionNode*>(m_rootStateMachine->GetChildNode(i));
  61. if (motionNode)
  62. {
  63. motionNode->AddMotionId(motionEntry->GetId());
  64. }
  65. }
  66. m_eventHandler = aznew AnimGraphEventHandlerCounter();
  67. m_animGraphInstance->AddEventHandler(m_eventHandler);
  68. }
  69. void TearDown() override
  70. {
  71. m_animGraphInstance->RemoveEventHandler(m_eventHandler);
  72. delete m_eventHandler;
  73. AnimGraphFixture::TearDown();
  74. }
  75. void SimulateTest(float simulationTime, float expectedFps, float fpsVariance)
  76. {
  77. Simulate(simulationTime, expectedFps, fpsVariance,
  78. /*preCallback*/[](AnimGraphInstance*)
  79. {
  80. },
  81. /*postCallback*/[this](AnimGraphInstance*)
  82. {
  83. const int numStates = GetParam().m_numStates;
  84. const int numExpectedEvents = numStates + 1 /*numDeferEnter*/; // +1 because we defer the entering to entrance state at the beginning of update.
  85. EXPECT_EQ(this->m_eventHandler->m_numStatesEntering, numExpectedEvents);
  86. EXPECT_EQ(this->m_eventHandler->m_numStatesEntered, numExpectedEvents);
  87. EXPECT_EQ(this->m_eventHandler->m_numStatesExited, numExpectedEvents);
  88. EXPECT_EQ(this->m_eventHandler->m_numStatesEnded, numExpectedEvents);
  89. EXPECT_EQ(this->m_eventHandler->m_numTransitionsStarted, numStates);
  90. EXPECT_EQ(this->m_eventHandler->m_numTransitionsEnded, numStates);
  91. },
  92. /*preUpdateCallback*/[](AnimGraphInstance*, float, float, int) {},
  93. /*postUpdateCallback*/[](AnimGraphInstance*, float, float, int) {});
  94. const int numStates = GetParam().m_numStates;
  95. if (numStates > 1)
  96. {
  97. // Rewind state machine and check the event numbers again
  98. m_rootStateMachine->Rewind(m_animGraphInstance);
  99. GetEMotionFX().Update(1.0f/expectedFps);
  100. const int numExpectedEvents = numStates + 1 /*rewind*/ + 2 /*numDeferEnter*/;
  101. EXPECT_EQ(this->m_eventHandler->m_numStatesEntering, numExpectedEvents);
  102. EXPECT_EQ(this->m_eventHandler->m_numStatesEntered, numExpectedEvents);
  103. EXPECT_EQ(this->m_eventHandler->m_numStatesExited, numExpectedEvents);
  104. EXPECT_EQ(this->m_eventHandler->m_numStatesEnded, numExpectedEvents);
  105. EXPECT_EQ(this->m_eventHandler->m_numTransitionsStarted, numStates);
  106. EXPECT_EQ(this->m_eventHandler->m_numTransitionsEnded, numStates);
  107. }
  108. }
  109. AnimGraphEventHandlerCounter* m_eventHandler = nullptr;
  110. };
  111. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  112. std::vector<AnimGraphEventTestParm> animGraphEventTestData
  113. {
  114. // Stable framerate
  115. {
  116. 1,/*m_numStates*/
  117. 1.0f,/*m_transitionBlendTime*/
  118. 1.0f,/*m_conditionCountDownTime*/
  119. 20.0f,/*m_simulationTime*/
  120. 60.0f,/*m_expectedFps*/
  121. 0.0f/*m_fpsVariance*/
  122. },
  123. {
  124. 2,
  125. 1.0f,
  126. 1.0f,
  127. 20.0f,
  128. 60.0f,
  129. 0.0f
  130. },
  131. {
  132. 3,
  133. 1.0f,
  134. 1.0f,
  135. 60.0f,
  136. 60.0f,
  137. 0.0f
  138. },
  139. {
  140. 8,
  141. 0.1f,
  142. 0.1f,
  143. 60.0f,
  144. 60.0f,
  145. 0.0f
  146. },
  147. {
  148. 16,
  149. 1.0f,
  150. 1.0f,
  151. 60.0f,
  152. 60.0f,
  153. 0.0f
  154. },
  155. // Unstable framerates
  156. {
  157. 16,
  158. 1.0f,
  159. 1.0f,
  160. 60.0f,
  161. 30.0f,
  162. 1.0f
  163. },
  164. {
  165. 16,
  166. 1.0f,
  167. 1.0f,
  168. 60.0f,
  169. 10.0f,
  170. 1.0f
  171. }
  172. };
  173. TEST_P(AnimGraphEventTestFixture, TestAnimGraphEvents)
  174. {
  175. const AnimGraphEventTestParm& params = GetParam();
  176. SimulateTest(params.m_simulationTime, params.m_expectedFps, params.m_fpsVariance);
  177. }
  178. INSTANTIATE_TEST_CASE_P(TestAnimGraphEvents,
  179. AnimGraphEventTestFixture,
  180. ::testing::ValuesIn(animGraphEventTestData));
  181. } // EMotionFX