BlendSpaceFixture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/shared_ptr.h>
  9. #include <EMotionFX/Source/Parameter/ParameterFactory.h>
  10. #include <EMotionFX/Source/MotionEventTrack.h>
  11. #include <EMotionFX/Source/MotionEventTable.h>
  12. #include <EMotionFX/Source/Motion.h>
  13. #include <EMotionFX/Source/MotionData/NonUniformMotionData.h>
  14. #include <EMotionFX/Source/TwoStringEventData.h>
  15. #include <Tests/BlendSpaceFixture.h>
  16. namespace EMotionFX
  17. {
  18. AZ_CLASS_ALLOCATOR_IMPL(BlendSpaceFixture::TestEventHandler, EventHandlerAllocator)
  19. void BlendSpaceFixture::ConstructGraph()
  20. {
  21. AnimGraphFixture::ConstructGraph();
  22. m_blendTree = aznew BlendTree();
  23. m_rootStateMachine->AddChildNode(m_blendTree);
  24. m_rootStateMachine->SetEntryState(m_blendTree);
  25. m_finalNode = aznew BlendTreeFinalNode();
  26. m_blendTree->AddChildNode(m_finalNode);
  27. }
  28. void BlendSpaceFixture::AddEvent(Motion* motion, float time)
  29. {
  30. MotionEventTrack* eventTrack = MotionEventTrack::Create(motion);
  31. AZStd::shared_ptr<const TwoStringEventData> eventData = EMotionFX::GetEventManager().FindOrCreateEventData<TwoStringEventData>("TestEvent", motion->GetName());
  32. eventTrack->AddEvent(time, eventData);
  33. motion->GetEventTable()->AddTrack(eventTrack);
  34. }
  35. size_t BlendSpaceFixture::CreateSubMotion(Motion* motion, const std::string& name, const Transform& transform)
  36. {
  37. return motion->GetMotionData()->AddJoint(name.c_str(), transform, transform);
  38. }
  39. void BlendSpaceFixture::CreateMotions()
  40. {
  41. // Idle motion
  42. {
  43. Motion* motion = aznew Motion("Idle");
  44. auto motionData = aznew NonUniformMotionData();
  45. motion->SetMotionData(motionData);
  46. const size_t motionJointIndex = CreateSubMotion(motion, m_rootJointName.c_str(), Transform::CreateIdentity());
  47. motionData->AllocateJointPositionSamples(motionJointIndex, 2);
  48. motionData->SetJointPositionSample(motionJointIndex, 0, {0.0f, AZ::Vector3(0.0f, 0.0f, 0.0f)});
  49. motionData->SetJointPositionSample(motionJointIndex, 1, {1.0f, AZ::Vector3(0.0f, 0.0f, 0.0f)});
  50. m_motions.emplace_back(motion);
  51. m_idleMotion = motion;
  52. }
  53. // Forward motion
  54. {
  55. Motion* motion = aznew Motion("Forward");
  56. auto motionData = aznew NonUniformMotionData();
  57. motion->SetMotionData(motionData);
  58. const size_t motionJointIndex = CreateSubMotion(motion, m_rootJointName.c_str(), Transform::CreateIdentity());
  59. motionData->AllocateJointPositionSamples(motionJointIndex, 2);
  60. motionData->SetJointPositionSample(motionJointIndex, 0, {0.0f, AZ::Vector3(0.0f, 0.0f, 0.0f)});
  61. motionData->SetJointPositionSample(motionJointIndex, 1, {1.0f, AZ::Vector3(0.0f, 1.0f, 0.0f)});
  62. m_motions.emplace_back(motion);
  63. m_forwardMotion = motion;
  64. }
  65. // Run motion
  66. {
  67. Motion* motion = aznew Motion("Run");
  68. auto motionData = aznew NonUniformMotionData();
  69. motion->SetMotionData(motionData);
  70. const size_t motionJointIndex = CreateSubMotion(motion, m_rootJointName.c_str(), Transform::CreateIdentity());
  71. motionData->AllocateJointPositionSamples(motionJointIndex, 2);
  72. motionData->SetJointPositionSample(motionJointIndex, 0, {0.0f, AZ::Vector3(0.0f, 0.0f, 0.0f)});
  73. motionData->SetJointPositionSample(motionJointIndex, 1, {1.0f, AZ::Vector3(0.0f, 2.0f, 0.0f)});
  74. m_motions.emplace_back(motion);
  75. m_runMotion = motion;
  76. }
  77. // Strafe motion
  78. {
  79. Motion* motion = aznew Motion("Strafe");
  80. auto motionData = aznew NonUniformMotionData();
  81. motion->SetMotionData(motionData);
  82. const size_t motionJointIndex = CreateSubMotion(motion, m_rootJointName.c_str(), Transform::CreateIdentity());
  83. motionData->AllocateJointPositionSamples(motionJointIndex, 2);
  84. motionData->SetJointPositionSample(motionJointIndex, 0, {0.0f, AZ::Vector3(0.0f, 0.0f, 0.0f)});
  85. motionData->SetJointPositionSample(motionJointIndex, 1, {1.0f, AZ::Vector3(1.0f, 0.0f, 0.0f)});
  86. m_motions.emplace_back(motion);
  87. m_strafeMotion = motion;
  88. }
  89. // Rotate left motion
  90. {
  91. Motion* motion = aznew Motion("Rotate Left");
  92. auto motionData = aznew NonUniformMotionData();
  93. motion->SetMotionData(motionData);
  94. const size_t motionJointIndex = CreateSubMotion(motion, m_rootJointName.c_str(), Transform::CreateIdentity());
  95. motionData->AllocateJointRotationSamples(motionJointIndex, 2);
  96. motionData->SetJointRotationSample(motionJointIndex, 0, {0.0f, AZ::Quaternion::CreateIdentity()});
  97. motionData->SetJointRotationSample(motionJointIndex, 1, {1.0f, AZ::Quaternion::CreateRotationZ(0.5f)});
  98. m_motions.emplace_back(motion);
  99. m_rotateLeftMotion = motion;
  100. }
  101. // Forward strafe 45 deg
  102. {
  103. Motion* motion = aznew Motion("Forward strafe 45 deg");
  104. auto motionData = aznew NonUniformMotionData();
  105. motion->SetMotionData(motionData);
  106. const size_t motionJointIndex = CreateSubMotion(motion, m_rootJointName.c_str(), Transform::CreateIdentity());
  107. motionData->AllocateJointPositionSamples(motionJointIndex, 2);
  108. motionData->SetJointPositionSample(motionJointIndex, 0, {0.0f, AZ::Vector3(0.0f, 0.0f, 0.0f)});
  109. motionData->SetJointPositionSample(motionJointIndex, 1, {1.0f, AZ::Vector3(1.0f, 1.0f, 0.0f)});
  110. m_motions.emplace_back(motion);
  111. m_forwardStrafe45 = motion;
  112. }
  113. // Forward slope 45 deg
  114. {
  115. Motion* motion = aznew Motion("Forward slope 45 deg");
  116. auto motionData = aznew NonUniformMotionData();
  117. motion->SetMotionData(motionData);
  118. const size_t motionJointIndex = CreateSubMotion(motion, m_rootJointName.c_str(), Transform::CreateIdentity());
  119. motionData->AllocateJointPositionSamples(motionJointIndex, 2);
  120. motionData->SetJointPositionSample(motionJointIndex, 0, {0.0f, AZ::Vector3(0.0f, 0.0f, 0.0f)});
  121. motionData->SetJointPositionSample(motionJointIndex, 1, {1.0f, AZ::Vector3(0.0f, 1.0f, 1.0f)});
  122. m_motions.emplace_back(motion);
  123. m_forwardSlope45 = motion;
  124. }
  125. for (Motion* motion : m_motions)
  126. {
  127. AddEvent(motion, 0.1f);
  128. motion->UpdateDuration();
  129. MotionSet::MotionEntry* motionEntry = aznew MotionSet::MotionEntry(motion->GetName(), motion->GetName(), motion);
  130. m_motionSet->AddMotionEntry(motionEntry);
  131. }
  132. }
  133. MotionSet::MotionEntry* BlendSpaceFixture::FindMotionEntry(Motion* motion) const
  134. {
  135. MotionSet::MotionEntry* entry = m_motionSet->FindMotionEntry(motion);
  136. EXPECT_NE(entry, nullptr) << "Cannot find motion entry for motion " << motion->GetName() << ".";
  137. return entry;
  138. }
  139. void BlendSpaceFixture::SetUp()
  140. {
  141. AnimGraphFixture::SetUp();
  142. m_eventHandler = aznew TestEventHandler();
  143. EMotionFX::GetEventManager().AddEventHandler(m_eventHandler);
  144. Node* rootJoint = m_actor->GetSkeleton()->GetNode(0);
  145. m_rootJointName = rootJoint->GetName();
  146. m_actor->SetMotionExtractionNode(rootJoint);
  147. CreateMotions();
  148. }
  149. void BlendSpaceFixture::TearDown()
  150. {
  151. for (Motion* motion : m_motions)
  152. {
  153. motion->Destroy();
  154. }
  155. m_motions.clear();
  156. EMotionFX::GetEventManager().RemoveEventHandler(m_eventHandler);
  157. delete m_eventHandler;
  158. AnimGraphFixture::TearDown();
  159. }
  160. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  161. void BlendSpace1DFixture::ConstructGraph()
  162. {
  163. BlendSpaceFixture::ConstructGraph();
  164. m_blendSpace1DNode = aznew BlendSpace1DNode();
  165. m_blendTree->AddChildNode(m_blendSpace1DNode);
  166. m_floatNodeX = aznew BlendTreeFloatConstantNode();
  167. m_blendTree->AddChildNode(m_floatNodeX);
  168. m_finalNode->AddConnection(m_blendSpace1DNode, BlendSpace1DNode::PORTID_OUTPUT_POSE, BlendTreeFinalNode::PORTID_INPUT_POSE);
  169. m_blendSpace1DNode->AddConnection(m_floatNodeX, BlendTreeFloatConstantNode::OUTPUTPORT_RESULT, BlendSpace1DNode::INPUTPORT_VALUE);
  170. }
  171. void BlendSpace1DFixture::SetUp()
  172. {
  173. BlendSpaceFixture::SetUp();
  174. m_blendSpace1DNode->SetEvaluatorType(azrtti_typeid<BlendSpaceMoveSpeedParamEvaluator>());
  175. m_blendSpace1DNode->SetMotions({
  176. FindMotionEntry(m_idleMotion)->GetId(),
  177. FindMotionEntry(m_forwardMotion)->GetId(),
  178. FindMotionEntry(m_runMotion)->GetId()
  179. });
  180. m_blendSpace1DNode->Reinit();
  181. GetEMotionFX().Update(0.0f);
  182. }
  183. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  184. void BlendSpace2DFixture::ConstructGraph()
  185. {
  186. BlendSpaceFixture::ConstructGraph();
  187. m_blendSpace2DNode = aznew BlendSpace2DNode();
  188. m_blendTree->AddChildNode(m_blendSpace2DNode);
  189. m_floatNodeX = aznew BlendTreeFloatConstantNode();
  190. m_blendTree->AddChildNode(m_floatNodeX);
  191. m_floatNodeY = aznew BlendTreeFloatConstantNode();
  192. m_blendTree->AddChildNode(m_floatNodeY);
  193. m_finalNode->AddConnection(m_blendSpace2DNode, BlendSpace2DNode::PORTID_OUTPUT_POSE, BlendTreeFinalNode::PORTID_INPUT_POSE);
  194. m_blendSpace2DNode->AddConnection(m_floatNodeX, BlendTreeFloatConstantNode::OUTPUTPORT_RESULT, BlendSpace2DNode::INPUTPORT_XVALUE);
  195. m_blendSpace2DNode->AddConnection(m_floatNodeY, BlendTreeFloatConstantNode::OUTPUTPORT_RESULT, BlendSpace2DNode::INPUTPORT_YVALUE);
  196. }
  197. void BlendSpace2DFixture::SetUp()
  198. {
  199. BlendSpaceFixture::SetUp();
  200. m_blendSpace2DNode->SetEvaluatorTypeX(azrtti_typeid<BlendSpaceMoveSpeedParamEvaluator>());
  201. m_blendSpace2DNode->SetEvaluatorTypeY(azrtti_typeid<BlendSpaceLeftRightVelocityParamEvaluator>());
  202. m_blendSpace2DNode->SetMotions({
  203. FindMotionEntry(m_idleMotion)->GetId(),
  204. FindMotionEntry(m_forwardMotion)->GetId(),
  205. FindMotionEntry(m_runMotion)->GetId(),
  206. FindMotionEntry(m_strafeMotion)->GetId()
  207. });
  208. m_blendSpace2DNode->Reinit();
  209. GetEMotionFX().Update(0.0f);
  210. }
  211. }