EditorMaterialSystemComponent.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/RPI.Reflect/Material/MaterialAsset.h>
  10. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  11. #include <Atom/RPI.Reflect/System/AnyAsset.h>
  12. #include <AtomLyIntegration/CommonFeatures/Material/EditorMaterialSystemComponentNotificationBus.h>
  13. #include <AtomLyIntegration/CommonFeatures/Material/EditorMaterialSystemComponentRequestBus.h>
  14. #include <AtomLyIntegration/CommonFeatures/Material/MaterialComponentBus.h>
  15. #include <AzCore/Asset/AssetCommon.h>
  16. #include <AzCore/Component/Component.h>
  17. #include <AzCore/Component/EntityBus.h>
  18. #include <AzCore/Component/TickBus.h>
  19. #include <AzFramework/Asset/AssetCatalogBus.h>
  20. #include <AzToolsFramework/ActionManager/ActionManagerRegistrationNotificationBus.h>
  21. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  22. #include <AzToolsFramework/AssetBrowser/AssetBrowserBus.h>
  23. #include <AzToolsFramework/Viewport/ActionBus.h>
  24. #include <Material/MaterialBrowserInteractions.h>
  25. #include <QPixmap>
  26. namespace AZ
  27. {
  28. namespace Render
  29. {
  30. //! System component that manages launching and maintaining connections with the material editor.
  31. class EditorMaterialSystemComponent final
  32. : public AZ::Component
  33. , public AZ::EntitySystemBus::Handler
  34. , public EditorMaterialSystemComponentNotificationBus::Handler
  35. , public EditorMaterialSystemComponentRequestBus::Handler
  36. , public MaterialComponentNotificationBus::Router
  37. , public AzToolsFramework::AssetBrowser::AssetBrowserInteractionNotificationBus::Handler
  38. , public AzToolsFramework::EditorEvents::Bus::Handler
  39. , public AzToolsFramework::ToolsApplicationNotificationBus::Handler
  40. , public AZ::SystemTickBus::Handler
  41. , public AzFramework::AssetCatalogEventBus::Handler
  42. , private AzToolsFramework::ActionManagerRegistrationNotificationBus::Handler
  43. {
  44. public:
  45. AZ_COMPONENT(EditorMaterialSystemComponent, "{96652157-DA0B-420F-B49C-0207C585144C}");
  46. static void Reflect(AZ::ReflectContext* context);
  47. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  48. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  49. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  50. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  51. protected:
  52. // AZ::Component interface overrides...
  53. void Init() override;
  54. void Activate() override;
  55. void Deactivate() override;
  56. private:
  57. //! EditorMaterialSystemComponentRequestBus::Handler overrides...
  58. void OpenMaterialEditor(const AZStd::string& sourcePath) override;
  59. void OpenMaterialCanvas(const AZStd::string& sourcePath) override;
  60. void OpenMaterialInspector(
  61. const AZ::EntityId& primaryEntityId,
  62. const AzToolsFramework::EntityIdSet& entityIdsToEdit,
  63. const AZ::Render::MaterialAssignmentId& materialAssignmentId) override;
  64. void RenderMaterialPreview(const AZ::EntityId& entityId, const AZ::Render::MaterialAssignmentId& materialAssignmentId) override;
  65. QPixmap GetRenderedMaterialPreview(
  66. const AZ::EntityId& entityId, const AZ::Render::MaterialAssignmentId& materialAssignmentId) const override;
  67. // AZ::EntitySystemBus::Handler overrides...
  68. void OnEntityDeactivated(const AZ::EntityId& entityId) override;
  69. //! AZ::SystemTickBus::Handler interface overrides...
  70. void OnSystemTick() override;
  71. // AzFramework::AssetCatalogEventBus::Handler overrides...
  72. void OnCatalogLoaded(const char* catalogFile) override;
  73. //! EditorMaterialSystemComponentNotificationBus::Handler overrides...
  74. void OnRenderMaterialPreviewRendered(
  75. const AZ::EntityId& entityId, const AZ::Render::MaterialAssignmentId& materialAssignmentId, const QPixmap& pixmap) override;
  76. //! MaterialComponentNotificationBus::Router overrides...
  77. void OnMaterialSlotLayoutChanged() override;
  78. //! AssetBrowserInteractionNotificationBus::Handler overrides...
  79. AzToolsFramework::AssetBrowser::SourceFileDetails GetSourceFileDetails(const char* fullSourceFileName) override;
  80. // AztoolsFramework::EditorEvents::Bus::Handler overrides...
  81. void NotifyRegisterViews() override;
  82. // AzToolsFramework::ToolsApplicationNotificationBus::Handler overrides...
  83. void AfterEntitySelectionChanged(const AzToolsFramework::EntityIdList& newlySelectedEntities, const AzToolsFramework::EntityIdList&) override;
  84. // AzToolsFramework::ActionManagerRegistrationNotificationBus::Handler...
  85. void OnActionRegistrationHook() override;
  86. void OnMenuBindingHook() override;
  87. void PurgePreviews();
  88. AZStd::unique_ptr<MaterialBrowserInteractions> m_materialBrowserInteractions;
  89. AZStd::unordered_set<AZStd::pair<AZ::EntityId, AZ::Render::MaterialAssignmentId>> m_materialPreviewRequests;
  90. AZStd::unordered_map<AZ::EntityId, AZStd::unordered_map<AZ::Render::MaterialAssignmentId, QPixmap>> m_materialPreviews;
  91. AZ::Data::Asset<AZ::RPI::ModelAsset> m_materialPreviewModelAsset;
  92. AZ::Data::Asset<AZ::RPI::AnyAsset> m_materialPreviewLightingPresetAsset;
  93. static constexpr const size_t MaterialPreviewLimit = 100;
  94. static constexpr const int MaterialPreviewResolution = 128;
  95. };
  96. } // namespace Render
  97. } // namespace AZ