LightCullingExampleComponent.h 7.7 KB

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