CheckerboardExampleComponent.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 modelAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>("objects/shaderball_simple.fbx.azmodel", traceLevel);
  38. auto materialAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::MaterialAsset>(DefaultPbrMaterialPath, traceLevel);
  39. auto materialInstance = AZ::RPI::Material::FindOrCreate(materialAsset);
  40. m_meshFeatureProcessor = m_scene->GetFeatureProcessor<AZ::Render::MeshFeatureProcessorInterface>();
  41. m_meshHandle = m_meshFeatureProcessor->AcquireMesh(AZ::Render::MeshHandleDescriptor(modelAsset, materialInstance));
  42. m_meshFeatureProcessor->SetTransform(m_meshHandle, AZ::Transform::CreateIdentity());
  43. AZ::Debug::CameraControllerRequestBus::Event(
  44. GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  45. azrtti_typeid<AZ::Debug::ArcBallControllerComponent>());
  46. // Add an Image based light.
  47. m_defaultIbl.Init(m_scene);
  48. AZ::TickBus::Handler::BusConnect();
  49. // connect to the bus before creating new pipeline
  50. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusConnect();
  51. ActivateCheckerboardPipeline();
  52. // Create an ImGuiActiveContextScope to ensure the ImGui context on the new pipeline's ImGui pass is activated.
  53. m_imguiScope = AZ::Render::ImGuiActiveContextScope::FromPass({ "CheckerboardPipeline", "ImGuiPass" });
  54. m_imguiSidebar.Activate();
  55. }
  56. void CheckerboardExampleComponent::Deactivate()
  57. {
  58. m_imguiSidebar.Deactivate();
  59. m_imguiScope = {}; // restores previous ImGui context.
  60. DeactivateCheckerboardPipeline();
  61. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusDisconnect();
  62. AZ::TickBus::Handler::BusDisconnect();
  63. m_defaultIbl.Reset();
  64. m_meshFeatureProcessor->ReleaseMesh(m_meshHandle);
  65. m_meshFeatureProcessor = nullptr;
  66. }
  67. void CheckerboardExampleComponent::DefaultWindowCreated()
  68. {
  69. AZ::Render::Bootstrap::DefaultWindowBus::BroadcastResult(m_windowContext,
  70. &AZ::Render::Bootstrap::DefaultWindowBus::Events::GetDefaultWindowContext);
  71. }
  72. void CheckerboardExampleComponent::ActivateCheckerboardPipeline()
  73. {
  74. // save original render pipeline first and remove it from the scene
  75. m_originalPipeline = m_scene->GetDefaultRenderPipeline();
  76. m_scene->RemoveRenderPipeline(m_originalPipeline->GetId());
  77. // add the checker board pipeline
  78. AZ::RPI::RenderPipelineDescriptor pipelineDesc;
  79. pipelineDesc.m_mainViewTagName = "MainCamera";
  80. pipelineDesc.m_name = "CheckerboardPipeline";
  81. pipelineDesc.m_rootPassTemplate = "CheckerboardPipeline";
  82. m_cbPipeline = AZ::RPI::RenderPipeline::CreateRenderPipelineForWindow(pipelineDesc, *m_windowContext);
  83. m_scene->AddRenderPipeline(m_cbPipeline);
  84. m_cbPipeline->SetDefaultView(m_originalPipeline->GetDefaultView());
  85. }
  86. void CheckerboardExampleComponent::DeactivateCheckerboardPipeline()
  87. {
  88. // remove cb pipeline before adding original pipeline.
  89. m_scene->RemoveRenderPipeline(m_cbPipeline->GetId());
  90. m_scene->AddRenderPipeline(m_originalPipeline);
  91. m_cbPipeline = nullptr;
  92. }
  93. void CheckerboardExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  94. {
  95. DrawSidebar();
  96. }
  97. void CheckerboardExampleComponent::DrawSidebar()
  98. {
  99. if (!m_imguiSidebar.Begin())
  100. {
  101. return;
  102. }
  103. ImGui::Text("Checkerboard debug render");
  104. m_imguiSidebar.End();
  105. }
  106. }