3
0

AutoSkeletonLODTests.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/SystemComponentFixture.h>
  9. #include <Tests/TestAssetCode/SimpleActors.h>
  10. #include <Tests/TestAssetCode/ActorFactory.h>
  11. #include <EMotionFX/Source/Actor.h>
  12. #include <EMotionFX/Source/ActorInstance.h>
  13. #include <EMotionFX/Source/Node.h>
  14. #include <EMotionFX/Source/Mesh.h>
  15. #include <EMotionFX/Source/SubMesh.h>
  16. #include <EMotionFX/Source/SkinningInfoVertexAttributeLayer.h>
  17. #include <EMotionFX/Source/Skeleton.h>
  18. #include <vector>
  19. #include <string>
  20. namespace EMotionFX
  21. {
  22. struct AutoLODTestParams
  23. {
  24. std::vector<AZ::u32> m_skinningJointIndices; // The joint indices used while skinning.
  25. std::vector<std::string> m_criticalJoints; // The list of critical joints
  26. std::vector<AZ::u32> m_expectedEnabledJointIndices; // The expected enabled joints.
  27. };
  28. class AutoSkeletonLODActor
  29. : public SimpleJointChainActor
  30. {
  31. /*
  32. This creates an Actor with following hierarchy.
  33. The numbers are the joint indices.
  34. 5
  35. /
  36. /
  37. 0-----1-----2-----3-----4
  38. \
  39. \
  40. 6
  41. 7 (a node with skinned mesh)
  42. The mesh is on node 7, which is also a root node, just like joint number 0.
  43. We (fake) skin the first six joints to the mesh of node 7.
  44. Our test will actually skin to only a selection of these first seven joints.
  45. We then test which joints get disabled and which not.
  46. */
  47. public:
  48. explicit AutoSkeletonLODActor(AZ::u32 numSubMeshJoints)
  49. : SimpleJointChainActor(5)
  50. {
  51. GetSkeleton()->GetNode(0)->SetName("Joint0");
  52. GetSkeleton()->GetNode(1)->SetName("Joint1");
  53. GetSkeleton()->GetNode(2)->SetName("Joint2");
  54. GetSkeleton()->GetNode(3)->SetName("Joint3");
  55. GetSkeleton()->GetNode(4)->SetName("Joint4");
  56. AddNode(5, "ChildA", 4);
  57. AddNode(6, "ChildB", 4);
  58. // Create a node that has a mesh.
  59. // Please note that we don't go the full way here, by also filling vertex position, normal and skinning data.
  60. // Every submesh stores a list of joints used to skin that submesh. We simply fill that list, as the auto-skeletal LOD algorithm looks at this list and doesn't
  61. // look at the actual per vertex skinning information.
  62. // This way we simplify the test code slightly, while achieving the same correct test results.
  63. Node* meshNode = AddNode(7, "MeshNode");
  64. Mesh* mesh = Mesh::Create();
  65. SubMesh* subMesh = SubMesh::Create(mesh, 0, 0, 0, 8, 24, 12, numSubMeshJoints); // The numbers are just some dummy values for a fake mesh.
  66. mesh->AddSubMesh(subMesh);
  67. if (numSubMeshJoints)
  68. {
  69. SkinningInfoVertexAttributeLayer* skinningLayer = SkinningInfoVertexAttributeLayer::Create(8); // Create a fake skinning layer.
  70. mesh->AddSharedVertexAttributeLayer(skinningLayer);
  71. }
  72. SetMesh(0, meshNode->GetNodeIndex(), mesh);
  73. }
  74. };
  75. class AutoSkeletonLODFixture
  76. : public SystemComponentFixture
  77. , public ::testing::WithParamInterface<AutoLODTestParams>
  78. {
  79. public:
  80. void TearDown() override
  81. {
  82. if (m_actorInstance)
  83. {
  84. m_actorInstance->Destroy();
  85. m_actorInstance = nullptr;
  86. }
  87. m_actor.reset();
  88. SystemComponentFixture::TearDown();
  89. }
  90. SubMesh* SetupActor(AZ::u32 numSubMeshJoints)
  91. {
  92. m_actor = ActorFactory::CreateAndInit<AutoSkeletonLODActor>(numSubMeshJoints);
  93. return m_actor->GetMesh(0, m_actor->GetSkeleton()->FindNodeByName("MeshNode")->GetNodeIndex())->GetSubMesh(0);
  94. }
  95. public:
  96. AZStd::unique_ptr<Actor> m_actor = nullptr;
  97. ActorInstance* m_actorInstance = nullptr;
  98. };
  99. TEST_F(AutoSkeletonLODFixture, VerifyHierarchy)
  100. {
  101. SetupActor(0);
  102. m_actorInstance = ActorInstance::Create(m_actor.get());
  103. ASSERT_NE(m_actor, nullptr);
  104. ASSERT_NE(m_actorInstance, nullptr);
  105. // Verify integrity of the hierarchy.
  106. ASSERT_EQ(m_actor->GetNumNodes(), 8);
  107. for (AZ::u32 i = 0; i < 4; ++i)
  108. {
  109. const Node* node = m_actor->GetSkeleton()->GetNode(i);
  110. const AZStd::string jointName = AZStd::string::format("Joint%d", i);
  111. ASSERT_EQ(node->GetNumChildNodes(), 1);
  112. ASSERT_STREQ(node->GetName(), jointName.c_str());
  113. }
  114. const Skeleton* skeleton = m_actor->GetSkeleton();
  115. ASSERT_EQ(skeleton->GetNode(4)->GetNumChildNodes(), 2);
  116. ASSERT_EQ(skeleton->GetNode(5)->GetParentNode(), skeleton->GetNode(4));
  117. ASSERT_EQ(skeleton->GetNode(5)->GetNumChildNodes(), 0);
  118. ASSERT_STREQ(skeleton->GetNode(5)->GetName(), "ChildA");
  119. ASSERT_EQ(skeleton->GetNode(6)->GetParentNode(), skeleton->GetNode(4));
  120. ASSERT_EQ(skeleton->GetNode(6)->GetNumChildNodes(), 0);
  121. ASSERT_STREQ(skeleton->GetNode(6)->GetName(), "ChildB");
  122. ASSERT_EQ(skeleton->GetNode(7)->GetNumChildNodes(), 0);
  123. ASSERT_STREQ(skeleton->GetNode(7)->GetName(), "MeshNode");
  124. }
  125. TEST_P(AutoSkeletonLODFixture, MainTest)
  126. {
  127. // Create a submesh that contains all joints, so act like we are skinned to all joints.
  128. const AZ::u32 numSkinJoints = static_cast<AZ::u32>(GetParam().m_skinningJointIndices.size());
  129. SubMesh* subMesh = SetupActor(numSkinJoints);
  130. for (AZ::u32 i = 0; i < numSkinJoints; ++i)
  131. {
  132. subMesh->SetBone(i, GetParam().m_skinningJointIndices[i]);
  133. }
  134. // Generate our skeletal LODs.
  135. AZStd::vector<AZStd::string> criticalJoints;
  136. for (const std::string& jointName : GetParam().m_criticalJoints)
  137. {
  138. criticalJoints.emplace_back(jointName.c_str());
  139. }
  140. m_actor->AutoSetupSkeletalLODsBasedOnSkinningData(criticalJoints);
  141. // Verify the skeletal LOD flags.
  142. m_actorInstance = ActorInstance::Create(m_actor.get());
  143. const Skeleton* skeleton = m_actor->GetSkeleton();
  144. // All nodes should be enabled as we use all joints.
  145. // And the mesh is also automatically enabled.
  146. for (AZ::u32 i = 0; i < 8; ++i) // 8 is the total number of joints plus one mesh node
  147. {
  148. const bool shouldBeEnabled = std::find(GetParam().m_expectedEnabledJointIndices.begin(), GetParam().m_expectedEnabledJointIndices.end(), i) != GetParam().m_expectedEnabledJointIndices.end();
  149. EXPECT_EQ(skeleton->GetNode(i)->GetSkeletalLODStatus(0), shouldBeEnabled);
  150. }
  151. // Make sure the actor instance's number of enabled joints is the same.
  152. EXPECT_EQ(m_actorInstance->GetNumEnabledNodes(), static_cast<AZ::u32>(GetParam().m_expectedEnabledJointIndices.size()));
  153. // Also make sure the enabled number of nodes contents reflects the expectation.
  154. for (AZ::u32 i = 0; i < m_actorInstance->GetNumEnabledNodes(); ++i)
  155. {
  156. const AZ::u32 enabledJointIndex = static_cast<AZ::u32>(m_actorInstance->GetEnabledNode(i));
  157. const bool enabledJointFound = std::find(GetParam().m_expectedEnabledJointIndices.begin(), GetParam().m_expectedEnabledJointIndices.end(), enabledJointIndex) != GetParam().m_expectedEnabledJointIndices.end();
  158. EXPECT_TRUE(enabledJointFound);
  159. }
  160. }
  161. // Our test parameters.
  162. const std::vector<AutoLODTestParams> testParams
  163. {
  164. {
  165. { 0, 1, 2, 3, 4, 5, 6 }, // All joints used in skinning. Number 7 excluded as that is the mesh.
  166. { }, // Critical joint list.
  167. { 0, 1, 2, 3, 4, 5, 6, 7 } // We expect all nodes to be enabled.
  168. },
  169. {
  170. { }, // No skinning joints used, so just an actor with a static mesh.
  171. { }, // Critical joint list.
  172. { 0, 1, 2, 3, 4, 5, 6, 7 } // We expect everything to remain enabled.
  173. },
  174. {
  175. { 0, 1, 2, 3 }, // Skin only to the first 4 joints.
  176. { }, // Critical joint list.
  177. { 0, 1, 2, 3, 7 } // Expected enabled joints.
  178. },
  179. {
  180. { 0 }, // Skin only to the first joint.
  181. { }, // Critical joint list.
  182. { 0, 7 } // Expected enabled joints.
  183. },
  184. {
  185. { 0, 1 }, // Skin only to the first two joints.
  186. { }, // Critical joint list.
  187. { 0, 1, 7 } // Expected enabled joints.
  188. },
  189. {
  190. { 0, 6 }, // Skin only to the first and a leaf joint.
  191. { }, // Critical joint list.
  192. { 0, 1, 2, 3, 4, 6, 7 } // Expected enabled joints.
  193. },
  194. {
  195. { 4, 5 }, // Skin only to two joints down the hierarchy.
  196. { }, // Critical joint list.
  197. { 0, 1, 2, 3, 4, 5, 7 } // We expect all joints up to the root, and the mesh.
  198. },
  199. {
  200. { 6 }, // Skin to only one leaf joint.
  201. { }, // Critical joint list.
  202. { 0, 1, 2, 3, 4, 6, 7 } // We expect all joints up to the root, and the mesh.
  203. },
  204. {
  205. { 0, 1, 2 }, // Skin to only one leaf joint.
  206. { "ChildA" }, // Critical joint list.
  207. { 0, 1, 2, 3, 4, 5, 7 } // We expect all joints up to the root, and the mesh.
  208. },
  209. {
  210. { 0 }, // One joint.
  211. { "ChildA", "ChildB" }, // Critical joint list.
  212. { 0, 1, 2, 3, 4, 5, 6, 7 } // We expect all joints up to the root, and the mesh.
  213. },
  214. {
  215. { }, // No joints.
  216. { "ChildA", "ChildB" }, // Critical joint list.
  217. { 0, 1, 2, 3, 4, 5, 6, 7 } // We expect all joints up to the root, and the mesh.
  218. }
  219. };
  220. INSTANTIATE_TEST_CASE_P(AutoSkeletonLOD_Tests,
  221. AutoSkeletonLODFixture,
  222. ::testing::ValuesIn(testParams)
  223. );
  224. } // namespace EMotionFX