3
0

ClothDebugDisplay.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/Console/IConsole.h>
  9. #include <AzFramework/Viewport/ViewportColors.h>
  10. #include <Components/ClothComponentMesh/ClothDebugDisplay.h>
  11. #include <Components/ClothComponentMesh/ActorClothColliders.h>
  12. #include <Components/ClothComponentMesh/ClothConstraints.h>
  13. #include <Components/ClothComponentMesh/ClothComponentMesh.h>
  14. #include <NvCloth/ICloth.h>
  15. namespace NvCloth
  16. {
  17. AZ_CVAR(int32_t, cloth_DebugDraw, 0, nullptr, AZ::ConsoleFunctorFlags::Null,
  18. "Draw cloth wireframe mesh:\n"
  19. " 0 - Disabled\n"
  20. " 1 - Cloth wireframe and particle weights");
  21. AZ_CVAR(int32_t, cloth_DebugDrawNormals, 0, nullptr, AZ::ConsoleFunctorFlags::Null,
  22. "Draw cloth normals:\n"
  23. " 0 - Disabled\n"
  24. " 1 - Cloth normals\n"
  25. " 2 - Cloth normals, tangents and bitangents");
  26. AZ_CVAR(int32_t, cloth_DebugDrawColliders, 0, nullptr, AZ::ConsoleFunctorFlags::Null,
  27. "Draw cloth colliders:\n"
  28. " 0 - Disabled\n"
  29. " 1 - Cloth colliders");
  30. AZ_CVAR(int32_t, cloth_DebugDrawMotionConstraints, 0, nullptr, AZ::ConsoleFunctorFlags::Null,
  31. "Draw cloth motion constraints:\n"
  32. " 0 - Disabled\n"
  33. " 1 - Cloth motion constraints");
  34. AZ_CVAR(int32_t, cloth_DebugDrawBackstop, 0, nullptr, AZ::ConsoleFunctorFlags::Null,
  35. "Draw cloth backstop:\n"
  36. " 0 - Disabled\n"
  37. " 1 - Cloth backstop");
  38. ClothDebugDisplay::ClothDebugDisplay(ClothComponentMesh* clothComponentMesh)
  39. : m_clothComponentMesh(clothComponentMesh)
  40. {
  41. AZ_Assert(m_clothComponentMesh, "Invalid cloth component mesh");
  42. AzFramework::EntityDebugDisplayEventBus::Handler::BusConnect(m_clothComponentMesh->m_entityId);
  43. }
  44. ClothDebugDisplay::~ClothDebugDisplay()
  45. {
  46. AzFramework::EntityDebugDisplayEventBus::Handler::BusDisconnect();
  47. }
  48. bool ClothDebugDisplay::IsDebugDrawEnabled() const
  49. {
  50. return cloth_DebugDraw > 0
  51. || cloth_DebugDrawNormals > 0
  52. || cloth_DebugDrawColliders > 0
  53. || cloth_DebugDrawMotionConstraints > 0
  54. || cloth_DebugDrawBackstop > 0;
  55. }
  56. void ClothDebugDisplay::DisplayEntityViewport(
  57. const AzFramework::ViewportInfo& viewportInfo,
  58. AzFramework::DebugDisplayRequests& debugDisplay)
  59. {
  60. AZ_UNUSED(viewportInfo);
  61. if (!IsDebugDrawEnabled() || !m_clothComponentMesh->m_cloth)
  62. {
  63. return;
  64. }
  65. AZ::Transform entityTransform = AZ::Transform::CreateIdentity();
  66. AZ::TransformBus::EventResult(entityTransform, m_clothComponentMesh->m_entityId, &AZ::TransformInterface::GetWorldTM);
  67. debugDisplay.PushMatrix(entityTransform);
  68. if (cloth_DebugDraw > 0)
  69. {
  70. DisplayParticles(debugDisplay);
  71. DisplayWireCloth(debugDisplay);
  72. }
  73. if (cloth_DebugDrawNormals > 0)
  74. {
  75. bool showTangents = (cloth_DebugDrawNormals > 1);
  76. DisplayNormals(debugDisplay, showTangents);
  77. }
  78. if (cloth_DebugDrawColliders > 0)
  79. {
  80. DisplayColliders(debugDisplay);
  81. }
  82. if (cloth_DebugDrawMotionConstraints > 0)
  83. {
  84. DisplayMotionConstraints(debugDisplay);
  85. }
  86. if (cloth_DebugDrawBackstop > 0)
  87. {
  88. DisplaySeparationConstraints(debugDisplay);
  89. }
  90. debugDisplay.PopMatrix();
  91. }
  92. void ClothDebugDisplay::DisplayParticles(AzFramework::DebugDisplayRequests& debugDisplay)
  93. {
  94. const float particleAlpha = 1.0f;
  95. const float particleRadius = 0.007f;
  96. const auto& clothRenderParticles = m_clothComponentMesh->m_cloth->GetParticles();
  97. for(const auto& particle : clothRenderParticles)
  98. {
  99. const AZ::Vector4 color = AZ::Vector4::CreateFromVector3AndFloat(AZ::Vector3(particle.GetW()), particleAlpha);
  100. debugDisplay.SetColor(color);
  101. debugDisplay.DrawBall(particle.GetAsVector3(), particleRadius, false/*drawShaded*/);
  102. }
  103. }
  104. void ClothDebugDisplay::DisplayWireCloth(AzFramework::DebugDisplayRequests& debugDisplay)
  105. {
  106. const float lineAlpha = 1.0f;
  107. const auto& clothIndices = m_clothComponentMesh->m_cloth->GetInitialIndices();
  108. const auto& clothRenderParticles = m_clothComponentMesh->m_cloth->GetParticles();
  109. const size_t numIndices = clothIndices.size();
  110. if (numIndices % 3 != 0)
  111. {
  112. AZ_Warning("ClothDebugDisplay", false,
  113. "Cloth indices contains a list of triangles but its count (%zu) is not a multiple of 3.", numIndices);
  114. return;
  115. }
  116. for (size_t index = 0; index < numIndices; index += 3)
  117. {
  118. const SimIndexType& vertexIndex0 = clothIndices[index + 0];
  119. const SimIndexType& vertexIndex1 = clothIndices[index + 1];
  120. const SimIndexType& vertexIndex2 = clothIndices[index + 2];
  121. const SimParticleFormat& particle0 = clothRenderParticles[vertexIndex0];
  122. const SimParticleFormat& particle1 = clothRenderParticles[vertexIndex1];
  123. const SimParticleFormat& particle2 = clothRenderParticles[vertexIndex2];
  124. const AZ::Vector3 position0 = particle0.GetAsVector3();
  125. const AZ::Vector3 position1 = particle1.GetAsVector3();
  126. const AZ::Vector3 position2 = particle2.GetAsVector3();
  127. const AZ::Vector4 color0 = AZ::Vector4::CreateFromVector3AndFloat(AZ::Vector3(particle0.GetW()), lineAlpha);
  128. const AZ::Vector4 color1 = AZ::Vector4::CreateFromVector3AndFloat(AZ::Vector3(particle1.GetW()), lineAlpha);
  129. const AZ::Vector4 color2 = AZ::Vector4::CreateFromVector3AndFloat(AZ::Vector3(particle2.GetW()), lineAlpha);
  130. debugDisplay.DrawLine(position0, position1, color0, color1);
  131. debugDisplay.DrawLine(position1, position2, color1, color2);
  132. debugDisplay.DrawLine(position2, position0, color2, color0);
  133. }
  134. }
  135. void ClothDebugDisplay::DisplayNormals(AzFramework::DebugDisplayRequests& debugDisplay, bool showTangents)
  136. {
  137. const auto& clothRenderData = m_clothComponentMesh->GetRenderData();
  138. const auto& clothRenderParticles = clothRenderData.m_particles;
  139. const auto& clothRenderTangents = clothRenderData.m_tangents;
  140. const auto& clothRenderBitangents = clothRenderData.m_bitangents;
  141. const auto& clothRenderNormals = clothRenderData.m_normals;
  142. if (clothRenderParticles.size() != clothRenderNormals.size())
  143. {
  144. AZ_Warning("ClothDebugDisplay", false,
  145. "Number of cloth particles (%zu) doesn't match with the number of normals (%zu).",
  146. clothRenderParticles.size(), clothRenderNormals.size());
  147. return;
  148. }
  149. const float normalLength = 0.05f;
  150. const float tangentLength = 0.05f;
  151. const float bitangentLength = 0.05f;
  152. const AZ::Vector4 colorNormal = AZ::Colors::Blue.GetAsVector4();
  153. const AZ::Vector4 colorTangent = AZ::Colors::Red.GetAsVector4();
  154. const AZ::Vector4 colorBitangent = AZ::Colors::Green.GetAsVector4();
  155. for (size_t i = 0; i < clothRenderParticles.size(); ++i)
  156. {
  157. if (m_clothComponentMesh->m_meshRemappedVertices[i] < 0)
  158. {
  159. // Removed particle
  160. continue;
  161. }
  162. const AZ::Vector3 position = clothRenderParticles[i].GetAsVector3();
  163. debugDisplay.DrawLine(position, position + normalLength * clothRenderNormals[i], colorNormal, colorNormal);
  164. if (showTangents)
  165. {
  166. debugDisplay.DrawLine(position, position + tangentLength * clothRenderTangents[i], colorTangent, colorTangent);
  167. debugDisplay.DrawLine(position, position + bitangentLength * clothRenderBitangents[i], colorBitangent, colorBitangent);
  168. }
  169. }
  170. }
  171. void ClothDebugDisplay::DisplayColliders(AzFramework::DebugDisplayRequests& debugDisplay)
  172. {
  173. if (!m_clothComponentMesh->m_actorClothColliders)
  174. {
  175. return;
  176. }
  177. for (const SphereCollider& collider : m_clothComponentMesh->m_actorClothColliders->GetSphereColliders())
  178. {
  179. DrawSphere(debugDisplay, collider.m_radius, collider.m_currentModelSpaceTransform.GetTranslation(), AzFramework::ViewportColors::DeselectedColor);
  180. }
  181. for (const CapsuleCollider& collider : m_clothComponentMesh->m_actorClothColliders->GetCapsuleColliders())
  182. {
  183. DrawCapsule(debugDisplay, collider.m_radius, collider.m_height, collider.m_currentModelSpaceTransform, AzFramework::ViewportColors::DeselectedColor);
  184. }
  185. }
  186. void ClothDebugDisplay::DisplayMotionConstraints(AzFramework::DebugDisplayRequests& debugDisplay)
  187. {
  188. const AZ::Vector4 particleColor = AZ::Colors::Green.GetAsVector4();
  189. const AZ::Vector4 staticPraticleColor = AZ::Colors::Black.GetAsVector4();
  190. const AZ::Vector4 lineColor = AZ::Colors::Magenta.GetAsVector4();
  191. const float ballsize = 0.008f;
  192. for (const auto& constraint : m_clothComponentMesh->m_motionConstraints)
  193. {
  194. const AZ::Vector3 position = constraint.GetAsVector3();
  195. const float radius = constraint.GetW();
  196. debugDisplay.SetColor((radius > 0.0f) ? particleColor : staticPraticleColor);
  197. debugDisplay.DrawBall(position, ballsize, false/*drawShaded*/);
  198. debugDisplay.DrawLine(position, position + AZ::Vector3::CreateAxisY(radius), lineColor, lineColor);
  199. }
  200. }
  201. void ClothDebugDisplay::DisplaySeparationConstraints(AzFramework::DebugDisplayRequests& debugDisplay)
  202. {
  203. if (m_clothComponentMesh->m_separationConstraints.empty())
  204. {
  205. return;
  206. }
  207. const AZ::Vector4 sphereColor = AZ::Colors::Red.GetAsVector4();
  208. const AZ::Vector4 lineColor = AZ::Colors::Aqua.GetAsVector4();
  209. const auto& particles = m_clothComponentMesh->m_cloth->GetParticles();
  210. for (size_t i = 0; i < particles.size(); ++i)
  211. {
  212. const AZ::Vector3 position = m_clothComponentMesh->m_separationConstraints[i].GetAsVector3();
  213. const float radius = m_clothComponentMesh->m_separationConstraints[i].GetW();
  214. DrawSphere(debugDisplay, radius, position, sphereColor);
  215. debugDisplay.DrawLine(position, particles[i].GetAsVector3(), lineColor, lineColor);
  216. }
  217. }
  218. void ClothDebugDisplay::DrawSphere(
  219. AzFramework::DebugDisplayRequests& debugDisplay,
  220. float radius,
  221. const AZ::Vector3& position,
  222. const AZ::Color& color)
  223. {
  224. debugDisplay.SetColor(color);
  225. debugDisplay.DrawBall(position, radius, false/*drawShaded*/);
  226. debugDisplay.SetColor(AzFramework::ViewportColors::WireColor);
  227. debugDisplay.DrawWireSphere(position, radius);
  228. }
  229. void ClothDebugDisplay::DrawCapsule(
  230. AzFramework::DebugDisplayRequests& debugDisplay,
  231. float radius, float height,
  232. const AZ::Transform& transform,
  233. [[maybe_unused]] const AZ::Color& color)
  234. {
  235. const float heightStraightSection = AZStd::max(AZ::Constants::FloatEpsilon, height - 2.0f * radius);
  236. debugDisplay.SetColor(AzFramework::ViewportColors::WireColor);
  237. debugDisplay.DrawWireCapsule(transform.GetTranslation(), transform.GetBasisZ(), radius, heightStraightSection);
  238. }
  239. } // namespace NvCloth