DebugDrawSystemComponent.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/RPI.Public/Shader/Shader.h>
  26. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  27. #include <Atom/Bootstrap/BootstrapNotificationBus.h>
  28. #include <RayTracing/RayTracingFeatureProcessor.h>
  29. namespace DebugDraw
  30. {
  31. // DebugDraw elements that don't have corresponding component representations yet
  32. class DebugDrawAabbElement
  33. {
  34. public:
  35. AZ_CLASS_ALLOCATOR(DebugDrawAabbElement, AZ::SystemAllocator);
  36. AZ_TYPE_INFO(DebugDrawAabbElement, "{3B3E45AC-95B5-477F-BC34-58765A031BF1}");
  37. AZ::EntityId m_targetEntityId;
  38. AZ::Aabb m_aabb;
  39. float m_duration;
  40. AZ::ScriptTimePoint m_activateTime;
  41. AZ::Color m_color;
  42. AZ::Vector3 m_worldLocation;
  43. AZ::ComponentId m_owningEditorComponent;
  44. DebugDrawAabbElement()
  45. : m_duration(0.f)
  46. , m_color(1.0f, 1.0f, 1.0f, 1.0f)
  47. , m_worldLocation(AZ::Vector3::CreateZero())
  48. , m_owningEditorComponent(AZ::InvalidComponentId)
  49. {
  50. }
  51. };
  52. class DebugDrawSystemComponent
  53. : public AZ::Component
  54. , public AZ::EntityBus::MultiHandler
  55. , protected DebugDrawRequestBus::Handler
  56. , protected DebugDrawInternalRequestBus::Handler
  57. , public AZ::RPI::SceneNotificationBus::Handler
  58. , public AZ::Render::Bootstrap::NotificationBus::Handler
  59. #ifdef DEBUGDRAW_GEM_EDITOR
  60. , protected AzToolsFramework::EditorEntityContextNotificationBus::Handler
  61. #endif // DEBUGDRAW_GEM_EDITOR
  62. {
  63. public:
  64. AZ_COMPONENT(DebugDrawSystemComponent, "{48D54C3C-F284-43A5-B070-106F2CEB7154}");
  65. static void Reflect(AZ::ReflectContext* context);
  66. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  67. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  68. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  69. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  70. #ifdef DEBUGDRAW_GEM_EDITOR
  71. // EditorEntityContextNotificationBus interface implementation
  72. void OnStopPlayInEditor() override;
  73. #endif // DEBUGDRAW_GEM_EDITOR
  74. protected:
  75. // DebugDrawRequestBus interface implementation
  76. void DrawAabb(const AZ::Aabb& aabb, const AZ::Color& color, float duration) override;
  77. void DrawAabbOnEntity(const AZ::EntityId& targetEntity, const AZ::Aabb& aabb, const AZ::Color& color, float duration) override;
  78. void DrawLineBatchLocationToLocation(const AZStd::vector<DebugDraw::DebugDrawLineElement>& lineBatch) override;
  79. void DrawLineLocationToLocation(const AZ::Vector3& startLocation, const AZ::Vector3& endLocation, const AZ::Color& color, float duration) override;
  80. void DrawLineEntityToLocation(const AZ::EntityId& startEntity, const AZ::Vector3& endLocation, const AZ::Color& color, float duration) override;
  81. void DrawLineEntityToEntity(const AZ::EntityId& startEntity, const AZ::EntityId& endEntity, const AZ::Color& color, float duration) override;
  82. void DrawObb(const AZ::Obb& obb, const AZ::Color& color, float duration) override;
  83. void DrawObbOnEntity(const AZ::EntityId& targetEntity, const AZ::Obb& obb, const AZ::Color& color, bool enableRayTracing, float duration) override;
  84. void DrawRayLocationToDirection(const AZ::Vector3& worldLocation, const AZ::Vector3& worldDirection, const AZ::Color& color, float duration) override;
  85. void DrawRayEntityToDirection(const AZ::EntityId& startEntity, const AZ::Vector3& worldDirection, const AZ::Color& color, float duration) override;
  86. void DrawRayEntityToEntity(const AZ::EntityId& startEntity, const AZ::EntityId& endEntity, const AZ::Color& color, float duration) override;
  87. void DrawSphereAtLocation(const AZ::Vector3& worldLocation, float radius, const AZ::Color& color, float duration) override;
  88. void DrawSphereOnEntity(const AZ::EntityId& targetEntity, float radius, const AZ::Color& color, bool enableRayTracing, float duration) override;
  89. void DrawTextAtLocation(const AZ::Vector3& worldLocation, const AZStd::string& text, const AZ::Color& color, float duration) override;
  90. void DrawTextOnEntity(const AZ::EntityId& targetEntity, const AZStd::string& text, const AZ::Color& color, float duration) override;
  91. void DrawTextOnScreen(const AZStd::string& text, const AZ::Color& color, float duration) override;
  92. void DrawScaledTextOnScreen(const AZStd::string& text, float fontScale, const AZ::Color& color, float duration) override;
  93. void DrawScaledTextOnScreenPos(float x, float y, const AZStd::string& text, float fontScale, const AZ::Color& color, float duration, bool bCenter = true) override;
  94. // DebugDrawInternalRequestBus interface implementation
  95. void RegisterDebugDrawComponent(AZ::Component* component) override;
  96. void UnregisterDebugDrawComponent(AZ::Component* component) override;
  97. // AZ::Component interface implementation
  98. void Init() override;
  99. void Activate() override;
  100. void Deactivate() override;
  101. // SceneNotificationBus
  102. void OnBeginPrepareRender() override;
  103. // AZ::Render::Bootstrap::NotificationBus
  104. void OnBootstrapSceneReady(AZ::RPI::Scene* scene) override;
  105. // EntityBus
  106. void OnEntityDeactivated(const AZ::EntityId& entityId) override;
  107. // Ticking functions for drawing debug elements
  108. void OnTickAabbs(AzFramework::DebugDisplayRequests& debugDisplay);
  109. void OnTickLines(AzFramework::DebugDisplayRequests& debugDisplay);
  110. void OnTickObbs(AzFramework::DebugDisplayRequests& debugDisplay);
  111. void OnTickRays(AzFramework::DebugDisplayRequests& debugDisplay);
  112. void OnTickSpheres(AzFramework::DebugDisplayRequests& debugDisplay);
  113. void OnTickText(AzFramework::DebugDisplayRequests& debugDisplay);
  114. // Element creation functions, used when DebugDraw components register themselves
  115. void CreateAabbEntryForComponent(const AZ::EntityId& componentEntityId, const DebugDrawAabbElement& element);
  116. void CreateLineEntryForComponent(const AZ::EntityId& componentEntityId, const DebugDrawLineElement& element);
  117. void CreateObbEntryForComponent(const AZ::EntityId& componentEntityId, const DebugDrawObbElement& element);
  118. void CreateRayEntryForComponent(const AZ::EntityId& componentEntityId, const DebugDrawRayElement& element);
  119. void CreateSphereEntryForComponent(const AZ::EntityId& componentEntityId, const DebugDrawSphereElement& element);
  120. void CreateTextEntryForComponent(const AZ::EntityId& componentEntityId, const DebugDrawTextElement& element);
  121. template <typename F>
  122. void removeExpiredDebugElementsFromVector(AZStd::vector<F>& vectorToExpire);
  123. struct DebugDrawSphereElementWrapper : DebugDrawSphereElement
  124. {
  125. AZ::Vector3 m_previousWorldLocation = AZ::Vector3::CreateZero();
  126. float m_previousRadius = 0;
  127. uint32_t m_localInstanceIndex = 0;
  128. };
  129. struct DebugDrawObbElementWrapper : DebugDrawObbElement
  130. {
  131. AZ::Vector3 m_previousWorldLocation;
  132. AZ::Vector3 m_previousScale;
  133. AZ::Quaternion m_previousRotation;
  134. };
  135. // Adds the debug shape to the ray tracing scene by using the ProceduralGeometry interface of RayTracingFeatureProcessor and custom
  136. // intersection shaders for hit detection
  137. void AddRaytracingData(DebugDrawSphereElementWrapper& element);
  138. void AddRaytracingData(DebugDrawObbElementWrapper& element);
  139. // Removes the debug shape from the ray tracing scene if it was added before
  140. void RemoveRaytracingData(const DebugDrawSphereElementWrapper& element);
  141. void RemoveRaytracingData(const DebugDrawObbElementWrapper& element);
  142. AZStd::vector<DebugDrawAabbElement> m_activeAabbs;
  143. AZStd::mutex m_activeAabbsMutex;
  144. AZStd::vector<DebugDrawLineElement> m_activeLines;
  145. AZStd::mutex m_activeLinesMutex;
  146. AZStd::vector<DebugDrawObbElementWrapper> m_activeObbs;
  147. AZStd::mutex m_activeObbsMutex;
  148. AZStd::vector<DebugDrawRayElement> m_activeRays;
  149. AZStd::mutex m_activeRaysMutex;
  150. AZStd::vector<DebugDrawSphereElementWrapper> m_activeSpheres;
  151. AZStd::mutex m_activeSpheresMutex;
  152. AZStd::vector<DebugDrawTextElement> m_activeTexts;
  153. AZStd::mutex m_activeTextsMutex;
  154. double m_currentTime;
  155. AZStd::vector<AZ::Vector3> m_batchPoints;
  156. AZStd::vector<AZ::Color> m_batchColors;
  157. AZ::Render::RayTracingFeatureProcessor* m_rayTracingFeatureProcessor = nullptr;
  158. AZ::RPI::Ptr<AZ::RPI::Buffer> m_spheresRayTracingIndicesBuffer;
  159. AZ::Render::RayTracingFeatureProcessor::ProceduralGeometryTypeHandle m_sphereRayTracingTypeHandle;
  160. AZ::Render::RayTracingIndexList<1> m_spheresRayTracingIndices;
  161. AZ::Render::RayTracingFeatureProcessor::ProceduralGeometryTypeHandle m_obbRayTracingTypeHandle;
  162. };
  163. }