MotionExtractionBusTests.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/Component/TransformBus.h>
  9. #include <AzFramework/Components/TransformComponent.h>
  10. #include <EMotionFX/Source/ActorManager.h>
  11. #include <EMotionFX/Source/AnimGraphMotionNode.h>
  12. #include <EMotionFX/Source/MotionSet.h>
  13. #include <EMotionFX/Source/Motion.h>
  14. #include <Integration/Components/ActorComponent.h>
  15. #include <Integration/Components/AnimGraphComponent.h>
  16. #include <Integration/MotionExtractionBus.h>
  17. #include <Tests/Integration/EntityComponentFixture.h>
  18. #include <Tests/TestAssetCode/ActorFactory.h>
  19. #include <Tests/TestAssetCode/AnimGraphFactory.h>
  20. #include <Tests/TestAssetCode/JackActor.h>
  21. #include <Tests/TestAssetCode/TestActorAssets.h>
  22. #include <Tests/TestAssetCode/TestMotionAssets.h>
  23. namespace EMotionFX
  24. {
  25. class MotionExtractionTestBus
  26. : Integration::MotionExtractionRequestBus::Handler
  27. {
  28. public:
  29. MotionExtractionTestBus(AZ::EntityId entityId)
  30. {
  31. Integration::MotionExtractionRequestBus::Handler::BusConnect(entityId);
  32. }
  33. ~MotionExtractionTestBus()
  34. {
  35. Integration::MotionExtractionRequestBus::Handler::BusDisconnect();
  36. }
  37. MOCK_METHOD2(ExtractMotion, void(const AZ::Vector3&, float));
  38. };
  39. class MotionExtractionBusTests
  40. : public EntityComponentFixture
  41. {
  42. public:
  43. void SetUp() override
  44. {
  45. EntityComponentFixture::SetUp();
  46. m_entityId = AZ::EntityId(740216387);
  47. m_entity = AZStd::make_unique<AZ::Entity>(m_entityId);
  48. // Actor asset.
  49. AZ::Data::AssetId actorAssetId("{5060227D-B6F4-422E-BF82-41AAC5F228A5}");
  50. AZStd::unique_ptr<Actor> actor = ActorFactory::CreateAndInit<JackNoMeshesActor>();
  51. AZ::Data::Asset<Integration::ActorAsset> actorAsset = TestActorAssets::GetAssetFromActor(actorAssetId, AZStd::move(actor));
  52. Integration::ActorComponent::Configuration actorConf;
  53. actorConf.m_actorAsset = actorAsset;
  54. m_entity->CreateComponent<AzFramework::TransformComponent>();
  55. auto actorComponent = m_entity->CreateComponent<Integration::ActorComponent>(&actorConf);
  56. auto animGraphComponent = m_entity->CreateComponent<Integration::AnimGraphComponent>();
  57. m_entity->Init();
  58. // Anim graph asset.
  59. AZ::Data::AssetId animGraphAssetId("{37629818-5166-4B96-83F5-5818B6A1F449}");
  60. animGraphComponent->SetAnimGraphAssetId(animGraphAssetId);
  61. AZ::Data::Asset<Integration::AnimGraphAsset> animGraphAsset = AZ::Data::AssetManager::Instance().CreateAsset<Integration::AnimGraphAsset>(animGraphAssetId);
  62. AZStd::unique_ptr<TwoMotionNodeAnimGraph> motionNodeAnimGraph = AnimGraphFactory::Create<TwoMotionNodeAnimGraph>();
  63. m_animGraph = motionNodeAnimGraph.get();
  64. motionNodeAnimGraph.release();
  65. animGraphAsset.GetAs<Integration::AnimGraphAsset>()->SetData(m_animGraph);
  66. EXPECT_EQ(animGraphAsset.IsReady(), true) << "Anim graph asset is not ready yet.";
  67. animGraphComponent->OnAssetReady(animGraphAsset);
  68. // Motion set asset.
  69. AZ::Data::AssetId motionSetAssetId("{224BFF5F-D0AD-4216-9CEF-42F419CC6265}");
  70. animGraphComponent->SetMotionSetAssetId(motionSetAssetId);
  71. AZ::Data::Asset<Integration::MotionSetAsset> motionSetAsset = AZ::Data::AssetManager::Instance().CreateAsset<Integration::MotionSetAsset>(motionSetAssetId);
  72. m_motionSet = aznew MotionSet("motionSet");
  73. Motion* motion = TestMotionAssets::GetJackWalkForward();
  74. AddMotionEntry(motion, "jack_walk_forward_aim_zup");
  75. m_animGraph->GetMotionNodeA()->AddMotionId("jack_walk_forward_aim_zup");
  76. motionSetAsset.GetAs<Integration::MotionSetAsset>()->SetData(new MotionSet());
  77. EXPECT_EQ(motionSetAsset.IsReady(), true) << "Motion set asset is not ready yet.";
  78. animGraphComponent->OnAssetReady(motionSetAsset);
  79. m_entity->Activate();
  80. // Actor component
  81. actorComponent->SetActorAsset(actorAsset);
  82. }
  83. void AddMotionEntry(Motion* motion, const AZStd::string& motionId)
  84. {
  85. m_motion = motion;
  86. EMotionFX::MotionSet::MotionEntry* newMotionEntry = aznew EMotionFX::MotionSet::MotionEntry();
  87. newMotionEntry->SetMotion(m_motion);
  88. m_motionSet->AddMotionEntry(newMotionEntry);
  89. m_motionSet->SetMotionEntryId(newMotionEntry, motionId);
  90. }
  91. void TearDown() override
  92. {
  93. m_motionSet->Clear();
  94. m_motion->Destroy();
  95. delete m_motionSet;
  96. m_entity.reset();
  97. EntityComponentFixture::TearDown();
  98. }
  99. public:
  100. AZ::EntityId m_entityId;
  101. AZStd::unique_ptr<AZ::Entity> m_entity;
  102. MotionSet* m_motionSet = nullptr;
  103. Motion* m_motion = nullptr;
  104. TwoMotionNodeAnimGraph* m_animGraph = nullptr;
  105. };
  106. TEST_F(MotionExtractionBusTests, ExtractMotionTests)
  107. {
  108. MotionExtractionTestBus testBus(m_entityId);
  109. const float timeDelta = 0.5f;
  110. const ActorManager* actorManager = GetEMotionFX().GetActorManager();
  111. const ActorInstance* actorInstance = actorManager->GetActorInstance(0);
  112. bool hasCustomMotionExtractionController = Integration::MotionExtractionRequestBus::FindFirstHandler(m_entityId) != nullptr;
  113. EXPECT_TRUE(hasCustomMotionExtractionController) << "MotionExtractionBus is not found.";
  114. AZ::Transform currentTransform = AZ::Transform::CreateIdentity();
  115. AZ::TransformBus::EventResult(currentTransform, m_entityId, &AZ::TransformBus::Events::GetWorldTM);
  116. const AZ::Vector3 actorInstancePosition = actorInstance->GetWorldSpaceTransform().m_position;
  117. const AZ::Vector3 positionDelta = actorInstancePosition - currentTransform.GetTranslation();
  118. EXPECT_CALL(testBus, ExtractMotion(testing::_, testing::_));
  119. Integration::MotionExtractionRequestBus::Event(m_entityId, &Integration::MotionExtractionRequestBus::Events::ExtractMotion, positionDelta, timeDelta);
  120. }
  121. } // end namespace EMotionFX