LightingPreset.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. ->Field("visible", &LightConfig::m_visible)
  68. ;
  69. }
  70. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  71. {
  72. behaviorContext->Class<LightConfig>("LightConfig")
  73. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  74. ->Attribute(AZ::Script::Attributes::Category, "Editor")
  75. ->Attribute(AZ::Script::Attributes::Module, "render")
  76. ->Constructor()
  77. ->Constructor<const LightConfig&>()
  78. ->Property("direction", BehaviorValueProperty(&LightConfig::m_direction))
  79. ->Property("color", BehaviorValueProperty(&LightConfig::m_color))
  80. ->Property("intensity", BehaviorValueProperty(&LightConfig::m_intensity))
  81. ->Property("shadowCascadeCount", BehaviorValueProperty(&LightConfig::m_shadowCascadeCount))
  82. ->Property("shadowRatioLogarithmUniform", BehaviorValueProperty(&LightConfig::m_shadowRatioLogarithmUniform))
  83. ->Property("shadowFarClipDistance", BehaviorValueProperty(&LightConfig::m_shadowFarClipDistance))
  84. ->Property("shadowmapSize", BehaviorValueProperty(&LightConfig::m_shadowmapSize))
  85. ->Property("enableShadowDebugColoring", BehaviorValueProperty(&LightConfig::m_enableShadowDebugColoring))
  86. ->Property("visible", BehaviorValueProperty(&LightConfig::m_visible))
  87. ;
  88. }
  89. }
  90. void LightingPreset::Reflect(AZ::ReflectContext* context)
  91. {
  92. ExposureControlConfig::Reflect(context);
  93. LightConfig::Reflect(context);
  94. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  95. {
  96. serializeContext->RegisterGenericType<AZStd::vector<LightConfig>>();
  97. serializeContext->Class<LightingPreset>()
  98. ->Version(6)
  99. ->Field("iblDiffuseImageAsset", &LightingPreset::m_iblDiffuseImageAsset)
  100. ->Field("iblSpecularImageAsset", &LightingPreset::m_iblSpecularImageAsset)
  101. ->Field("skyboxImageAsset", &LightingPreset::m_skyboxImageAsset)
  102. ->Field("alternateSkyboxImageAsset", &LightingPreset::m_alternateSkyboxImageAsset)
  103. ->Field("iblExposure", &LightingPreset::m_iblExposure)
  104. ->Field("skyboxExposure", &LightingPreset::m_skyboxExposure)
  105. ->Field("shadowCatcherOpacity", &LightingPreset::m_shadowCatcherOpacity)
  106. ->Field("exposure", &LightingPreset::m_exposure)
  107. ->Field("lights", &LightingPreset::m_lights)
  108. ;
  109. }
  110. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  111. {
  112. behaviorContext->Class<LightingPreset>("LightingPreset")
  113. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  114. ->Attribute(AZ::Script::Attributes::Category, "Editor")
  115. ->Attribute(AZ::Script::Attributes::Module, "render")
  116. ->Constructor()
  117. ->Constructor<const LightingPreset&>()
  118. ->Property("alternateSkyboxImageAsset", BehaviorValueProperty(&LightingPreset::m_alternateSkyboxImageAsset))
  119. ->Property("skyboxImageAsset", BehaviorValueProperty(&LightingPreset::m_skyboxImageAsset))
  120. ->Property("iblSpecularImageAsset", BehaviorValueProperty(&LightingPreset::m_iblSpecularImageAsset))
  121. ->Property("iblDiffuseImageAsset", BehaviorValueProperty(&LightingPreset::m_iblDiffuseImageAsset))
  122. ->Property("iblExposure", BehaviorValueProperty(&LightingPreset::m_iblExposure))
  123. ->Property("skyboxExposure", BehaviorValueProperty(&LightingPreset::m_skyboxExposure))
  124. ->Property("exposure", BehaviorValueProperty(&LightingPreset::m_exposure))
  125. ->Property("lights", BehaviorValueProperty(&LightingPreset::m_lights))
  126. ->Property("shadowCatcherOpacity", BehaviorValueProperty(&LightingPreset::m_shadowCatcherOpacity))
  127. ;
  128. }
  129. }
  130. void LightingPreset::ApplyLightingPreset(
  131. ImageBasedLightFeatureProcessorInterface* iblFeatureProcessor,
  132. SkyBoxFeatureProcessorInterface* skyboxFeatureProcessor,
  133. ExposureControlSettingsInterface* exposureControlSettingsInterface,
  134. DirectionalLightFeatureProcessorInterface* directionalLightFeatureProcessor,
  135. const Camera::Configuration& cameraConfig,
  136. AZStd::vector<DirectionalLightFeatureProcessorInterface::LightHandle>& lightHandles,
  137. bool enableAlternateSkybox) const
  138. {
  139. if (iblFeatureProcessor)
  140. {
  141. iblFeatureProcessor->SetDiffuseImage(m_iblDiffuseImageAsset);
  142. iblFeatureProcessor->SetSpecularImage(m_iblSpecularImageAsset);
  143. iblFeatureProcessor->SetExposure(m_iblExposure);
  144. }
  145. if (skyboxFeatureProcessor)
  146. {
  147. auto skyboxAsset = (enableAlternateSkybox && m_alternateSkyboxImageAsset.GetId().IsValid()) ? m_alternateSkyboxImageAsset : m_skyboxImageAsset;
  148. skyboxFeatureProcessor->SetCubemap(RPI::StreamingImage::FindOrCreate(skyboxAsset));
  149. skyboxFeatureProcessor->SetCubemapExposure(m_skyboxExposure);
  150. }
  151. if (exposureControlSettingsInterface)
  152. {
  153. exposureControlSettingsInterface->SetExposureControlType(static_cast<ExposureControl::ExposureControlType>(m_exposure.m_exposureControlType));
  154. exposureControlSettingsInterface->SetManualCompensation(m_exposure.m_manualCompensationValue);
  155. exposureControlSettingsInterface->SetEyeAdaptationExposureMin(m_exposure.m_autoExposureMin);
  156. exposureControlSettingsInterface->SetEyeAdaptationExposureMax(m_exposure.m_autoExposureMax);
  157. exposureControlSettingsInterface->SetEyeAdaptationSpeedUp(m_exposure.m_autoExposureSpeedUp);
  158. exposureControlSettingsInterface->SetEyeAdaptationSpeedDown(m_exposure.m_autoExposureSpeedDown);
  159. }
  160. if (directionalLightFeatureProcessor)
  161. {
  162. // Destroy previous lights
  163. for (DirectionalLightFeatureProcessorInterface::LightHandle& handle : lightHandles)
  164. {
  165. directionalLightFeatureProcessor->ReleaseLight(handle);
  166. }
  167. lightHandles.clear();
  168. // Create new lights
  169. for (const auto& lightConfig : m_lights)
  170. {
  171. const DirectionalLightFeatureProcessorInterface::LightHandle lightHandle = directionalLightFeatureProcessor->AcquireLight();
  172. PhotometricColor<PhotometricUnit::Lux> lightColor(lightConfig.m_color * lightConfig.m_intensity);
  173. directionalLightFeatureProcessor->SetDirection(lightHandle, lightConfig.m_direction);
  174. directionalLightFeatureProcessor->SetRgbIntensity(lightHandle, lightColor);
  175. directionalLightFeatureProcessor->SetCascadeCount(lightHandle, lightConfig.m_shadowCascadeCount);
  176. directionalLightFeatureProcessor->SetShadowmapFrustumSplitSchemeRatio(lightHandle, lightConfig.m_shadowRatioLogarithmUniform);
  177. directionalLightFeatureProcessor->SetShadowFarClipDistance(lightHandle, lightConfig.m_shadowFarClipDistance);
  178. directionalLightFeatureProcessor->SetShadowmapSize(lightHandle, lightConfig.m_shadowmapSize);
  179. directionalLightFeatureProcessor->SetVisible(lightHandle, lightConfig.m_visible);
  180. int flags = lightConfig.m_enableShadowDebugColoring ?
  181. DirectionalLightFeatureProcessorInterface::DebugDrawFlags::DebugDrawAll :
  182. DirectionalLightFeatureProcessorInterface::DebugDrawFlags::DebugDrawNone;
  183. directionalLightFeatureProcessor->SetDebugFlags(lightHandle, static_cast<DirectionalLightFeatureProcessorInterface::DebugDrawFlags>(flags));
  184. directionalLightFeatureProcessor->SetCameraConfiguration(lightHandle, cameraConfig);
  185. lightHandles.push_back(lightHandle);
  186. }
  187. }
  188. }
  189. } // namespace Render
  190. } // namespace AZ