EditorModeFeedbackFeatureProcessor.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <EditorModeFeedbackFeatureProcessor.h>
  9. #include <Pass/State/FocusedEntityState.h>
  10. #include <Pass/State/SelectedEntityState.h>
  11. #include <Atom/RPI.Public/Pass/PassFilter.h>
  12. #include <Atom/RPI.Public/RenderPipeline.h>
  13. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  14. #include <Atom/Utils/Utils.h>
  15. namespace AZ
  16. {
  17. namespace Render
  18. {
  19. namespace
  20. {
  21. [[maybe_unused]] const char* const Window = "EditorModeFeedback";
  22. }
  23. // Creates the material for the mask pass shader
  24. static Data::Instance<RPI::Material> CreateMaskMaterial()
  25. {
  26. const AZStd::string path = "shaders/editormodemask.azmaterial";
  27. const auto materialAsset = RPI::AssetUtils::LoadCriticalAsset<RPI::MaterialAsset>(path);
  28. const auto maskMaterial = RPI::Material::FindOrCreate(materialAsset);
  29. return maskMaterial;
  30. }
  31. void EditorModeFeatureProcessor::Reflect(ReflectContext* context)
  32. {
  33. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  34. {
  35. serializeContext
  36. ->Class<EditorModeFeatureProcessor, RPI::FeatureProcessor>()
  37. ->Version(1);
  38. }
  39. }
  40. void EditorModeFeatureProcessor::Activate()
  41. {
  42. EnableSceneNotification();
  43. EditorStateList editorStates;
  44. editorStates.push_back(AZStd::make_unique<FocusedEntityState>());
  45. editorStates.push_back(AZStd::make_unique<SelectedEntityState>());
  46. m_editorStatePassSystem = AZStd::make_unique<EditorStatePassSystem>(AZStd::move(editorStates));
  47. AZ::TickBus::Handler::BusConnect();
  48. }
  49. void EditorModeFeatureProcessor::Deactivate()
  50. {
  51. AZ::TickBus::Handler::BusDisconnect();
  52. m_editorStatePassSystem.reset();
  53. DisableSceneNotification();
  54. }
  55. void EditorModeFeatureProcessor::OnRenderPipelineChanged(RPI::RenderPipeline* renderPipeline,
  56. RPI::SceneNotification::RenderPipelineChangeType changeType)
  57. {
  58. if (!m_editorStatePassSystem)
  59. {
  60. return;
  61. }
  62. if (changeType == RPI::SceneNotification::RenderPipelineChangeType::Added
  63. || changeType == RPI::SceneNotification::RenderPipelineChangeType::PassChanged)
  64. {
  65. m_editorStatePassSystem->ConfigureStatePassesForPipeline(renderPipeline);
  66. }
  67. else if (changeType == RPI::SceneNotification::RenderPipelineChangeType::Removed)
  68. {
  69. m_editorStatePassSystem->RemoveStatePassesForPipeline(renderPipeline);
  70. }
  71. }
  72. void EditorModeFeatureProcessor::AddRenderPasses(RPI::RenderPipeline* renderPipeline)
  73. {
  74. if (!m_editorStatePassSystem)
  75. {
  76. return;
  77. }
  78. m_editorStatePassSystem->AddPassesToRenderPipeline(renderPipeline);
  79. if(!m_maskRenderers.empty())
  80. {
  81. return;
  82. }
  83. for (const auto& mask : m_editorStatePassSystem->GetMasks())
  84. {
  85. // Emplaces the mask key and mask renderer value in place for the mask renderers map
  86. m_maskRenderers.emplace(
  87. AZStd::piecewise_construct, AZStd::forward_as_tuple(mask), AZStd::forward_as_tuple(mask));
  88. }
  89. }
  90. void EditorModeFeatureProcessor::Render([[maybe_unused]] const RenderPacket& packet)
  91. {
  92. if (!m_editorStatePassSystem || !m_maskMaterial)
  93. {
  94. return;
  95. }
  96. const auto entityMaskMap = m_editorStatePassSystem->GetEntitiesForEditorStates();
  97. for (const auto& [mask, entities] : entityMaskMap)
  98. {
  99. if(auto it = m_maskRenderers.find(mask);
  100. it != m_maskRenderers.end())
  101. {
  102. it->second.RenderMaskEntities(m_maskMaterial, entities);
  103. }
  104. }
  105. }
  106. void EditorModeFeatureProcessor::Simulate([[maybe_unused]] const SimulatePacket& packet)
  107. {
  108. if (!m_editorStatePassSystem)
  109. {
  110. return;
  111. }
  112. m_editorStatePassSystem->Update();
  113. }
  114. void EditorModeFeatureProcessor::OnTick(float, AZ::ScriptTimePoint)
  115. {
  116. // Attempt deferred loading of mask material until the asset is ready
  117. if (!m_maskMaterial)
  118. {
  119. m_maskMaterial = CreateMaskMaterial();
  120. }
  121. if (m_maskMaterial)
  122. {
  123. AZ::TickBus::Handler::BusDisconnect();
  124. }
  125. }
  126. void EditorModeFeatureProcessor::SetEnableRender(bool enableRender)
  127. {
  128. if (!m_editorStatePassSystem)
  129. {
  130. return;
  131. }
  132. const auto templateName = Name(m_editorStatePassSystem->GetParentPassTemplateName());
  133. auto passFilter = AZ::RPI::PassFilter::CreateWithTemplateName(templateName, GetParentScene());
  134. AZ::RPI::PassSystemInterface::Get()->ForEachPass(passFilter, [enableRender](RPI::Pass* pass) -> RPI::PassFilterExecutionFlow
  135. {
  136. pass->SetEnabled(enableRender);
  137. return RPI::PassFilterExecutionFlow::ContinueVisitingPasses;
  138. });
  139. }
  140. } // namespace Render
  141. } // namespace AZ