3
0

PassCanvasViewportContent.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/Feature/SkyBox/SkyboxConstants.h>
  9. #include <Atom/Feature/Utils/ModelPreset.h>
  10. #include <Atom/RPI.Edit/Common/AssetUtils.h>
  11. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  12. #include <AtomLyIntegration/CommonFeatures/Grid/GridComponentBus.h>
  13. #include <AtomLyIntegration/CommonFeatures/Grid/GridComponentConfig.h>
  14. #include <AtomLyIntegration/CommonFeatures/Grid/GridComponentConstants.h>
  15. #include <AtomLyIntegration/CommonFeatures/ImageBasedLights/ImageBasedLightComponentBus.h>
  16. #include <AtomLyIntegration/CommonFeatures/ImageBasedLights/ImageBasedLightComponentConstants.h>
  17. #include <AtomLyIntegration/CommonFeatures/Material/MaterialComponentBus.h>
  18. #include <AtomLyIntegration/CommonFeatures/Material/MaterialComponentConstants.h>
  19. #include <AtomLyIntegration/CommonFeatures/Mesh/MeshComponentBus.h>
  20. #include <AtomLyIntegration/CommonFeatures/Mesh/MeshComponentConstants.h>
  21. #include <AtomLyIntegration/CommonFeatures/PostProcess/DisplayMapper/DisplayMapperComponentBus.h>
  22. #include <AtomLyIntegration/CommonFeatures/PostProcess/DisplayMapper/DisplayMapperComponentConstants.h>
  23. #include <AtomLyIntegration/CommonFeatures/PostProcess/ExposureControl/ExposureControlBus.h>
  24. #include <AtomLyIntegration/CommonFeatures/PostProcess/ExposureControl/ExposureControlComponentConstants.h>
  25. #include <AtomLyIntegration/CommonFeatures/PostProcess/PostFxLayerComponentConstants.h>
  26. #include <AtomLyIntegration/CommonFeatures/SkyBox/HDRiSkyboxBus.h>
  27. #include <AtomToolsFramework/EntityPreviewViewport/EntityPreviewViewportSettingsRequestBus.h>
  28. #include <AtomToolsFramework/Graph/GraphDocumentRequestBus.h>
  29. #include <AtomToolsFramework/Util/Util.h>
  30. #include <AzFramework/Components/NonUniformScaleComponent.h>
  31. #include <AzFramework/Components/TransformComponent.h>
  32. #include <Window/PassCanvasViewportContent.h>
  33. namespace PassCanvas
  34. {
  35. PassCanvasViewportContent::PassCanvasViewportContent(
  36. const AZ::Crc32& toolId,
  37. AtomToolsFramework::RenderViewportWidget* widget,
  38. AZStd::shared_ptr<AzFramework::EntityContext> entityContext)
  39. : AtomToolsFramework::EntityPreviewViewportContent(toolId, widget, entityContext)
  40. {
  41. // Configure tone mapper
  42. m_postFxEntity = CreateEntity(
  43. "PostFxEntity",
  44. { AZ::Render::PostFxLayerComponentTypeId, AZ::Render::DisplayMapperComponentTypeId, AZ::Render::ExposureControlComponentTypeId,
  45. azrtti_typeid<AzFramework::TransformComponent>() });
  46. // Create IBL
  47. m_environmentEntity = CreateEntity(
  48. "EnvironmentEntity",
  49. { AZ::Render::HDRiSkyboxComponentTypeId, AZ::Render::ImageBasedLightComponentTypeId,
  50. azrtti_typeid<AzFramework::TransformComponent>() });
  51. // Create model
  52. m_objectEntity = CreateEntity(
  53. "ObjectEntity",
  54. { AZ::Render::MeshComponentTypeId, AZ::Render::MaterialComponentTypeId, azrtti_typeid<AzFramework::TransformComponent>() });
  55. // Create shadow catcher
  56. m_shadowCatcherEntity = CreateEntity(
  57. "ShadowCatcherEntity",
  58. { AZ::Render::MeshComponentTypeId, AZ::Render::MaterialComponentTypeId, azrtti_typeid<AzFramework::TransformComponent>(),
  59. azrtti_typeid<AzFramework::NonUniformScaleComponent>() });
  60. AZ::NonUniformScaleRequestBus::Event(
  61. GetShadowCatcherEntityId(), &AZ::NonUniformScaleRequests::SetScale, AZ::Vector3(100, 100, 1.0));
  62. // Avoid z-fighting with the cube model when double-sided rendering is enabled
  63. AZ::TransformBus::Event(
  64. GetShadowCatcherEntityId(), &AZ::TransformInterface::SetWorldZ, -0.01f);
  65. AZ::Render::MeshComponentRequestBus::Event(
  66. GetShadowCatcherEntityId(), &AZ::Render::MeshComponentRequestBus::Events::SetModelAssetId,
  67. AZ::RPI::AssetUtils::GetAssetIdForProductPath("materialeditor/viewportmodels/plane_1x1.fbx.azmodel"));
  68. AZ::Render::MaterialComponentRequestBus::Event(
  69. GetShadowCatcherEntityId(), &AZ::Render::MaterialComponentRequestBus::Events::SetMaterialAssetId,
  70. AZ::Render::DefaultMaterialAssignmentId,
  71. AZ::RPI::AssetUtils::GetAssetIdForProductPath("materials/special/shadowcatcher.azmaterial"));
  72. // Create grid
  73. m_gridEntity = CreateEntity("GridEntity", { AZ::Render::GridComponentTypeId, azrtti_typeid<AzFramework::TransformComponent>() });
  74. AZ::Render::GridComponentRequestBus::Event(
  75. GetGridEntityId(),
  76. [&](AZ::Render::GridComponentRequests* gridComponentRequests)
  77. {
  78. gridComponentRequests->SetSize(4.0f);
  79. gridComponentRequests->SetAxisColor(AZ::Color(0.1f, 0.1f, 0.1f, 1.0f));
  80. gridComponentRequests->SetPrimaryColor(AZ::Color(0.1f, 0.1f, 0.1f, 1.0f));
  81. gridComponentRequests->SetSecondaryColor(AZ::Color(0.1f, 0.1f, 0.1f, 1.0f));
  82. });
  83. AtomToolsFramework::AtomToolsDocumentNotificationBus::Handler::BusConnect(m_toolId);
  84. AtomToolsFramework::GraphDocumentNotificationBus::Handler::BusConnect(m_toolId);
  85. OnDocumentOpened(AZ::Uuid::CreateNull());
  86. }
  87. PassCanvasViewportContent::~PassCanvasViewportContent()
  88. {
  89. AtomToolsFramework::GraphDocumentNotificationBus::Handler::BusDisconnect();
  90. AtomToolsFramework::AtomToolsDocumentNotificationBus::Handler::BusDisconnect();
  91. }
  92. AZ::EntityId PassCanvasViewportContent::GetObjectEntityId() const
  93. {
  94. return m_objectEntity ? m_objectEntity->GetId() : AZ::EntityId();
  95. }
  96. AZ::EntityId PassCanvasViewportContent::GetEnvironmentEntityId() const
  97. {
  98. return m_environmentEntity ? m_environmentEntity->GetId() : AZ::EntityId();
  99. }
  100. AZ::EntityId PassCanvasViewportContent::GetPostFxEntityId() const
  101. {
  102. return m_postFxEntity ? m_postFxEntity->GetId() : AZ::EntityId();
  103. }
  104. AZ::EntityId PassCanvasViewportContent::GetShadowCatcherEntityId() const
  105. {
  106. return m_shadowCatcherEntity ? m_shadowCatcherEntity->GetId() : AZ::EntityId();
  107. }
  108. AZ::EntityId PassCanvasViewportContent::GetGridEntityId() const
  109. {
  110. return m_gridEntity ? m_gridEntity->GetId() : AZ::EntityId();
  111. }
  112. void PassCanvasViewportContent::OnDocumentClosed([[maybe_unused]] const AZ::Uuid& documentId)
  113. {
  114. AZ::Render::MaterialComponentRequestBus::Event(
  115. GetObjectEntityId(), &AZ::Render::MaterialComponentRequestBus::Events::SetMaterialAssetIdOnDefaultSlot, AZ::Data::AssetId());
  116. }
  117. void PassCanvasViewportContent::OnDocumentOpened([[maybe_unused]] const AZ::Uuid& documentId)
  118. {
  119. m_lastOpenedDocumentId = documentId;
  120. ApplyPass(documentId);
  121. }
  122. void PassCanvasViewportContent::OnCompileGraphStarted([[maybe_unused]] const AZ::Uuid& documentId)
  123. {
  124. if (m_lastOpenedDocumentId == documentId)
  125. {
  126. ApplyPass({});
  127. }
  128. }
  129. void PassCanvasViewportContent::OnCompileGraphCompleted(const AZ::Uuid& documentId)
  130. {
  131. if (m_lastOpenedDocumentId == documentId)
  132. {
  133. ApplyPass(documentId);
  134. }
  135. }
  136. void PassCanvasViewportContent::OnCompileGraphFailed([[maybe_unused]] const AZ::Uuid& documentId)
  137. {
  138. if (m_lastOpenedDocumentId == documentId)
  139. {
  140. ApplyPass({});
  141. }
  142. }
  143. void PassCanvasViewportContent::OnViewportSettingsChanged()
  144. {
  145. AtomToolsFramework::EntityPreviewViewportContent::OnViewportSettingsChanged();
  146. AtomToolsFramework::EntityPreviewViewportSettingsRequestBus::Event(
  147. m_toolId,
  148. [this](AtomToolsFramework::EntityPreviewViewportSettingsRequestBus::Events* viewportRequests)
  149. {
  150. const auto& modelPreset = viewportRequests->GetModelPreset();
  151. const auto& lightingPreset = viewportRequests->GetLightingPreset();
  152. AZ::Render::MeshComponentRequestBus::Event(
  153. GetObjectEntityId(),
  154. [&](AZ::Render::MeshComponentRequests* meshComponentRequests)
  155. {
  156. if (meshComponentRequests->GetModelAsset() != modelPreset.m_modelAsset)
  157. {
  158. meshComponentRequests->SetModelAsset(modelPreset.m_modelAsset);
  159. }
  160. });
  161. AZ::Render::HDRiSkyboxRequestBus::Event(
  162. GetEnvironmentEntityId(),
  163. [&](AZ::Render::HDRiSkyboxRequests* skyboxComponentRequests)
  164. {
  165. skyboxComponentRequests->SetExposure(lightingPreset.m_skyboxExposure);
  166. skyboxComponentRequests->SetCubemapAsset(
  167. viewportRequests->GetAlternateSkyboxEnabled() ? lightingPreset.m_alternateSkyboxImageAsset
  168. : lightingPreset.m_skyboxImageAsset);
  169. });
  170. AZ::Render::MeshComponentRequestBus::Event(
  171. GetShadowCatcherEntityId(), &AZ::Render::MeshComponentRequestBus::Events::SetVisibility,
  172. viewportRequests->GetShadowCatcherEnabled());
  173. AZ::Render::MaterialComponentRequestBus::Event(
  174. GetShadowCatcherEntityId(), &AZ::Render::MaterialComponentRequestBus::Events::SetPropertyValue,
  175. AZ::Render::DefaultMaterialAssignmentId, "settings.opacity", AZStd::any(lightingPreset.m_shadowCatcherOpacity));
  176. AZ::Render::DisplayMapperComponentRequestBus::Event(
  177. GetPostFxEntityId(), &AZ::Render::DisplayMapperComponentRequestBus::Events::SetDisplayMapperOperationType,
  178. viewportRequests->GetDisplayMapperOperationType());
  179. AZ::Render::GridComponentRequestBus::Event(
  180. GetGridEntityId(), &AZ::Render::GridComponentRequestBus::Events::SetSize,
  181. viewportRequests->GetGridEnabled() ? 4.0f : 0.0f);
  182. });
  183. }
  184. void PassCanvasViewportContent::ApplyPass(const AZ::Uuid& documentId)
  185. {
  186. AZStd::vector<AZStd::string> generatedFiles;
  187. AtomToolsFramework::GraphDocumentRequestBus::EventResult(
  188. generatedFiles, documentId, &AtomToolsFramework::GraphDocumentRequestBus::Events::GetGeneratedFilePaths);
  189. }
  190. } // namespace PassCanvas