3
0

ClothConstraints.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/Interface/Interface.h>
  9. #include <Components/ClothComponentMesh/ClothConstraints.h>
  10. #include <NvCloth/ITangentSpaceHelper.h>
  11. namespace NvCloth
  12. {
  13. AZStd::unique_ptr<ClothConstraints> ClothConstraints::Create(
  14. const AZStd::vector<float>& motionConstraintsData,
  15. const float motionConstraintsMaxDistance,
  16. const AZStd::vector<AZ::Vector2>& backstopData,
  17. const float backstopMaxRadius,
  18. const float backstopMaxBackOffset,
  19. const float backstopMaxFrontOffset,
  20. const AZStd::vector<SimParticleFormat>& simParticles,
  21. const AZStd::vector<SimIndexType>& simIndices,
  22. const AZStd::vector<int>& meshRemappedVertices)
  23. {
  24. AZStd::unique_ptr<ClothConstraints> clothConstraints = AZStd::make_unique<ClothConstraints>();
  25. clothConstraints->m_motionConstraintsData.resize(simParticles.size(), AZ::Constants::FloatMax);
  26. clothConstraints->m_motionConstraintsMaxDistance = motionConstraintsMaxDistance;
  27. clothConstraints->m_motionConstraints.resize(simParticles.size());
  28. for (size_t i = 0; i < motionConstraintsData.size(); ++i)
  29. {
  30. const int remappedIndex = meshRemappedVertices[i];
  31. if (remappedIndex < 0)
  32. {
  33. // Removed particle
  34. continue;
  35. }
  36. // Keep the minimum distance for remapped duplicated vertices
  37. if (motionConstraintsData[i] < clothConstraints->m_motionConstraintsData[remappedIndex])
  38. {
  39. clothConstraints->m_motionConstraintsData[remappedIndex] = motionConstraintsData[i];
  40. }
  41. }
  42. const bool hasBackstopData = AZStd::any_of(
  43. backstopData.cbegin(),
  44. backstopData.cend(),
  45. [](const AZ::Vector2& backstop)
  46. {
  47. const float radius = backstop.GetY();
  48. return radius > 0.0f;
  49. });
  50. if (hasBackstopData)
  51. {
  52. clothConstraints->m_backstopData.resize(simParticles.size(), AZ::Vector2(0.0f, AZ::Constants::FloatMax));
  53. clothConstraints->m_backstopMaxRadius = backstopMaxRadius;
  54. clothConstraints->m_backstopMaxBackOffset = backstopMaxBackOffset;
  55. clothConstraints->m_backstopMaxFrontOffset = backstopMaxFrontOffset;
  56. clothConstraints->m_separationConstraints.resize(simParticles.size());
  57. for (size_t i = 0; i < backstopData.size(); ++i)
  58. {
  59. const int remappedIndex = meshRemappedVertices[i];
  60. if (remappedIndex < 0)
  61. {
  62. // Removed particle
  63. continue;
  64. }
  65. // Keep the minimum radius for remapped duplicated vertices
  66. if (backstopData[i].GetY() < clothConstraints->m_backstopData[remappedIndex].GetY())
  67. {
  68. clothConstraints->m_backstopData[remappedIndex] = backstopData[i];
  69. }
  70. }
  71. }
  72. // Calculates the current constraints and fills the data as nvcloth needs them,
  73. // ready to be queried by the cloth component.
  74. clothConstraints->CalculateConstraints(simParticles, simIndices);
  75. return clothConstraints;
  76. }
  77. void ClothConstraints::CalculateConstraints(
  78. const AZStd::vector<SimParticleFormat>& simParticles,
  79. const AZStd::vector<SimIndexType>& simIndices)
  80. {
  81. if (simParticles.size() != m_motionConstraints.size())
  82. {
  83. return;
  84. }
  85. m_simParticles = simParticles;
  86. CalculateMotionConstraints();
  87. if (!m_separationConstraints.empty())
  88. {
  89. [[maybe_unused]] bool normalsCalculated = AZ::Interface<ITangentSpaceHelper>::Get()->CalculateNormals(simParticles, simIndices, m_normals);
  90. AZ_Assert(normalsCalculated, "Cloth constraints failed to calculate normals.");
  91. CalculateSeparationConstraints();
  92. }
  93. }
  94. const AZStd::vector<AZ::Vector4>& ClothConstraints::GetMotionConstraints() const
  95. {
  96. return m_motionConstraints;
  97. }
  98. const AZStd::vector<AZ::Vector4>& ClothConstraints::GetSeparationConstraints() const
  99. {
  100. return m_separationConstraints;
  101. }
  102. void ClothConstraints::SetMotionConstraintMaxDistance(float distance)
  103. {
  104. m_motionConstraintsMaxDistance = distance;
  105. CalculateMotionConstraints();
  106. }
  107. void ClothConstraints::SetBackstopMaxRadius(float radius)
  108. {
  109. m_backstopMaxRadius = radius;
  110. CalculateSeparationConstraints();
  111. }
  112. void ClothConstraints::SetBackstopMaxOffsets(float backOffset, float frontOffset)
  113. {
  114. m_backstopMaxBackOffset = backOffset;
  115. m_backstopMaxFrontOffset = frontOffset;
  116. CalculateSeparationConstraints();
  117. }
  118. void ClothConstraints::CalculateMotionConstraints()
  119. {
  120. for (size_t i = 0; i < m_motionConstraints.size(); ++i)
  121. {
  122. const float maxDistance = (m_simParticles[i].GetW() > 0.0f)
  123. ? m_motionConstraintsData[i] * m_motionConstraintsMaxDistance
  124. : 0.0f;
  125. m_motionConstraints[i].Set(m_simParticles[i].GetAsVector3(), maxDistance);
  126. }
  127. }
  128. void ClothConstraints::CalculateSeparationConstraints()
  129. {
  130. for (size_t i = 0; i < m_separationConstraints.size(); ++i)
  131. {
  132. const float offsetScale = m_backstopData[i].GetX();
  133. const float offset = offsetScale * ((offsetScale >= 0.0f) ? m_backstopMaxBackOffset : m_backstopMaxFrontOffset);
  134. const float radiusScale = m_backstopData[i].GetY();
  135. const float radius = radiusScale * m_backstopMaxRadius;
  136. const AZ::Vector3 position = CalculateBackstopSpherePosition(
  137. m_simParticles[i].GetAsVector3(), m_normals[i], offset, radius);
  138. m_separationConstraints[i].Set(position, radius);
  139. }
  140. }
  141. AZ::Vector3 ClothConstraints::CalculateBackstopSpherePosition(
  142. const AZ::Vector3& position,
  143. const AZ::Vector3& normal,
  144. float offset,
  145. float radius) const
  146. {
  147. AZ::Vector3 spherePosition = position;
  148. if (offset >= 0.0f)
  149. {
  150. spherePosition -= normal * (radius + offset); // Place sphere behind the particle
  151. }
  152. else
  153. {
  154. spherePosition += normal * (radius - offset); // Place sphere in front of the particle
  155. }
  156. return spherePosition;
  157. }
  158. } // namespace NvCloth