CullingAndLodExampleComponent.h 5.5 KB

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