3
0

MaterialCanvasMainWindow.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <AtomToolsFramework/SettingsDialog/SettingsDialog.h>
  9. #include <AzCore/IO/FileIO.h>
  10. #include <AzQtComponents/Components/StyleManager.h>
  11. #include <GraphCanvas/Widgets/NodePalette/TreeItems/NodePaletteTreeItem.h>
  12. #include <Window/MaterialCanvasMainWindow.h>
  13. #include <Window/MaterialCanvasViewportContent.h>
  14. #include <QMessageBox>
  15. namespace MaterialCanvas
  16. {
  17. MaterialCanvasMainWindow::MaterialCanvasMainWindow(
  18. const AZ::Crc32& toolId, AtomToolsFramework::GraphViewSettingsPtr graphViewSettingsPtr, QWidget* parent)
  19. : Base(toolId, "MaterialCanvasMainWindow", parent)
  20. , m_graphViewSettingsPtr(graphViewSettingsPtr)
  21. , m_styleManager(toolId, graphViewSettingsPtr->m_styleManagerPath)
  22. {
  23. m_assetBrowser->GetSearchWidget()->ClearTypeFilter();
  24. m_assetBrowser->GetSearchWidget()->SetTypeFilterVisible(false);
  25. m_assetBrowser->SetFileTypeFilters({
  26. { "Material", { "material" }, true },
  27. { "Material Graph", { "materialgraph", "materialgraphtemplate" }, true },
  28. { "Material Graph Node", { "materialgraphnode", "materialgraphnodetemplate" }, true },
  29. { "Material Type", { "materialtype" }, true },
  30. { "Material Pipeline", { "materialpipeline" }, true },
  31. { "Shader", { "shader" }, true },
  32. { "Shader Template", { "shader.template" }, true },
  33. { "Shader Variant List", { "shadervariantlist" }, true },
  34. { "Image", { "tif", "tiff", "png", "bmp", "jpg", "jpeg", "tga", "gif", "dds", "exr" }, true },
  35. { "Lua", { "lua" }, true },
  36. { "AZSL", { "azsl", "azsli", "srgi" }, true },
  37. });
  38. m_documentInspector = new AtomToolsFramework::AtomToolsDocumentInspector(m_toolId, this);
  39. m_documentInspector->SetDocumentSettingsPrefix("/O3DE/Atom/MaterialCanvas/DocumentInspector");
  40. AddDockWidget("Inspector", m_documentInspector, Qt::RightDockWidgetArea);
  41. // Set up the toolbar that controls the viewport settings
  42. m_toolBar = new AtomToolsFramework::EntityPreviewViewportToolBar(m_toolId, this);
  43. // Create the dockable viewport widget that will be shared between all Material Canvas documents
  44. m_materialViewport = new AtomToolsFramework::EntityPreviewViewportWidget(m_toolId, this);
  45. // Initialize the entity context that will be used to create all of the entities displayed in the viewport
  46. auto entityContext = AZStd::make_shared<AzFramework::EntityContext>();
  47. entityContext->InitContext();
  48. // Initialize the atom scene and pipeline that will bind to the viewport window to render entities and presets
  49. auto viewportScene = AZStd::make_shared<AtomToolsFramework::EntityPreviewViewportScene>(
  50. m_toolId, m_materialViewport, entityContext, "MaterialCanvasViewportWidget", "passes/mainrenderpipeline.azasset");
  51. // Viewport content will instantiate all of the entities that will be displayed and controlled by the viewport
  52. auto viewportContent = AZStd::make_shared<MaterialCanvasViewportContent>(m_toolId, m_materialViewport, entityContext);
  53. // The input controller creates and binds input behaviors to control viewport objects
  54. auto viewportController = AZStd::make_shared<AtomToolsFramework::EntityPreviewViewportInputController>(m_toolId, m_materialViewport, viewportContent);
  55. // Inject the entity context, scene, content, and controller into the viewport widget
  56. m_materialViewport->Init(entityContext, viewportScene, viewportContent, viewportController);
  57. // Combine the shared toolbar in viewport into stacked widget that will be docked as a single view
  58. auto viewPortAndToolbar = new QWidget(this);
  59. viewPortAndToolbar->setLayout(new QVBoxLayout(viewPortAndToolbar));
  60. viewPortAndToolbar->layout()->setContentsMargins(0, 0, 0, 0);
  61. viewPortAndToolbar->layout()->setMargin(0);
  62. viewPortAndToolbar->layout()->setSpacing(0);
  63. viewPortAndToolbar->layout()->addWidget(m_toolBar);
  64. viewPortAndToolbar->layout()->addWidget(m_materialViewport);
  65. AddDockWidget("Viewport", viewPortAndToolbar, Qt::BottomDockWidgetArea);
  66. m_viewportSettingsInspector = new AtomToolsFramework::EntityPreviewViewportSettingsInspector(m_toolId, this);
  67. AddDockWidget("Viewport Settings", m_viewportSettingsInspector, Qt::LeftDockWidgetArea);
  68. SetDockWidgetVisible("Viewport Settings", false);
  69. m_bookmarkDockWidget = aznew GraphCanvas::BookmarkDockWidget(m_toolId, this);
  70. AddDockWidget("Bookmarks", m_bookmarkDockWidget, Qt::BottomDockWidgetArea);
  71. SetDockWidgetVisible("Bookmarks", false);
  72. AddDockWidget("MiniMap", aznew GraphCanvas::MiniMapDockWidget(m_toolId, this), Qt::BottomDockWidgetArea);
  73. SetDockWidgetVisible("MiniMap", false);
  74. GraphCanvas::NodePaletteConfig nodePaletteConfig;
  75. nodePaletteConfig.m_rootTreeItem = m_graphViewSettingsPtr->m_createNodeTreeItemsFn(m_toolId);
  76. nodePaletteConfig.m_editorId = m_toolId;
  77. nodePaletteConfig.m_mimeType = m_graphViewSettingsPtr->m_nodeMimeType.c_str();
  78. nodePaletteConfig.m_isInContextMenu = false;
  79. nodePaletteConfig.m_saveIdentifier = m_graphViewSettingsPtr->m_nodeSaveIdentifier;
  80. m_nodePalette = aznew GraphCanvas::NodePaletteDockWidget(this, "Node Palette", nodePaletteConfig);
  81. AddDockWidget("Node Palette", m_nodePalette, Qt::LeftDockWidgetArea);
  82. AZ::IO::FixedMaxPath resolvedPath;
  83. AZ::IO::FileIOBase::GetInstance()->ReplaceAlias(resolvedPath, m_graphViewSettingsPtr->m_translationPath.c_str());
  84. const AZ::IO::FixedMaxPathString translationFilePath = resolvedPath.LexicallyNormal().FixedMaxPathString();
  85. if (m_translator.load(QLocale::Language::English, translationFilePath.c_str()))
  86. {
  87. if (!qApp->installTranslator(&m_translator))
  88. {
  89. AZ_Warning("MaterialCanvas", false, "Error installing translation %s!", translationFilePath.c_str());
  90. }
  91. }
  92. else
  93. {
  94. AZ_Warning("MaterialCanvas", false, "Error loading translation file %s", translationFilePath.c_str());
  95. }
  96. // Set up style sheet to fix highlighting in the node palette
  97. AzQtComponents::StyleManager::setStyleSheet(this, QStringLiteral(":/GraphView/GraphView.qss"));
  98. OnDocumentOpened(AZ::Uuid::CreateNull());
  99. }
  100. void MaterialCanvasMainWindow::OnDocumentOpened(const AZ::Uuid& documentId)
  101. {
  102. Base::OnDocumentOpened(documentId);
  103. m_documentInspector->SetDocumentId(documentId);
  104. }
  105. void MaterialCanvasMainWindow::ResizeViewportRenderTarget(uint32_t width, uint32_t height)
  106. {
  107. QSize requestedViewportSize = QSize(width, height) / devicePixelRatioF();
  108. QSize currentViewportSize = m_materialViewport->size();
  109. QSize offset = requestedViewportSize - currentViewportSize;
  110. QSize requestedWindowSize = size() + offset;
  111. resize(requestedWindowSize);
  112. AZ_Assert(
  113. m_materialViewport->size() == requestedViewportSize,
  114. "Resizing the window did not give the expected viewport size. Requested %d x %d but got %d x %d.",
  115. requestedViewportSize.width(), requestedViewportSize.height(), m_materialViewport->size().width(),
  116. m_materialViewport->size().height());
  117. [[maybe_unused]] QSize newDeviceSize = m_materialViewport->size();
  118. AZ_Warning(
  119. "Material Canvas",
  120. static_cast<uint32_t>(newDeviceSize.width()) == width && static_cast<uint32_t>(newDeviceSize.height()) == height,
  121. "Resizing the window did not give the expected frame size. Requested %d x %d but got %d x %d.", width, height,
  122. newDeviceSize.width(), newDeviceSize.height());
  123. }
  124. void MaterialCanvasMainWindow::LockViewportRenderTargetSize(uint32_t width, uint32_t height)
  125. {
  126. m_materialViewport->LockRenderTargetSize(width, height);
  127. }
  128. void MaterialCanvasMainWindow::UnlockViewportRenderTargetSize()
  129. {
  130. m_materialViewport->UnlockRenderTargetSize();
  131. }
  132. void MaterialCanvasMainWindow::PopulateSettingsInspector(AtomToolsFramework::InspectorWidget* inspector) const
  133. {
  134. m_materialCanvasCompileSettingsGroup = AtomToolsFramework::CreateSettingsPropertyGroup(
  135. "Material Canvas Settings",
  136. "Material Canvas Settings",
  137. { AtomToolsFramework::CreateSettingsPropertyValue(
  138. "/O3DE/Atom/MaterialCanvas/EnableFasterShaderBuilds",
  139. "Enable Faster Shader Builds",
  140. "By default, some platforms perform an exhaustive compilation of shaders for multiple RHI. For example, the default "
  141. "Windows shader builder settings automatically compiles shaders for DX12, Vulkan, and the Null renderer.\n\nThis option "
  142. "overrides those registry settings and makes compilation and preview times much faster by only compiling shaders for the "
  143. "currently active platform and RHI.\n\nThis also disables automatic shader variant generation.\n\nChanging this setting "
  144. "requires restarting Material Canvas and the Asset Processor.\n\nChanging the active RHI with this setting enabled may "
  145. "require clearing the cache to regenerate shaders for the new RHI.\n\nThe settings files containing the overrides will be "
  146. "placed in the user/Registry folder for the current project.",
  147. false),
  148. AtomToolsFramework::CreateSettingsPropertyValue(
  149. "/O3DE/Atom/MaterialCanvas/ForceDeleteGeneratedFiles",
  150. "Delete Files On Compile",
  151. "This option forces files previously generated from the current graph to be deleted before creating new ones.",
  152. false),
  153. AtomToolsFramework::CreateSettingsPropertyValue(
  154. "/O3DE/Atom/MaterialCanvas/ForceClearAssetFingerprints",
  155. "Clear Asset Fingerprints On Compile",
  156. "This option forces the AP to reprocess generated files even if no differences were detected since last generated. This "
  157. "guarantees that notifications are sent for assets like materials that may not be changed even if their dependent "
  158. "material types or shaders are. This setting is most useful to ensure that other systems or applications are able to "
  159. "recognize and not reload yeah materials after shaders are modified. Enabling this setting may affect the time it takes "
  160. "for the viewport to reflect shader and material changes.",
  161. false),
  162. AtomToolsFramework::CreateSettingsPropertyValue(
  163. "/O3DE/AtomToolsFramework/GraphCompiler/CompileOnOpen",
  164. "Enable Compile On Open",
  165. "If enabled, shaders and materials will automatically be generated whenever a material graph is opened.",
  166. true),
  167. AtomToolsFramework::CreateSettingsPropertyValue(
  168. "/O3DE/AtomToolsFramework/GraphCompiler/CompileOnSave",
  169. "Enable Compile On Save",
  170. "If enabled, shaders and materials will automatically be generated whenever a material graph is saved.",
  171. true),
  172. AtomToolsFramework::CreateSettingsPropertyValue(
  173. "/O3DE/AtomToolsFramework/GraphCompiler/CompileOnEdit",
  174. "Enable Compile On Edit",
  175. "If enabled, shaders and materials will automatically be generated whenever a material graph is edited.",
  176. true),
  177. AtomToolsFramework::CreateSettingsPropertyValue(
  178. "/O3DE/Atom/MaterialCanvas/Viewport/ClearMaterialOnCompileGraphStarted",
  179. "Clear Viewport Material When Compiling Starts",
  180. "Clear the viewport model's material whenever compiling shaders and materials starts.",
  181. true),
  182. AtomToolsFramework::CreateSettingsPropertyValue(
  183. "/O3DE/Atom/MaterialCanvas/Viewport/ClearMaterialOnCompileGraphFailed",
  184. "Clear Viewport Material When Compiling Fails",
  185. "Clear the viewport model's material whenever compiling shaders and materials fails.",
  186. true),
  187. AtomToolsFramework::CreateSettingsPropertyValue(
  188. "/O3DE/AtomToolsFramework/GraphCompiler/EnableLogging",
  189. "Enable Compiler Logging",
  190. "Toggle verbose logging for material graph generation.",
  191. false),
  192. AtomToolsFramework::CreateSettingsPropertyValue(
  193. "/O3DE/AtomToolsFramework/DynamicNode/EnablePropertyEditingOnNodeUI",
  194. "Enable Property Editing On Nodes",
  195. "Toggle settings to display properties and allow them to be added directly on graph nodes.",
  196. true),
  197. AtomToolsFramework::CreateSettingsPropertyValue(
  198. "/O3DE/Atom/MaterialCanvas/CreateDefaultDocumentOnStart",
  199. "Create Untitled Graph Document On Start",
  200. "Create a default, untitled graph document when Material Canvas starts.",
  201. true),
  202. AtomToolsFramework::CreateSettingsPropertyValue(
  203. "/O3DE/AtomToolsFramework/GraphCompiler/QueueGraphCompileIntervalMs",
  204. "Queue Graph Compile Interval Ms",
  205. "The delay (in milliseconds) before the graph is recompiled after changes.",
  206. aznumeric_cast<AZ::s64>(500),
  207. aznumeric_cast<AZ::s64>(0),
  208. aznumeric_cast<AZ::s64>(1000)) });
  209. inspector->AddGroup(
  210. m_materialCanvasCompileSettingsGroup->m_name,
  211. m_materialCanvasCompileSettingsGroup->m_displayName,
  212. m_materialCanvasCompileSettingsGroup->m_description,
  213. new AtomToolsFramework::InspectorPropertyGroupWidget(
  214. m_materialCanvasCompileSettingsGroup.get(),
  215. m_materialCanvasCompileSettingsGroup.get(),
  216. azrtti_typeid<AtomToolsFramework::DynamicPropertyGroup>()));
  217. inspector->AddGroup(
  218. "Graph View Settings",
  219. "Graph View Settings",
  220. "Configuration settings for the graph view interaction, animation, and other behavior.",
  221. new AtomToolsFramework::InspectorPropertyGroupWidget(
  222. m_graphViewSettingsPtr.get(), m_graphViewSettingsPtr.get(), m_graphViewSettingsPtr->RTTI_Type()));
  223. Base::PopulateSettingsInspector(inspector);
  224. }
  225. void MaterialCanvasMainWindow::OnSettingsDialogClosed()
  226. {
  227. AtomToolsFramework::SetSettingsObject("/O3DE/Atom/GraphView/ViewSettings", m_graphViewSettingsPtr);
  228. Base::OnSettingsDialogClosed();
  229. }
  230. AZStd::string MaterialCanvasMainWindow::GetHelpUrl() const
  231. {
  232. return "https://docs.o3de.org/docs/atom-guide/look-dev/tools/material-canvas/";
  233. }
  234. } // namespace MaterialCanvas
  235. #include <Window/moc_MaterialCanvasMainWindow.cpp>