SSRExampleComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <SSRExampleComponent.h>
  9. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  10. #include <Atom/RHI/RHISystemInterface.h>
  11. #include <Atom/RPI.Public/Scene.h>
  12. #include <Atom/RPI.Public/RPISystemInterface.h>
  13. #include <Atom/RPI.Public/Pass/PassFilter.h>
  14. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  15. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  16. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  17. #include <Atom/Feature/SpecularReflections/SpecularReflectionsFeatureProcessorInterface.h>
  18. #include <Automation/ScriptableImGui.h>
  19. #include <Automation/ScriptRunnerBus.h>
  20. #include <Utils/Utils.h>
  21. #include <SSRExampleComponent_Traits_Platform.h>
  22. namespace AtomSampleViewer
  23. {
  24. void SSRExampleComponent::Reflect(AZ::ReflectContext* context)
  25. {
  26. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  27. {
  28. serializeContext->Class < SSRExampleComponent, AZ::Component>()
  29. ->Version(0)
  30. ;
  31. }
  32. }
  33. void SSRExampleComponent::Activate()
  34. {
  35. AZ::TickBus::Handler::BusConnect();
  36. m_imguiSidebar.Activate();
  37. // setup the camera
  38. Camera::CameraRequestBus::EventResult(m_originalFarClipDistance, GetCameraEntityId(), &Camera::CameraRequestBus::Events::GetFarClipDistance);
  39. Camera::CameraRequestBus::Event(GetCameraEntityId(), &Camera::CameraRequestBus::Events::SetFarClipDistance, 180.f);
  40. // create scene
  41. CreateModels();
  42. CreateGroundPlane();
  43. InitLightingPresets(true);
  44. // enable the SSR pass in the pipeline
  45. m_enableSSR = true;
  46. UpdateSSROptions();
  47. }
  48. void SSRExampleComponent::Deactivate()
  49. {
  50. // disable the SSR pass in the pipeline
  51. m_enableSSR = false;
  52. UpdateSSROptions();
  53. ShutdownLightingPresets();
  54. GetMeshFeatureProcessor()->ReleaseMesh(m_statueMeshHandle);
  55. GetMeshFeatureProcessor()->ReleaseMesh(m_boxMeshHandle);
  56. GetMeshFeatureProcessor()->ReleaseMesh(m_shaderBallMeshHandle);
  57. GetMeshFeatureProcessor()->ReleaseMesh(m_groundMeshHandle);
  58. Camera::CameraRequestBus::Event(GetCameraEntityId(), &Camera::CameraRequestBus::Events::SetFarClipDistance, m_originalFarClipDistance);
  59. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Disable);
  60. AZ::TickBus::Handler::BusDisconnect();
  61. m_imguiSidebar.Deactivate();
  62. }
  63. void SSRExampleComponent::CreateModels()
  64. {
  65. GetMeshFeatureProcessor();
  66. // statue
  67. {
  68. AZ::Data::Asset<AZ::RPI::MaterialAsset> materialAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::MaterialAsset>("objects/hermanubis/hermanubis_stone.azmaterial", AZ::RPI::AssetUtils::TraceLevel::Assert);
  69. AZ::Data::Asset<AZ::RPI::ModelAsset> modelAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>(ATOMSAMPLEVIEWER_TRAIT_SSR_SAMPLE_HERMANUBIS_MODEL_NAME, AZ::RPI::AssetUtils::TraceLevel::Assert);
  70. AZ::Transform transform = AZ::Transform::CreateIdentity();
  71. transform.SetTranslation(0.0f, 0.0f, -0.05f);
  72. m_statueMeshHandle = GetMeshFeatureProcessor()->AcquireMesh(
  73. AZ::Render::MeshHandleDescriptor(modelAsset, AZ::RPI::Material::FindOrCreate(materialAsset)));
  74. GetMeshFeatureProcessor()->SetTransform(m_statueMeshHandle, transform);
  75. }
  76. // cube
  77. {
  78. AZ::Data::Asset<AZ::RPI::MaterialAsset> materialAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::MaterialAsset>("materials/ssrexample/cube.azmaterial", AZ::RPI::AssetUtils::TraceLevel::Assert);
  79. AZ::Data::Asset<AZ::RPI::ModelAsset> modelAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>("objects/cube.fbx.azmodel", AZ::RPI::AssetUtils::TraceLevel::Assert);
  80. AZ::Transform transform = AZ::Transform::CreateIdentity();
  81. transform.SetTranslation(-4.5f, 0.0f, 0.49f);
  82. m_boxMeshHandle = GetMeshFeatureProcessor()->AcquireMesh(
  83. AZ::Render::MeshHandleDescriptor(modelAsset, AZ::RPI::Material::FindOrCreate(materialAsset)));
  84. GetMeshFeatureProcessor()->SetTransform(m_boxMeshHandle, transform);
  85. }
  86. // shader ball
  87. {
  88. AZ::Data::Asset<AZ::RPI::MaterialAsset> materialAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::MaterialAsset>("Materials/Presets/PBR/default_grid.azmaterial", AZ::RPI::AssetUtils::TraceLevel::Assert);
  89. AZ::Data::Asset<AZ::RPI::ModelAsset> modelAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>("objects/ShaderBall_simple.fbx.azmodel", AZ::RPI::AssetUtils::TraceLevel::Assert);
  90. AZ::Transform transform = AZ::Transform::CreateIdentity();
  91. transform *= AZ::Transform::CreateRotationZ(AZ::Constants::Pi);
  92. transform.SetTranslation(4.5f, 0.0f, 0.89f);
  93. m_shaderBallMeshHandle = GetMeshFeatureProcessor()->AcquireMesh(
  94. AZ::Render::MeshHandleDescriptor(modelAsset, AZ::RPI::Material::FindOrCreate(materialAsset)));
  95. GetMeshFeatureProcessor()->SetTransform(m_shaderBallMeshHandle, transform);
  96. }
  97. }
  98. void SSRExampleComponent::CreateGroundPlane()
  99. {
  100. AZ::Render::MeshFeatureProcessorInterface* meshFeatureProcessor = GetMeshFeatureProcessor();
  101. if (m_groundMeshHandle.IsValid())
  102. {
  103. meshFeatureProcessor->ReleaseMesh(m_groundMeshHandle);
  104. }
  105. // load material
  106. AZStd::string materialName;
  107. switch (m_groundPlaneMaterial)
  108. {
  109. case 0:
  110. materialName = AZStd::string::format("materials/ssrexample/groundplanechrome.azmaterial");
  111. break;
  112. case 1:
  113. materialName = AZStd::string::format("materials/ssrexample/groundplanealuminum.azmaterial");
  114. break;
  115. case 2:
  116. materialName = AZStd::string::format("materials/presets/pbr/default_grid.azmaterial");
  117. break;
  118. default:
  119. materialName = AZStd::string::format("materials/ssrexample/groundplanemirror.azmaterial");
  120. break;
  121. }
  122. AZ::Data::AssetId groundMaterialAssetId = AZ::RPI::AssetUtils::GetAssetIdForProductPath(materialName.c_str(), AZ::RPI::AssetUtils::TraceLevel::Error);
  123. m_groundMaterialAsset.Create(groundMaterialAssetId);
  124. // load mesh
  125. AZ::Data::Asset<AZ::RPI::ModelAsset> planeModel = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>("objects/plane.fbx.azmodel", AZ::RPI::AssetUtils::TraceLevel::Error);
  126. m_groundMeshHandle = GetMeshFeatureProcessor()->AcquireMesh(
  127. AZ::Render::MeshHandleDescriptor(planeModel, AZ::RPI::Material::FindOrCreate(m_groundMaterialAsset)));
  128. AZ::Transform transform = AZ::Transform::CreateIdentity();
  129. const AZ::Vector3 nonUniformScale(15.0f, 15.0f, 1.0f);
  130. GetMeshFeatureProcessor()->SetTransform(m_groundMeshHandle, transform, nonUniformScale);
  131. }
  132. void SSRExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  133. {
  134. if (m_resetCamera)
  135. {
  136. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Reset);
  137. AZ::TransformBus::Event(GetCameraEntityId(), &AZ::TransformBus::Events::SetWorldTranslation, AZ::Vector3(7.5f, -10.5f, 3.0f));
  138. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable, azrtti_typeid<AZ::Debug::NoClipControllerComponent>());
  139. AZ::Debug::NoClipControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::NoClipControllerRequests::SetHeading, AZ::DegToRad(22.5f));
  140. AZ::Debug::NoClipControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::NoClipControllerRequests::SetPitch, AZ::DegToRad(-10.0f));
  141. m_resetCamera = false;
  142. }
  143. DrawSidebar();
  144. }
  145. void SSRExampleComponent::DrawSidebar()
  146. {
  147. if (!m_imguiSidebar.Begin())
  148. {
  149. return;
  150. }
  151. AZ::RHI::Ptr<AZ::RHI::Device> device = AZ::RHI::RHISystemInterface::Get()->GetDevice();
  152. bool rayTracingSupported = device->GetFeatures().m_rayTracing;
  153. bool optionsChanged = false;
  154. ImGui::NewLine();
  155. optionsChanged |= ImGui::Checkbox("Enable SSR", &m_enableSSR);
  156. if (rayTracingSupported)
  157. {
  158. optionsChanged |= ImGui::Combo(
  159. "Reflection Method",
  160. reinterpret_cast<AZStd::underlying_type_t<AZ::Render::SSROptions::ReflectionMethod>*>(&m_reflectionMethod),
  161. "Screen Space\0Hybrid SSR-RT\0Hybrid SSR-RT + Ray Tracing fallback\0Ray Tracing\0");
  162. }
  163. ImGui::NewLine();
  164. ImGui::Text("Temporal Filtering Strength");
  165. optionsChanged |= ImGui::SliderFloat("##Temporal Filtering Strength", &m_temporalFilteringStrength, 0.0f, 3.0f);
  166. if (optionsChanged)
  167. {
  168. UpdateSSROptions();
  169. }
  170. ImGui::NewLine();
  171. ImGui::Text("Ground Plane Material");
  172. bool materialChanged = false;
  173. materialChanged |= ScriptableImGui::RadioButton("Chrome", &m_groundPlaneMaterial, 0);
  174. materialChanged |= ScriptableImGui::RadioButton("Aluminum", &m_groundPlaneMaterial, 1);
  175. materialChanged |= ScriptableImGui::RadioButton("Default Grid", &m_groundPlaneMaterial, 2);
  176. materialChanged |= ScriptableImGui::RadioButton("Mirror", &m_groundPlaneMaterial, 3);
  177. if (materialChanged)
  178. {
  179. CreateGroundPlane();
  180. }
  181. ImGui::NewLine();
  182. ImGuiLightingPreset();
  183. m_imguiSidebar.End();
  184. }
  185. void SSRExampleComponent::UpdateSSROptions()
  186. {
  187. AZ::Render::SpecularReflectionsFeatureProcessorInterface* specularReflectionsFeatureProcessor = m_scene->GetFeatureProcessorForEntityContextId<AZ::Render::SpecularReflectionsFeatureProcessorInterface>(GetEntityContextId());
  188. AZ_Assert(specularReflectionsFeatureProcessor, "SpecularReflectionsFeatureProcessor not available.");
  189. AZ::Render::SSROptions ssrOptions = specularReflectionsFeatureProcessor->GetSSROptions();
  190. ssrOptions.m_enable = m_enableSSR;
  191. ssrOptions.m_reflectionMethod = m_reflectionMethod;
  192. ssrOptions.m_coneTracing = false;
  193. ssrOptions.m_maxRoughness = 0.5f;
  194. ssrOptions.m_maxDepthThreshold = m_maxDepthThreshold;
  195. ssrOptions.m_temporalFilteringStrength = m_temporalFilteringStrength;
  196. specularReflectionsFeatureProcessor->SetSSROptions(ssrOptions);
  197. }
  198. }