LightCullingExampleComponent.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #pragma once
  8. #include <CommonSampleComponentBase.h>
  9. #include <AzCore/Component/EntityBus.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzCore/Math/Aabb.h>
  12. #include <AzCore/Math/Color.h>
  13. #include <AzCore/Math/Quaternion.h>
  14. #include <AzCore/Math/Random.h>
  15. #include <Utils/ImGuiSidebar.h>
  16. #include <Atom/Feature/CoreLights/DiskLightFeatureProcessorInterface.h>
  17. #include <Atom/Feature/CoreLights/PointLightFeatureProcessorInterface.h>
  18. #include <Atom/Feature/CoreLights/CapsuleLightFeatureProcessorInterface.h>
  19. #include <Atom/Feature/Decals/DecalFeatureProcessorInterface.h>
  20. #include <Atom/Feature/CoreLights/QuadLightFeatureProcessorInterface.h>
  21. namespace AZ
  22. {
  23. namespace RPI
  24. {
  25. using AuxGeomDrawPtr = AZStd::shared_ptr<class AuxGeomDraw>;
  26. }
  27. namespace Data
  28. {
  29. class AssetData;
  30. }
  31. }
  32. namespace AtomSampleViewer
  33. {
  34. class LightCullingExampleComponent final
  35. : public CommonSampleComponentBase
  36. , public AZ::TickBus::Handler
  37. {
  38. public:
  39. AZ_COMPONENT(LightCullingExampleComponent, "56B28789-4104-49B1-9C67-1DFC440DD800", CommonSampleComponentBase);
  40. static void Reflect(AZ::ReflectContext* context);
  41. LightCullingExampleComponent();
  42. ~LightCullingExampleComponent() override = default;
  43. void Activate() override;
  44. void Deactivate() override;
  45. private:
  46. enum class LightType
  47. {
  48. Point,
  49. Disk,
  50. Capsule,
  51. Quad,
  52. Decal,
  53. Count
  54. };
  55. struct LightSettings
  56. {
  57. bool m_enableDebugDraws = false;
  58. float m_intensity = 40.0f;
  59. float m_attenuationRadius = 3.0f;
  60. bool m_enableAutomaticFalloff = true;
  61. int m_numActive = 0;
  62. };
  63. using PointLightHandle = AZ::Render::PointLightFeatureProcessorInterface::LightHandle;
  64. using DiskLightHandle = AZ::Render::DiskLightFeatureProcessorInterface::LightHandle;
  65. using CapsuleLightHandle = AZ::Render::CapsuleLightFeatureProcessorInterface::LightHandle;
  66. using QuadLightHandle = AZ::Render::QuadLightFeatureProcessorInterface::LightHandle;
  67. template<typename LightHandle>
  68. struct Light
  69. {
  70. AZ::Vector3 m_position;
  71. AZ::Vector3 m_direction;
  72. AZ::Color m_color;
  73. LightHandle m_lightHandle;
  74. };
  75. // AZ::TickBus::Handler
  76. void OnTick(float deltaTime, AZ::ScriptTimePoint timePoint) override;
  77. // CommonSampleComponentBase overrides...
  78. void OnAllAssetsReadyActivate() override;
  79. void DrawDebuggingHelpers();
  80. void DrawPointLightDebugSpheres(AZ::RPI::AuxGeomDrawPtr auxGeom);
  81. void DrawDiskLightDebugObjects(AZ::RPI::AuxGeomDrawPtr auxGeom);
  82. void DrawCapsuleLightDebugObjects(AZ::RPI::AuxGeomDrawPtr auxGeom);
  83. void DrawDecalDebugBoxes(AZ::RPI::AuxGeomDrawPtr auxGeom);
  84. void SaveCameraConfiguration();
  85. void RestoreCameraConfiguration();
  86. void SetupScene();
  87. void CreateOpaqueModels();
  88. void DestroyOpaqueModels();
  89. void CreateTransparentModels();
  90. void DestroyTransparentModels();
  91. void SetupCamera();
  92. void CreatePointLights();
  93. void CreatePointLight(int index);
  94. void CreateDiskLights();
  95. void CreateDiskLight(int index);
  96. void CreateCapsuleLights();
  97. void CreateCapsuleLight(int index);
  98. void CreateQuadLights();
  99. void CreateQuadLight(int index);
  100. template<typename FP, typename LA>
  101. void DestroyLights(FP* fp, LA& lightArray);
  102. void CreateDecals();
  103. void CreateDecal(int index);
  104. void DestroyDecals();
  105. AZ::Color GetRandomColor();
  106. void DrawSidebar();
  107. void DrawSidebarTimingSection();
  108. void DrawSidebarTimingSectionCPU();
  109. void UpdateLights();
  110. void CreateLightsAndDecals();
  111. void DestroyLightsAndDecals();
  112. void DrawSidebarPointLightsSection(LightSettings* lightSettings);
  113. void DrawSidebarDiskLightsSection(LightSettings* lightSettings);
  114. void DrawSidebarCapsuleLightSection(LightSettings* lightSettings);
  115. void DrawSidebarDecalSection(LightSettings* lightSettings);
  116. void DrawSidebarQuadLightsSections(LightSettings* lightSettings);
  117. void DrawSidebarHeatmapOpacity();
  118. using DecalHandle = AZ::Render::DecalFeatureProcessorInterface::DecalHandle;
  119. struct Decal
  120. {
  121. AZ::Vector3 m_position;
  122. AZ::Quaternion m_quaternion;
  123. float m_opacity;
  124. float m_angleAttenuation;
  125. DecalHandle m_decalHandle;
  126. };
  127. void CalculateSmoothedFPS(float deltaTime);
  128. AZ::Vector3 GetRandomPositionInsideWorldModel();
  129. AZ::Vector3 GetRandomDirection();
  130. float GetRandomNumber(float low, float high);
  131. void InitLightArrays();
  132. void OnModelReady(AZ::Data::Instance<AZ::RPI::Model> model);
  133. void GetFeatureProcessors();
  134. static float AutoCalculateAttenuationRadius(const AZ::Color& color, float intensity);
  135. void MoveCameraToStartPosition();
  136. void UpdateHeatmapOpacity();
  137. void DisableHeatmap();
  138. void DrawQuadLightDebugObjects(AZ::RPI::AuxGeomDrawPtr auxGeom);
  139. void LoadDecalMaterial();
  140. AZStd::array<LightSettings, (size_t)LightType::Count> m_settings;
  141. AZStd::vector<Light<PointLightHandle>> m_pointLights;
  142. AZStd::vector<Light<DiskLightHandle>> m_diskLights;
  143. AZStd::vector<Light<CapsuleLightHandle>> m_capsuleLights;
  144. AZStd::vector<Light<QuadLightHandle>> m_quadLights;
  145. AZStd::vector<Decal> m_decals;
  146. AZStd::vector<AZ::Render::MeshFeatureProcessorInterface::MeshHandle> m_transparentMeshHandles;
  147. float m_originalFarClipDistance = 0.f;
  148. AZ::Render::MeshFeatureProcessorInterface::MeshHandle m_meshHandle;
  149. AZ::Render::MeshFeatureProcessorInterface::ModelChangedEvent::Handler m_meshChangedHandler
  150. {
  151. [&](AZ::Data::Instance<AZ::RPI::Model> model) { OnModelReady(model); }
  152. };
  153. bool m_worldModelAssetLoaded = false;
  154. AZ::Aabb m_worldModelAABB;
  155. AZ::SimpleLcgRandom m_random;
  156. ImGuiSidebar m_imguiSidebar;
  157. float m_smoothedFPS = 0.0f;
  158. float m_bulbRadius = 3.0f;
  159. bool m_diskConesEnabled = false;
  160. float m_diskInnerConeDegrees = 22.0f;
  161. float m_diskOuterConeDegrees = 90.0f;
  162. float m_capsuleRadius = 0.1f;
  163. float m_capsuleLength = 3.0f;
  164. float m_diskRadius = 3.0f;
  165. bool m_isQuadLightDoubleSided = false;
  166. bool m_quadLightsUseFastApproximation = false;
  167. AZStd::array<float, 3> m_decalSize = {
  168. { 5, 5, 5 }
  169. };
  170. float m_decalAngleAttenuation = 0.0f;
  171. float m_decalOpacity = 1.0f;
  172. bool m_refreshLights = false;
  173. float m_heatmapOpacity = 0.0f;
  174. AZStd::array<float, 2> m_quadLightSize = { 4, 2 };
  175. AZ::Data::Asset<AZ::Data::AssetData> m_decalMaterial;
  176. AZ::Render::PointLightFeatureProcessorInterface* m_pointLightFeatureProcessor = nullptr;
  177. AZ::Render::DiskLightFeatureProcessorInterface* m_diskLightFeatureProcessor = nullptr;
  178. AZ::Render::CapsuleLightFeatureProcessorInterface* m_capsuleLightFeatureProcessor = nullptr;
  179. AZ::Render::QuadLightFeatureProcessorInterface* m_quadLightFeatureProcessor = nullptr;
  180. AZ::Render::DecalFeatureProcessorInterface* m_decalFeatureProcessor = nullptr;
  181. };
  182. template<typename FP, typename LA>
  183. inline void AtomSampleViewer::LightCullingExampleComponent::DestroyLights(FP* fp, LA& lightArray)
  184. {
  185. for (auto& elem : lightArray)
  186. {
  187. fp->ReleaseLight(elem.m_lightHandle);
  188. }
  189. }
  190. } // namespace AtomSampleViewer