ShadowedSponzaExampleComponent.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <Atom/Feature/CoreLights/DirectionalLightFeatureProcessorInterface.h>
  11. #include <Atom/Feature/CoreLights/ShadowConstants.h>
  12. #include <Atom/Feature/CoreLights/DiskLightFeatureProcessorInterface.h>
  13. #include <AzCore/Component/TickBus.h>
  14. #include <AzCore/std/containers/vector.h>
  15. #include <AzCore/Math/Aabb.h>
  16. #include <AzCore/Math/Random.h>
  17. #include <Utils/ImGuiSidebar.h>
  18. namespace AtomSampleViewer
  19. {
  20. /*
  21. * This component creates a scene of Sponza with shadowing.
  22. */
  23. class ShadowedSponzaExampleComponent final
  24. : public CommonSampleComponentBase
  25. , public AZ::TickBus::Handler
  26. {
  27. public:
  28. AZ_COMPONENT(ShadowedSponzaExampleComponent, "AAA320C7-1CF7-4CBA-9279-D29BB04B9CA9", CommonSampleComponentBase);
  29. static void Reflect(AZ::ReflectContext* context);
  30. ShadowedSponzaExampleComponent() = default;
  31. ~ShadowedSponzaExampleComponent() override = default;
  32. void Activate() override;
  33. void Deactivate() override;
  34. private:
  35. using DirectionalLightHandle = AZ::Render::DirectionalLightFeatureProcessorInterface::LightHandle;
  36. using DiskLightHandle = AZ::Render::DiskLightFeatureProcessorInterface::LightHandle;
  37. class DiskLight
  38. {
  39. public:
  40. DiskLight() = delete;
  41. explicit DiskLight(
  42. const AZ::Color& color,
  43. const AZ::Vector3& relativePosition,
  44. AZ::Render::ShadowmapSize shadowmapSize)
  45. : m_color{ color }
  46. , m_relativePosition{ relativePosition }
  47. , m_shadowmapSize{ shadowmapSize }
  48. {}
  49. ~DiskLight() = default;
  50. const AZ::Color m_color;
  51. const AZ::Vector3 m_relativePosition;
  52. const AZ::Render::ShadowmapSize m_shadowmapSize;
  53. DiskLightHandle m_handle;
  54. };
  55. static constexpr int DiskLightCountMax = 50;
  56. static constexpr int DiskLightCountDefault = 10;
  57. static constexpr float CutoffIntensity = 0.1f;
  58. static const AZ::Color DirectionalLightColor;
  59. // AZ::TickBus::Handler
  60. void OnTick(float deltaTime, AZ::ScriptTimePoint timePoint) override;
  61. void OnModelReady(AZ::Data::Instance<AZ::RPI::Model> model);
  62. void SaveCameraConfiguration();
  63. void RestoreCameraConfiguration();
  64. void SetInitialCameraTransform();
  65. void SetupScene();
  66. void BuildDiskLightParameters();
  67. void UpdateDiskLightCount(uint16_t count);
  68. const AZ::Color& GetRandomColor();
  69. AZ::Vector3 GetRandomPosition();
  70. AZ::Render::ShadowmapSize GetRandomShadowmapSize();
  71. void DrawSidebar();
  72. void UpdateDiskLightShadowmapSize();
  73. void UpdateDiskLightPositions();
  74. void UpdateDiskLightPosition(int index);
  75. void SetupDebugFlags();
  76. float m_originalFarClipDistance = 0.f;
  77. // lights
  78. AZ::Render::DirectionalLightFeatureProcessorInterface* m_directionalLightFeatureProcessor = nullptr;
  79. AZ::Render::DiskLightFeatureProcessorInterface* m_diskLightFeatureProcessor = nullptr;
  80. DirectionalLightHandle m_directionalLightHandle;
  81. AZStd::vector<DiskLight> m_diskLights;
  82. // scene setup
  83. AZ::SimpleLcgRandom m_random;
  84. AZ::Aabb m_worldAabb = AZ::Aabb::CreateNull();
  85. // model
  86. AZ::Render::MeshFeatureProcessorInterface::MeshHandle m_meshHandle;
  87. bool m_sponzaExteriorAssetLoaded = false;
  88. // GUI
  89. ImGuiSidebar m_imguiSidebar;
  90. float m_directionalLightPitch = -AZ::Constants::QuarterPi;
  91. float m_directionalLightYaw = 0.f;
  92. float m_directionalLightIntensity = 5.f;
  93. float m_diskLightIntensity = 500.f;
  94. int m_diskLightCount = 0;
  95. float m_diskLightsBasePosition[3] = {0.f, 0.f, 0.f};
  96. float m_diskLightsPositionScatteringRatio = 0.0f;
  97. bool m_cameraTransformInitialized = false;
  98. // Shadowmap
  99. static const AZ::Render::ShadowmapSize s_shadowmapSizes[];
  100. static const char* s_directionalLightShadowmapSizeLabels[];
  101. static constexpr int s_shadowmapSizeIndexDefault = 3;
  102. static constexpr int s_cascadesCountDefault = 4;
  103. static constexpr float s_ratioLogarithmUniformDefault = 0.8f;
  104. int m_directionalLightShadowmapSizeIndex = 0;
  105. int m_cascadeCount = 0;
  106. float m_ratioLogarithmUniform = 0.f;
  107. AZ::Render::ShadowmapSize m_diskLightShadowmapSize = AZ::Render::ShadowmapSize::None;
  108. bool m_diskLightShadowEnabled = true;
  109. // Edge-softening of directional light shadows
  110. static const AZ::Render::ShadowFilterMethod s_shadowFilterMethods[];
  111. static const char* s_shadowFilterMethodLabels[];
  112. int m_shadowFilterMethodIndexDirectional = 0; // filter method is None.
  113. int m_shadowFilterMethodIndexDisk = 0; // filter method is None
  114. int m_filteringSampleCountDirectional = 16;
  115. int m_filteringSampleCountDisk = 16;
  116. bool m_shadowEnabled = true;
  117. bool m_useFullscreenBlur = false;
  118. bool m_isCascadeCorrectionEnabled = false;
  119. bool m_isDebugColoringEnabled = false;
  120. bool m_isDebugBoundingBoxEnabled = false;
  121. };
  122. } // namespace AtomSampleViewer