RenderTargetTextureExampleComponent.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #pragma once
  9. #include <CommonSampleComponentBase.h>
  10. #include <AzCore/Component/EntityBus.h>
  11. #include <AzCore/Component/TickBus.h>
  12. #include <Utils/Utils.h>
  13. #include <Utils/ImGuiSidebar.h>
  14. #include <Atom/Feature/ImGui/ImGuiUtils.h>
  15. #include <Atom/RPI.Public/DynamicDraw/DynamicDrawContext.h>
  16. #include <Atom/RPI.Public/Image/AttachmentImage.h>
  17. #include <Atom/RPI.Public/Pass/Specific/ImageAttachmentPreviewPass.h>
  18. namespace AtomSampleViewer
  19. {
  20. // This example shows how to use a render target as a texture for a mesh's material.
  21. // It does following things:
  22. // 1. creates a raster pass at runtime with one render target
  23. // 2. the render target is used as a texture input for a standard pbr material
  24. // 3. A mesh with this material is rendered to the scene with IBL lighting.
  25. class RenderTargetTextureExampleComponent final
  26. : public CommonSampleComponentBase
  27. , public AZ::TickBus::Handler
  28. {
  29. public:
  30. AZ_COMPONENT(RenderTargetTextureExampleComponent, "{43069FB5-EE01-4799-8870-3CFC422D09DF}", CommonSampleComponentBase);
  31. static void Reflect(AZ::ReflectContext* context);
  32. RenderTargetTextureExampleComponent();
  33. ~RenderTargetTextureExampleComponent() override = default;
  34. // AZ::Component overrides...
  35. void Activate() override;
  36. void Deactivate() override;
  37. private:
  38. // AZ::TickBus::Handler overrides...
  39. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  40. // CommonSampleComponentBase overrides...
  41. void OnAllAssetsReadyActivate() override;
  42. // create the pass render to a render target and add it to render pipeline
  43. void AddRenderTargetPass();
  44. void CreateDynamicDraw();
  45. // Remove the render target pass
  46. void RemoveRenderTargetPass();
  47. void DrawToRenderTargetPass();
  48. // show or hide render target preview
  49. void UpdateRenderTargetPreview();
  50. // Rendering content
  51. // Mesh
  52. AZ::Render::MeshFeatureProcessorInterface::MeshHandle m_meshHandle;
  53. AZ::Data::Asset<AZ::RPI::ModelAsset> m_modelAsset;
  54. // materiala and texture
  55. AZ::Data::Instance<AZ::RPI::Material> m_material;
  56. AZ::Data::Instance<AZ::RPI::Image> m_baseColorTexture;
  57. Utils::DefaultIBL m_ibl;
  58. // render target
  59. AZ::RHI::Ptr<AZ::RPI::Pass> m_renderTargetPass;
  60. AZ::Data::Instance<AZ::RPI::AttachmentImage> m_renderTarget;
  61. AZ::Name m_renderTargetPassDrawListTag;
  62. // to preview the render target
  63. AZ::RPI::Ptr<AZ::RPI::ImageAttachmentPreviewPass> m_previewPass;
  64. // For render something to render target pass
  65. AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> m_dynamicDraw;
  66. AZ::Data::Instance<AZ::RPI::Image> m_texture;
  67. uint32_t m_currentFrame = 0;
  68. // for ImGui ui
  69. bool m_updatePerSecond = false;
  70. double m_lastUpdateTime = 0;
  71. bool m_showRenderTargetPreview = true;
  72. // UI
  73. ImGuiSidebar m_imguiSidebar;
  74. };
  75. } // namespace AtomSampleViewer