LightingPreset.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #undef RC_INVOKED
  9. #include <Atom/Feature/Utils/LightingPreset.h>
  10. #include <AzCore/Asset/AssetSerializer.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/RTTI/BehaviorContext.h>
  13. #include <Atom/RPI.Public/Image/StreamingImage.h>
  14. #include <Atom/Feature/SkyBox/SkyBoxFeatureProcessorInterface.h>
  15. #include <Atom/Feature/PostProcess/PostProcessFeatureProcessorInterface.h>
  16. #include <Atom/Feature/ImageBasedLights/ImageBasedLightFeatureProcessorInterface.h>
  17. #include <Atom/Feature/CoreLights/DirectionalLightFeatureProcessorInterface.h>
  18. namespace AZ
  19. {
  20. namespace Render
  21. {
  22. void ExposureControlConfig::Reflect(AZ::ReflectContext* context)
  23. {
  24. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  25. {
  26. serializeContext->Class<ExposureControlConfig>()
  27. ->Version(4)
  28. ->Field("compensateValue", &ExposureControlConfig::m_manualCompensationValue)
  29. ->Field("exposureControlType", &ExposureControlConfig::m_exposureControlType)
  30. ->Field("autoExposureMin", &ExposureControlConfig::m_autoExposureMin)
  31. ->Field("autoExposureMax", &ExposureControlConfig::m_autoExposureMax)
  32. ->Field("autoExposureSpeedUp", &ExposureControlConfig::m_autoExposureSpeedUp)
  33. ->Field("autoExposureSpeedDown", &ExposureControlConfig::m_autoExposureSpeedDown)
  34. ;
  35. }
  36. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  37. {
  38. behaviorContext->Class<ExposureControlConfig>("ExposureControlConfig")
  39. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  40. ->Attribute(AZ::Script::Attributes::Category, "Editor")
  41. ->Attribute(AZ::Script::Attributes::Module, "render")
  42. ->Constructor()
  43. ->Constructor<const ExposureControlConfig&>()
  44. ->Property("compensateValue", BehaviorValueProperty(&ExposureControlConfig::m_manualCompensationValue))
  45. ->Property("exposureControlType", BehaviorValueProperty(&ExposureControlConfig::m_exposureControlType))
  46. ->Property("autoExposureMin", BehaviorValueProperty(&ExposureControlConfig::m_autoExposureMin))
  47. ->Property("autoExposureMax", BehaviorValueProperty(&ExposureControlConfig::m_autoExposureMax))
  48. ->Property("autoExposureSpeedUp", BehaviorValueProperty(&ExposureControlConfig::m_autoExposureSpeedUp))
  49. ->Property("autoExposureSpeedDown", BehaviorValueProperty(&ExposureControlConfig::m_autoExposureSpeedDown))
  50. ;
  51. }
  52. }
  53. void LightConfig::Reflect(AZ::ReflectContext* context)
  54. {
  55. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  56. {
  57. serializeContext->Class<LightConfig>()
  58. ->Version(2)
  59. ->Field("direction", &LightConfig::m_direction)
  60. ->Field("color", &LightConfig::m_color)
  61. ->Field("intensity", &LightConfig::m_intensity)
  62. ->Field("shadowCascadeCount", &LightConfig::m_shadowCascadeCount)
  63. ->Field("shadowRatioLogarithmUniform", &LightConfig::m_shadowRatioLogarithmUniform)
  64. ->Field("shadowFarClipDistance", &LightConfig::m_shadowFarClipDistance)
  65. ->Field("shadowmapSize", &LightConfig::m_shadowmapSize)
  66. ->Field("enableShadowDebugColoring", &LightConfig::m_enableShadowDebugColoring)
  67. ;
  68. }
  69. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  70. {
  71. behaviorContext->Class<LightConfig>("LightConfig")
  72. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  73. ->Attribute(AZ::Script::Attributes::Category, "Editor")
  74. ->Attribute(AZ::Script::Attributes::Module, "render")
  75. ->Constructor()
  76. ->Constructor<const LightConfig&>()
  77. ->Property("direction", BehaviorValueProperty(&LightConfig::m_direction))
  78. ->Property("color", BehaviorValueProperty(&LightConfig::m_color))
  79. ->Property("intensity", BehaviorValueProperty(&LightConfig::m_intensity))
  80. ->Property("shadowCascadeCount", BehaviorValueProperty(&LightConfig::m_shadowCascadeCount))
  81. ->Property("shadowRatioLogarithmUniform", BehaviorValueProperty(&LightConfig::m_shadowRatioLogarithmUniform))
  82. ->Property("shadowFarClipDistance", BehaviorValueProperty(&LightConfig::m_shadowFarClipDistance))
  83. ->Property("shadowmapSize", BehaviorValueProperty(&LightConfig::m_shadowmapSize))
  84. ->Property("enableShadowDebugColoring", BehaviorValueProperty(&LightConfig::m_enableShadowDebugColoring))
  85. ;
  86. }
  87. }
  88. void LightingPreset::Reflect(AZ::ReflectContext* context)
  89. {
  90. ExposureControlConfig::Reflect(context);
  91. LightConfig::Reflect(context);
  92. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  93. {
  94. serializeContext->RegisterGenericType<AZStd::vector<LightConfig>>();
  95. serializeContext->Class<LightingPreset>()
  96. ->Version(6)
  97. ->Field("iblDiffuseImageAsset", &LightingPreset::m_iblDiffuseImageAsset)
  98. ->Field("iblSpecularImageAsset", &LightingPreset::m_iblSpecularImageAsset)
  99. ->Field("skyboxImageAsset", &LightingPreset::m_skyboxImageAsset)
  100. ->Field("alternateSkyboxImageAsset", &LightingPreset::m_alternateSkyboxImageAsset)
  101. ->Field("iblExposure", &LightingPreset::m_iblExposure)
  102. ->Field("skyboxExposure", &LightingPreset::m_skyboxExposure)
  103. ->Field("shadowCatcherOpacity", &LightingPreset::m_shadowCatcherOpacity)
  104. ->Field("exposure", &LightingPreset::m_exposure)
  105. ->Field("lights", &LightingPreset::m_lights)
  106. ;
  107. }
  108. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  109. {
  110. behaviorContext->Class<LightingPreset>("LightingPreset")
  111. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  112. ->Attribute(AZ::Script::Attributes::Category, "Editor")
  113. ->Attribute(AZ::Script::Attributes::Module, "render")
  114. ->Constructor()
  115. ->Constructor<const LightingPreset&>()
  116. ->Property("alternateSkyboxImageAsset", BehaviorValueProperty(&LightingPreset::m_alternateSkyboxImageAsset))
  117. ->Property("skyboxImageAsset", BehaviorValueProperty(&LightingPreset::m_skyboxImageAsset))
  118. ->Property("iblSpecularImageAsset", BehaviorValueProperty(&LightingPreset::m_iblSpecularImageAsset))
  119. ->Property("iblDiffuseImageAsset", BehaviorValueProperty(&LightingPreset::m_iblDiffuseImageAsset))
  120. ->Property("iblExposure", BehaviorValueProperty(&LightingPreset::m_iblExposure))
  121. ->Property("skyboxExposure", BehaviorValueProperty(&LightingPreset::m_skyboxExposure))
  122. ->Property("exposure", BehaviorValueProperty(&LightingPreset::m_exposure))
  123. ->Property("lights", BehaviorValueProperty(&LightingPreset::m_lights))
  124. ->Property("shadowCatcherOpacity", BehaviorValueProperty(&LightingPreset::m_shadowCatcherOpacity))
  125. ;
  126. }
  127. }
  128. void LightingPreset::ApplyLightingPreset(
  129. ImageBasedLightFeatureProcessorInterface* iblFeatureProcessor,
  130. SkyBoxFeatureProcessorInterface* skyboxFeatureProcessor,
  131. ExposureControlSettingsInterface* exposureControlSettingsInterface,
  132. DirectionalLightFeatureProcessorInterface* directionalLightFeatureProcessor,
  133. const Camera::Configuration& cameraConfig,
  134. AZStd::vector<DirectionalLightFeatureProcessorInterface::LightHandle>& lightHandles,
  135. bool enableAlternateSkybox) const
  136. {
  137. if (iblFeatureProcessor)
  138. {
  139. iblFeatureProcessor->SetDiffuseImage(m_iblDiffuseImageAsset);
  140. iblFeatureProcessor->SetSpecularImage(m_iblSpecularImageAsset);
  141. iblFeatureProcessor->SetExposure(m_iblExposure);
  142. }
  143. if (skyboxFeatureProcessor)
  144. {
  145. auto skyboxAsset = (enableAlternateSkybox && m_alternateSkyboxImageAsset.GetId().IsValid()) ? m_alternateSkyboxImageAsset : m_skyboxImageAsset;
  146. skyboxFeatureProcessor->SetCubemap(RPI::StreamingImage::FindOrCreate(skyboxAsset));
  147. skyboxFeatureProcessor->SetCubemapExposure(m_skyboxExposure);
  148. }
  149. if (exposureControlSettingsInterface)
  150. {
  151. exposureControlSettingsInterface->SetExposureControlType(static_cast<ExposureControl::ExposureControlType>(m_exposure.m_exposureControlType));
  152. exposureControlSettingsInterface->SetManualCompensation(m_exposure.m_manualCompensationValue);
  153. exposureControlSettingsInterface->SetEyeAdaptationExposureMin(m_exposure.m_autoExposureMin);
  154. exposureControlSettingsInterface->SetEyeAdaptationExposureMax(m_exposure.m_autoExposureMax);
  155. exposureControlSettingsInterface->SetEyeAdaptationSpeedUp(m_exposure.m_autoExposureSpeedUp);
  156. exposureControlSettingsInterface->SetEyeAdaptationSpeedDown(m_exposure.m_autoExposureSpeedDown);
  157. }
  158. if (directionalLightFeatureProcessor)
  159. {
  160. // Destroy previous lights
  161. for (DirectionalLightFeatureProcessorInterface::LightHandle& handle : lightHandles)
  162. {
  163. directionalLightFeatureProcessor->ReleaseLight(handle);
  164. }
  165. lightHandles.clear();
  166. // Create new lights
  167. for (const auto& lightConfig : m_lights)
  168. {
  169. const DirectionalLightFeatureProcessorInterface::LightHandle lightHandle = directionalLightFeatureProcessor->AcquireLight();
  170. PhotometricColor<PhotometricUnit::Lux> lightColor(lightConfig.m_color * lightConfig.m_intensity);
  171. directionalLightFeatureProcessor->SetDirection(lightHandle, lightConfig.m_direction);
  172. directionalLightFeatureProcessor->SetRgbIntensity(lightHandle, lightColor);
  173. directionalLightFeatureProcessor->SetCascadeCount(lightHandle, lightConfig.m_shadowCascadeCount);
  174. directionalLightFeatureProcessor->SetShadowmapFrustumSplitSchemeRatio(lightHandle, lightConfig.m_shadowRatioLogarithmUniform);
  175. directionalLightFeatureProcessor->SetShadowFarClipDistance(lightHandle, lightConfig.m_shadowFarClipDistance);
  176. directionalLightFeatureProcessor->SetShadowmapSize(lightHandle, lightConfig.m_shadowmapSize);
  177. int flags = lightConfig.m_enableShadowDebugColoring ?
  178. DirectionalLightFeatureProcessorInterface::DebugDrawFlags::DebugDrawAll :
  179. DirectionalLightFeatureProcessorInterface::DebugDrawFlags::DebugDrawNone;
  180. directionalLightFeatureProcessor->SetDebugFlags(lightHandle, static_cast<DirectionalLightFeatureProcessorInterface::DebugDrawFlags>(flags));
  181. directionalLightFeatureProcessor->SetCameraConfiguration(lightHandle, cameraConfig);
  182. lightHandles.push_back(lightHandle);
  183. }
  184. }
  185. }
  186. } // namespace Render
  187. } // namespace AZ