BlendTreeMotionFrameNodeTests.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <Tests/AnimGraphFixture.h>
  9. #include <EMotionFX/Source/AnimGraph.h>
  10. #include <EMotionFX/Source/AnimGraphMotionNode.h>
  11. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  12. #include <EMotionFX/Source/BlendTree.h>
  13. #include <EMotionFX/Source/BlendTreeMotionFrameNode.h>
  14. #include <EMotionFX/Source/Motion.h>
  15. #include <EMotionFX/Source/MotionSet.h>
  16. #include <EMotionFX/Source/MotionData/NonUniformMotionData.h>
  17. namespace EMotionFX
  18. {
  19. class BlendTreeMotionFrameTests
  20. : public AnimGraphFixture
  21. {
  22. public:
  23. void ConstructGraph() override
  24. {
  25. AnimGraphFixture::ConstructGraph();
  26. m_blendTreeAnimGraph = AnimGraphFactory::Create<OneBlendTreeNodeAnimGraph>();
  27. m_rootStateMachine = m_blendTreeAnimGraph->GetRootStateMachine();
  28. m_blendTree = m_blendTreeAnimGraph->GetBlendTreeNode();
  29. /*
  30. +--------+ +--------------+ +------------+
  31. | Motion +--->+ Motion Frame +--->+ Final Node |
  32. +--------+ +--------------+ +------------+
  33. */
  34. m_motionFrameNode = aznew BlendTreeMotionFrameNode();
  35. m_blendTree->AddChildNode(m_motionFrameNode);
  36. BlendTreeFinalNode* finalNode = aznew BlendTreeFinalNode();
  37. m_blendTree->AddChildNode(finalNode);
  38. finalNode->AddConnection(m_motionFrameNode, BlendTreeMotionFrameNode::OUTPUTPORT_RESULT, BlendTreeFinalNode::PORTID_INPUT_POSE);
  39. m_motionNode = aznew AnimGraphMotionNode();
  40. m_blendTree->AddChildNode(m_motionNode);
  41. m_motionFrameNode->AddConnection(m_motionNode, AnimGraphMotionNode::PORTID_OUTPUT_MOTION, BlendTreeMotionFrameNode::INPUTPORT_MOTION);
  42. m_blendTreeAnimGraph->InitAfterLoading();
  43. }
  44. void SetUp() override
  45. {
  46. AnimGraphFixture::SetUp();
  47. m_animGraphInstance->Destroy();
  48. m_animGraphInstance = m_blendTreeAnimGraph->GetAnimGraphInstance(m_actorInstance, m_motionSet);
  49. const char* motionId = "Test Motion";
  50. Motion* motion = aznew Motion(motionId);
  51. motion->SetMotionData(aznew NonUniformMotionData());
  52. motion->GetMotionData()->SetDuration(m_motionDuration);
  53. MotionSet::MotionEntry* motionEntry = aznew MotionSet::MotionEntry(motionId, motionId, motion);
  54. m_motionSet->AddMotionEntry(motionEntry);
  55. m_motionNode->AddMotionId(motionId);
  56. m_motionNode->RecursiveOnChangeMotionSet(m_animGraphInstance, m_motionSet);
  57. m_motionNode->PickNewActiveMotion(m_animGraphInstance, static_cast<AnimGraphMotionNode::UniqueData*>(m_motionNode->FindOrCreateUniqueNodeData(m_animGraphInstance)));
  58. // The motion node creates its motion instance in the Output() call, which means that at the first
  59. // evaluation of the motion frame node the duration is 0.0 (due to the missing motion instance) and
  60. // that results in the normalized time also being 0.0 at that frame which we need for testing.
  61. GetEMotionFX().Update(0.0f);
  62. }
  63. void SetAndTestTimeValue(BlendTreeMotionFrameNode::UniqueData* uniqueData, float newNormalizedTime, bool rewind = false)
  64. {
  65. const float prevNewTime = uniqueData->m_newTime;
  66. m_motionFrameNode->SetNormalizedTimeValue(newNormalizedTime);
  67. if (rewind)
  68. {
  69. m_motionFrameNode->Rewind(m_animGraphInstance);
  70. }
  71. GetEMotionFX().Update(0.0f);
  72. EXPECT_EQ(m_motionFrameNode->GetNormalizedTimeValue(), newNormalizedTime);
  73. float expectedOldTime = prevNewTime;
  74. if (rewind)
  75. {
  76. if (m_motionFrameNode->GetEmitEventsFromStart())
  77. {
  78. expectedOldTime = 0.0f;
  79. }
  80. else
  81. {
  82. expectedOldTime = newNormalizedTime * m_motionDuration;
  83. }
  84. }
  85. EXPECT_EQ(uniqueData->m_oldTime, expectedOldTime);
  86. EXPECT_EQ(uniqueData->m_newTime, newNormalizedTime * m_motionDuration);
  87. }
  88. public:
  89. float m_motionDuration = 1.0f;
  90. AnimGraphMotionNode* m_motionNode = nullptr;
  91. BlendTreeMotionFrameNode* m_motionFrameNode = nullptr;
  92. BlendTreeMotionFrameNode::UniqueData* m_motionFrameNodeUniqueData = nullptr;
  93. BlendTree* m_blendTree = nullptr;
  94. };
  95. TEST_F(BlendTreeMotionFrameTests, SetNormalizedTime)
  96. {
  97. BlendTreeMotionFrameNode::UniqueData* uniqueData = static_cast<BlendTreeMotionFrameNode::UniqueData*>(m_motionFrameNode->FindOrCreateUniqueNodeData(m_animGraphInstance));
  98. SetAndTestTimeValue(uniqueData, 0.2f);
  99. SetAndTestTimeValue(uniqueData, 0.4f);
  100. SetAndTestTimeValue(uniqueData, 0.3f);
  101. SetAndTestTimeValue(uniqueData, 1.0f);
  102. SetAndTestTimeValue(uniqueData, 0.0f);
  103. }
  104. TEST_F(BlendTreeMotionFrameTests, RewindTest)
  105. {
  106. BlendTreeMotionFrameNode::UniqueData* uniqueData = static_cast<BlendTreeMotionFrameNode::UniqueData*>(m_motionFrameNode->FindOrCreateUniqueNodeData(m_animGraphInstance));
  107. SetAndTestTimeValue(uniqueData, 0.2f);
  108. SetAndTestTimeValue(uniqueData, 0.4f, true);
  109. }
  110. TEST_F(BlendTreeMotionFrameTests, RewindTest_SetEmitEventsFromStart)
  111. {
  112. BlendTreeMotionFrameNode::UniqueData* uniqueData = static_cast<BlendTreeMotionFrameNode::UniqueData*>(m_motionFrameNode->FindOrCreateUniqueNodeData(m_animGraphInstance));
  113. m_motionFrameNode->SetEmitEventsFromStart(true);
  114. SetAndTestTimeValue(uniqueData, 0.2f);
  115. SetAndTestTimeValue(uniqueData, 0.4f, true);
  116. }
  117. } // namespace EMotionFX