Files
o3de/Gems/PhysXDebug/Code/Source/SystemComponent.h
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>

# Conflicts:
#	Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h
#	Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h
#	Code/Editor/Plugins/EditorCommon/stdafx.cpp
#	Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h
#	Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h
#	Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h
#	Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h
#	Code/Tools/AssetProcessor/native/precompiled.h
#	Code/Tools/Standalone/StandaloneTools_precompiled.h
#	Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h
#	Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h
#	Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h
#	Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake
#	Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h
#	Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h
#	Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp
#	Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake
#	Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h
#	Gems/Blast/Code/Source/StdAfx.cpp
#	Gems/Camera/Code/Source/Camera_precompiled.h
#	Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h
#	Gems/FastNoise/Code/Source/FastNoise_precompiled.h
#	Gems/Gestures/Code/Source/Gestures_precompiled.h
#	Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h
#	Gems/GraphCanvas/Code/precompiled.h
#	Gems/ImGui/Code/Source/ImGui_precompiled.h
#	Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h
#	Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h
#	Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp
#	Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h
#	Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h
#	Gems/LyShine/Code/Source/LyShine_precompiled.h
#	Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h
#	Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h
#	Gems/Maestro/Code/Source/Maestro_precompiled.h
#	Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h
#	Gems/Metastream/Code/Source/Metastream_precompiled.h
#	Gems/Microphone/Code/Source/Microphone_precompiled.h
#	Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h
#	Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h
#	Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h
#	Gems/PhysX/Code/Source/PhysX_precompiled.h
#	Gems/PhysX/Code/physx_unsupported_files.cmake
#	Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h
#	Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h
#	Gems/ScriptCanvas/Code/Editor/precompiled.h
#	Gems/ScriptCanvas/Code/Source/precompiled.h
#	Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h
#	Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h
#	Gems/ScriptEvents/Code/Source/precompiled.h
#	Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp
#	Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h
#	Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h
#	Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h
#	Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h
#	Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h
#	Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h
#	Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h
#	Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h
#	Gems/Twitch/Code/Source/Twitch_precompiled.h
#	Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
2021-07-16 15:42:37 -07:00

