LightCullingExampleComponent.h 8.8 KB

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