3
0

ActorBuilderTests.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "InitSceneAPIFixture.h"
  9. #include <AzCore/std/smart_ptr/make_shared.h>
  10. #include <AzCore/std/smart_ptr/unique_ptr.h>
  11. #include <AzFramework/StringFunc/StringFunc.h>
  12. #include <AzToolsFramework/UI/PropertyEditor/PropertyManagerComponent.h>
  13. #include <SceneAPI/SceneCore/Mocks/Containers/MockScene.h>
  14. #include <SceneAPI/SceneCore/DataTypes/Rules/IMaterialRule.h>
  15. #include <SceneAPI/SceneData/GraphData/RootBoneData.h>
  16. #include <SceneAPI/SceneData/GraphData/MeshData.h>
  17. #include <SceneAPI/SceneData/GraphData/TransformData.h>
  18. #include <EMotionFX/Source/Node.h>
  19. #include <EMotionFX/Source/Actor.h>
  20. #include <EMotionFX/Source/Mesh.h>
  21. #include <EMotionFX/Pipeline/RCExt/Actor/ActorBuilder.h>
  22. #include <EMotionFX/Pipeline/RCExt/ExportContexts.h>
  23. #include <EMotionFX/Pipeline/SceneAPIExt/Groups/ActorGroup.h>
  24. #include <Integration/System/SystemCommon.h>
  25. #include <Tests/Matchers.h>
  26. #include <Tests/TestAssetCode/ActorFactory.h>
  27. #include <AzTest/Utils.h>
  28. namespace EMotionFX
  29. {
  30. // This fixture is responsible for creating the scene description used by
  31. // the actor builder pipeline tests
  32. using ActorBuilderPipelineFixtureBase = InitSceneAPIFixture<
  33. AZ::AssetManagerComponent,
  34. AZ::JobManagerComponent,
  35. AZ::StreamerComponent,
  36. AzToolsFramework::Components::PropertyManagerComponent,
  37. EMotionFX::Integration::SystemComponent,
  38. EMotionFX::Pipeline::ActorBuilder
  39. >;
  40. class ActorBuilderPipelineFixture
  41. : public ActorBuilderPipelineFixtureBase
  42. {
  43. public:
  44. void SetUp() override
  45. {
  46. ActorBuilderPipelineFixtureBase::SetUp();
  47. m_actor = EMotionFX::ActorFactory::CreateAndInit<Actor>("testActor");
  48. // Set up the scene graph
  49. m_scene = new AZ::SceneAPI::Containers::MockScene("MockScene");
  50. m_scene->SetOriginalSceneOrientation(AZ::SceneAPI::Containers::Scene::SceneOrientation::ZUp);
  51. AZStd::string testSourceFile;
  52. AzFramework::StringFunc::Path::Join(this->GetAssetFolder().c_str(), "TestFile.fbx", testSourceFile);
  53. AzFramework::StringFunc::Path::Normalize(testSourceFile);
  54. m_scene->SetSource(testSourceFile, AZ::Uuid::CreateRandom());
  55. }
  56. AZ::SceneAPI::Events::ProcessingResult Process(EMotionFX::Pipeline::Group::ActorGroup& actorGroup)
  57. {
  58. AZ::SceneAPI::Events::ProcessingResultCombiner result;
  59. AZStd::string workingDir = this->GetAssetFolder();
  60. AZStd::vector<AZStd::string> empty;
  61. EMotionFX::Pipeline::ActorBuilderContext actorBuilderContext(*m_scene, workingDir, actorGroup, m_actor.get(), empty, AZ::RC::Phase::Construction);
  62. result += AZ::SceneAPI::Events::Process(actorBuilderContext);
  63. result += AZ::SceneAPI::Events::Process<EMotionFX::Pipeline::ActorBuilderContext>(actorBuilderContext, AZ::RC::Phase::Filling);
  64. result += AZ::SceneAPI::Events::Process<EMotionFX::Pipeline::ActorBuilderContext>(actorBuilderContext, AZ::RC::Phase::Finalizing);
  65. return result.GetResult();
  66. }
  67. void TearDown() override
  68. {
  69. m_actor.reset();
  70. delete m_scene;
  71. m_scene = nullptr;
  72. ActorBuilderPipelineFixtureBase::TearDown();
  73. }
  74. void ProcessScene()
  75. {
  76. // Set up the actor group, which controls which parts of the scene graph
  77. // are used to generate the actor
  78. EMotionFX::Pipeline::Group::ActorGroup actorGroup;
  79. actorGroup.SetName("testActor");
  80. actorGroup.SetSelectedRootBone("root_joint");
  81. const AZ::SceneAPI::Events::ProcessingResult result = Process(actorGroup);
  82. ASSERT_EQ(result, AZ::SceneAPI::Events::ProcessingResult::Success) << "Failed to build actor";
  83. }
  84. AZStd::unique_ptr<Actor> m_actor;
  85. AZ::SceneAPI::Containers::Scene* m_scene = nullptr;
  86. };
  87. TEST_F(ActorBuilderPipelineFixture, ActorBuilder_Basic_Three_Joint)
  88. {
  89. // Set up a scene graph like this for testing
  90. // root_joint
  91. // |____joint_1
  92. // |____joint_2
  93. using SceneGraph = AZ::SceneAPI::Containers::SceneGraph;
  94. using namespace AZ::SceneData::GraphData;
  95. SceneGraph& graph = m_scene->GetGraph();
  96. AZStd::shared_ptr<BoneData> boneData = AZStd::make_shared<BoneData>();
  97. const SceneGraph::NodeIndex rootJointIndex = graph.AddChild(graph.GetRoot(), "root_joint", boneData);
  98. const SceneGraph::NodeIndex joint1Index = graph.AddChild(rootJointIndex, "joint_1", boneData);
  99. graph.AddChild(joint1Index, "joint_2", boneData);
  100. ProcessScene();
  101. ASSERT_EQ(m_actor->GetNumNodes(), 3);
  102. const Node* rootJoint = m_actor->GetSkeleton()->FindNodeByName("root_joint");
  103. const Node* joint1 = m_actor->GetSkeleton()->FindNodeByName("joint_1");
  104. const Node* joint2 = m_actor->GetSkeleton()->FindNodeByName("joint_2");
  105. EXPECT_TRUE(rootJoint);
  106. EXPECT_TRUE(rootJoint->GetIsRootNode());
  107. EXPECT_TRUE(joint1);
  108. EXPECT_TRUE(joint2);
  109. EXPECT_EQ(joint1->GetParentNode(), rootJoint);
  110. EXPECT_EQ(joint2->GetParentNode(), joint1);
  111. }
  112. TEST_F(ActorBuilderPipelineFixture, ActorBuilder_Basic_Mesh)
  113. {
  114. // Set up a scene graph like this for testing
  115. // root_joint
  116. // |____joint_1
  117. // |____mesh_1
  118. using SceneGraph = AZ::SceneAPI::Containers::SceneGraph;
  119. using namespace AZ::SceneData::GraphData;
  120. SceneGraph& graph = m_scene->GetGraph();
  121. AZStd::shared_ptr<BoneData> boneData = AZStd::make_shared<BoneData>();
  122. const SceneGraph::NodeIndex rootJointIndex = graph.AddChild(graph.GetRoot(), "root_joint", boneData);
  123. const SceneGraph::NodeIndex joint1Index = graph.AddChild(rootJointIndex, "joint_1", boneData);
  124. AZStd::shared_ptr<MeshData> meshData = AZStd::make_shared<MeshData>();
  125. graph.AddChild(joint1Index, "mesh_1", meshData);
  126. ProcessScene();
  127. // NOTE: End point mesh node should be skipped in the emfx skeleton structure.
  128. ASSERT_EQ(m_actor->GetNumNodes(), 2);
  129. const Node* rootJoint = m_actor->GetSkeleton()->FindNodeByName("root_joint");
  130. const Node* joint1 = m_actor->GetSkeleton()->FindNodeByName("joint_1");
  131. const Node* mesh1 = m_actor->GetSkeleton()->FindNodeByName("mesh_1");
  132. EXPECT_TRUE(rootJoint);
  133. EXPECT_TRUE(joint1);
  134. EXPECT_TRUE(!mesh1);
  135. }
  136. TEST_F(ActorBuilderPipelineFixture, ActorBuilder_Basic_Mesh_Chained)
  137. {
  138. // Set up a scene graph like this for testing
  139. // root_joint
  140. // |____joint_1
  141. // |____mesh_1
  142. // |____joint_2
  143. using SceneGraph = AZ::SceneAPI::Containers::SceneGraph;
  144. using namespace AZ::SceneData::GraphData;
  145. SceneGraph& graph = m_scene->GetGraph();
  146. AZStd::shared_ptr<BoneData> boneData = AZStd::make_shared<BoneData>();
  147. const SceneGraph::NodeIndex rootJointIndex = graph.AddChild(graph.GetRoot(), "root_joint", boneData);
  148. const SceneGraph::NodeIndex joint1Index = graph.AddChild(rootJointIndex, "joint_1", boneData);
  149. AZStd::shared_ptr<MeshData> meshData = AZStd::make_shared<MeshData>();
  150. SceneGraph::NodeIndex meshIndex = graph.AddChild(joint1Index, "mesh_1", meshData);
  151. graph.AddChild(meshIndex, "joint_2", boneData);
  152. ProcessScene();
  153. // NOTE: Mesh node that's part of the chain should NOT be skipped in the emfx skeleton structure.
  154. ASSERT_EQ(m_actor->GetNumNodes(), 4);
  155. const Node* rootJoint = m_actor->GetSkeleton()->FindNodeByName("root_joint");
  156. const Node* joint1 = m_actor->GetSkeleton()->FindNodeByName("joint_1");
  157. const Node* mesh1 = m_actor->GetSkeleton()->FindNodeByName("mesh_1");
  158. const Node* joint2 = m_actor->GetSkeleton()->FindNodeByName("joint_2");
  159. EXPECT_TRUE(rootJoint);
  160. EXPECT_TRUE(joint1);
  161. EXPECT_TRUE(mesh1);
  162. EXPECT_TRUE(joint2);
  163. EXPECT_EQ(mesh1->GetParentNode(), joint1);
  164. EXPECT_EQ(joint2->GetParentNode(), mesh1);
  165. }
  166. class ActorBuilderPipelineTransformTestFixture : public ActorBuilderPipelineFixture
  167. , public ::testing::WithParamInterface<AZ::Matrix3x4>
  168. {
  169. };
  170. static const AZ::Matrix3x4 Matrix3x4s[] = {
  171. AZ::Matrix3x4::CreateIdentity(),
  172. AZ::Matrix3x4::CreateRotationX(-0.6f),
  173. AZ::Matrix3x4::CreateFromQuaternion(AZ::Quaternion(0.24f, -0.08f, -0.48f, 0.84f)),
  174. AZ::Matrix3x4::CreateTranslation(AZ::Vector3(7.9f, 2.4f, -4.6f)),
  175. AZ::Matrix3x4::CreateFromQuaternionAndTranslation(AZ::Quaternion(0.12f, -0.24f, -0.72f, 0.64f), AZ::Vector3(2.3f, -5.2f, 0.7f))
  176. };
  177. TEST_P(ActorBuilderPipelineTransformTestFixture, ActorBuilder_Transforms)
  178. {
  179. // Set up a scene graph like this for testing
  180. // root_joint
  181. // |____TransformData
  182. using SceneGraph = AZ::SceneAPI::Containers::SceneGraph;
  183. using RootBoneData = AZ::SceneData::GraphData::RootBoneData;
  184. using GraphTransformData = AZ::SceneData::GraphData::TransformData;
  185. SceneGraph& graph = m_scene->GetGraph();
  186. const AZ::Matrix3x4 globalTransform = GetParam();
  187. AZStd::shared_ptr<RootBoneData> rootBoneData = AZStd::make_shared<RootBoneData>();
  188. const SceneGraph::NodeIndex rootJointIndex = graph.AddChild(graph.GetRoot(), "root_joint", rootBoneData);
  189. AZStd::shared_ptr<GraphTransformData> transformData = AZStd::make_shared<GraphTransformData>();
  190. transformData->SetMatrix(globalTransform);
  191. const SceneGraph::NodeIndex transformIndex = graph.AddChild(rootJointIndex, "transform", transformData);
  192. graph.MakeEndPoint(transformIndex);
  193. ProcessScene();
  194. ASSERT_EQ(m_actor->GetNumNodes(), 1);
  195. const EMotionFX::Pose* pose = m_actor->GetBindPose();
  196. AZ::Transform emfxLocal = pose->GetLocalSpaceTransform(0).ToAZTransform();
  197. AZ::Transform builderLocal = AZ::Transform::CreateFromMatrix3x4(globalTransform);
  198. EXPECT_TRUE(emfxLocal.IsClose(builderLocal));
  199. }
  200. INSTANTIATE_TEST_CASE_P(ActorBuilder_Transforms, ActorBuilderPipelineTransformTestFixture, ::testing::ValuesIn(EMotionFX::Matrix3x4s));
  201. } // namespace EMotionFX