229 lines
7.8 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#pragma once
#include <AzCore/Component/Component.h>
#include <AzCore/Component/TickBus.h>
#include <AzFramework/Entity/EntityDebugDisplayBus.h>
#include <AzFramework/Physics/Common/PhysicsEvents.h>
#include <PhysXDebug/PhysXDebugBus.h>
#include <PxPhysicsAPI.h>
#include <Cry_Camera.h>
#include <IRenderAuxGeom.h>
#include <AzFramework/Physics/SystemBus.h>
#ifdef IMGUI_ENABLED
#include <imgui/imgui.h>
#include <ImGuiBus.h>
#endif // #ifdef IMGUI_ENABLED
namespace PhysXDebug
{
struct PhysXVisualizationSettings
{
AZ_RTTI(PhysXVisualizationSettings, "{A3A03872-36A3-44AB-B0A9-29F709E8E3B0}");
virtual ~PhysXVisualizationSettings() = default;
bool m_visualizationEnabled = false;
bool m_visualizeCollidersByProximity = false;
// physx culling only applied to: eCOLLISION_SHAPES, eCOLLISION_EDGES and eCOLLISION_FNORMALS (eCOLLISION_AABBS are not culled by PhysX!)
// see: \PhysX_3.4\Source\PhysX\src\NpShapeManager.cpp
float m_scale = 1.0f;
bool m_collisionShapes = true;
bool m_collisionEdges = true;
bool m_collisionFNormals = false;
// the remaining properties will start *disable*
bool m_collisionAabbs = false;
bool m_collisionAxes = false;
bool m_collisionCompounds = false;
bool m_collisionStatic = false;
bool m_collisionDynamic = false;
bool m_bodyAxes = false;
bool m_bodyMassAxes = false;
bool m_bodyLinVelocity = false;
bool m_bodyAngVelocity = false;
bool m_contactPoint = false;
bool m_contactNormal = false;
bool m_jointLocalFrames = false;
bool m_jointLimits = false;
bool m_mbpRegions = false;
bool m_actorAxes = false;
/// Determine if the PhysX Debug Gem Visualization is currently enabled (for the editor context)
inline bool IsPhysXDebugEnabled() { return m_visualizationEnabled; };
};
struct Culling
{
AZ_RTTI(Culling, "{20727A63-4FF7-4F31-B6F5-7FEFCB7CB153}");
virtual ~Culling() = default;
bool m_enabled = true;
bool m_boxWireframe = false;
float m_boxSize = 35.0f;
};
struct ColorMappings
{
AZ_RTTI(ColorMappings, "{021E40A6-568E-430A-9332-EF180DACD3C0}");
// user defined colors for physx debug primitives
AZ::Color m_defaultColor;
AZ::Color m_black;
AZ::Color m_red;
AZ::Color m_green;
AZ::Color m_blue;
AZ::Color m_yellow;
AZ::Color m_magenta;
AZ::Color m_cyan;
AZ::Color m_white;
AZ::Color m_grey;
AZ::Color m_darkRed;
AZ::Color m_darkGreen;
AZ::Color m_darkBlue;
};
class SystemComponent
: public AZ::Component
, protected PhysXDebugRequestBus::Handler
, public AZ::TickBus::Handler
, public CrySystemEventBus::Handler
#ifdef IMGUI_ENABLED
, public ImGui::ImGuiUpdateListenerBus::Handler
#endif // IMGUI_ENABLED
{
public:
AZ_COMPONENT(SystemComponent, "{111041CE-4C75-48E0-87C3-20938C05B9E0}");
static void Reflect(AZ::ReflectContext* context);
static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
SystemComponent();
// PhysxDebugDraw::PhysxDebugDrawRequestBus
void SetVisualization(bool enabled) override;
void ToggleVisualizationConfiguration() override;
void SetCullingBoxSize(float cullingBoxSize) override;
void ToggleCullingWireFrame() override;
void ToggleColliderProximityDebugVisualization() override;
#ifdef IMGUI_ENABLED
void OnImGuiMainMenuUpdate() override;
#endif // IMGUI_ENABLED
protected:
// AZ::Component interface implementation
void Activate() override;
void Deactivate() override;
// TickBus::Handler
void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
int GetTickOrder() override { return AZ::ComponentTickBus::TICK_FIRST + 1; }
// CrySystemEvents
void OnCrySystemInitialized(ISystem&, const SSystemInitParams&) override;
private:
/// Configure a PhysX scene debug visualization properties.
void ConfigurePhysXVisualizationParameters();
/// Convert from PhysX Visualization debug colors to user defined colors.
/// @param originalColor a color from the PhysX debug visualization data.
/// @return a user specified color mapping (defaulting to the original PhysX color).
AZ::Color MapOriginalPhysXColorToUserDefinedValues(const physx::PxU32& originalColor);
/// Initialise the PhysX debug draw colors based on defaults.
void InitPhysXColorMappings();
/// Draw the culling box being used by the viewport.
/// @param cullingBoxAabb culling box Aabb to debug draw.
void DrawDebugCullingBox(const AZ::Aabb& cullingBoxAabb);
/// Configure primary debug draw settings for PhysX
/// @param context the reflect context to utilize.
static void ReflectPhysXDebugSettings(AZ::ReflectContext* context);
/// Configure a culling box for PhysX visualization from the active camera.
void ConfigureCullingBox();
/// Gather visualization lines for this scene.
void GatherLines(const physx::PxRenderBuffer& rb);
/// Gather visualization triangles for this scene.
void GatherTriangles(const physx::PxRenderBuffer& rb);
/// Gather Joint Limits.
void GatherJointLimits();
/// Helper functions to wrap buffer management functionality.
void ClearBuffers();
void GatherBuffers();
void RenderBuffers();
/// Updates PhysX preferences to perform collider visualization based on proximity to camera.
void UpdateColliderVisualizationByProximity();
#ifdef IMGUI_ENABLED
/// Build a specific color picker menu option.
void BuildColorPickingMenuItem(const AZStd::string& label, AZ::Color& color);
#endif // IMGUI_ENABLED
physx::PxScene* GetCurrentPxScene();
// Main configuration
PhysXVisualizationSettings m_settings;
Culling m_culling;
ColorMappings m_colorMappings;
AZ::ScriptTimePoint m_currentTime;
bool m_registered = false;
physx::PxBounds3 m_cullingBox;
bool m_editorPhysicsSceneDirty = true;
static const float m_maxCullingBoxSize;
AZStd::vector<AZ::Vector3> m_linePoints;
AZStd::vector<AZ::Color> m_lineColors;
AZStd::vector<AZ::Vector3> m_trianglePoints;
AZStd::vector<AZ::Color> m_triangleColors;
// joint limit buffers
AZStd::vector<AZ::Vector3> m_jointVertexBuffer;
AZStd::vector<AZ::u32> m_jointIndexBuffer;
AZStd::vector<AZ::Vector3> m_jointLineBuffer;
AZStd::vector<bool> m_jointLineValidityBuffer;
AzPhysics::SceneEvents::OnSceneSimulationFinishHandler m_sceneFinishSimHandler;
};
/// Possible console parameters for physx_Debug cvar.
enum class DebugCVarValues : uint8_t
{
Disable, ///< Disable debug visualization.
Enable, ///< Enable debug visualization.
SwitchConfigurationPreference, ///< Switch between basic and full visualization configuration.
ColliderProximityDebug ///< Toggle visualize collision shapes by proximity to camera in editor mode.
};
}