CommonSampleComponentBase.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <AtomSampleComponent.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/Component/EntityBus.h>
  12. #include <AzFramework/Entity/EntityContextBus.h>
  13. #include <Atom/Feature/Mesh/MeshFeatureProcessorInterface.h>
  14. #include <Atom/Feature/Utils/LightingPreset.h>
  15. #include <Atom/Feature/SkyBox/SkyBoxFeatureProcessorInterface.h>
  16. #include <Atom/Feature/PostProcess/PostProcessFeatureProcessorInterface.h>
  17. #include <Atom/Feature/ImageBasedLights/ImageBasedLightFeatureProcessorInterface.h>
  18. #include <Atom/Feature/CoreLights/DirectionalLightFeatureProcessorInterface.h>
  19. #include <Atom/Utils/AssetCollectionAsyncLoader.h>
  20. #include <Utils/ImGuiProgressList.h>
  21. namespace AtomSampleViewer
  22. {
  23. class CommonSampleComponentBase
  24. : public AtomSampleComponent
  25. , public AZ::TransformNotificationBus::MultiHandler
  26. , public AZ::EntityBus::MultiHandler
  27. {
  28. public:
  29. AZ_RTTI(CommonSampleComponentBase, "{7EECDF09-B774-46C1-AD6E-060CE5717C05}", AtomSampleComponent);
  30. static void Reflect(AZ::ReflectContext* context);
  31. // AZ::Component overrides...
  32. bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override;
  33. protected:
  34. //! Init and shut down should be called in derived components' Activate() and Deactivate().
  35. //! @param loadDefaultLightingPresets if true, it will scan all lighting presets in the project and load them.
  36. void InitLightingPresets(bool loadDefaultLightingPresets = false);
  37. void ShutdownLightingPresets();
  38. //! Add a drop down list to select lighting preset for this sample.
  39. //! Lighting presets must be loaded before calling this function, otherwise the list will be hide.
  40. //! It should be called between ImGui::Begin() and ImGui::End().
  41. //! e.g. Calling it between ImGuiSidebar::Begin() and ImGuiSidebar::End() will embed this list into the side bar.
  42. void ImGuiLightingPreset();
  43. //! Load lighting presets from an asset.
  44. //! It will clear any presets loaded previously.
  45. void LoadLightingPresetsFromAsset(const AZStd::string& assetPath);
  46. //! Load lighting presets from an asset.
  47. //! Append the presets to the current existing presets.
  48. void AppendLightingPresetsFromAsset(const AZStd::string& assetPath);
  49. //! Clear all lighting presets.
  50. void ClearLightingPresets();
  51. //! Reset internal scene related data
  52. void ResetScene();
  53. //! Apply lighting presets to the scene.
  54. //! Derived samples can override this function to have custom behaviors.
  55. virtual void OnLightingPresetSelected(const AZ::Render::LightingPreset& preset, bool useAltSkybox);
  56. //! Return the AtomSampleViewer EntityContextId, retrieved from the ComponentConfig
  57. AzFramework::EntityContextId GetEntityContextId() const;
  58. //! Return the AtomSampleViewer camera EntityId, retrieved from the ComponentConfig
  59. AZ::EntityId GetCameraEntityId() const;
  60. AZ::Render::MeshFeatureProcessorInterface* GetMeshFeatureProcessor() const;
  61. // Preload assets
  62. void PreloadAssets(const AZStd::vector<AZ::AssetCollectionAsyncLoader::AssetToLoadInfo>& assetList);
  63. //! Iterate to the next lighting preset in a loop
  64. void IterateToNextLightingPreset();
  65. //! Async asset load
  66. AZ::AssetCollectionAsyncLoader m_assetLoadManager;
  67. //! Showing the loading progress of preload assets
  68. ImGuiProgressList m_imguiProgressList;
  69. // The callback might be called more than one time if there are more than one asset are ready in one frame.
  70. // Use this flag to prevent OnAllAssetsReadyActivate be called more than one time.
  71. bool m_isAllAssetsReady = false;
  72. AZStd::string m_sampleName;
  73. AZ::RPI::Scene* m_scene = nullptr;
  74. private:
  75. // AZ::TransformNotificationBus::MultiHandler overrides...
  76. void OnTransformChanged(const AZ::Transform&, const AZ::Transform&) override;
  77. // virtual call back function which is called when all preloaded assets are loaded.
  78. virtual void OnAllAssetsReadyActivate() {};
  79. AzFramework::EntityContextId m_entityContextId;
  80. AZ::EntityId m_cameraEntityId;
  81. mutable AZ::Render::MeshFeatureProcessorInterface* m_meshFeatureProcessor = nullptr;
  82. //! All loaded lighting presets.
  83. struct LightingPresetEntry
  84. {
  85. AZStd::string m_displayName;
  86. AZ::Render::LightingPreset m_preset;
  87. };
  88. AZStd::vector<LightingPresetEntry> m_lightingPresets;
  89. //! Lights created by lighting presets.
  90. AZStd::vector<AZ::Render::DirectionalLightFeatureProcessorInterface::LightHandle> m_lightHandles;
  91. //! Dirty flag is set to true when m_lightingPresets is modified.
  92. bool m_lightingPresetsDirty = true;
  93. //! Current active lighting preset.
  94. constexpr static int32_t InvalidLightingPresetIndex = -1;
  95. int32_t m_currentLightingPresetIndex = InvalidLightingPresetIndex;
  96. bool m_useAlternateSkybox = false; //!< LightingPresets have an alternate skybox that can be used, when this is true. This is usually a blurred version of the primary skybox.
  97. };
  98. } // namespace AtomSampleViewer