DebugDrawSystemComponent.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #pragma once
  9. #include <AzCore/Component/Component.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzCore/Component/EntityBus.h>
  12. #include <AzCore/std/parallel/mutex.h>
  13. #include <DebugDraw/DebugDrawBus.h>
  14. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  15. // DebugDraw components
  16. #include "DebugDrawLineComponent.h"
  17. #include "DebugDrawRayComponent.h"
  18. #include "DebugDrawSphereComponent.h"
  19. #include "DebugDrawObbComponent.h"
  20. #include "DebugDrawTextComponent.h"
  21. #ifdef DEBUGDRAW_GEM_EDITOR
  22. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  23. #endif // DEBUGDRAW_GEM_EDITOR
  24. #include <Atom/RPI.Public/SceneBus.h>
  25. #include <Atom/Bootstrap/BootstrapNotificationBus.h>
  26. namespace DebugDraw
  27. {
  28. // DebugDraw elements that don't have corresponding component representations yet
  29. class DebugDrawAabbElement
  30. {
  31. public:
  32. AZ_CLASS_ALLOCATOR(DebugDrawAabbElement, AZ::SystemAllocator);
  33. AZ_TYPE_INFO(DebugDrawAabbElement, "{3B3E45AC-95B5-477F-BC34-58765A031BF1}");
  34. AZ::EntityId m_targetEntityId;
  35. AZ::Aabb m_aabb;
  36. float m_duration;
  37. AZ::ScriptTimePoint m_activateTime;
  38. AZ::Color m_color;
  39. AZ::Vector3 m_worldLocation;
  40. AZ::ComponentId m_owningEditorComponent;
  41. DebugDrawAabbElement()
  42. : m_duration(0.f)
  43. , m_color(1.0f, 1.0f, 1.0f, 1.0f)
  44. , m_worldLocation(AZ::Vector3::CreateZero())
  45. , m_owningEditorComponent(AZ::InvalidComponentId)
  46. {
  47. }
  48. };
  49. class DebugDrawSystemComponent
  50. : public AZ::Component
  51. , public AZ::EntityBus::MultiHandler
  52. , protected DebugDrawRequestBus::Handler
  53. , protected DebugDrawInternalRequestBus::Handler
  54. , public AZ::RPI::SceneNotificationBus::Handler
  55. , public AZ::Render::Bootstrap::NotificationBus::Handler
  56. #ifdef DEBUGDRAW_GEM_EDITOR
  57. , protected AzToolsFramework::EditorEntityContextNotificationBus::Handler
  58. #endif // DEBUGDRAW_GEM_EDITOR
  59. {
  60. public:
  61. AZ_COMPONENT(DebugDrawSystemComponent, "{48D54C3C-F284-43A5-B070-106F2CEB7154}");
  62. static void Reflect(AZ::ReflectContext* context);
  63. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  64. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  65. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  66. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  67. #ifdef DEBUGDRAW_GEM_EDITOR
  68. // EditorEntityContextNotificationBus interface implementation
  69. void OnStopPlayInEditor() override;
  70. #endif // DEBUGDRAW_GEM_EDITOR
  71. protected:
  72. // DebugDrawRequestBus interface implementation
  73. void DrawAabb(const AZ::Aabb& aabb, const AZ::Color& color, float duration) override;
  74. void DrawAabbOnEntity(const AZ::EntityId& targetEntity, const AZ::Aabb& aabb, const AZ::Color& color, float duration) override;
  75. void DrawLineBatchLocationToLocation(const AZStd::vector<DebugDraw::DebugDrawLineElement>& lineBatch) override;
  76. void DrawLineLocationToLocation(const AZ::Vector3& startLocation, const AZ::Vector3& endLocation, const AZ::Color& color, float duration) override;
  77. void DrawLineEntityToLocation(const AZ::EntityId& startEntity, const AZ::Vector3& endLocation, const AZ::Color& color, float duration) override;
  78. void DrawLineEntityToEntity(const AZ::EntityId& startEntity, const AZ::EntityId& endEntity, const AZ::Color& color, float duration) override;
  79. void DrawObb(const AZ::Obb& obb, const AZ::Color& color, float duration) override;
  80. void DrawObbOnEntity(const AZ::EntityId& targetEntity, const AZ::Obb& obb, const AZ::Color& color, float duration) override;
  81. void DrawRayLocationToDirection(const AZ::Vector3& worldLocation, const AZ::Vector3& worldDirection, const AZ::Color& color, float duration) override;
  82. void DrawRayEntityToDirection(const AZ::EntityId& startEntity, const AZ::Vector3& worldDirection, const AZ::Color& color, float duration) override;
  83. void DrawRayEntityToEntity(const AZ::EntityId& startEntity, const AZ::EntityId& endEntity, const AZ::Color& color, float duration) override;
  84. void DrawSphereAtLocation(const AZ::Vector3& worldLocation, float radius, const AZ::Color& color, float duration) override;
  85. void DrawSphereOnEntity(const AZ::EntityId& targetEntity, float radius, const AZ::Color& color, float duration) override;
  86. void DrawTextAtLocation(const AZ::Vector3& worldLocation, const AZStd::string& text, const AZ::Color& color, float duration) override;
  87. void DrawTextOnEntity(const AZ::EntityId& targetEntity, const AZStd::string& text, const AZ::Color& color, float duration) override;
  88. void DrawTextOnScreen(const AZStd::string& text, const AZ::Color& color, float duration) override;
  89. // DebugDrawInternalRequestBus interface implementation
  90. void RegisterDebugDrawComponent(AZ::Component* component) override;
  91. void UnregisterDebugDrawComponent(AZ::Component* component) override;
  92. // AZ::Component interface implementation
  93. void Init() override;
  94. void Activate() override;
  95. void Deactivate() override;
  96. // SceneNotificationBus
  97. void OnBeginPrepareRender() override;
  98. // AZ::Render::Bootstrap::NotificationBus
  99. void OnBootstrapSceneReady(AZ::RPI::Scene* scene) override;
  100. // EntityBus
  101. void OnEntityDeactivated(const AZ::EntityId& entityId) override;
  102. // Ticking functions for drawing debug elements
  103. void OnTickAabbs(AzFramework::DebugDisplayRequests& debugDisplay);
  104. void OnTickLines(AzFramework::DebugDisplayRequests& debugDisplay);
  105. void OnTickObbs(AzFramework::DebugDisplayRequests& debugDisplay);
  106. void OnTickRays(AzFramework::DebugDisplayRequests& debugDisplay);
  107. void OnTickSpheres(AzFramework::DebugDisplayRequests& debugDisplay);
  108. void OnTickText(AzFramework::DebugDisplayRequests& debugDisplay);
  109. // Element creation functions, used when DebugDraw components register themselves
  110. void CreateAabbEntryForComponent(const AZ::EntityId& componentEntityId, const DebugDrawAabbElement& element);
  111. void CreateLineEntryForComponent(const AZ::EntityId& componentEntityId, const DebugDrawLineElement& element);
  112. void CreateObbEntryForComponent(const AZ::EntityId& componentEntityId, const DebugDrawObbElement& element);
  113. void CreateRayEntryForComponent(const AZ::EntityId& componentEntityId, const DebugDrawRayElement& element);
  114. void CreateSphereEntryForComponent(const AZ::EntityId& componentEntityId, const DebugDrawSphereElement& element);
  115. void CreateTextEntryForComponent(const AZ::EntityId& componentEntityId, const DebugDrawTextElement& element);
  116. template <typename F>
  117. void removeExpiredDebugElementsFromVector(AZStd::vector<F>& vectorToExpire);
  118. AZStd::vector<DebugDrawAabbElement> m_activeAabbs;
  119. AZStd::mutex m_activeAabbsMutex;
  120. AZStd::vector<DebugDrawLineElement> m_activeLines;
  121. AZStd::mutex m_activeLinesMutex;
  122. AZStd::vector<DebugDrawObbElement> m_activeObbs;
  123. AZStd::mutex m_activeObbsMutex;
  124. AZStd::vector<DebugDrawRayElement> m_activeRays;
  125. AZStd::mutex m_activeRaysMutex;
  126. AZStd::vector<DebugDrawSphereElement> m_activeSpheres;
  127. AZStd::mutex m_activeSpheresMutex;
  128. AZStd::vector<DebugDrawTextElement> m_activeTexts;
  129. AZStd::mutex m_activeTextsMutex;
  130. double m_currentTime;
  131. AZStd::vector<AZ::Vector3> m_batchPoints;
  132. AZStd::vector<AZ::Color> m_batchColors;
  133. };
  134. }