RecastNavigationDebugDraw.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <Misc/RecastNavigationDebugDraw.h>
  9. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  10. namespace RecastNavigation
  11. {
  12. RecastNavigationDebugDraw::RecastNavigationDebugDraw(bool drawLines) : m_drawLines(drawLines)
  13. {
  14. }
  15. void RecastNavigationDebugDraw::begin(duDebugDrawPrimitives prim, [[maybe_unused]] float size)
  16. {
  17. m_currentPrim = prim;
  18. m_verticesToDraw.clear();
  19. }
  20. void RecastNavigationDebugDraw::vertex(const float* pos, unsigned color)
  21. {
  22. AddVertex(pos[0], pos[1], pos[2], color);
  23. }
  24. void RecastNavigationDebugDraw::vertex(float x, float y, float z, unsigned color)
  25. {
  26. AddVertex(x, y, z, color);
  27. }
  28. void RecastNavigationDebugDraw::vertex(const float* pos, unsigned color, [[maybe_unused]] const float* uv)
  29. {
  30. AddVertex(pos[0], pos[1], pos[2], color);
  31. }
  32. void RecastNavigationDebugDraw::vertex(float x, float y, float z, unsigned color,
  33. [[maybe_unused]] const float u, [[maybe_unused]] const float v)
  34. {
  35. AddVertex(x, y, z, color);
  36. }
  37. void RecastNavigationDebugDraw::end()
  38. {
  39. constexpr AZ::Crc32 viewportId = AzFramework::g_defaultSceneEntityDebugDisplayId;
  40. AzFramework::DebugDisplayRequestBus::BusPtr debugDisplayBus;
  41. AzFramework::DebugDisplayRequestBus::Bind(debugDisplayBus, viewportId);
  42. AzFramework::DebugDisplayRequests* debugDisplay = AzFramework::DebugDisplayRequestBus::FindFirstHandler(debugDisplayBus);
  43. if (!debugDisplay)
  44. {
  45. return;
  46. }
  47. switch (m_currentPrim)
  48. {
  49. case DU_DRAW_POINTS:
  50. for (auto& i : m_verticesToDraw)
  51. {
  52. if (m_viewAabb.Contains(i.first))
  53. {
  54. AZ::Color color = AZ::Color::CreateZero();
  55. color.FromU32(i.second);
  56. debugDisplay->SetColor(color);
  57. debugDisplay->DrawPoint(i.first, 1);
  58. }
  59. }
  60. break;
  61. case DU_DRAW_TRIS:
  62. for (size_t i = 2; i < m_verticesToDraw.size(); i += 3)
  63. {
  64. if (m_viewAabb.Contains(m_verticesToDraw[i - 2].first) ||
  65. m_viewAabb.Contains(m_verticesToDraw[i - 1].first) ||
  66. m_viewAabb.Contains(m_verticesToDraw[i].first))
  67. {
  68. AZ::Color color = AZ::Color::CreateZero();
  69. color.FromU32(m_verticesToDraw[i].second);
  70. debugDisplay->DrawTriangles(
  71. {
  72. m_verticesToDraw[i - 2].first,
  73. m_verticesToDraw[i - 1].first,
  74. m_verticesToDraw[i - 0].first
  75. }, color);
  76. }
  77. }
  78. break;
  79. case DU_DRAW_QUADS:
  80. for (size_t i = 3; i < m_verticesToDraw.size(); i += 4)
  81. {
  82. if (m_viewAabb.Contains(m_verticesToDraw[i - 3].first) ||
  83. m_viewAabb.Contains(m_verticesToDraw[i - 2].first) ||
  84. m_viewAabb.Contains(m_verticesToDraw[i - 1].first) ||
  85. m_viewAabb.Contains(m_verticesToDraw[i].first))
  86. {
  87. AZ::Color color = AZ::Color::CreateZero();
  88. color.FromU32(m_verticesToDraw[i].second);
  89. debugDisplay->SetColor(color);
  90. debugDisplay->DrawQuad(
  91. m_verticesToDraw[i - 3].first, m_verticesToDraw[i - 2].first,
  92. m_verticesToDraw[i - 1].first, m_verticesToDraw[i - 0].first);
  93. }
  94. }
  95. break;
  96. case DU_DRAW_LINES:
  97. for (size_t i = 1; m_drawLines && i < m_verticesToDraw.size(); i++)
  98. {
  99. if (m_viewAabb.Contains(m_verticesToDraw[i - 1].first) ||
  100. m_viewAabb.Contains(m_verticesToDraw[i].first))
  101. {
  102. AZ::Color color0 = AZ::Color::CreateZero();
  103. color0.FromU32(m_verticesToDraw[i].second);
  104. AZ::Color color1 = AZ::Color::CreateZero();
  105. color1.FromU32(m_verticesToDraw[i].second);
  106. debugDisplay->DrawLine(m_verticesToDraw[i - 1].first, m_verticesToDraw[i].first,
  107. color0.GetAsVector4(), color1.GetAsVector4());
  108. }
  109. }
  110. break;
  111. }
  112. }
  113. void RecastNavigationDebugDraw::SetViewableAabb(const AZ::Aabb& cullingAabb)
  114. {
  115. m_viewAabb = cullingAabb;
  116. }
  117. void RecastNavigationDebugDraw::AddVertex(float x, float y, float z, unsigned color)
  118. {
  119. const float temp[3] = { x, y, z };
  120. const RecastVector3 v = RecastVector3::CreateFromFloatValuesWithoutAxisSwapping(temp);
  121. m_verticesToDraw.emplace_back(v.AsVector3WithZup(), color);
  122. }
  123. } // namespace RecastNavigation