CommonSampleComponentBase.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #pragma once
  13. #include <AzCore/Component/Component.h>
  14. #include <AzCore/Component/TransformBus.h>
  15. #include <AzCore/Component/EntityBus.h>
  16. #include <AzFramework/Entity/EntityContextBus.h>
  17. #include <Atom/Feature/Mesh/MeshFeatureProcessorInterface.h>
  18. #include <Atom/Feature/Utils/LightingPreset.h>
  19. #include <Atom/Feature/SkyBox/SkyBoxFeatureProcessorInterface.h>
  20. #include <Atom/Feature/PostProcess/PostProcessFeatureProcessorInterface.h>
  21. #include <Atom/Feature/ImageBasedLights/ImageBasedLightFeatureProcessorInterface.h>
  22. #include <Atom/Feature/CoreLights/DirectionalLightFeatureProcessorInterface.h>
  23. #include <Atom/Utils/AssetCollectionAsyncLoader.h>
  24. #include <Utils/ImGuiProgressList.h>
  25. namespace AtomSampleViewer
  26. {
  27. class CommonSampleComponentBase
  28. : public AZ::Component
  29. , public AZ::TransformNotificationBus::MultiHandler
  30. , public AZ::EntityBus::MultiHandler
  31. {
  32. public:
  33. AZ_TYPE_INFO(MaterialHotReloadTestComponent, "{7EECDF09-B774-46C1-AD6E-060CE5717C05}");
  34. // AZ::Component overrides...
  35. bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override;
  36. protected:
  37. //! Init and shut down should be called in derived components' Activate() and Deactivate().
  38. //! @param loadDefaultLightingPresets if true, it will scan all lighting presets in the project and load them.
  39. void InitLightingPresets(bool loadDefaultLightingPresets = false);
  40. void ShutdownLightingPresets();
  41. //! Add a drop down list to select lighting preset for this sample.
  42. //! Lighting presets must be loaded before calling this function, otherwise the list will be hide.
  43. //! It should be called between ImGui::Begin() and ImGui::End().
  44. //! e.g. Calling it between ImGuiSidebar::Begin() and ImGuiSidebar::End() will embed this list into the side bar.
  45. void ImGuiLightingPreset();
  46. //! Load lighting presets from an asset.
  47. //! It will clear any presets loaded previously.
  48. void LoadLightingPresetsFromAsset(const AZStd::string& assetPath);
  49. //! Load lighting presets from an asset.
  50. //! Append the presets to the current existing presets.
  51. void AppendLightingPresetsFromAsset(const AZStd::string& assetPath);
  52. //! Clear all lighting presets.
  53. void ClearLightingPresets();
  54. //! Apply lighting presets to the scene.
  55. //! Derived samples can override this function to have custom behaviors.
  56. virtual void OnLightingPresetSelected(const AZ::Render::LightingPreset& preset);
  57. //! Return the AtomSampleViewer EntityContextId, retrieved from the ComponentConfig
  58. AzFramework::EntityContextId GetEntityContextId() const;
  59. //! Return the AtomSampleViewer camera EntityId, retrieved from the ComponentConfig
  60. AZ::EntityId GetCameraEntityId() const;
  61. AZ::Render::MeshFeatureProcessorInterface* GetMeshFeatureProcessor() const;
  62. void OnLightingPresetEntityShutdown(const AZ::EntityId& entityId);
  63. // Preload assets
  64. void PreloadAssets(const AZStd::vector<AZ::AssetCollectionAsyncLoader::AssetToLoadInfo>& assetList);
  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. private:
  74. // AZ::TransformNotificationBus::MultiHandler overrides...
  75. void OnTransformChanged(const AZ::Transform&, const AZ::Transform&) override;
  76. // virtual call back function which is called when all preloaded assets are loaded.
  77. virtual void OnAllAssetsReadyActivate() {};
  78. AzFramework::EntityContextId m_entityContextId;
  79. AZ::EntityId m_cameraEntityId;
  80. mutable AZ::Render::MeshFeatureProcessorInterface* m_meshFeatureProcessor = nullptr;
  81. //! All loaded lighting presets.
  82. AZStd::vector<AZ::Render::LightingPreset> m_lightingPresets;
  83. //! Lights created by lighting presets.
  84. AZStd::vector<AZ::Render::DirectionalLightFeatureProcessorInterface::LightHandle> m_lightHandles;
  85. //! Post process entity to handle ExposureControlSettings.
  86. AZ::Entity* m_postProcessEntity = nullptr;
  87. //! Dirty flag is set to true when m_lightingPresets is modified.
  88. bool m_lightingPresetsDirty = true;
  89. //! Current active lighting preset.
  90. constexpr static int32_t InvalidLightingPresetIndex = -1;
  91. int32_t m_currentLightingPresetIndex = InvalidLightingPresetIndex;
  92. };
  93. } // namespace AtomSampleViewer