3
0

MeshComponentController.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <Atom/Feature/Mesh/MeshFeatureProcessorInterface.h>
  10. #include <Atom/RPI.Public/Model/Model.h>
  11. #include <Atom/Feature/LightingChannel/LightingChannelConfiguration.h>
  12. #include <AtomCore/Instance/InstanceDatabase.h>
  13. #include <AtomLyIntegration/AtomImGuiTools/AtomImGuiToolsBus.h>
  14. #include <AtomLyIntegration/CommonFeatures/Material/MaterialAssignment.h>
  15. #include <AtomLyIntegration/CommonFeatures/Material/MaterialComponentBus.h>
  16. #include <AtomLyIntegration/CommonFeatures/Mesh/MeshComponentBus.h>
  17. #include <AtomLyIntegration/CommonFeatures/Mesh/MeshHandleStateBus.h>
  18. #include <AzCore/Component/Component.h>
  19. #include <AzCore/Component/ComponentBus.h>
  20. #include <AzCore/Component/NonUniformScaleBus.h>
  21. #include <AzCore/Component/TransformBus.h>
  22. #include <AzFramework/Render/GeometryIntersectionBus.h>
  23. #include <AzFramework/Visibility/BoundsBus.h>
  24. #include <AzFramework/Visibility/VisibleGeometryBus.h>
  25. namespace AZ
  26. {
  27. namespace Render
  28. {
  29. //! A configuration structure for the MeshComponentController
  30. class MeshComponentConfig final
  31. : public AZ::ComponentConfig
  32. {
  33. public:
  34. AZ_RTTI(AZ::Render::MeshComponentConfig, "{63737345-51B1-472B-9355-98F99993909B}", ComponentConfig);
  35. AZ_CLASS_ALLOCATOR(MeshComponentConfig, SystemAllocator);
  36. static void Reflect(AZ::ReflectContext* context);
  37. // Editor helper functions
  38. bool IsAssetSet();
  39. bool LodTypeIsScreenCoverage();
  40. bool LodTypeIsSpecificLOD();
  41. bool ShowLodConfig();
  42. AZStd::vector<AZStd::pair<RPI::Cullable::LodOverride, AZStd::string>> GetLodOverrideValues();
  43. Data::Asset<RPI::ModelAsset> m_modelAsset = { AZ::Data::AssetLoadBehavior::QueueLoad };
  44. RHI::DrawItemSortKey m_sortKey = 0;
  45. bool m_excludeFromReflectionCubeMaps = false;
  46. bool m_isAlwaysDynamic = false;
  47. bool m_useForwardPassIblSpecular = false;
  48. bool m_isRayTracingEnabled = true;
  49. // the ModelAsset should support ray intersection if any one of the two flags are enabled.
  50. bool m_editorRayIntersection = false; // Set to true if the config is for EditorMeshComponent so the ModelAsset always support ray intersection
  51. bool m_enableRayIntersection = false; // Set to true if a mesh need ray intersection support at runtime.
  52. RPI::Cullable::LodType m_lodType = RPI::Cullable::LodType::Default;
  53. RPI::Cullable::LodOverride m_lodOverride = aznumeric_cast<RPI::Cullable::LodOverride>(0);
  54. float m_minimumScreenCoverage = 1.0f / 1080.0f;
  55. float m_qualityDecayRate = 0.5f;
  56. AZ::Render::LightingChannelConfiguration m_lightingChannelConfig;
  57. };
  58. class MeshComponentController final
  59. : private MeshComponentRequestBus::Handler
  60. , private MeshHandleStateRequestBus::Handler
  61. , private AtomImGuiTools::AtomImGuiMeshCallbackBus::Handler
  62. , public AzFramework::BoundsRequestBus::Handler
  63. , public AzFramework::VisibleGeometryRequestBus::Handler
  64. , public AzFramework::RenderGeometry::IntersectionRequestBus::Handler
  65. , private TransformNotificationBus::Handler
  66. , private MaterialConsumerRequestBus::Handler
  67. , private MaterialComponentNotificationBus::Handler
  68. {
  69. public:
  70. friend class EditorMeshComponent;
  71. AZ_CLASS_ALLOCATOR(MeshComponentController, AZ::SystemAllocator);
  72. AZ_RTTI(AZ::Render::MeshComponentController, "{D0F35FAC-4194-4C89-9487-D000DDB8B272}");
  73. ~MeshComponentController();
  74. static void Reflect(AZ::ReflectContext* context);
  75. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  76. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  77. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  78. MeshComponentController() = default;
  79. MeshComponentController(const MeshComponentConfig& config);
  80. void Activate(const AZ::EntityComponentIdPair& entityComponentIdPair);
  81. void Deactivate();
  82. void SetConfiguration(const MeshComponentConfig& config);
  83. const MeshComponentConfig& GetConfiguration() const;
  84. private:
  85. AZ_DISABLE_COPY(MeshComponentController);
  86. // MeshComponentRequestBus overrides ...
  87. void SetModelAsset(Data::Asset<RPI::ModelAsset> modelAsset) override;
  88. Data::Asset<const RPI::ModelAsset> GetModelAsset() const override;
  89. void SetModelAssetId(Data::AssetId modelAssetId) override;
  90. Data::AssetId GetModelAssetId() const override;
  91. void SetModelAssetPath(const AZStd::string& modelAssetPath) override;
  92. AZStd::string GetModelAssetPath() const override;
  93. AZ::Data::Instance<RPI::Model> GetModel() const override;
  94. // AtomImGuiTools::AtomImGuiMeshCallbackBus::Handler overrides ...
  95. const RPI::MeshDrawPacketLods* GetDrawPackets() const override;
  96. // MeshHandleStateRequestBus overrides ...
  97. const MeshFeatureProcessorInterface::MeshHandle* GetMeshHandle() const override;
  98. void SetSortKey(RHI::DrawItemSortKey sortKey) override;
  99. RHI::DrawItemSortKey GetSortKey() const override;
  100. void SetIsAlwaysDynamic(bool isAlwaysDynamic) override;
  101. bool GetIsAlwaysDynamic() const override;
  102. void SetLodType(RPI::Cullable::LodType lodType) override;
  103. RPI::Cullable::LodType GetLodType() const override;
  104. void SetLodOverride(RPI::Cullable::LodOverride lodOverride) override;
  105. RPI::Cullable::LodOverride GetLodOverride() const override;
  106. void SetMinimumScreenCoverage(float minimumScreenCoverage) override;
  107. float GetMinimumScreenCoverage() const override;
  108. void SetQualityDecayRate(float qualityDecayRate) override;
  109. float GetQualityDecayRate() const override;
  110. void SetVisibility(bool visible) override;
  111. bool GetVisibility() const override;
  112. void SetRayTracingEnabled(bool enabled) override;
  113. bool GetRayTracingEnabled() const override;
  114. void SetExcludeFromReflectionCubeMaps(bool excludeFromReflectionCubeMaps) override;
  115. bool GetExcludeFromReflectionCubeMaps() const override;
  116. // AzFramework::BoundsRequestBus::Handler overrides ...
  117. AZ::Aabb GetWorldBounds() const override;
  118. AZ::Aabb GetLocalBounds() const override;
  119. // AzFramework::VisibleGeometryRequestBus::Handler overrides ...
  120. void GetVisibleGeometry(const AZ::Aabb& bounds, AzFramework::VisibleGeometryContainer& geometryContainer) const override;
  121. // Searches all shader items referenced by the material for one with a matching draw list tag.
  122. // Returns true if a matching tag was found. Otherwise, false.
  123. bool DoesMaterialUseDrawListTag(
  124. const AZ::Data::Instance<AZ::RPI::Material> material, const AZ::RHI::DrawListTag searchDrawListTag) const;
  125. // IntersectionRequestBus overrides ...
  126. AzFramework::RenderGeometry::RayResult RenderGeometryIntersect(const AzFramework::RenderGeometry::RayRequest& ray) const override;
  127. // TransformNotificationBus::Handler overrides ...
  128. void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override;
  129. // MaterialConsumerRequestBus::Handler overrides ...
  130. MaterialAssignmentId FindMaterialAssignmentId(
  131. const MaterialAssignmentLodIndex lod, const AZStd::string& label) const override;
  132. MaterialAssignmentLabelMap GetMaterialLabels() const override;
  133. MaterialAssignmentMap GetDefaultMaterialMap() const override;
  134. AZStd::unordered_set<AZ::Name> GetModelUvNames() const override;
  135. // MaterialComponentNotificationBus::Handler overrides ...
  136. void OnMaterialsUpdated(const MaterialAssignmentMap& materials) override;
  137. void OnMaterialPropertiesUpdated(const MaterialAssignmentMap& materials) override;
  138. //! Check if the model asset requires to be cloned (e.g. cloth) for unique model instances.
  139. //! @param modelAsset The model asset to check.
  140. //! @result True in case the model asset needs to be cloned before creating the model. False if there is a 1:1 relationship between
  141. //! the model asset and the model and it is static and shared. In the second case the m_originalModelAsset of the mesh handle is
  142. //! equal to the model asset that the model is linked to.
  143. static bool RequiresCloning(const Data::Asset<RPI::ModelAsset>& modelAsset);
  144. void HandleModelChange(const Data::Instance<RPI::Model>& model);
  145. void HandleObjectSrgCreate(const Data::Instance<RPI::ShaderResourceGroup>& objectSrg);
  146. void RegisterModel();
  147. void UnregisterModel();
  148. void RefreshModelRegistration();
  149. RPI::Cullable::LodConfiguration GetMeshLodConfiguration() const;
  150. void HandleNonUniformScaleChange(const AZ::Vector3& nonUniformScale);
  151. void LightingChannelMaskChanged();
  152. Render::MeshFeatureProcessorInterface* m_meshFeatureProcessor = nullptr;
  153. Render::MeshFeatureProcessorInterface::MeshHandle m_meshHandle;
  154. TransformInterface* m_transformInterface = nullptr;
  155. AZ::EntityComponentIdPair m_entityComponentIdPair;
  156. bool m_isVisible = true;
  157. MeshComponentConfig m_configuration;
  158. AZ::Vector3 m_cachedNonUniformScale = AZ::Vector3::CreateOne();
  159. //! Cached bus to use to notify RenderGeometry::Intersector the entity/component has changed.
  160. AzFramework::RenderGeometry::IntersectionNotificationBus::BusPtr m_intersectionNotificationBus;
  161. MeshHandleDescriptor::ModelChangedEvent::Handler m_modelChangedEventHandler
  162. {
  163. [&](const Data::Instance<RPI::Model>& model) { HandleModelChange(model); }
  164. };
  165. MeshHandleDescriptor::ObjectSrgCreatedEvent::Handler m_objectSrgCreatedHandler
  166. {
  167. [&](const Data::Instance<RPI::ShaderResourceGroup>& objectSrg) { HandleObjectSrgCreate(objectSrg); }
  168. };
  169. AZ::NonUniformScaleChangedEvent::Handler m_nonUniformScaleChangedHandler
  170. {
  171. [&](const AZ::Vector3& nonUniformScale) { HandleNonUniformScaleChange(nonUniformScale); }
  172. };
  173. };
  174. } // namespace Render
  175. } // namespace AZ