/* * 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace AzPhysics { class SceneInterface; struct SimulatedBody; } namespace PhysX { struct EditorProxyAssetShapeConfig { AZ_CLASS_ALLOCATOR(EditorProxyAssetShapeConfig, AZ::SystemAllocator, 0); AZ_RTTI(EditorProxyAssetShapeConfig, "{C1B46450-C2A3-4115-A2FB-E5FF3BAAAD15}"); static void Reflect(AZ::ReflectContext* context); virtual ~EditorProxyAssetShapeConfig() = default; AZ::Data::Asset m_pxAsset{ AZ::Data::AssetLoadBehavior::QueueLoad }; Physics::PhysicsAssetShapeConfiguration m_configuration; }; //! Proxy container for only displaying a specific shape configuration depending on the shapeType selected. struct EditorProxyShapeConfig { AZ_CLASS_ALLOCATOR(EditorProxyShapeConfig, AZ::SystemAllocator, 0); AZ_RTTI(EditorProxyShapeConfig, "{531FB42A-42A9-4234-89BA-FD349EF83D0C}"); static void Reflect(AZ::ReflectContext* context); EditorProxyShapeConfig() = default; EditorProxyShapeConfig(const Physics::ShapeConfiguration& shapeConfiguration); virtual ~EditorProxyShapeConfig() = default; Physics::ShapeType m_shapeType = Physics::ShapeType::PhysicsAsset; Physics::SphereShapeConfiguration m_sphere; Physics::BoxShapeConfiguration m_box; Physics::CapsuleShapeConfiguration m_capsule; EditorProxyAssetShapeConfig m_physicsAsset; bool m_hasNonUniformScale = false; //!< Whether there is a non-uniform scale component on this entity. AZ::u8 m_subdivisionLevel = 4; //!< The level of subdivision if a primitive shape is replaced with a convex mesh due to scaling. Physics::CookedMeshShapeConfiguration m_cookedMesh; bool IsSphereConfig() const; bool IsBoxConfig() const; bool IsCapsuleConfig() const; bool IsAssetConfig() const; Physics::ShapeConfiguration& GetCurrent(); const Physics::ShapeConfiguration& GetCurrent() const; AZStd::shared_ptr CloneCurrent() const; private: bool ShowingSubdivisionLevel() const; AZ::u32 OnShapeTypeChanged(); AZ::u32 OnConfigurationChanged(); Physics::ShapeType m_lastShapeType = Physics::ShapeType::PhysicsAsset; }; class EditorColliderComponentDescriptor; //! Editor PhysX Collider Component. //! class EditorColliderComponent : public AzToolsFramework::Components::EditorComponentBase , protected DebugDraw::DisplayCallback , protected AzToolsFramework::EntitySelectionEvents::Bus::Handler , private AzToolsFramework::BoxManipulatorRequestBus::Handler , private AZ::Data::AssetBus::MultiHandler , private PhysX::MeshColliderComponentRequestsBus::Handler , private AZ::TransformNotificationBus::Handler , private PhysX::ColliderShapeRequestBus::Handler , private AZ::Render::MeshComponentNotificationBus::Handler , private PhysX::EditorColliderComponentRequestBus::Handler , private PhysX::EditorColliderValidationRequestBus::Handler , private AzPhysics::SimulatedBodyComponentRequestsBus::Handler { public: AZ_RTTI(EditorColliderComponent, "{FD429282-A075-4966-857F-D0BBF186CFE6}", AzToolsFramework::Components::EditorComponentBase); AZ_EDITOR_COMPONENT_INTRUSIVE_DESCRIPTOR_TYPE(EditorColliderComponent); AZ_CLASS_ALLOCATOR(EditorColliderComponent, AZ::SystemAllocator, 0); friend class EditorColliderComponentDescriptor; static void Reflect(AZ::ReflectContext* context); static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided); static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required); static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent); static AZ::ComponentDescriptor* CreateDescriptor(); EditorColliderComponent() = default; EditorColliderComponent( const Physics::ColliderConfiguration& colliderConfiguration, const Physics::ShapeConfiguration& shapeConfiguration); // these functions are made virtual because we call them from other modules virtual const EditorProxyShapeConfig& GetShapeConfiguration() const; virtual const Physics::ColliderConfiguration& GetColliderConfiguration() const; virtual Physics::ColliderConfiguration GetColliderConfigurationScaled() const; void BuildGameEntity(AZ::Entity* gameEntity) override; private: AZ_DISABLE_COPY_MOVE(EditorColliderComponent) // AZ::Component void Activate() override; void Deactivate() override; //! AzToolsFramework::EntitySelectionEvents void OnSelected() override; void OnDeselected() override; // DisplayCallback void Display(AzFramework::DebugDisplayRequests& debugDisplay) const override; void DisplayMeshCollider(AzFramework::DebugDisplayRequests& debugDisplay) const; void DisplayUnscaledPrimitiveCollider(AzFramework::DebugDisplayRequests& debugDisplay) const; void DisplayScaledPrimitiveCollider(AzFramework::DebugDisplayRequests& debugDisplay) const; // AZ::Data::AssetBus::Handler void OnAssetReady(AZ::Data::Asset asset) override; void OnAssetReloaded(AZ::Data::Asset asset) override; // PhysXMeshColliderComponentRequestBus AZ::Data::Asset GetMeshAsset() const override; Physics::MaterialId GetMaterialId() const override; void SetMeshAsset(const AZ::Data::AssetId& id) override; void SetMaterialId(const Physics::MaterialId& id) override; void UpdateMaterialSlotsFromMeshAsset(); // TransformBus void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override; // non-uniform scale handling void OnNonUniformScaleChanged(const AZ::Vector3& nonUniformScale); // AzToolsFramework::BoxManipulatorRequestBus AZ::Vector3 GetDimensions() override; void SetDimensions(const AZ::Vector3& dimensions) override; AZ::Transform GetCurrentTransform() override; AZ::Transform GetCurrentLocalTransform() override; AZ::Vector3 GetBoxScale() override; // AZ::Render::MeshComponentNotificationBus void OnModelReady(const AZ::Data::Asset& modelAsset, const AZ::Data::Instance& model) override; // PhysX::ColliderShapeBus AZ::Aabb GetColliderShapeAabb() override; bool IsTrigger() override; // PhysX::EditorColliderComponentBus void SetColliderOffset(const AZ::Vector3& offset) override; AZ::Vector3 GetColliderOffset() override; void SetColliderRotation(const AZ::Quaternion& rotation) override; AZ::Quaternion GetColliderRotation() override; AZ::Transform GetColliderWorldTransform() override; void SetShapeType(Physics::ShapeType shapeType) override; Physics::ShapeType GetShapeType() override; void SetSphereRadius(float radius) override; float GetSphereRadius() override; void SetCapsuleRadius(float radius) override; float GetCapsuleRadius() override; void SetCapsuleHeight(float height) override; float GetCapsuleHeight() override; void SetAssetScale(const AZ::Vector3& scale) override; AZ::Vector3 GetAssetScale() override; // PhysX::EditorColliderValidationRequestBus overrides ... void ValidateRigidBodyMeshGeometryType() override; AZ::Transform GetColliderLocalTransform() const; EditorProxyShapeConfig m_shapeConfiguration; Physics::ColliderConfiguration m_configuration; AZ::u32 OnConfigurationChanged(); void UpdateShapeConfigurationScale(); // AzPhysics::SimulatedBodyComponentRequestsBus::Handler overrides ... void EnablePhysics() override; void DisablePhysics() override; bool IsPhysicsEnabled() const override; AZ::Aabb GetAabb() const override; AzPhysics::SimulatedBody* GetSimulatedBody() override; AzPhysics::SimulatedBodyHandle GetSimulatedBodyHandle() const override; AzPhysics::SceneQueryHit RayCast(const AzPhysics::RayCastRequest& request) override; // Mesh collider void UpdateMeshAsset(); bool IsAssetConfig() const; void CreateStaticEditorCollider(); void ClearStaticEditorCollider(); void BuildDebugDrawMesh() const; AZ::ComponentDescriptor::StringWarningArray GetComponentWarnings() const { return m_componentWarnings; }; using ComponentModeDelegate = AzToolsFramework::ComponentModeFramework::ComponentModeDelegate; ComponentModeDelegate m_componentModeDelegate; //!< Responsible for detecting ComponentMode activation //!< and creating a concrete ComponentMode. AzPhysics::SceneInterface* m_sceneInterface = nullptr; AzPhysics::SceneHandle m_editorSceneHandle = AzPhysics::InvalidSceneHandle; AzPhysics::SimulatedBodyHandle m_editorBodyHandle = AzPhysics::InvalidSimulatedBodyHandle; // Auto-assigning collision mesh utility functions bool ShouldUpdateCollisionMeshFromRender() const; void SetCollisionMeshFromRender(); AZ::Data::Asset GetRenderMeshAsset() const; AZ::Data::AssetId FindMatchingPhysicsAsset(const AZ::Data::Asset& renderMeshAsset, const AZStd::vector& physicsAssets); void ValidateAssetMaterials(); void InitEventHandlers(); DebugDraw::Collider m_colliderDebugDraw; AzPhysics::SystemEvents::OnConfigurationChangedEvent::Handler m_physXConfigChangedHandler; AzPhysics::SystemEvents::OnMaterialLibraryChangedEvent::Handler m_onMaterialLibraryChangedEventHandler; AZ::Transform m_cachedWorldTransform; AZ::NonUniformScaleChangedEvent::Handler m_nonUniformScaleChangedHandler; //!< Responds to changes in non-uniform scale. bool m_hasNonUniformScale = false; //!< Whether there is a non-uniform scale component on this entity. AZ::Vector3 m_cachedNonUniformScale = AZ::Vector3::CreateOne(); //!< Caches the current non-uniform scale. mutable AZStd::optional m_scaledPrimitive; //!< Approximation for non-uniformly scaled primitive. AZ::Aabb m_cachedAabb = AZ::Aabb::CreateNull(); //!< Cache the Aabb to avoid recalculating it. bool m_cachedAabbDirty = true; //!< Track whether the cached Aabb needs to be recomputed. AZ::ComponentDescriptor::StringWarningArray m_componentWarnings; }; class EditorColliderComponentDescriptor : public AZ::ComponentDescriptorHelper { public: AZ_CLASS_ALLOCATOR(EditorColliderComponentDescriptor, AZ::SystemAllocator, 0); AZ_TYPE_INFO(EditorColliderComponentDescriptor, "{E099B5D6-B03F-436C-AB8E-7ADE4DAD74A0}"); EditorColliderComponentDescriptor() = default; void Reflect(AZ::ReflectContext* reflection) const override; void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided, const AZ::Component* instance) const override; void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent, const AZ::Component* instance) const override; void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required, const AZ::Component* instance) const override; void GetWarnings(AZ::ComponentDescriptor::StringWarningArray& warnings, const AZ::Component* instance) const override; }; } // namespace PhysX