ClothTest.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 <AZTestShared/Math/MathTestHelpers.h>
  9. #include <UnitTestHelper.h>
  10. #include <TriangleInputHelper.h>
  11. #include <System/Factory.h>
  12. #include <System/Fabric.h>
  13. #include <System/FabricCooker.h>
  14. #include <System/Cloth.h>
  15. #include <System/NvTypes.h>
  16. // NvCloth library includes
  17. #include <NvCloth/Cloth.h>
  18. namespace NvCloth
  19. {
  20. namespace Internal
  21. {
  22. physx::PxVec3& AsPxVec3(AZ::Vector3& azVec);
  23. const physx::PxVec3& AsPxVec3(const AZ::Vector3& azVec);
  24. physx::PxQuat& AsPxQuat(AZ::Quaternion& azQuat);
  25. const physx::PxQuat& AsPxQuat(const AZ::Quaternion& azQuat);
  26. void FastCopy(const AZStd::vector<AZ::Vector4>& azVector, nv::cloth::Range<physx::PxVec4>& nvRange);
  27. void FastCopy(const nv::cloth::Range<physx::PxVec4>& nvRange, AZStd::vector<AZ::Vector4>& azVector);
  28. void FastMove(AZStd::vector<AZ::Vector4>&& azVector, nv::cloth::Range<physx::PxVec4>& nvRange);
  29. void FastMove(nv::cloth::Range<physx::PxVec4>&& nvRange, AZStd::vector<AZ::Vector4>& azVector);
  30. } // namespace Internal
  31. } // namespace NvCloth
  32. namespace UnitTest
  33. {
  34. TEST(NvClothSystem, Cloth_AzVector3AsPxVec3_PxVec3ElementsAreTheSameAsAzVector3)
  35. {
  36. AZ::Vector3 zero = AZ::Vector3::CreateZero();
  37. AZ::Vector3 one = AZ::Vector3::CreateOne();
  38. AZ::Vector3 axisX = AZ::Vector3::CreateAxisX();
  39. AZ::Vector3 axisY = AZ::Vector3::CreateAxisY();
  40. AZ::Vector3 axisZ = AZ::Vector3::CreateAxisZ();
  41. AZ::Vector3 vec3 = AZ::Vector3(26.0f, -462.366f, 15.384f);
  42. physx::PxVec3& pxZero = NvCloth::Internal::AsPxVec3(zero);
  43. physx::PxVec3& pxOne = NvCloth::Internal::AsPxVec3(one);
  44. physx::PxVec3& pxAxisX = NvCloth::Internal::AsPxVec3(axisX);
  45. physx::PxVec3& pxAxisY = NvCloth::Internal::AsPxVec3(axisY);
  46. physx::PxVec3& pxAxisZ = NvCloth::Internal::AsPxVec3(axisZ);
  47. physx::PxVec3& pxVec3 = NvCloth::Internal::AsPxVec3(vec3);
  48. ExpectEq(zero, pxZero);
  49. ExpectEq(one, pxOne);
  50. ExpectEq(axisX, pxAxisX);
  51. ExpectEq(axisY, pxAxisY);
  52. ExpectEq(axisZ, pxAxisZ);
  53. ExpectEq(vec3, pxVec3);
  54. }
  55. TEST(NvClothSystem, Cloth_AzVector3AsPxVec3Const_PxVec3ElementsAreTheSameAsAzVector3)
  56. {
  57. const AZ::Vector3 zero = AZ::Vector3::CreateZero();
  58. const AZ::Vector3 one = AZ::Vector3::CreateOne();
  59. const AZ::Vector3 axisX = AZ::Vector3::CreateAxisX();
  60. const AZ::Vector3 axisY = AZ::Vector3::CreateAxisY();
  61. const AZ::Vector3 axisZ = AZ::Vector3::CreateAxisZ();
  62. const AZ::Vector3 vec3 = AZ::Vector3(26.0f, -462.366f, 15.384f);
  63. const physx::PxVec3& pxZero = NvCloth::Internal::AsPxVec3(zero);
  64. const physx::PxVec3& pxOne = NvCloth::Internal::AsPxVec3(one);
  65. const physx::PxVec3& pxAxisX = NvCloth::Internal::AsPxVec3(axisX);
  66. const physx::PxVec3& pxAxisY = NvCloth::Internal::AsPxVec3(axisY);
  67. const physx::PxVec3& pxAxisZ = NvCloth::Internal::AsPxVec3(axisZ);
  68. const physx::PxVec3& pxVec3 = NvCloth::Internal::AsPxVec3(vec3);
  69. ExpectEq(zero, pxZero);
  70. ExpectEq(one, pxOne);
  71. ExpectEq(axisX, pxAxisX);
  72. ExpectEq(axisY, pxAxisY);
  73. ExpectEq(axisZ, pxAxisZ);
  74. ExpectEq(vec3, pxVec3);
  75. }
  76. TEST(NvClothSystem, Cloth_AzQuaternionAsPxQuat_QuatElementsAreTheSameAsAzQuaternion)
  77. {
  78. AZ::Quaternion zero = AZ::Quaternion::CreateZero();
  79. AZ::Quaternion one = AZ::Quaternion::CreateIdentity();
  80. AZ::Quaternion rotX = AZ::Quaternion::CreateRotationX(AZ::DegToRad(26.5f));
  81. AZ::Quaternion rotY = AZ::Quaternion::CreateRotationY(AZ::DegToRad(-196.5f));
  82. AZ::Quaternion rotZ = AZ::Quaternion::CreateRotationZ(AZ::DegToRad(263.2f));
  83. AZ::Quaternion quat = AZ::Quaternion(26.0f, -62.366f, 15.384f, 5.0f);
  84. physx::PxQuat& pxZero = NvCloth::Internal::AsPxQuat(zero);
  85. physx::PxQuat& pxOne = NvCloth::Internal::AsPxQuat(one);
  86. physx::PxQuat& pxRotX = NvCloth::Internal::AsPxQuat(rotX);
  87. physx::PxQuat& pxRotY = NvCloth::Internal::AsPxQuat(rotY);
  88. physx::PxQuat& pxRotZ = NvCloth::Internal::AsPxQuat(rotZ);
  89. physx::PxQuat& pxQuat = NvCloth::Internal::AsPxQuat(quat);
  90. ExpectEq(zero, pxZero);
  91. ExpectEq(one, pxOne);
  92. ExpectEq(rotX, pxRotX);
  93. ExpectEq(rotY, pxRotY);
  94. ExpectEq(rotZ, pxRotZ);
  95. ExpectEq(quat, pxQuat);
  96. }
  97. TEST(NvClothSystem, Cloth_AzQuaternionAsPxQuatConst_QuatElementsAreTheSameAsAzQuaternion)
  98. {
  99. const AZ::Quaternion zero = AZ::Quaternion::CreateZero();
  100. const AZ::Quaternion one = AZ::Quaternion::CreateIdentity();
  101. const AZ::Quaternion rotX = AZ::Quaternion::CreateRotationX(AZ::DegToRad(26.5f));
  102. const AZ::Quaternion rotY = AZ::Quaternion::CreateRotationY(AZ::DegToRad(-196.5f));
  103. const AZ::Quaternion rotZ = AZ::Quaternion::CreateRotationZ(AZ::DegToRad(263.2f));
  104. const AZ::Quaternion quat = AZ::Quaternion(26.0f, -62.366f, 15.384f, 5.0f);
  105. const physx::PxQuat& pxZero = NvCloth::Internal::AsPxQuat(zero);
  106. const physx::PxQuat& pxOne = NvCloth::Internal::AsPxQuat(one);
  107. const physx::PxQuat& pxRotX = NvCloth::Internal::AsPxQuat(rotX);
  108. const physx::PxQuat& pxRotY = NvCloth::Internal::AsPxQuat(rotY);
  109. const physx::PxQuat& pxRotZ = NvCloth::Internal::AsPxQuat(rotZ);
  110. const physx::PxQuat& pxQuat = NvCloth::Internal::AsPxQuat(quat);
  111. ExpectEq(zero, pxZero);
  112. ExpectEq(one, pxOne);
  113. ExpectEq(rotX, pxRotX);
  114. ExpectEq(rotY, pxRotY);
  115. ExpectEq(rotZ, pxRotZ);
  116. ExpectEq(quat, pxQuat);
  117. }
  118. TEST(NvClothSystem, Cloth_FastCopy_nvRangeElmenentsAreTheSameAsAZStdVector)
  119. {
  120. const AZStd::vector<AZ::Vector4> azEmpty;
  121. const AZStd::vector<AZ::Vector4> azValues = {{
  122. AZ::Vector4(15.0f, -692.0f, 65.0f, -15.0f),
  123. AZ::Vector4(1851.594f, 1.0f, -125.0f, 168.0f),
  124. AZ::Vector4(2384.05f, -692.0f, 41865.153f, 1567.0f),
  125. AZ::Vector4(35.02f, 2572.453f, 2465.0f, 987.0f),
  126. AZ::Vector4(-14.161f, 47.0f, 65.0f, -6358.52f)
  127. }};
  128. nv::cloth::Vector<physx::PxVec4>::Type nvEmpty;
  129. nv::cloth::Vector<physx::PxVec4>::Type nvValues(static_cast<uint32_t>(azValues.size()));
  130. nv::cloth::Range<physx::PxVec4> nvEmptyRange(nvEmpty.begin(), nvEmpty.end());
  131. nv::cloth::Range<physx::PxVec4> nvValuesRange(nvValues.begin(), nvValues.end());
  132. NvCloth::Internal::FastCopy(azEmpty, nvEmptyRange);
  133. NvCloth::Internal::FastCopy(azValues, nvValuesRange);
  134. ExpectEq(azEmpty, nvEmptyRange);
  135. ExpectEq(azValues, nvValuesRange);
  136. }
  137. TEST(NvClothSystem, Cloth_FastCopy_AZStdVectorElmenentsAreTheSameAsNvRange)
  138. {
  139. nv::cloth::Vector<physx::PxVec4>::Type nvEmpty;
  140. nv::cloth::Vector<physx::PxVec4>::Type nvValues;
  141. nvValues.pushBack(physx::PxVec4(15.0f, -692.0f, 65.0f, -15.0f));
  142. nvValues.pushBack(physx::PxVec4(1851.594f, 1.0f, -125.0f, 168.0f));
  143. nvValues.pushBack(physx::PxVec4(2384.05f, -692.0f, 41865.153f, 1567.0f));
  144. nvValues.pushBack(physx::PxVec4(35.02f, 2572.453f, 2465.0f, 987.0f));
  145. nvValues.pushBack(physx::PxVec4(-14.161f, 47.0f, 65.0f, -6358.52f));
  146. const nv::cloth::Range<physx::PxVec4> nvEmptyRange(nvEmpty.begin(), nvEmpty.end());
  147. const nv::cloth::Range<physx::PxVec4> nvValuesRange(nvValues.begin(), nvValues.end());
  148. AZStd::vector<AZ::Vector4> azEmpty;
  149. AZStd::vector<AZ::Vector4> azValues(nvValuesRange.size());
  150. NvCloth::Internal::FastCopy(nvEmptyRange, azEmpty);
  151. NvCloth::Internal::FastCopy(nvValuesRange, azValues);
  152. ExpectEq(azEmpty, nvEmptyRange);
  153. ExpectEq(azValues, nvValuesRange);
  154. }
  155. TEST(NvClothSystem, Cloth_FastMove_nvRangeElmenentsAreTheSameAsAZStdVector)
  156. {
  157. const AZStd::vector<AZ::Vector4> azEmpty;
  158. const AZStd::vector<AZ::Vector4> azValues = {{
  159. AZ::Vector4(15.0f, -692.0f, 65.0f, -15.0f),
  160. AZ::Vector4(1851.594f, 1.0f, -125.0f, 168.0f),
  161. AZ::Vector4(2384.05f, -692.0f, 41865.153f, 1567.0f),
  162. AZ::Vector4(35.02f, 2572.453f, 2465.0f, 987.0f),
  163. AZ::Vector4(-14.161f, 47.0f, 65.0f, -6358.52f)
  164. }};
  165. nv::cloth::Vector<physx::PxVec4>::Type nvEmpty;
  166. nv::cloth::Vector<physx::PxVec4>::Type nvValues(static_cast<uint32_t>(azValues.size()));
  167. nv::cloth::Range<physx::PxVec4> nvEmptyRange(nvEmpty.begin(), nvEmpty.end());
  168. nv::cloth::Range<physx::PxVec4> nvValuesRange(nvValues.begin(), nvValues.end());
  169. {
  170. AZStd::vector<AZ::Vector4> azEmptyCopy = azEmpty;
  171. AZStd::vector<AZ::Vector4> azValuesCopy = azValues;
  172. NvCloth::Internal::FastMove(AZStd::move(azEmptyCopy), nvEmptyRange);
  173. NvCloth::Internal::FastMove(AZStd::move(azValuesCopy), nvValuesRange);
  174. }
  175. ExpectEq(azEmpty, nvEmptyRange);
  176. ExpectEq(azValues, nvValuesRange);
  177. }
  178. TEST(NvClothSystem, Cloth_FastMove_AZStdVectorElmenentsAreTheSameAsNvRange)
  179. {
  180. nv::cloth::Vector<physx::PxVec4>::Type nvEmpty;
  181. nv::cloth::Vector<physx::PxVec4>::Type nvValues;
  182. nvValues.pushBack(physx::PxVec4(15.0f, -692.0f, 65.0f, -15.0f));
  183. nvValues.pushBack(physx::PxVec4(1851.594f, 1.0f, -125.0f, 168.0f));
  184. nvValues.pushBack(physx::PxVec4(2384.05f, -692.0f, 41865.153f, 1567.0f));
  185. nvValues.pushBack(physx::PxVec4(35.02f, 2572.453f, 2465.0f, 987.0f));
  186. nvValues.pushBack(physx::PxVec4(-14.161f, 47.0f, 65.0f, -6358.52f));
  187. const nv::cloth::Range<physx::PxVec4> nvEmptyRange(nvEmpty.begin(), nvEmpty.end());
  188. const nv::cloth::Range<physx::PxVec4> nvValuesRange(nvValues.begin(), nvValues.end());
  189. AZStd::vector<AZ::Vector4> azEmpty;
  190. AZStd::vector<AZ::Vector4> azValues(nvValuesRange.size());
  191. {
  192. nv::cloth::Vector<physx::PxVec4>::Type nvEmptyCopy = nvEmpty;
  193. nv::cloth::Vector<physx::PxVec4>::Type nvValuesCopy = nvValues;
  194. nv::cloth::Range<physx::PxVec4> nvEmptyRangeCopy(nvEmptyCopy.begin(), nvEmptyCopy.end());
  195. nv::cloth::Range<physx::PxVec4> nvValuesRangeCopy(nvValuesCopy.begin(), nvValuesCopy.end());
  196. NvCloth::Internal::FastMove(AZStd::move(nvEmptyRangeCopy), azEmpty);
  197. NvCloth::Internal::FastMove(AZStd::move(nvValuesRangeCopy), azValues);
  198. }
  199. ExpectEq(azEmpty, nvEmptyRange);
  200. ExpectEq(azValues, nvValuesRange);
  201. }
  202. //! Sets up a cloth for each test case with access to its native cloth instance.
  203. //! Creating cloth using direct calls to the library, instead of using Factory, to
  204. //! be able to keep a pointer the native cloth instance.
  205. class NvClothSystemCloth
  206. : public ::testing::Test
  207. {
  208. protected:
  209. // ::testing::Test overrides ...
  210. void SetUp() override;
  211. void TearDown() override;
  212. AZStd::unique_ptr<NvCloth::Cloth> m_cloth;
  213. nv::cloth::Cloth* m_nvCloth = nullptr; // Pointer to native cloth instance of m_cloth
  214. private:
  215. void CreateFabric();
  216. void CreateCloth();
  217. NvCloth::NvFactoryUniquePtr m_nvFactory;
  218. AZStd::unique_ptr<NvCloth::Fabric> m_fabric;
  219. };
  220. void NvClothSystemCloth::SetUp()
  221. {
  222. m_nvFactory = NvCloth::NvFactoryUniquePtr(NvClothCreateFactoryCPU());
  223. CreateFabric();
  224. CreateCloth();
  225. }
  226. void NvClothSystemCloth::TearDown()
  227. {
  228. m_nvCloth = nullptr;
  229. m_cloth.reset();
  230. m_fabric.reset();
  231. m_nvFactory.reset();
  232. }
  233. void NvClothSystemCloth::CreateFabric()
  234. {
  235. const NvCloth::FabricCookedData fabricCookedData = CreateTestFabricCookedData();
  236. NvCloth::NvFabricUniquePtr nvFabric(
  237. m_nvFactory->createFabric(
  238. fabricCookedData.m_internalData.m_numParticles,
  239. NvCloth::ToNvRange(fabricCookedData.m_internalData.m_phaseIndices),
  240. NvCloth::ToNvRange(fabricCookedData.m_internalData.m_sets),
  241. NvCloth::ToNvRange(fabricCookedData.m_internalData.m_restValues),
  242. NvCloth::ToNvRange(fabricCookedData.m_internalData.m_stiffnessValues),
  243. NvCloth::ToNvRange(fabricCookedData.m_internalData.m_indices),
  244. NvCloth::ToNvRange(fabricCookedData.m_internalData.m_anchors),
  245. NvCloth::ToNvRange(fabricCookedData.m_internalData.m_tetherLengths),
  246. NvCloth::ToNvRange(fabricCookedData.m_internalData.m_triangles)));
  247. EXPECT_TRUE(nvFabric.get() != nullptr);
  248. m_fabric = AZStd::make_unique<NvCloth::Fabric>(
  249. fabricCookedData,
  250. AZStd::move(nvFabric));
  251. }
  252. void NvClothSystemCloth::CreateCloth()
  253. {
  254. NvCloth::NvClothUniquePtr nvCloth(
  255. m_nvFactory->createCloth(
  256. NvCloth::ToPxVec4NvRange(m_fabric->m_cookedData.m_particles),
  257. *m_fabric->m_nvFabric.get()));
  258. EXPECT_TRUE(nvCloth.get() != nullptr);
  259. m_nvCloth = nvCloth.get();
  260. m_cloth = AZStd::make_unique<NvCloth::Cloth>(
  261. NvCloth::ClothId(1),
  262. m_fabric->m_cookedData.m_particles,
  263. m_fabric.get(),
  264. AZStd::move(nvCloth));
  265. }
  266. TEST_F(NvClothSystemCloth, Cloth_SetParticles_ParticlesAreSetToClothAndNativeCloth)
  267. {
  268. auto newParticles = m_cloth->GetParticles();
  269. for (auto& particle : newParticles)
  270. {
  271. particle *= 2.0f;
  272. }
  273. m_cloth->SetParticles(newParticles);
  274. EXPECT_THAT(newParticles, ::testing::Pointwise(ContainerIsCloseTolerance(Tolerance), m_cloth->GetParticles()));
  275. const nv::cloth::MappedRange<const physx::PxVec4> nvClothCurrentParticles = nv::cloth::readCurrentParticles(*m_nvCloth);
  276. ExpectEq(newParticles, nvClothCurrentParticles);
  277. // The inverse masses (W element) should have been copied into the previous particles inside NvCloth
  278. // to take effect for the next simulation update.
  279. const nv::cloth::MappedRange<const physx::PxVec4> nvClothPreviousParticles = nv::cloth::readPreviousParticles(*m_nvCloth);
  280. for (size_t i = 0; i < newParticles.size(); ++i)
  281. {
  282. EXPECT_NEAR(newParticles[i].GetW(), nvClothPreviousParticles[static_cast<uint32_t>(i)].w, Tolerance);
  283. }
  284. }
  285. TEST_F(NvClothSystemCloth, Cloth_SetParticlesMove_ParticlesAreSetToClothAndNativeCloth)
  286. {
  287. auto newParticles = m_cloth->GetParticles();
  288. for (auto& particle : newParticles)
  289. {
  290. particle *= 2.0f;
  291. }
  292. {
  293. auto newParticlesCopy = newParticles;
  294. m_cloth->SetParticles(AZStd::move(newParticlesCopy));
  295. }
  296. EXPECT_THAT(newParticles, ::testing::Pointwise(ContainerIsCloseTolerance(Tolerance), m_cloth->GetParticles()));
  297. const nv::cloth::MappedRange<const physx::PxVec4> nvClothCurrentParticles = nv::cloth::readCurrentParticles(*m_nvCloth);
  298. ExpectEq(newParticles, nvClothCurrentParticles);
  299. // The inverse masses (W element) should have been copied into the previous particles inside NvCloth
  300. // to take effect for the next simulation update.
  301. const nv::cloth::MappedRange<const physx::PxVec4> nvClothPreviousParticles = nv::cloth::readPreviousParticles(*m_nvCloth);
  302. for (size_t i = 0; i < newParticles.size(); ++i)
  303. {
  304. EXPECT_NEAR(newParticles[i].GetW(), nvClothPreviousParticles[static_cast<uint32_t>(i)].w, Tolerance);
  305. }
  306. }
  307. TEST_F(NvClothSystemCloth, Cloth_DiscardParticleDelta_NativeClothPreviousAndCurrentParticlesAreTheSame)
  308. {
  309. m_cloth->DiscardParticleDelta();
  310. const nv::cloth::MappedRange<const physx::PxVec4> nvClothCurrentParticles = nv::cloth::readCurrentParticles(*m_nvCloth);
  311. const nv::cloth::MappedRange<const physx::PxVec4> nvClothPreviousParticles = nv::cloth::readPreviousParticles(*m_nvCloth);
  312. EXPECT_EQ(nvClothCurrentParticles.size(), nvClothPreviousParticles.size());
  313. for (size_t i = 0; i < nvClothCurrentParticles.size(); ++i)
  314. {
  315. ExpectEq(nvClothCurrentParticles[static_cast<uint32_t>(i)], nvClothPreviousParticles[static_cast<uint32_t>(i)]);
  316. }
  317. }
  318. TEST_F(NvClothSystemCloth, Cloth_Update_SimParticlesAreUpdated)
  319. {
  320. const AZ::Vector3 movement(6.0f, 1.0f, 3.0f);
  321. const auto previousParticles = m_cloth->GetParticles();
  322. // Fake all particles have been moved during simulation.
  323. {
  324. nv::cloth::MappedRange<physx::PxVec4> nvParticles = m_nvCloth->getCurrentParticles();
  325. for (auto& particle : nvParticles)
  326. {
  327. if (particle.w != 0.0f)
  328. {
  329. particle.x += movement.GetX();
  330. particle.y += movement.GetY();
  331. particle.z += movement.GetZ();
  332. }
  333. }
  334. // nvcloth particles are set once nvParticles gets out of scope
  335. }
  336. m_cloth->Update();
  337. const auto& particles = m_cloth->GetParticles();
  338. for (size_t i = 0; i < particles.size(); ++i)
  339. {
  340. if (particles[i].GetW() == 0.0f)
  341. {
  342. EXPECT_THAT(particles[i].GetAsVector3(), IsCloseTolerance(previousParticles[i].GetAsVector3(), Tolerance));
  343. }
  344. else
  345. {
  346. EXPECT_THAT(particles[i].GetAsVector3(), IsCloseTolerance(previousParticles[i].GetAsVector3() + movement, Tolerance));
  347. }
  348. }
  349. }
  350. TEST_F(NvClothSystemCloth, Cloth_UpdateInvalidParticles_SimParticlesAreNotUpdated)
  351. {
  352. const auto previousParticles = m_cloth->GetParticles();
  353. // Fake a particle has been set to non-finite values during simulation
  354. {
  355. nv::cloth::MappedRange<physx::PxVec4> nvParticles = m_nvCloth->getCurrentParticles();
  356. for (auto& particle : nvParticles)
  357. {
  358. if (particle.w != 0.0f)
  359. {
  360. particle.x = std::numeric_limits<float>::quiet_NaN();
  361. particle.y = std::numeric_limits<float>::infinity();
  362. break;
  363. }
  364. }
  365. // nvcloth particles are set once nvParticles gets out of scope
  366. }
  367. m_cloth->Update();
  368. const auto& particles = m_cloth->GetParticles();
  369. EXPECT_THAT(particles, ::testing::Pointwise(ContainerIsCloseTolerance(Tolerance), previousParticles));
  370. }
  371. TEST_F(NvClothSystemCloth, Cloth_UpdateInvalidParticles_NativeClothParticlesAreRestored)
  372. {
  373. // Fake a particle has been set to non-finite values during simulation
  374. {
  375. nv::cloth::MappedRange<physx::PxVec4> nvParticles = m_nvCloth->getCurrentParticles();
  376. for (auto& particle : nvParticles)
  377. {
  378. if (particle.w != 0.0f)
  379. {
  380. particle.x = std::numeric_limits<float>::quiet_NaN();
  381. particle.y = std::numeric_limits<float>::infinity();
  382. break;
  383. }
  384. }
  385. // nvcloth particles are set once nvParticles gets out of scope
  386. }
  387. m_cloth->Update();
  388. const nv::cloth::MappedRange<const physx::PxVec4> nvClothCurrentParticles = nv::cloth::readCurrentParticles(*m_nvCloth);
  389. const nv::cloth::MappedRange<const physx::PxVec4> nvClothPreviousParticles = nv::cloth::readPreviousParticles(*m_nvCloth);
  390. EXPECT_EQ(nvClothCurrentParticles.size(), nvClothPreviousParticles.size());
  391. for (size_t i = 0; i < nvClothCurrentParticles.size(); ++i)
  392. {
  393. ExpectEq(nvClothCurrentParticles[static_cast<uint32_t>(i)], nvClothPreviousParticles[static_cast<uint32_t>(i)]);
  394. }
  395. }
  396. TEST_F(NvClothSystemCloth, Cloth_UpdateInvalidParticlesManyAttempts_NativeClothParticlesAreRestoredToInitialPositions)
  397. {
  398. const auto& initialParticles = m_cloth->GetInitialParticles();
  399. const size_t numInvalidSimulations = 30;
  400. for (size_t i = 0; i < numInvalidSimulations; ++i)
  401. {
  402. // Fake a particle has been set to non-finite values during simulation
  403. {
  404. nv::cloth::MappedRange<physx::PxVec4> nvParticles = m_nvCloth->getCurrentParticles();
  405. for (auto& particle : nvParticles)
  406. {
  407. if (particle.w != 0.0f)
  408. {
  409. particle.x = std::numeric_limits<float>::quiet_NaN();
  410. particle.y = std::numeric_limits<float>::infinity();
  411. break;
  412. }
  413. }
  414. // nvcloth particles are set once nvParticles gets out of scope
  415. }
  416. m_cloth->Update();
  417. }
  418. const nv::cloth::MappedRange<const physx::PxVec4> nvClothCurrentParticles = nv::cloth::readCurrentParticles(*m_nvCloth);
  419. const nv::cloth::MappedRange<const physx::PxVec4> nvClothPreviousParticles = nv::cloth::readPreviousParticles(*m_nvCloth);
  420. EXPECT_EQ(initialParticles.size(), nvClothCurrentParticles.size());
  421. EXPECT_EQ(initialParticles.size(), nvClothPreviousParticles.size());
  422. for (size_t i = 0; i < initialParticles.size(); ++i)
  423. {
  424. ExpectEq(initialParticles[i], nvClothCurrentParticles[static_cast<uint32_t>(i)]);
  425. ExpectEq(initialParticles[i], nvClothPreviousParticles[static_cast<uint32_t>(i)]);
  426. }
  427. }
  428. TEST_F(NvClothSystemCloth, Cloth_CollisionAffectsStaticParticles_StaticParticlesAreModifiedDuringUpdate)
  429. {
  430. const AZ::Vector3 movement(6.0f, 1.0f, 3.0f);
  431. const auto previousParticles = m_cloth->GetParticles();
  432. m_cloth->GetClothConfigurator()->SetCollisionAffectsStaticParticles(true);
  433. // Fake all particles have been moved during simulation, cloth contains static particles.
  434. {
  435. nv::cloth::MappedRange<physx::PxVec4> nvParticles = m_nvCloth->getCurrentParticles();
  436. for (auto& particle : nvParticles)
  437. {
  438. particle.x += movement.GetX();
  439. particle.y += movement.GetY();
  440. particle.z += movement.GetZ();
  441. }
  442. // nvcloth particles are set once nvParticles gets out of scope
  443. }
  444. m_cloth->Update();
  445. const auto& particles = m_cloth->GetParticles();
  446. for (size_t i = 0; i < particles.size(); ++i)
  447. {
  448. EXPECT_THAT(particles[i].GetAsVector3(), IsCloseTolerance(previousParticles[i].GetAsVector3() + movement, Tolerance));
  449. }
  450. }
  451. TEST_F(NvClothSystemCloth, Cloth_CollisionDoesNotAffectStaticParticles_StaticParticlesAreNotModifiedDuringUpdate)
  452. {
  453. const AZ::Vector3 movement(6.0f, 1.0f, 3.0f);
  454. const auto previousParticles = m_cloth->GetParticles();
  455. m_cloth->GetClothConfigurator()->SetCollisionAffectsStaticParticles(false);
  456. // Fake all particles have been moved during simulation, cloth contains static particles.
  457. {
  458. nv::cloth::MappedRange<physx::PxVec4> nvParticles = m_nvCloth->getCurrentParticles();
  459. for (auto& particle : nvParticles)
  460. {
  461. particle.x += movement.GetX();
  462. particle.y += movement.GetY();
  463. particle.z += movement.GetZ();
  464. }
  465. // nvcloth particles are set once nvParticles gets out of scope
  466. }
  467. m_cloth->Update();
  468. const auto& particles = m_cloth->GetParticles();
  469. for (size_t i = 0; i < particles.size(); ++i)
  470. {
  471. if (particles[i].GetW() == 0.0f)
  472. {
  473. EXPECT_THAT(particles[i].GetAsVector3(), IsCloseTolerance(previousParticles[i].GetAsVector3(), Tolerance));
  474. }
  475. else
  476. {
  477. EXPECT_THAT(particles[i].GetAsVector3(), IsCloseTolerance(previousParticles[i].GetAsVector3() + movement, Tolerance));
  478. }
  479. }
  480. }
  481. TEST_F(NvClothSystemCloth, Cloth_ClothConfigurationSetTransform_TranslationAndRotationAreAppliedToNativeCloth)
  482. {
  483. const AZ::Transform identity = AZ::Transform::CreateIdentity();
  484. const AZ::Transform rotationX = AZ::Transform::CreateRotationX(AZ::DegToRad(35.0f));
  485. const AZ::Transform rotationYAndTranslation = AZ::Transform::CreateFromQuaternionAndTranslation(
  486. AZ::Quaternion::CreateRotationY(AZ::DegToRad(-135.0f)),
  487. AZ::Vector3(36.0f, 50.0f, -69.35f));
  488. m_cloth->GetClothConfigurator()->SetTransform(identity);
  489. ExpectEq(identity.GetTranslation(), m_nvCloth->getTranslation());
  490. ExpectEq(identity.GetRotation(), m_nvCloth->getRotation());
  491. m_cloth->GetClothConfigurator()->SetTransform(rotationX);
  492. ExpectEq(rotationX.GetTranslation(), m_nvCloth->getTranslation());
  493. ExpectEq(rotationX.GetRotation(), m_nvCloth->getRotation());
  494. m_cloth->GetClothConfigurator()->SetTransform(rotationYAndTranslation);
  495. ExpectEq(rotationYAndTranslation.GetTranslation(), m_nvCloth->getTranslation());
  496. ExpectEq(rotationYAndTranslation.GetRotation(), m_nvCloth->getRotation());
  497. }
  498. TEST_F(NvClothSystemCloth, Cloth_ClothConfigurationSetMass_MassIsAppliedToClothSimParticlesAndNativeClothPreviousParticles)
  499. {
  500. const float globalMass = 2.0f;
  501. const auto& initialParticles = m_cloth->GetInitialParticles();
  502. m_cloth->GetClothConfigurator()->SetMass(globalMass);
  503. const auto& particles = m_cloth->GetParticles();
  504. for (size_t i = 0; i < initialParticles.size(); ++i)
  505. {
  506. EXPECT_NEAR(particles[i].GetW(), initialParticles[i].GetW() / globalMass, Tolerance);
  507. }
  508. // The inverse masses (W element) should have been copied into the previous particles inside NvCloth
  509. // to take effect for the next simulation update.
  510. const nv::cloth::MappedRange<const physx::PxVec4> nvClothPreviousParticles = nv::cloth::readPreviousParticles(*m_nvCloth);
  511. for (size_t i = 0; i < initialParticles.size(); ++i)
  512. {
  513. EXPECT_NEAR(nvClothPreviousParticles[static_cast<uint32_t>(i)].w, initialParticles[i].GetW() / globalMass, Tolerance);
  514. }
  515. }
  516. } // namespace UnitTest