EditorSkyAtmosphereComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include <SkyAtmosphere/EditorSkyAtmosphereComponent.h>
  9. #include <AtomLyIntegration/CommonFeatures/SkyAtmosphere/SkyAtmosphereComponentConfig.h>
  10. namespace AZ::Render
  11. {
  12. void EditorSkyAtmosphereComponent::Reflect(AZ::ReflectContext* context)
  13. {
  14. BaseClass::Reflect(context);
  15. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  16. {
  17. serializeContext->Class<EditorSkyAtmosphereComponent, BaseClass>()
  18. ->Version(1);
  19. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  20. {
  21. editContext->Class<EditorSkyAtmosphereComponent>(
  22. "Sky Atmosphere", "Sky atmosphere component that renders a physical atmosphere ")
  23. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  24. ->Attribute(AZ::Edit::Attributes::Category, "Graphics/Environment")
  25. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Component_Placeholder.svg")
  26. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/Component_Placeholder.svg")
  27. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  28. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  29. ;
  30. editContext->Class<SkyAtmosphereComponentController>(
  31. "SkyAtmosphereComponentController", "")
  32. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  33. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  34. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentController::m_configuration, "Configuration", "")
  35. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  36. ;
  37. editContext->Class<SkyAtmosphereComponentConfig>(
  38. "SkyAtmosphereComponentConfig", "")
  39. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  40. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  41. ->ClassElement(AZ::Edit::ClassElements::Group, "Planet")
  42. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  43. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &SkyAtmosphereComponentConfig::m_originMode, "Origin", "The origin to use for the atmosphere")
  44. ->EnumAttribute(SkyAtmosphereComponentConfig::AtmosphereOrigin::GroundAtWorldOrigin, "Ground at world origin")
  45. ->EnumAttribute(SkyAtmosphereComponentConfig::AtmosphereOrigin::GroundAtLocalOrigin, "Ground at local origin")
  46. ->EnumAttribute(SkyAtmosphereComponentConfig::AtmosphereOrigin::PlanetCenterAtLocalOrigin, "Planet center at local origin")
  47. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_groundRadius, "Ground radius", "Ground radius")
  48. ->Attribute(AZ::Edit::Attributes::Suffix, " km")
  49. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  50. ->Attribute(AZ::Edit::Attributes::Max, 100000.0f)
  51. ->DataElement(AZ::Edit::UIHandlers::Color, &SkyAtmosphereComponentConfig::m_groundAlbedo, "Ground albedo", "Additional light from the surface of the ground")
  52. ->EndGroup()
  53. ->ClassElement(AZ::Edit::ClassElements::Group, "Atmosphere")
  54. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  55. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_atmosphereHeight, "Atmosphere height", "Atmosphere height")
  56. ->Attribute(AZ::Edit::Attributes::Suffix, " km")
  57. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  58. ->Attribute(AZ::Edit::Attributes::Max, 10000.0f)
  59. ->DataElement(AZ::Edit::UIHandlers::Slider, &SkyAtmosphereComponentConfig::m_luminanceFactor, "Illuminance factor", "An additional factor to brighten or darken the overall atmosphere")
  60. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  61. ->Attribute(AZ::Edit::Attributes::Max, 100.0f)
  62. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_rayleighScatteringScale, "Rayleigh scattering scale", "Raleigh scattering scale")
  63. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  64. ->Attribute(AZ::Edit::Attributes::Max, 1.0f)
  65. ->DataElement(AZ::Edit::UIHandlers::Color, &SkyAtmosphereComponentConfig::m_rayleighScattering, "Rayleigh scattering", "Raleigh scattering coefficients from air molecules at surface of the planet")
  66. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_rayleighExponentialDistribution, "Rayleigh exponential distribution", "Altitude at which Rayleigh scattering is reduced to roughly 40%")
  67. ->Attribute(AZ::Edit::Attributes::Suffix, " km")
  68. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  69. ->Attribute(AZ::Edit::Attributes::Max, 400.0f)
  70. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_mieScatteringScale, "Mie scattering scale", "Mie scattering scale")
  71. ->Attribute(AZ::Edit::Attributes::Min, 0.00f)
  72. ->Attribute(AZ::Edit::Attributes::Max, 1.0f)
  73. ->DataElement(AZ::Edit::UIHandlers::Color, &SkyAtmosphereComponentConfig::m_mieScattering, "Mie scattering", "Mie scattering coefficients from aerosole molecules at surface of the planet")
  74. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_mieAbsorptionScale, "Mie absorption scale", "Mie absorption scale")
  75. ->Attribute(AZ::Edit::Attributes::Min, 0.00f)
  76. ->Attribute(AZ::Edit::Attributes::Max, 1.0f)
  77. ->DataElement(AZ::Edit::UIHandlers::Color, &SkyAtmosphereComponentConfig::m_mieAbsorption, "Mie absorption", "Mie absorption coefficients from aerosole molecules at surface of the planet")
  78. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_mieExponentialDistribution, "Mie exponential distribution", "Altitude at which Mie scattering is reduced to roughly 40%")
  79. ->Attribute(AZ::Edit::Attributes::Suffix, " km")
  80. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  81. ->Attribute(AZ::Edit::Attributes::Max, 400.0f)
  82. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_absorptionScale, "Ozone absorption scale", "Ozone molecule absorption scale")
  83. ->Attribute(AZ::Edit::Attributes::Min, 0.00f)
  84. ->Attribute(AZ::Edit::Attributes::Max, 1.0f)
  85. ->DataElement(AZ::Edit::UIHandlers::Color, &SkyAtmosphereComponentConfig::m_absorption, "Ozone absorption", "Absorption coefficients from ozone molecules in a layer most dense at roughly the middle height of the atmosphere")
  86. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_aerialDepthFactor, "Aerial depth factor", "A factor applied to object's depth to increase or decrease aeriel perspective")
  87. ->Attribute(AZ::Edit::Attributes::Min, 0.00f)
  88. ->Attribute(AZ::Edit::Attributes::Max, 100.0f)
  89. ->EndGroup()
  90. ->ClassElement(AZ::Edit::ClassElements::Group, "Sun")
  91. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  92. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &SkyAtmosphereComponentConfig::m_drawSun, "Show sun", "Whether to show the sun or not")
  93. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_sun, "Sun orientation", "Optional sun entity to use for orientation")
  94. ->DataElement(AZ::Edit::UIHandlers::Color, &SkyAtmosphereComponentConfig::m_sunColor, "Sun color", "Sun color")
  95. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_sunLuminanceFactor, "Sun luminance factor", "Sun luminance factor")
  96. ->Attribute(AZ::Edit::Attributes::Min, 0.00f)
  97. ->Attribute(AZ::Edit::Attributes::Max, 100000.0f)
  98. ->DataElement(AZ::Edit::UIHandlers::Color, &SkyAtmosphereComponentConfig::m_sunLimbColor, "Sun limb color", "Sun limb color, for adjusting outer edge color of sun")
  99. ->DataElement(AZ::Edit::UIHandlers::Slider, &SkyAtmosphereComponentConfig::m_sunRadiusFactor, "Sun radius factor", "Sun radius factor")
  100. ->Attribute(AZ::Edit::Attributes::Min, 0.0001f)
  101. ->Attribute(AZ::Edit::Attributes::Max, 100.0f)
  102. ->DataElement(AZ::Edit::UIHandlers::Slider, &SkyAtmosphereComponentConfig::m_sunFalloffFactor, "Sun falloff factor", "Sun falloff factor")
  103. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  104. ->Attribute(AZ::Edit::Attributes::Max, 200.0f)
  105. ->EndGroup()
  106. ->ClassElement(AZ::Edit::ClassElements::Group, "Advanced")
  107. ->Attribute(AZ::Edit::Attributes::AutoExpand, false)
  108. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &SkyAtmosphereComponentConfig::m_fastSkyEnabled, "Fast sky", "Enable to use a less accurate but faster performing sky algorithm")
  109. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &SkyAtmosphereComponentConfig::m_fastAerialPerspectiveEnabled, "Fast aerial perspective", "Enable to use a volume look-up-texture for faster performance but more memory")
  110. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &SkyAtmosphereComponentConfig::m_aerialPerspectiveEnabled, "Aerial perspective", "Enable to draw the effect of atmosphere scattering on objects in the scene.")
  111. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &SkyAtmosphereComponentConfig::m_shadowsEnabled, "Enable shadows", "Enable sampling of shadows in atmosphere")
  112. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_nearClip, "Near clip", "Distance at which to start drawing atmosphere")
  113. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  114. ->DataElement(AZ::Edit::UIHandlers::Default, &SkyAtmosphereComponentConfig::m_nearFadeDistance, "Near fade distance", "Distance over which to fade in the atmosphere")
  115. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  116. ->DataElement(AZ::Edit::UIHandlers::Slider, &SkyAtmosphereComponentConfig::m_minSamples, "Min samples", "Minimum number of samples when tracing")
  117. ->Attribute(AZ::Edit::Attributes::Min, 1)
  118. ->Attribute(AZ::Edit::Attributes::Max, 64)
  119. ->DataElement(AZ::Edit::UIHandlers::Slider, &SkyAtmosphereComponentConfig::m_maxSamples, "Max samples", "Maximum number of samples when tracing")
  120. ->Attribute(AZ::Edit::Attributes::Min, 1)
  121. ->Attribute(AZ::Edit::Attributes::Max, 64)
  122. ->EndGroup()
  123. ;
  124. }
  125. }
  126. if (auto* behaviorContext = azrtti_cast<BehaviorContext*>(context))
  127. {
  128. behaviorContext->Class<EditorSkyAtmosphereComponent>()
  129. ->RequestBus("SkyAtmosphereRequestBus");
  130. }
  131. }
  132. EditorSkyAtmosphereComponent::EditorSkyAtmosphereComponent(const SkyAtmosphereComponentConfig& config)
  133. : BaseClass(config)
  134. {
  135. }
  136. AZ::u32 EditorSkyAtmosphereComponent::OnConfigurationChanged()
  137. {
  138. if(auto featureProcessor = m_controller.m_featureProcessorInterface)
  139. {
  140. featureProcessor->SetAtmosphereParams(m_controller.m_atmosphereId, m_controller.GetUpdatedSkyAtmosphereParams());
  141. }
  142. return Edit::PropertyRefreshLevels::AttributesAndValues;
  143. }
  144. void EditorSkyAtmosphereComponent::OnEntityVisibilityChanged(bool visibility)
  145. {
  146. if(auto featureProcessor = m_controller.m_featureProcessorInterface)
  147. {
  148. if (visibility && m_controller.m_atmosphereId.IsNull())
  149. {
  150. m_controller.m_atmosphereId = featureProcessor->CreateAtmosphere();
  151. featureProcessor->SetAtmosphereParams(m_controller.m_atmosphereId, m_controller.GetUpdatedSkyAtmosphereParams());
  152. }
  153. else if (!visibility && !m_controller.m_atmosphereId.IsNull())
  154. {
  155. featureProcessor->ReleaseAtmosphere(m_controller.m_atmosphereId);
  156. m_controller.m_atmosphereId.Reset();
  157. }
  158. }
  159. }
  160. }