CheckerboardExampleComponent.cpp 6.0 KB

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