3
0

SystemComponent.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <AzFramework/Entity/EntityDebugDisplayBus.h>
  12. #include <AzFramework/Physics/Common/PhysicsEvents.h>
  13. #include <PhysXDebug/PhysXDebugBus.h>
  14. #include <PxPhysicsAPI.h>
  15. #include <IRenderAuxGeom.h>
  16. #include <CryCommon/CrySystemBus.h>
  17. #include <AzFramework/Physics/SystemBus.h>
  18. #ifdef IMGUI_ENABLED
  19. #include <imgui/imgui.h>
  20. #include <ImGuiBus.h>
  21. #endif // #ifdef IMGUI_ENABLED
  22. namespace PhysXDebug
  23. {
  24. struct PhysXVisualizationSettings
  25. {
  26. AZ_RTTI(PhysXVisualizationSettings, "{A3A03872-36A3-44AB-B0A9-29F709E8E3B0}");
  27. virtual ~PhysXVisualizationSettings() = default;
  28. bool m_visualizationEnabled = false;
  29. bool m_visualizeCollidersByProximity = false;
  30. // physx culling only applied to: eCOLLISION_SHAPES, eCOLLISION_EDGES and eCOLLISION_FNORMALS (eCOLLISION_AABBS are not culled by PhysX!)
  31. // see: \PhysX_3.4\Source\PhysX\src\NpShapeManager.cpp
  32. float m_scale = 1.0f;
  33. bool m_collisionShapes = true;
  34. bool m_collisionEdges = true;
  35. bool m_collisionFNormals = false;
  36. // the remaining properties will start *disable*
  37. bool m_collisionAabbs = false;
  38. bool m_collisionAxes = false;
  39. bool m_collisionCompounds = false;
  40. bool m_collisionStatic = false;
  41. bool m_collisionDynamic = false;
  42. bool m_bodyAxes = false;
  43. bool m_bodyMassAxes = false;
  44. bool m_bodyLinVelocity = false;
  45. bool m_bodyAngVelocity = false;
  46. bool m_contactPoint = false;
  47. bool m_contactNormal = false;
  48. bool m_jointLocalFrames = false;
  49. bool m_jointLimits = false;
  50. bool m_mbpRegions = false;
  51. bool m_actorAxes = false;
  52. /// Determine if the PhysX Debug Gem Visualization is currently enabled (for the editor context)
  53. inline bool IsPhysXDebugEnabled() { return m_visualizationEnabled; };
  54. };
  55. struct Culling
  56. {
  57. AZ_RTTI(Culling, "{20727A63-4FF7-4F31-B6F5-7FEFCB7CB153}");
  58. virtual ~Culling() = default;
  59. bool m_enabled = true;
  60. bool m_boxWireframe = false;
  61. float m_boxSize = 35.0f;
  62. };
  63. struct ColorMappings
  64. {
  65. AZ_RTTI(ColorMappings, "{021E40A6-568E-430A-9332-EF180DACD3C0}");
  66. // user defined colors for physx debug primitives
  67. AZ::Color m_defaultColor;
  68. AZ::Color m_black;
  69. AZ::Color m_red;
  70. AZ::Color m_green;
  71. AZ::Color m_blue;
  72. AZ::Color m_yellow;
  73. AZ::Color m_magenta;
  74. AZ::Color m_cyan;
  75. AZ::Color m_white;
  76. AZ::Color m_grey;
  77. AZ::Color m_darkRed;
  78. AZ::Color m_darkGreen;
  79. AZ::Color m_darkBlue;
  80. };
  81. class SystemComponent
  82. : public AZ::Component
  83. , protected PhysXDebugRequestBus::Handler
  84. , public AZ::TickBus::Handler
  85. , public CrySystemEventBus::Handler
  86. #ifdef IMGUI_ENABLED
  87. , public ImGui::ImGuiUpdateListenerBus::Handler
  88. #endif // IMGUI_ENABLED
  89. {
  90. public:
  91. AZ_COMPONENT(SystemComponent, "{111041CE-4C75-48E0-87C3-20938C05B9E0}");
  92. static void Reflect(AZ::ReflectContext* context);
  93. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  94. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  95. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  96. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  97. SystemComponent();
  98. // PhysxDebugDraw::PhysxDebugDrawRequestBus
  99. void SetVisualization(bool enabled) override;
  100. void ToggleVisualizationConfiguration() override;
  101. void SetCullingBoxSize(float cullingBoxSize) override;
  102. void ToggleCullingWireFrame() override;
  103. void ToggleColliderProximityDebugVisualization() override;
  104. #ifdef IMGUI_ENABLED
  105. void OnImGuiMainMenuUpdate() override;
  106. #endif // IMGUI_ENABLED
  107. protected:
  108. // AZ::Component interface implementation
  109. void Activate() override;
  110. void Deactivate() override;
  111. // TickBus::Handler
  112. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  113. int GetTickOrder() override { return AZ::ComponentTickBus::TICK_FIRST + 1; }
  114. // CrySystemEvents
  115. void OnCrySystemInitialized(ISystem&, const SSystemInitParams&) override;
  116. private:
  117. /// Configure a PhysX scene debug visualization properties.
  118. void ConfigurePhysXVisualizationParameters();
  119. /// Convert from PhysX Visualization debug colors to user defined colors.
  120. /// @param originalColor a color from the PhysX debug visualization data.
  121. /// @return a user specified color mapping (defaulting to the original PhysX color).
  122. AZ::Color MapOriginalPhysXColorToUserDefinedValues(const physx::PxU32& originalColor);
  123. /// Initialise the PhysX debug draw colors based on defaults.
  124. void InitPhysXColorMappings();
  125. /// Draw the culling box being used by the viewport.
  126. /// @param cullingBoxAabb culling box Aabb to debug draw.
  127. void DrawDebugCullingBox(const AZ::Aabb& cullingBoxAabb);
  128. /// Configure primary debug draw settings for PhysX
  129. /// @param context the reflect context to utilize.
  130. static void ReflectPhysXDebugSettings(AZ::ReflectContext* context);
  131. /// Configure a culling box for PhysX visualization from the active camera.
  132. void ConfigureCullingBox();
  133. /// Gather visualization lines for this scene.
  134. void GatherLines(const physx::PxRenderBuffer& rb);
  135. /// Gather visualization triangles for this scene.
  136. void GatherTriangles(const physx::PxRenderBuffer& rb);
  137. /// Gather Joint Limits.
  138. void GatherJointLimits();
  139. /// Helper functions to wrap buffer management functionality.
  140. void ClearBuffers();
  141. void GatherBuffers();
  142. void RenderBuffers();
  143. /// Updates PhysX preferences to perform collider visualization based on proximity to camera.
  144. void UpdateColliderVisualizationByProximity();
  145. #ifdef IMGUI_ENABLED
  146. /// Build a specific color picker menu option.
  147. void BuildColorPickingMenuItem(const AZStd::string& label, AZ::Color& color);
  148. #endif // IMGUI_ENABLED
  149. physx::PxScene* GetCurrentPxScene();
  150. // Main configuration
  151. PhysXVisualizationSettings m_settings;
  152. Culling m_culling;
  153. ColorMappings m_colorMappings;
  154. AZ::ScriptTimePoint m_currentTime;
  155. bool m_registered = false;
  156. physx::PxBounds3 m_cullingBox;
  157. bool m_editorPhysicsSceneDirty = true;
  158. static const float m_maxCullingBoxSize;
  159. AZStd::vector<AZ::Vector3> m_linePoints;
  160. AZStd::vector<AZ::Color> m_lineColors;
  161. AZStd::vector<AZ::Vector3> m_trianglePoints;
  162. AZStd::vector<AZ::Color> m_triangleColors;
  163. // joint limit buffers
  164. AZStd::vector<AZ::Vector3> m_jointVertexBuffer;
  165. AZStd::vector<AZ::u32> m_jointIndexBuffer;
  166. AZStd::vector<AZ::Vector3> m_jointLineBuffer;
  167. AZStd::vector<bool> m_jointLineValidityBuffer;
  168. AzPhysics::SceneEvents::OnSceneSimulationFinishHandler m_sceneFinishSimHandler;
  169. };
  170. /// Possible console parameters for physx_Debug cvar.
  171. enum class DebugCVarValues : uint8_t
  172. {
  173. Disable, ///< Disable debug visualization.
  174. Enable, ///< Enable debug visualization.
  175. SwitchConfigurationPreference, ///< Switch between basic and full visualization configuration.
  176. ColliderProximityDebug ///< Toggle visualize collision shapes by proximity to camera in editor mode.
  177. };
  178. }