CullingAndLodExampleComponent.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/DiskLightFeatureProcessorInterface.h>
  12. #include <Atom/Feature/CoreLights/ShadowConstants.h>
  13. #include <Atom/Feature/Mesh/MeshFeatureProcessorInterface.h>
  14. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  15. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  16. #include <AzCore/Component/TickBus.h>
  17. #include <AzCore/std/containers/vector.h>
  18. #include <AzCore/Math/Aabb.h>
  19. #include <AzCore/Math/Random.h>
  20. #include <AzFramework/Entity/EntityContext.h>
  21. #include <Utils/ImGuiSidebar.h>
  22. namespace AtomSampleViewer
  23. {
  24. class CullingAndLodExampleComponent final
  25. : public CommonSampleComponentBase
  26. , public AZ::TickBus::Handler
  27. {
  28. public:
  29. AZ_COMPONENT(CullingAndLodExampleComponent, "CA7AB736-5C80-425E-8DF3-E1C22971D79C", CommonSampleComponentBase);
  30. static void Reflect(AZ::ReflectContext* context);
  31. CullingAndLodExampleComponent() = default;
  32. ~CullingAndLodExampleComponent() override = default;
  33. // need to remove the copy constructor because Handles cannot be copied, only moved
  34. CullingAndLodExampleComponent(CullingAndLodExampleComponent&) = delete;
  35. // AZ::Component overrides...
  36. void Activate() override;
  37. void Deactivate() override;
  38. private:
  39. using DirectionalLightHandle = AZ::Render::DirectionalLightFeatureProcessorInterface::LightHandle;
  40. using DiskLightHandle = AZ::Render::DiskLightFeatureProcessorInterface::LightHandle;
  41. class DiskLight
  42. {
  43. public:
  44. DiskLight() = delete;
  45. explicit DiskLight(const AZ::Color& color, const AZ::Vector3& position,
  46. const AZ::Vector3& direction, AZ::Render::ShadowmapSize shadowmapSize)
  47. : m_color(color)
  48. , m_position(position)
  49. , m_direction(direction)
  50. , m_shadowmapSize(shadowmapSize)
  51. {}
  52. ~DiskLight() = default;
  53. const AZ::Color m_color;
  54. const AZ::Vector3 m_position;
  55. const AZ::Vector3 m_direction;
  56. const AZ::Render::ShadowmapSize m_shadowmapSize;
  57. DiskLightHandle m_handle;
  58. };
  59. static constexpr int DiskLightCountMax = 100;
  60. static constexpr int DiskLightCountDefault = 10;
  61. static constexpr float CutoffIntensity = 0.5f;
  62. static const AZ::Color DirectionalLightColor;
  63. // AZ::TickBus::Handler
  64. void OnTick(float deltaTime, AZ::ScriptTimePoint timePoint) override;
  65. void ResetNoClipController();
  66. void SaveCameraConfiguration();
  67. void RestoreCameraConfiguration();
  68. void SetupScene();
  69. void ClearMeshes();
  70. void SpawnModelsIn2DGrid(uint32_t numAlongXAxis, uint32_t numAlongYAxis);
  71. void SetupLights();
  72. void UpdateDiskLightCount(uint16_t count);
  73. void DrawSidebar();
  74. void UpdateDiskLightShadowmapSize();
  75. float m_originalFarClipDistance = 0.f;
  76. // lights
  77. AZ::Render::DirectionalLightFeatureProcessorInterface* m_directionalLightFeatureProcessor = nullptr;
  78. AZ::Render::DiskLightFeatureProcessorInterface* m_diskLightFeatureProcessor = nullptr;
  79. DirectionalLightHandle m_directionalLightHandle;
  80. AZStd::vector<DiskLight> m_diskLights;
  81. // models
  82. AZStd::vector<AZ::Render::MeshFeatureProcessorInterface::MeshHandle> m_meshHandles;
  83. AZStd::vector<AZ::Render::MeshHandleDescriptor::ModelChangedEvent::Handler> m_modelChangedHandlers;
  84. // GUI
  85. ImGuiSidebar m_imguiSidebar;
  86. float m_directionalLightPitch = -1.22;
  87. float m_directionalLightYaw = 0.7f;
  88. float m_directionalLightIntensity = 4.f;
  89. float m_diskLightIntensity = 2000.f;
  90. int m_diskLightCount = 0;
  91. // Shadowmap
  92. static const AZ::Render::ShadowmapSize s_shadowmapSizes[];
  93. static const char* s_directionalLightShadowmapSizeLabels[];
  94. static constexpr int s_shadowmapSizeIndexDefault = 3;
  95. static constexpr int s_cascadesCountDefault = 4;
  96. static constexpr float s_ratioLogarithmUniformDefault = 0.8f;
  97. bool m_dirShadowEnabled = true;
  98. int m_directionalLightShadowmapSizeIndex = 0;
  99. int m_cascadeCount = 0;
  100. float m_ratioLogarithmUniform = 0.f;
  101. AZ::Render::ShadowmapSize m_diskLightShadowmapSize = AZ::Render::ShadowmapSize::None;
  102. bool m_diskLightShadowEnabled = true;
  103. // Edge-softening of directional light shadows
  104. static const AZ::Render::ShadowFilterMethod s_shadowFilterMethods[];
  105. static const char* s_shadowFilterMethodLabels[];
  106. int m_shadowFilterMethodIndex = 0; // filter method is None.
  107. int m_predictionSampleCount = 4;
  108. int m_filteringSampleCount = 16;
  109. bool m_isCascadeCorrectionEnabled = false;
  110. bool m_isDebugColoringEnabled = false;
  111. };
  112. } // namespace AtomSampleViewer