CheckerboardExampleComponent.cpp 6.0 KB

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