CullingAndLodExampleComponent.h 5.2 KB

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