EditorMaterialModelUvNameMapInspector.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #include <Atom/RPI.Edit/Common/AssetUtils.h>
  9. #include <Atom/RPI.Edit/Common/JsonUtils.h>
  10. #include <Atom/RPI.Edit/Material/MaterialPropertyId.h>
  11. #include <Atom/RPI.Edit/Material/MaterialSourceData.h>
  12. #include <Atom/RPI.Edit/Material/MaterialTypeSourceData.h>
  13. #include <Atom/RPI.Edit/Material/MaterialUtils.h>
  14. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  15. #include <Atom/RPI.Reflect/Material/MaterialPropertiesLayout.h>
  16. #include <Atom/RPI.Reflect/Material/MaterialTypeAsset.h>
  17. #include <AtomToolsFramework/Inspector/InspectorPropertyGroupWidget.h>
  18. #include <AtomToolsFramework/Util/Util.h>
  19. #include <AzFramework/API/ApplicationAPI.h>
  20. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  21. #include <AzToolsFramework/API/EditorWindowRequestBus.h>
  22. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  23. #include <Material/EditorMaterialModelUvNameMapInspector.h>
  24. AZ_PUSH_DISABLE_WARNING(4251 4800, "-Wunknown-warning-option") // disable warnings spawned by QT
  25. #include <QApplication>
  26. #include <QDialog>
  27. #include <QDialogButtonBox>
  28. #include <QHBoxLayout>
  29. #include <QMenu>
  30. #include <QPushButton>
  31. #include <QToolButton>
  32. #include <QVBoxLayout>
  33. AZ_POP_DISABLE_WARNING
  34. namespace AZ
  35. {
  36. namespace Render
  37. {
  38. namespace EditorMaterialComponentInspector
  39. {
  40. MaterialModelUvNameMapInspector::MaterialModelUvNameMapInspector(
  41. const AZ::Data::AssetId& assetId,
  42. const RPI::MaterialModelUvOverrideMap& matModUvOverrides,
  43. const AZStd::unordered_set<AZ::Name>& modelUvNames,
  44. MaterialModelUvOverrideMapChangedCallBack matModUvOverrideMapChangedCallBack,
  45. QWidget* parent)
  46. : AtomToolsFramework::InspectorWidget(parent)
  47. , m_matModUvOverrideMapChangedCallBack(matModUvOverrideMapChangedCallBack)
  48. , m_matModUvOverrides(matModUvOverrides)
  49. {
  50. // Load the originating product asset from which the new source has set will be generated
  51. auto materialAssetOutcome = AZ::RPI::AssetUtils::LoadAsset<AZ::RPI::MaterialAsset>(assetId);
  52. AZ_Error(
  53. "AZ::Render::EditorMaterialComponentInspector", materialAssetOutcome, "Failed to load material asset: %s",
  54. assetId.ToString<AZStd::string>().c_str());
  55. auto materialAsset = materialAssetOutcome.GetValue();
  56. // Get material UV names
  57. m_materialUvNames = materialAsset->GetMaterialTypeAsset()->GetUvNameMap();
  58. SetModelUvNames(modelUvNames);
  59. ResetModelUvNameIndices();
  60. }
  61. MaterialModelUvNameMapInspector::~MaterialModelUvNameMapInspector()
  62. {
  63. }
  64. void MaterialModelUvNameMapInspector::Reset()
  65. {
  66. m_activeProperty = {};
  67. m_group = {};
  68. AtomToolsFramework::InspectorWidget::Reset();
  69. }
  70. void MaterialModelUvNameMapInspector::Populate()
  71. {
  72. AddGroupsBegin();
  73. const AZStd::string groupName = "ModelUvMap";
  74. const AZStd::string groupDisplayName = "Material to Model UV Map";
  75. const AZStd::string groupDescription = "Custom map that maps a UV name from the material to one from the model.";
  76. const size_t uvSize = m_materialUvNames.size();
  77. m_group.m_properties.reserve(uvSize);
  78. for (size_t i = 0; i < uvSize; ++i)
  79. {
  80. AtomToolsFramework::DynamicPropertyConfig propertyConfig;
  81. const AZStd::string shaderInput = m_materialUvNames[i].m_shaderInput.ToString();
  82. const AZStd::string materialUvName = m_materialUvNames[i].m_uvName.GetStringView();
  83. propertyConfig.m_id = AZ::RPI::MaterialPropertyId(groupName, shaderInput);
  84. propertyConfig.m_name = shaderInput;
  85. propertyConfig.m_displayName = materialUvName;
  86. propertyConfig.m_description = shaderInput;
  87. propertyConfig.m_defaultValue = 0u;
  88. propertyConfig.m_originalValue = 0u;
  89. propertyConfig.m_parentValue = 0u;
  90. propertyConfig.m_enumValues = m_modelUvNames;
  91. m_group.m_properties.emplace_back(propertyConfig);
  92. m_group.m_properties.back().SetValue(AZStd::any(m_modelUvNameIndices[i]));
  93. }
  94. AddGroup(groupName, groupDisplayName, groupDescription,
  95. new AtomToolsFramework::InspectorPropertyGroupWidget(&m_group, nullptr, m_group.TYPEINFO_Uuid(), this));
  96. AddGroupsEnd();
  97. }
  98. void MaterialModelUvNameMapInspector::BeforePropertyModified(AzToolsFramework::InstanceDataNode* pNode)
  99. {
  100. // For some reason the reflected property editor notifications are not symmetrical
  101. // This function is called continuously anytime a property changes until the edit has completed
  102. // Because of that, we have to track whether or not we are continuing to edit the same property to know when editing has started and ended
  103. const auto property = AtomToolsFramework::FindAncestorInstanceDataNodeByType<AtomToolsFramework::DynamicProperty>(pNode);
  104. if (property && m_activeProperty != property)
  105. {
  106. m_activeProperty = property;
  107. }
  108. }
  109. void MaterialModelUvNameMapInspector::AfterPropertyModified(AzToolsFramework::InstanceDataNode* pNode)
  110. {
  111. const auto property = AtomToolsFramework::FindAncestorInstanceDataNodeByType<AtomToolsFramework::DynamicProperty>(pNode);
  112. if (property && m_activeProperty == property)
  113. {
  114. uint32_t index = 0;
  115. while (&(m_group.m_properties[index]) != property)
  116. {
  117. ++index;
  118. }
  119. AZ_Assert(index < m_group.m_properties.size(), "The property doesn't exist in the group.");
  120. uint32_t modelUvIndex = AZStd::any_cast<uint32_t>(m_activeProperty->GetValue());
  121. if (modelUvIndex == 0u)
  122. {
  123. m_matModUvOverrides[m_materialUvNames[index].m_shaderInput] = Name();
  124. }
  125. else
  126. {
  127. m_matModUvOverrides[m_materialUvNames[index].m_shaderInput] = Name(m_modelUvNames[modelUvIndex]);
  128. }
  129. if (m_matModUvOverrideMapChangedCallBack)
  130. {
  131. m_matModUvOverrideMapChangedCallBack(m_matModUvOverrides);
  132. }
  133. }
  134. }
  135. void MaterialModelUvNameMapInspector::SetPropertyEditingComplete(AzToolsFramework::InstanceDataNode* pNode)
  136. {
  137. // As above, there are symmetrical functions on the notification interface for when editing begins and ends and has been completed but they are not being called following that pattern.
  138. // when this function executes the changes to the property are ready to be committed or reverted
  139. const auto property = AtomToolsFramework::FindAncestorInstanceDataNodeByType<AtomToolsFramework::DynamicProperty>(pNode);
  140. if (property && m_activeProperty == property)
  141. {
  142. uint32_t index = 0;
  143. while (&(m_group.m_properties[index]) != property)
  144. {
  145. ++index;
  146. }
  147. AZ_Assert(index < m_group.m_properties.size(), "The property doesn't exist in the group.");
  148. uint32_t modelUvIndex = AZStd::any_cast<uint32_t>(m_activeProperty->GetValue());
  149. if (modelUvIndex == 0u)
  150. {
  151. m_matModUvOverrides[m_materialUvNames[index].m_shaderInput] = Name();
  152. }
  153. else
  154. {
  155. m_matModUvOverrides[m_materialUvNames[index].m_shaderInput] = Name(m_modelUvNames[modelUvIndex]);
  156. }
  157. if (m_matModUvOverrideMapChangedCallBack)
  158. {
  159. m_matModUvOverrideMapChangedCallBack(m_matModUvOverrides);
  160. }
  161. m_activeProperty = nullptr;
  162. }
  163. }
  164. void MaterialModelUvNameMapInspector::ResetModelUvNameIndices()
  165. {
  166. m_modelUvNameIndices.clear();
  167. const uint32_t uvSize = aznumeric_cast<uint32_t>(m_materialUvNames.size());
  168. m_modelUvNameIndices.resize(uvSize, 0u);
  169. AZStd::unordered_map<AZ::Name, uint32_t> tempModelUvIndexLookup;
  170. uint32_t index = 0u;
  171. for (const AZStd::string& modelUvName : m_modelUvNames)
  172. {
  173. tempModelUvIndexLookup[AZ::Name(modelUvName)] = index++;
  174. }
  175. index = 0u;
  176. for (const auto& materialUvNamePair : m_materialUvNames)
  177. {
  178. const auto overrideIter = m_matModUvOverrides.find(materialUvNamePair.m_shaderInput);
  179. if (overrideIter != m_matModUvOverrides.end())
  180. {
  181. const auto modelUvIndexIter = tempModelUvIndexLookup.find(overrideIter->second);
  182. if (modelUvIndexIter != tempModelUvIndexLookup.end())
  183. {
  184. m_modelUvNameIndices[index++] = modelUvIndexIter->second;
  185. }
  186. }
  187. }
  188. }
  189. void MaterialModelUvNameMapInspector::SetModelUvNames(const AZStd::unordered_set<AZ::Name>& modelUvNames)
  190. {
  191. static constexpr const char DefaultModelUvName[] = "[Same as in the material]";
  192. m_modelUvNames.clear();
  193. // Plus the default name
  194. m_modelUvNames.reserve(modelUvNames.size() + 1u);
  195. m_modelUvNames.push_back(DefaultModelUvName);
  196. for (const AZ::Name& modelUvName : modelUvNames)
  197. {
  198. m_modelUvNames.push_back(modelUvName.GetStringView());
  199. }
  200. }
  201. void MaterialModelUvNameMapInspector::SetUvNameMap(const RPI::MaterialModelUvOverrideMap& matModUvOverrides)
  202. {
  203. m_matModUvOverrides = matModUvOverrides;
  204. ResetModelUvNameIndices();
  205. const AZStd::string groupName = "ModelUvMap";
  206. size_t uvSize = m_materialUvNames.size();
  207. for (size_t i = 0u; i < uvSize; ++i)
  208. {
  209. AtomToolsFramework::DynamicPropertyConfig propertyConfig;
  210. const AZStd::string shaderInput = m_materialUvNames[i].m_shaderInput.ToString();
  211. const AZStd::string materialUvName = m_materialUvNames[i].m_uvName.GetStringView();
  212. propertyConfig.m_id = AZ::RPI::MaterialPropertyId(groupName, shaderInput);
  213. propertyConfig.m_name = shaderInput;
  214. propertyConfig.m_displayName = materialUvName;
  215. propertyConfig.m_description = shaderInput;
  216. propertyConfig.m_defaultValue = 0u;
  217. propertyConfig.m_originalValue = 0u;
  218. propertyConfig.m_parentValue = 0u;
  219. propertyConfig.m_enumValues = m_modelUvNames;
  220. m_group.m_properties[i].SetConfig(propertyConfig);
  221. m_group.m_properties[i].SetValue(AZStd::any(m_modelUvNameIndices[i]));
  222. }
  223. if (m_matModUvOverrideMapChangedCallBack)
  224. {
  225. m_matModUvOverrideMapChangedCallBack(matModUvOverrides);
  226. }
  227. RebuildAll();
  228. }
  229. bool OpenInspectorDialog(
  230. const AZ::Data::AssetId& assetId,
  231. const RPI::MaterialModelUvOverrideMap& matModUvOverrides,
  232. const AZStd::unordered_set<AZ::Name>& modelUvNames,
  233. MaterialModelUvOverrideMapChangedCallBack matModUvOverrideMapChangedCallBack)
  234. {
  235. QWidget* activeWindow = nullptr;
  236. AzToolsFramework::EditorWindowRequestBus::BroadcastResult(activeWindow, &AzToolsFramework::EditorWindowRequests::GetAppMainWindow);
  237. // Constructing a dialog with a table to display all configurable material export items
  238. QDialog dialog(activeWindow);
  239. dialog.setWindowTitle("Material Inspector");
  240. MaterialModelUvNameMapInspector* inspector = new MaterialModelUvNameMapInspector(assetId, matModUvOverrides, modelUvNames, matModUvOverrideMapChangedCallBack, &dialog);
  241. inspector->Populate();
  242. // Create the menu button
  243. QToolButton* menuButton = new QToolButton(&dialog);
  244. menuButton->setAutoRaise(true);
  245. menuButton->setIcon(QIcon(":/Cards/img/UI20/Cards/menu_ico.svg"));
  246. menuButton->setVisible(true);
  247. QObject::connect(
  248. menuButton, &QToolButton::clicked, &dialog, [&]()
  249. {
  250. QMenu menu(&dialog);
  251. menu.addAction(
  252. "Clear", [&]
  253. {
  254. inspector->SetUvNameMap(RPI::MaterialModelUvOverrideMap());
  255. });
  256. menu.addAction(
  257. "Revert", [&]
  258. {
  259. inspector->SetUvNameMap(matModUvOverrides);
  260. });
  261. menu.exec(QCursor::pos());
  262. });
  263. QDialogButtonBox* buttonBox = new QDialogButtonBox(&dialog);
  264. buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
  265. QObject::connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
  266. QObject::connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
  267. QVBoxLayout* dialogLayout = new QVBoxLayout(&dialog);
  268. dialogLayout->addWidget(menuButton);
  269. dialogLayout->addWidget(inspector);
  270. dialogLayout->addWidget(buttonBox);
  271. dialog.setLayout(dialogLayout);
  272. dialog.setModal(true);
  273. // Forcing the initial dialog size to accomodate typical content.
  274. // Temporarily settng fixed size because dialog.show/exec invokes WindowDecorationWrapper::showEvent.
  275. // This forces the dialog to be centered and sized based on the layout of content.
  276. // Resizing the dialog after show will not be centered and moving the dialog programatically doesn't m0ve the custmk frame.
  277. dialog.setFixedSize(300, 300);
  278. dialog.show();
  279. // Removing fixed size to allow drag resizing
  280. dialog.setMinimumSize(0, 0);
  281. dialog.setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
  282. // Return true if the user press the export button
  283. return dialog.exec() == QDialog::Accepted;
  284. }
  285. } // namespace EditorMaterialComponentInspector
  286. } // namespace Render
  287. } // namespace AZ