DebugDraw.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <AzFramework/Entity/EntityDebugDisplayBus.h>
  10. #include <AzFramework/Physics/ShapeConfiguration.h>
  11. #include <AzFramework/Physics/Shape.h>
  12. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  13. #include <AzToolsFramework/Viewport/ViewportMessages.h>
  14. #include <PhysX/MeshAsset.h>
  15. #include <PhysX/Debug/PhysXDebugConfiguration.h>
  16. #include <PhysX/Debug/PhysXDebugInterface.h>
  17. namespace AZ
  18. {
  19. class ReflectContext;
  20. }
  21. namespace physx
  22. {
  23. class PxBase;
  24. }
  25. namespace PhysX
  26. {
  27. namespace DebugDraw
  28. {
  29. //! Open the PhysX Settings Window on the Global Settings tab.
  30. void OpenPhysXSettingsWindow();
  31. //! Determine if the global debug draw preference is set to the specified state.
  32. //! @param requiredState The collider debug state to check against the global state.
  33. //! @return True if global collider debug state matches the input requiredState.
  34. bool IsGlobalColliderDebugCheck(Debug::DebugDisplayData::GlobalCollisionDebugState requiredState);
  35. class DisplayCallback
  36. {
  37. public:
  38. virtual void Display(const AzFramework::ViewportInfo& viewportInfo,
  39. AzFramework::DebugDisplayRequests& debugDisplay) const = 0;
  40. protected:
  41. ~DisplayCallback() = default;
  42. };
  43. class Collider
  44. : protected AzFramework::EntityDebugDisplayEventBus::Handler
  45. , protected AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Handler
  46. , protected AzToolsFramework::EntitySelectionEvents::Bus::Handler
  47. {
  48. public:
  49. AZ_CLASS_ALLOCATOR(Collider, AZ::SystemAllocator);
  50. AZ_RTTI(Collider, "{7DE9CA01-DF1E-4D72-BBF4-76C9136BE6A2}");
  51. static void Reflect(AZ::ReflectContext* context);
  52. Collider();
  53. void Connect(AZ::EntityId entityId);
  54. void SetDisplayCallback(const DisplayCallback* callback);
  55. void Disconnect();
  56. bool HasCachedGeometry() const;
  57. void ClearCachedGeometry();
  58. void SetDisplayFlag(bool enable);
  59. bool IsDisplayFlagEnabled() const;
  60. void BuildMeshes(const Physics::ShapeConfiguration& shapeConfig, AZ::u32 geomIndex) const;
  61. struct ElementDebugInfo
  62. {
  63. // Note: this doesn't use member initializer because of a bug in Mac OS compiler
  64. ElementDebugInfo()
  65. : m_materialSlotIndex(0)
  66. , m_numTriangles(0)
  67. {}
  68. int m_materialSlotIndex;
  69. AZ::u32 m_numTriangles;
  70. };
  71. AZ::Color CalcDebugColor(const Physics::ColliderConfiguration& colliderConfig
  72. , const ElementDebugInfo& elementToDebugInfo = ElementDebugInfo()) const;
  73. AZ::Color CalcDebugColorWarning(const AZ::Color& baseColor, AZ::u32 triangleCount) const;
  74. void DrawSphere(AzFramework::DebugDisplayRequests& debugDisplay,
  75. const Physics::ColliderConfiguration& colliderConfig,
  76. const Physics::SphereShapeConfiguration& sphereShapeConfig,
  77. const AZ::Vector3& colliderScale = AZ::Vector3::CreateOne()) const;
  78. void DrawBox(
  79. AzFramework::DebugDisplayRequests& debugDisplay,
  80. const Physics::ColliderConfiguration& colliderConfig,
  81. const Physics::BoxShapeConfiguration& boxShapeConfig,
  82. const AZ::Vector3& colliderScale = AZ::Vector3::CreateOne()) const;
  83. void DrawCapsule(AzFramework::DebugDisplayRequests& debugDisplay,
  84. const Physics::ColliderConfiguration& colliderConfig,
  85. const Physics::CapsuleShapeConfiguration& capsuleShapeConfig,
  86. const AZ::Vector3& colliderScale = AZ::Vector3::CreateOne()) const;
  87. void DrawMesh(AzFramework::DebugDisplayRequests& debugDisplay,
  88. const Physics::ColliderConfiguration& colliderConfig,
  89. const Physics::CookedMeshShapeConfiguration& assetConfig,
  90. const AZ::Vector3& meshScale,
  91. AZ::u32 geomIndex) const;
  92. void DrawHeightfield(
  93. AzFramework::DebugDisplayRequests& debugDisplay,
  94. const AZ::Vector3& aabbCenterLocalBody,
  95. float drawDistance,
  96. const AZStd::shared_ptr<const Physics::Shape>& shape) const;
  97. void DrawPolygonPrism(
  98. AzFramework::DebugDisplayRequests& debugDisplay,
  99. const Physics::ColliderConfiguration& colliderConfig, const AZStd::vector<AZ::Vector3>& points) const;
  100. AZ::Transform GetColliderLocalTransform(const Physics::ColliderConfiguration& colliderConfig,
  101. const AZ::Vector3& colliderScale = AZ::Vector3::CreateOne()) const;
  102. AZ::u32 GetNumShapes() const;
  103. const AZStd::vector<AZ::Vector3>& GetVerts(AZ::u32 geomIndex) const;
  104. const AZStd::vector<AZ::Vector3>& GetPoints(AZ::u32 geomIndex) const;
  105. const AZStd::vector<AZ::u32>& GetIndices(AZ::u32 geomIndex) const;
  106. protected:
  107. // AzFramework::EntityDebugDisplayEventBus overrides ...
  108. void DisplayEntityViewport(
  109. const AzFramework::ViewportInfo& viewportInfo,
  110. AzFramework::DebugDisplayRequests& debugDisplay) override;
  111. // AzToolsFramework::ViewportInteraction::ViewportSettingsNotificationBus::Handler overrides ...
  112. void OnDrawHelpersChanged(bool enabled) override;
  113. // AzToolsFramework::EntitySelectionEvents::Bus::Handler overrides ...
  114. void OnSelected() override;
  115. void OnDeselected() override;
  116. void RefreshTreeHelper();
  117. // Internal mesh drawing subroutines
  118. void DrawTriangleMesh(
  119. AzFramework::DebugDisplayRequests& debugDisplay, const Physics::ColliderConfiguration& colliderConfig, AZ::u32 geomIndex,
  120. const AZ::Vector3& meshScale = AZ::Vector3::CreateOne()) const;
  121. void DrawConvexMesh(
  122. AzFramework::DebugDisplayRequests& debugDisplay, const Physics::ColliderConfiguration& colliderConfig, AZ::u32 geomIndex,
  123. const AZ::Vector3& meshScale = AZ::Vector3::CreateOne()) const;
  124. void BuildTriangleMesh(physx::PxBase* meshData, AZ::u32 geomIndex) const;
  125. void BuildConvexMesh(physx::PxBase* meshData, AZ::u32 geomIndex) const;
  126. AZStd::string GetEntityName() const;
  127. bool m_locallyEnabled = true; //!< Local setting to enable displaying the collider in editor view.
  128. AZ::EntityId m_entityId;
  129. const DisplayCallback* m_displayCallback = nullptr;
  130. struct GeometryData
  131. {
  132. AZStd::unordered_map<int, AZStd::vector<AZ::u32>> m_triangleIndexesByMaterialSlot;
  133. AZStd::vector<AZ::Vector3> m_verts;
  134. AZStd::vector<AZ::Vector3> m_points;
  135. AZStd::vector<AZ::u32> m_indices;
  136. };
  137. mutable AZStd::vector<GeometryData> m_geometry;
  138. PhysX::Debug::DebugDisplayDataChangedEvent::Handler m_debugDisplayDataChangedEvent;
  139. };
  140. } // namespace DebugDraw
  141. } // namespace PhysX