CheckerboardExampleComponent.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #include <CheckerboardExampleComponent.h>
  13. #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
  14. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  15. #include <Atom/RPI.Public/View.h>
  16. #include <Atom/RPI.Public/Image/StreamingImage.h>
  17. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  18. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  19. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  20. #include <AzFramework/Input/Devices/Keyboard/InputDeviceKeyboard.h>
  21. #include <SampleComponentManager.h>
  22. #include <SampleComponentConfig.h>
  23. #include <RHI/BasicRHIComponent.h>
  24. namespace AtomSampleViewer
  25. {
  26. void CheckerboardExampleComponent::Reflect(AZ::ReflectContext* context)
  27. {
  28. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  29. {
  30. serializeContext->Class < CheckerboardExampleComponent, AZ::Component>()
  31. ->Version(0)
  32. ;
  33. }
  34. }
  35. CheckerboardExampleComponent::CheckerboardExampleComponent()
  36. {
  37. }
  38. void CheckerboardExampleComponent::Activate()
  39. {
  40. AZ::RPI::AssetUtils::TraceLevel traceLevel = AZ::RPI::AssetUtils::TraceLevel::Assert;
  41. auto meshAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>("objects/shaderball_simple.azmodel", traceLevel);
  42. auto materialAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::MaterialAsset>(DefaultPbrMaterialPath, traceLevel);
  43. AZ::Render::MaterialAssignmentMap materials;
  44. AZ::Render::MaterialAssignment& defaultMaterial = materials[AZ::Render::DefaultMaterialAssignmentId];
  45. defaultMaterial.m_materialAsset = materialAsset;
  46. defaultMaterial.m_materialInstance = AZ::RPI::Material::FindOrCreate(defaultMaterial.m_materialAsset);
  47. AZ::RPI::ScenePtr scene = AZ::RPI::RPISystemInterface::Get()->GetDefaultScene();
  48. m_meshFeatureProcessor = scene->GetFeatureProcessor<AZ::Render::MeshFeatureProcessorInterface>();
  49. m_meshHandle = m_meshFeatureProcessor->AcquireMesh(meshAsset, materials);
  50. m_meshFeatureProcessor->SetTransform(m_meshHandle, AZ::Transform::CreateIdentity());
  51. AZ::Debug::CameraControllerRequestBus::Event(m_cameraEntityId, &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  52. azrtti_typeid<AZ::Debug::ArcBallControllerComponent>());
  53. // Add an Image based light.
  54. m_defaultIbl.Init(scene.get());
  55. AZ::TickBus::Handler::BusConnect();
  56. // connect to the bus before creating new pipeline
  57. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusConnect();
  58. ActivateCheckerboardPipeline();
  59. // Create an ImGuiActiveContextScope to ensure the ImGui context on the new pipeline's ImGui pass is activated.
  60. m_imguiScope = AZ::Render::ImGuiActiveContextScope::FromPass(AZ::RPI::PassHierarchyFilter({ "CheckerboardPipeline", "ImGuiPass" }));
  61. m_imguiSidebar.Activate();
  62. }
  63. void CheckerboardExampleComponent::Deactivate()
  64. {
  65. m_imguiSidebar.Deactivate();
  66. m_imguiScope = {}; // restores previous ImGui context.
  67. DeactivateCheckerboardPipeline();
  68. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusDisconnect();
  69. AZ::TickBus::Handler::BusDisconnect();
  70. m_defaultIbl.Reset();
  71. m_meshFeatureProcessor->ReleaseMesh(m_meshHandle);
  72. m_meshFeatureProcessor = nullptr;
  73. }
  74. bool CheckerboardExampleComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  75. {
  76. auto config = azrtti_cast<const SampleComponentConfig*>(baseConfig);
  77. AZ_Assert(config && config->IsValid(), "SampleComponentConfig required for sample component configuration.");
  78. m_cameraEntityId = config->m_cameraEntityId;
  79. m_entityContextId = config->m_entityContextId;
  80. return true;
  81. }
  82. void CheckerboardExampleComponent::DefaultWindowCreated()
  83. {
  84. AZ::Render::Bootstrap::DefaultWindowBus::BroadcastResult(m_windowContext,
  85. &AZ::Render::Bootstrap::DefaultWindowBus::Events::GetDefaultWindowContext);
  86. }
  87. void CheckerboardExampleComponent::ActivateCheckerboardPipeline()
  88. {
  89. // save original render pipeline first and remove it from the scene
  90. AZ::RPI::ScenePtr defaultScene = AZ::RPI::RPISystemInterface::Get()->GetDefaultScene();
  91. m_originalPipeline = defaultScene->GetDefaultRenderPipeline();
  92. defaultScene->RemoveRenderPipeline(m_originalPipeline->GetId());
  93. // add the checker board pipeline
  94. AZ::RPI::RenderPipelineDescriptor pipelineDesc;
  95. pipelineDesc.m_mainViewTagName = "MainCamera";
  96. pipelineDesc.m_name = "Checkerboard";
  97. pipelineDesc.m_rootPassTemplate = "CheckerboardPipeline";
  98. m_cbPipeline = AZ::RPI::RenderPipeline::CreateRenderPipelineForWindow(pipelineDesc, *m_windowContext);
  99. defaultScene->AddRenderPipeline(m_cbPipeline);
  100. m_cbPipeline->SetDefaultView(m_originalPipeline->GetDefaultView());
  101. }
  102. void CheckerboardExampleComponent::DeactivateCheckerboardPipeline()
  103. {
  104. // remove cb pipeline before adding original pipeline.
  105. AZ::RPI::ScenePtr defaultScene = AZ::RPI::RPISystemInterface::Get()->GetDefaultScene();
  106. defaultScene->RemoveRenderPipeline(m_cbPipeline->GetId());
  107. defaultScene->AddRenderPipeline(m_originalPipeline);
  108. m_cbPipeline = nullptr;
  109. }
  110. void CheckerboardExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  111. {
  112. DrawSidebar();
  113. }
  114. void CheckerboardExampleComponent::DrawSidebar()
  115. {
  116. if (!m_imguiSidebar.Begin())
  117. {
  118. return;
  119. }
  120. ImGui::Text("Checkerboard debug render");
  121. m_imguiSidebar.End();
  122. }
  123. }