CheckerboardExampleComponent.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. m_meshFeatureProcessor = m_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(m_scene);
  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({ "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. void CheckerboardExampleComponent::DefaultWindowCreated()
  70. {
  71. AZ::Render::Bootstrap::DefaultWindowBus::BroadcastResult(m_windowContext,
  72. &AZ::Render::Bootstrap::DefaultWindowBus::Events::GetDefaultWindowContext);
  73. }
  74. void CheckerboardExampleComponent::ActivateCheckerboardPipeline()
  75. {
  76. // save original render pipeline first and remove it from the scene
  77. m_originalPipeline = m_scene->GetDefaultRenderPipeline();
  78. m_scene->RemoveRenderPipeline(m_originalPipeline->GetId());
  79. // add the checker board pipeline
  80. AZ::RPI::RenderPipelineDescriptor pipelineDesc;
  81. pipelineDesc.m_mainViewTagName = "MainCamera";
  82. pipelineDesc.m_name = "Checkerboard";
  83. pipelineDesc.m_rootPassTemplate = "CheckerboardPipeline";
  84. m_cbPipeline = AZ::RPI::RenderPipeline::CreateRenderPipelineForWindow(pipelineDesc, *m_windowContext);
  85. m_scene->AddRenderPipeline(m_cbPipeline);
  86. m_cbPipeline->SetDefaultView(m_originalPipeline->GetDefaultView());
  87. }
  88. void CheckerboardExampleComponent::DeactivateCheckerboardPipeline()
  89. {
  90. // remove cb pipeline before adding original pipeline.
  91. m_scene->RemoveRenderPipeline(m_cbPipeline->GetId());
  92. m_scene->AddRenderPipeline(m_originalPipeline);
  93. m_cbPipeline = nullptr;
  94. }
  95. void CheckerboardExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  96. {
  97. DrawSidebar();
  98. }
  99. void CheckerboardExampleComponent::DrawSidebar()
  100. {
  101. if (!m_imguiSidebar.Begin())
  102. {
  103. return;
  104. }
  105. ImGui::Text("Checkerboard debug render");
  106. m_imguiSidebar.End();
  107. }
  108. }