EditorMaterialComponentSlot.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <AtomLyIntegration/CommonFeatures/Material/EditorMaterialSystemComponentNotificationBus.h>
  11. #include <AtomLyIntegration/CommonFeatures/Material/MaterialAssignment.h>
  12. #include <AzCore/Asset/AssetCommon.h>
  13. #include <AzCore/std/containers/vector.h>
  14. #include <AzCore/std/string/string.h>
  15. #include <QPixmap>
  16. namespace AZ
  17. {
  18. namespace Render
  19. {
  20. //! Details for a single editable material assignment
  21. //! EditorMaterialComponentSlot will attempt to forward all changes to selected entities for actions invoked from a context menu or
  22. //! entity inspector. The set of selected or pinned entities will be retrieved directly from the inspector. Updates will be
  23. //! applied to the entities and their materials using MaterialComponentRequestBus.
  24. //! Undo and redo for all of those entities will be managed by calls to OnDataChanged.
  25. struct EditorMaterialComponentSlot final
  26. : public AZ::Data::AssetBus::Handler
  27. , public EditorMaterialSystemComponentNotificationBus::Handler
  28. {
  29. AZ_RTTI(EditorMaterialComponentSlot, "{344066EB-7C3D-4E92-B53D-3C9EBD546488}");
  30. AZ_CLASS_ALLOCATOR(EditorMaterialComponentSlot, SystemAllocator);
  31. static bool ConvertVersion(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement);
  32. static void Reflect(ReflectContext* context);
  33. EditorMaterialComponentSlot() = default;
  34. EditorMaterialComponentSlot(const AZ::EntityId& entityId, const MaterialAssignmentId& materialAssignmentId);
  35. ~EditorMaterialComponentSlot();
  36. //! Get cached preview image as a buffer to use as an RPE attribute
  37. //! If a cached image isn't avalible then a request will be made to render one
  38. AZStd::vector<char> GetPreviewPixmapData() const;
  39. //! Returns the overridden asset id if it's valid, otherwise gets the default asseet id
  40. AZ::Data::AssetId GetActiveAssetId() const;
  41. //! Returns the default asseet id of the material provded by the model
  42. AZ::Data::AssetId GetDefaultAssetId() const;
  43. //! Returns the display name of the material slot
  44. AZStd::string GetLabel() const;
  45. //! Returns true if the active material asset has a source material
  46. bool HasSourceData() const;
  47. //! Assign a new material override asset
  48. void SetAsset(const Data::Asset<RPI::MaterialAsset>& asset);
  49. //! Assign a new material override asset
  50. void SetAssetId(const Data::AssetId& assetId);
  51. //! Remove material and property overrides
  52. void Clear();
  53. //! Remove material overrides
  54. void ClearMaterial();
  55. //! Remove property overrides
  56. void ClearProperties();
  57. //! Launch the material canvas
  58. void OpenMaterialCanvas() const;
  59. //! Launch the material editor application and open the active material for this slot
  60. void OpenMaterialEditor() const;
  61. //! Open the material exporter, aka generate source materials dialog, and apply resulting materials to a set of entities
  62. void OpenMaterialExporter(const AzToolsFramework::EntityIdSet& entityIdsToEdit);
  63. //! Open the material instance inspector to edit material property overrides for a set of entities
  64. void OpenMaterialInspector(const AzToolsFramework::EntityIdSet& entityIdsToEdit);
  65. //! Open the dialog for mapping UV channels for models and materials
  66. void OpenUvNameMapInspector(const AzToolsFramework::EntityIdSet& entityIdsToEdit);
  67. //! Bypass the UI to export the active material to the export path
  68. void ExportMaterial(const AZStd::string& exportPath, bool overwrite);
  69. AZ::EntityId m_entityId;
  70. MaterialAssignmentId m_id;
  71. Data::Asset<RPI::MaterialAsset> m_materialAsset;
  72. private:
  73. void OpenPopupMenu(const AZ::Data::AssetId& assetId, const AZ::Data::AssetType& assetType);
  74. void OnMaterialChangedFromRPE();
  75. void OnDataChanged(const AzToolsFramework::EntityIdSet& entityIdsToEdit, bool updateAsset);
  76. //! EditorMaterialSystemComponentNotificationBus::Handler overrides...
  77. void OnRenderMaterialPreviewReady(
  78. const AZ::EntityId& entityId, const AZ::Render::MaterialAssignmentId& materialAssignmentId, const QPixmap& pixmap) override;
  79. //! AZ::Data::AssetBus::Handler overrides...
  80. void OnAssetReloaded(Data::Asset<Data::AssetData> asset) override;
  81. void UpdatePreview() const;
  82. mutable bool m_updatePreview = true;
  83. };
  84. // Vector of slots for assignable or overridable material data.
  85. using EditorMaterialComponentSlotContainer = AZStd::vector<EditorMaterialComponentSlot>;
  86. // Table containing all editable material data that is displayed in the edit context and inspector
  87. // The vector represents all the LODs that can have material overrides.
  88. // The container will be populated with every potential material slot on an associated model, using its default values.
  89. // Whenever changes are made to this container, the modified values are copied into the controller configuration material assignment
  90. // map as overrides that will be applied to material instances
  91. using EditorMaterialComponentSlotsByLodContainer = AZStd::vector<EditorMaterialComponentSlotContainer>;
  92. } // namespace Render
  93. } // namespace AZ