3
0

DirectionalLightComponentController.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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 <CoreLights/DirectionalLightComponentController.h>
  9. #include <Atom/RPI.Public/Scene.h>
  10. #include <Atom/Feature/CoreLights/ShadowConstants.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/Component/TransformBus.h>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <AzCore/std/containers/vector.h>
  15. #include <Atom/RPI.Public/ViewportContext.h>
  16. #include <Atom/RPI.Public/ViewportContextBus.h>
  17. #include <Atom/RPI.Public/ViewProviderBus.h>
  18. namespace AZ
  19. {
  20. namespace Render
  21. {
  22. void DirectionalLightComponentController::Reflect(ReflectContext* context)
  23. {
  24. DirectionalLightComponentConfig::Reflect(context);
  25. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  26. {
  27. serializeContext->Class<DirectionalLightComponentController>()
  28. ->Version(1)
  29. ->Field("Configuration", &DirectionalLightComponentController::m_configuration);
  30. ;
  31. }
  32. if (BehaviorContext* behaviorContext = azrtti_cast<BehaviorContext*>(context))
  33. {
  34. behaviorContext
  35. ->Enum<static_cast<uint32_t>(ShadowFilterMethod::None)>("ShadowFilterMethod_None")
  36. ->Enum<static_cast<uint32_t>(ShadowFilterMethod::Pcf)>("ShadowFilterMethod_PCF")
  37. ->Enum<static_cast<uint32_t>(ShadowFilterMethod::Esm)>("ShadowFilterMethod_ESM")
  38. ->Enum<static_cast<uint32_t>(ShadowFilterMethod::EsmPcf)>("ShadowFilterMethod_ESM_PCF")
  39. ->Enum<static_cast<uint32_t>(ShadowmapSize::None)>("ShadowmapSize_None")
  40. ->Enum<static_cast<uint32_t>(ShadowmapSize::Size256)>("ShadowmapSize_256")
  41. ->Enum<static_cast<uint32_t>(ShadowmapSize::Size512)>("ShadowmapSize_512")
  42. ->Enum<static_cast<uint32_t>(ShadowmapSize::Size1024)>("ShadowmapSize_1024")
  43. ->Enum<static_cast<uint32_t>(ShadowmapSize::Size2048)>("ShadowmapSize_2045")
  44. ;
  45. behaviorContext->EBus<DirectionalLightRequestBus>("DirectionalLightRequestBus")
  46. ->Attribute(AZ::Script::Attributes::Module, "render")
  47. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  48. ->Event("GetColor", &DirectionalLightRequestBus::Events::GetColor)
  49. ->Event("SetColor", &DirectionalLightRequestBus::Events::SetColor)
  50. ->Event("GetIntensity", &DirectionalLightRequestBus::Events::GetIntensity)
  51. ->Event("SetIntensity", static_cast<void(DirectionalLightRequestBus::Events::*)(float)>(&DirectionalLightRequestBus::Events::SetIntensity))
  52. ->Event("GetAngularDiameter", &DirectionalLightRequestBus::Events::GetAngularDiameter)
  53. ->Event("SetAngularDiameter", &DirectionalLightRequestBus::Events::SetAngularDiameter)
  54. ->Event("GetShadowmapSize", &DirectionalLightRequestBus::Events::GetShadowmapSize)
  55. ->Event("SetShadowmapSize", &DirectionalLightRequestBus::Events::SetShadowmapSize)
  56. ->Event("GetCascadeCount", &DirectionalLightRequestBus::Events::GetCascadeCount)
  57. ->Event("SetCascadeCount", &DirectionalLightRequestBus::Events::SetCascadeCount)
  58. ->Event("GetSplitRatio", &DirectionalLightRequestBus::Events::GetShadowmapFrustumSplitSchemeRatio)
  59. ->Event("SetSplitRatio", &DirectionalLightRequestBus::Events::SetShadowmapFrustumSplitSchemeRatio)
  60. ->Event("GetCascadeFarDepth", &DirectionalLightRequestBus::Events::GetCascadeFarDepth)
  61. ->Event("SetCascadeFarDepth", &DirectionalLightRequestBus::Events::SetCascadeFarDepth)
  62. ->Event("GetSplitAutomatic", &DirectionalLightRequestBus::Events::GetShadowmapFrustumSplitAutomatic)
  63. ->Event("SetSplitAutomatic", &DirectionalLightRequestBus::Events::SetShadowmapFrustumSplitAutomatic)
  64. ->Event("GetCameraEntityId", &DirectionalLightRequestBus::Events::GetCameraEntityId)
  65. ->Event("SetCameraEntityId", &DirectionalLightRequestBus::Events::SetCameraEntityId)
  66. ->Event("GetShadowFarClipDistance", &DirectionalLightRequestBus::Events::GetShadowFarClipDistance)
  67. ->Event("SetShadowFarClipDistance", &DirectionalLightRequestBus::Events::SetShadowFarClipDistance)
  68. ->Event("GetGroundHeight", &DirectionalLightRequestBus::Events::GetGroundHeight)
  69. ->Event("SetGroundHeight", &DirectionalLightRequestBus::Events::SetGroundHeight)
  70. ->Event("GetViewFrustumCorrectionEnabled", &DirectionalLightRequestBus::Events::GetViewFrustumCorrectionEnabled)
  71. ->Event("SetViewFrustumCorrectionEnabled", &DirectionalLightRequestBus::Events::SetViewFrustumCorrectionEnabled)
  72. ->Event("GetDebugColoringEnabled", &DirectionalLightRequestBus::Events::GetDebugColoringEnabled)
  73. ->Event("SetDebugColoringEnabled", &DirectionalLightRequestBus::Events::SetDebugColoringEnabled)
  74. ->Event("GetShadowFilterMethod", &DirectionalLightRequestBus::Events::GetShadowFilterMethod)
  75. ->Event("SetShadowFilterMethod", &DirectionalLightRequestBus::Events::SetShadowFilterMethod)
  76. ->Event("GetFilteringSampleCount", &DirectionalLightRequestBus::Events::GetFilteringSampleCount)
  77. ->Event("SetFilteringSampleCount", &DirectionalLightRequestBus::Events::SetFilteringSampleCount)
  78. ->Event("GetShadowReceiverPlaneBiasEnabled", &DirectionalLightRequestBus::Events::GetShadowReceiverPlaneBiasEnabled)
  79. ->Event("SetShadowReceiverPlaneBiasEnabled", &DirectionalLightRequestBus::Events::SetShadowReceiverPlaneBiasEnabled)
  80. ->Event("GetShadowBias", &DirectionalLightRequestBus::Events::GetShadowBias)
  81. ->Event("SetShadowBias", &DirectionalLightRequestBus::Events::SetShadowBias)
  82. ->Event("GetNormalShadowBias", &DirectionalLightRequestBus::Events::GetNormalShadowBias)
  83. ->Event("SetNormalShadowBias", &DirectionalLightRequestBus::Events::SetNormalShadowBias)
  84. ->Event("GetCascadeBlendingEnabled", &DirectionalLightRequestBus::Events::GetCascadeBlendingEnabled)
  85. ->Event("SetCascadeBlendingEnabled", &DirectionalLightRequestBus::Events::SetCascadeBlendingEnabled)
  86. ->Event("GetAffectsGI", &DirectionalLightRequestBus::Events::GetAffectsGI)
  87. ->Event("SetAffectsGI", &DirectionalLightRequestBus::Events::SetAffectsGI)
  88. ->Event("GetAffectsGIFactor", &DirectionalLightRequestBus::Events::GetAffectsGIFactor)
  89. ->Event("SetAffectsGIFactor", &DirectionalLightRequestBus::Events::SetAffectsGIFactor)
  90. ->Event("GetLightingChannelMask", &DirectionalLightRequestBus::Events::GetLightingChannelMask)
  91. ->Event("SetLightingChannelMask", &DirectionalLightRequestBus::Events::SetLightingChannelMask)
  92. ->VirtualProperty("Color", "GetColor", "SetColor")
  93. ->VirtualProperty("Intensity", "GetIntensity", "SetIntensity")
  94. ->VirtualProperty("AngularDiameter", "GetAngularDiameter", "SetAngularDiameter")
  95. ->VirtualProperty("ShadowmapSize", "GetShadowmapSize", "SetShadowmapSize")
  96. ->VirtualProperty("CascadeCount", "GetCascadeCount", "SetCascadeCount")
  97. ->VirtualProperty("SplitRatio", "GetSplitRatio", "SetSplitRatio")
  98. ->VirtualProperty("CascadeDepth", "GetCascadeFarDepth", "SetCascadeFarDepth")
  99. ->VirtualProperty("SplitAutomatic", "GetSplitAutomatic", "SetSplitAutomatic")
  100. ->VirtualProperty("ShadowFarClipDistance", "GetShadowFarClipDistance", "SetShadowFarClipDistance")
  101. ->VirtualProperty("GroundHeight", "GetGroundHeight", "SetGroundHeight")
  102. ->VirtualProperty("ViewFrustumCorrectionEnabled", "GetViewFrustumCorrectionEnabled", "SetViewFrustumCorrectionEnabled")
  103. ->VirtualProperty("DebugColoringEnabled", "GetDebugColoringEnabled", "SetDebugColoringEnabled")
  104. ->VirtualProperty("ShadowFilterMethod", "GetShadowFilterMethod", "SetShadowFilterMethod")
  105. ->VirtualProperty("FilteringSampleCount", "GetFilteringSampleCount", "SetFilteringSampleCount")
  106. ->VirtualProperty("ShadowReceiverPlaneBiasEnabled", "GetShadowReceiverPlaneBiasEnabled", "SetShadowReceiverPlaneBiasEnabled")
  107. ->VirtualProperty("ShadowBias", "GetShadowBias", "SetShadowBias")
  108. ->VirtualProperty("NormalShadowBias", "GetNormalShadowBias", "SetNormalShadowBias")
  109. ->VirtualProperty("BlendBetweenCascadesEnabled", "GetCascadeBlendingEnabled", "SetCascadeBlendingEnabled")
  110. ->VirtualProperty("AffectsGI", "GetAffectsGI", "SetAffectsGI")
  111. ->VirtualProperty("AffectsGIFactor", "GetAffectsGIFactor", "SetAffectsGIFactor")
  112. ->VirtualProperty("LightingChannelMask", "GetLightingChannelMask", "SetLightingChannelMask");
  113. ;
  114. }
  115. }
  116. void DirectionalLightComponentController::GetDependentServices(ComponentDescriptor::DependencyArrayType& dependent)
  117. {
  118. dependent.push_back(AZ_CRC("TransformService", 0x8ee22c50));
  119. }
  120. void DirectionalLightComponentController::GetIncompatibleServices(ComponentDescriptor::DependencyArrayType& incompatible)
  121. {
  122. incompatible.push_back(AZ_CRC("DirectionalLightService", 0x5270619f));
  123. incompatible.push_back(AZ_CRC_CE("NonUniformScaleComponent"));
  124. }
  125. void DirectionalLightComponentController::GetProvidedServices(ComponentDescriptor::DependencyArrayType& provided)
  126. {
  127. provided.push_back(AZ_CRC("DirectionalLightService", 0x5270619f));
  128. }
  129. DirectionalLightComponentController::DirectionalLightComponentController(const DirectionalLightComponentConfig& config)
  130. : m_configuration(config)
  131. {
  132. }
  133. void DirectionalLightComponentController::Activate(EntityId entityId)
  134. {
  135. m_entityId = entityId;
  136. m_featureProcessor = RPI::Scene::GetFeatureProcessorForEntity<DirectionalLightFeatureProcessorInterface>(entityId);
  137. AZ_Error("DirectionalLightComponentController", m_featureProcessor, "Could not find a DirectionalLightFeatureProcessorInterface on the scene.");
  138. if (m_featureProcessor)
  139. {
  140. m_lightHandle = m_featureProcessor->AcquireLight();
  141. ApplyConfiguration();
  142. DirectionalLightRequestBus::Handler::BusConnect(entityId);
  143. TickBus::Handler::BusConnect();
  144. TransformNotificationBus::MultiHandler::BusConnect(entityId);
  145. Camera::CameraNotificationBus::Handler::BusConnect();
  146. }
  147. }
  148. void DirectionalLightComponentController::Deactivate()
  149. {
  150. Camera::CameraNotificationBus::Handler::BusDisconnect();
  151. TransformNotificationBus::MultiHandler::BusDisconnect();
  152. TickBus::Handler::BusDisconnect();
  153. DirectionalLightRequestBus::Handler::BusDisconnect();
  154. if (m_featureProcessor)
  155. {
  156. m_featureProcessor->ReleaseLight(m_lightHandle);
  157. }
  158. m_entityId.SetInvalid();
  159. }
  160. void DirectionalLightComponentController::SetConfiguration(const DirectionalLightComponentConfig& config)
  161. {
  162. m_configuration = config;
  163. if (m_featureProcessor)
  164. {
  165. ApplyConfiguration();
  166. }
  167. }
  168. const Color& DirectionalLightComponentController::GetColor() const
  169. {
  170. return m_configuration.m_color;
  171. }
  172. void DirectionalLightComponentController::SetColor(const Color& color)
  173. {
  174. m_configuration.m_color = color;
  175. ColorIntensityChanged();
  176. }
  177. float DirectionalLightComponentController::GetIntensity() const
  178. {
  179. return m_configuration.m_intensity;
  180. }
  181. void DirectionalLightComponentController::SetIntensity(float intensity, PhotometricUnit unit)
  182. {
  183. m_photometricValue.ConvertToPhotometricUnit(unit);
  184. m_photometricValue.SetIntensity(intensity);
  185. m_configuration.m_intensityMode = unit;
  186. m_configuration.m_intensity = intensity;
  187. ColorIntensityChanged();
  188. }
  189. void DirectionalLightComponentController::SetIntensity(float intensity)
  190. {
  191. m_photometricValue.SetIntensity(intensity);
  192. m_configuration.m_intensity = intensity;
  193. ColorIntensityChanged();
  194. }
  195. float DirectionalLightComponentController::GetAngularDiameter() const
  196. {
  197. return m_configuration.m_angularDiameter;
  198. }
  199. void DirectionalLightComponentController::SetAngularDiameter(float angularDiameter)
  200. {
  201. m_configuration.m_angularDiameter = angularDiameter;
  202. if (m_featureProcessor)
  203. {
  204. m_featureProcessor->SetAngularDiameter(m_lightHandle, m_configuration.m_angularDiameter);
  205. }
  206. }
  207. ShadowmapSize DirectionalLightComponentController::GetShadowmapSize() const
  208. {
  209. return m_configuration.m_shadowmapSize;
  210. }
  211. void DirectionalLightComponentController::SetShadowmapSize(ShadowmapSize size)
  212. {
  213. // The minimum valid ShadowmapSize is 256 and maximum one is 2048.
  214. const uint32_t sizeInt = aznumeric_cast<uint32_t>(size);
  215. if (sizeInt < aznumeric_cast<uint32_t>(ShadowmapSize::Size512))
  216. {
  217. size = ShadowmapSize::Size256;
  218. }
  219. else if (sizeInt < aznumeric_cast<uint32_t>(ShadowmapSize::Size1024))
  220. {
  221. size = ShadowmapSize::Size512;
  222. }
  223. else if (sizeInt < aznumeric_cast<uint32_t>(ShadowmapSize::Size2048))
  224. {
  225. size = ShadowmapSize::Size1024;
  226. }
  227. else
  228. {
  229. size = ShadowmapSize::Size2048;
  230. }
  231. m_configuration.m_shadowmapSize = size;
  232. if (m_featureProcessor)
  233. {
  234. m_featureProcessor->SetShadowmapSize(m_lightHandle, size);
  235. }
  236. }
  237. uint32_t DirectionalLightComponentController::GetCascadeCount() const
  238. {
  239. return aznumeric_cast<uint32_t>(m_configuration.m_cascadeCount);
  240. }
  241. void DirectionalLightComponentController::SetCascadeCount(uint32_t cascadeCount)
  242. {
  243. const uint16_t cascadeCount16 = GetMin(static_cast<uint16_t>(Shadow::MaxNumberOfCascades), GetMax<uint16_t>(1, aznumeric_cast<uint16_t>(cascadeCount)));
  244. cascadeCount = cascadeCount16;
  245. m_configuration.m_cascadeCount = cascadeCount16;
  246. if (m_featureProcessor)
  247. {
  248. m_featureProcessor->SetCascadeCount(m_lightHandle, cascadeCount16);
  249. }
  250. }
  251. float DirectionalLightComponentController::GetShadowmapFrustumSplitSchemeRatio() const
  252. {
  253. return m_configuration.m_shadowmapFrustumSplitSchemeRatio;
  254. }
  255. void DirectionalLightComponentController::SetShadowmapFrustumSplitSchemeRatio(float ratio)
  256. {
  257. ratio = GetMin(1.f, GetMax(0.f, ratio));
  258. m_configuration.m_shadowmapFrustumSplitSchemeRatio = ratio;
  259. m_configuration.m_isShadowmapFrustumSplitAutomatic = true;
  260. if (m_featureProcessor)
  261. {
  262. m_featureProcessor->SetShadowmapFrustumSplitSchemeRatio(m_lightHandle, ratio);
  263. }
  264. }
  265. const Vector4& DirectionalLightComponentController::GetCascadeFarDepth()
  266. {
  267. return m_configuration.m_cascadeFarDepths;
  268. }
  269. void DirectionalLightComponentController::SetCascadeFarDepth(const Vector4& farDepth)
  270. {
  271. m_configuration.m_cascadeFarDepths = farDepth;
  272. if (m_featureProcessor)
  273. {
  274. for (uint16_t index = 0; index < Shadow::MaxNumberOfCascades; ++index)
  275. {
  276. m_featureProcessor->SetCascadeFarDepth(m_lightHandle, index, farDepth.GetElement(index));
  277. }
  278. }
  279. }
  280. bool DirectionalLightComponentController::GetShadowmapFrustumSplitAutomatic() const
  281. {
  282. return m_configuration.m_isShadowmapFrustumSplitAutomatic;
  283. }
  284. void DirectionalLightComponentController::SetShadowmapFrustumSplitAutomatic(bool isAutomatic)
  285. {
  286. m_configuration.m_isShadowmapFrustumSplitAutomatic = isAutomatic;
  287. }
  288. EntityId DirectionalLightComponentController::GetCameraEntityId() const
  289. {
  290. return m_configuration.m_cameraEntityId;
  291. }
  292. void DirectionalLightComponentController::SetCameraEntityId(EntityId cameraEntityId)
  293. {
  294. if (m_configuration.m_cameraEntityId.IsValid())
  295. {
  296. TransformNotificationBus::MultiHandler::BusDisconnect(m_configuration.m_cameraEntityId);
  297. }
  298. if (cameraEntityId.IsValid())
  299. {
  300. TransformNotificationBus::MultiHandler::BusConnect(cameraEntityId);
  301. }
  302. m_configuration.m_cameraEntityId = cameraEntityId;
  303. }
  304. float DirectionalLightComponentController::GetShadowFarClipDistance() const
  305. {
  306. return m_configuration.m_shadowFarClipDistance;
  307. }
  308. void DirectionalLightComponentController::SetShadowFarClipDistance(float farDist)
  309. {
  310. m_configuration.m_shadowFarClipDistance = farDist;
  311. if (m_featureProcessor)
  312. {
  313. m_featureProcessor->SetShadowFarClipDistance(m_lightHandle, farDist);
  314. }
  315. }
  316. float DirectionalLightComponentController::GetGroundHeight() const
  317. {
  318. return m_configuration.m_groundHeight;
  319. }
  320. void DirectionalLightComponentController::SetGroundHeight(float groundHeight)
  321. {
  322. m_configuration.m_groundHeight = groundHeight;
  323. if (m_featureProcessor)
  324. {
  325. m_featureProcessor->SetGroundHeight(m_lightHandle, groundHeight);
  326. }
  327. }
  328. bool DirectionalLightComponentController::GetViewFrustumCorrectionEnabled() const
  329. {
  330. return m_configuration.m_isCascadeCorrectionEnabled;
  331. }
  332. void DirectionalLightComponentController::SetViewFrustumCorrectionEnabled(bool enabled)
  333. {
  334. m_configuration.m_isCascadeCorrectionEnabled = enabled;
  335. if (m_featureProcessor)
  336. {
  337. m_featureProcessor->SetViewFrustumCorrectionEnabled(m_lightHandle, enabled);
  338. }
  339. }
  340. bool DirectionalLightComponentController::GetDebugColoringEnabled() const
  341. {
  342. return m_configuration.m_isDebugColoringEnabled;
  343. }
  344. void DirectionalLightComponentController::SetDebugColoringEnabled(bool enabled)
  345. {
  346. m_configuration.m_isDebugColoringEnabled = enabled;
  347. if (m_featureProcessor)
  348. {
  349. m_featureProcessor->SetDebugFlags(m_lightHandle, enabled ?
  350. DirectionalLightFeatureProcessorInterface::DebugDrawFlags::DebugDrawAll :
  351. DirectionalLightFeatureProcessorInterface::DebugDrawFlags::DebugDrawNone
  352. );
  353. }
  354. }
  355. ShadowFilterMethod DirectionalLightComponentController::GetShadowFilterMethod() const
  356. {
  357. return m_configuration.m_shadowFilterMethod;
  358. }
  359. void DirectionalLightComponentController::SetShadowFilterMethod(ShadowFilterMethod method)
  360. {
  361. if (aznumeric_cast<uint32_t>(method) >= aznumeric_cast<uint32_t>(ShadowFilterMethod::Count))
  362. {
  363. method = ShadowFilterMethod::None;
  364. }
  365. m_configuration.m_shadowFilterMethod = method;
  366. if (m_featureProcessor)
  367. {
  368. m_featureProcessor->SetShadowFilterMethod(m_lightHandle, method);
  369. }
  370. }
  371. uint32_t DirectionalLightComponentController::GetFilteringSampleCount() const
  372. {
  373. return aznumeric_cast<uint32_t>(m_configuration.m_filteringSampleCount);
  374. }
  375. void DirectionalLightComponentController::SetShadowBias(float bias)
  376. {
  377. m_configuration.m_shadowBias = bias;
  378. if (m_featureProcessor)
  379. {
  380. m_featureProcessor->SetShadowBias(m_lightHandle, bias);
  381. }
  382. }
  383. float DirectionalLightComponentController::GetShadowBias() const
  384. {
  385. return m_configuration.m_shadowBias;
  386. }
  387. void DirectionalLightComponentController::SetNormalShadowBias(float bias)
  388. {
  389. m_configuration.m_normalShadowBias = bias;
  390. if (m_featureProcessor)
  391. {
  392. m_featureProcessor->SetNormalShadowBias(m_lightHandle, bias);
  393. }
  394. }
  395. float DirectionalLightComponentController::GetNormalShadowBias() const
  396. {
  397. return m_configuration.m_normalShadowBias;
  398. }
  399. void DirectionalLightComponentController::SetFilteringSampleCount(uint32_t count)
  400. {
  401. const uint16_t count16 = GetMin(Shadow::MaxPcfSamplingCount, aznumeric_cast<uint16_t>(count));
  402. m_configuration.m_filteringSampleCount = count16;
  403. if (m_featureProcessor)
  404. {
  405. m_featureProcessor->SetFilteringSampleCount(m_lightHandle, count16);
  406. }
  407. }
  408. void DirectionalLightComponentController::SetFullscreenBlurEnabled(bool enable)
  409. {
  410. m_configuration.m_fullscreenBlurEnabled = enable;
  411. if (m_featureProcessor)
  412. {
  413. m_featureProcessor->SetFullscreenBlurEnabled(m_lightHandle, enable);
  414. }
  415. }
  416. void DirectionalLightComponentController::SetFullscreenBlurConstFalloff(float blurConstFalloff)
  417. {
  418. m_configuration.m_fullscreenBlurConstFalloff = blurConstFalloff;
  419. if (m_featureProcessor)
  420. {
  421. m_featureProcessor->SetFullscreenBlurConstFalloff(m_lightHandle, blurConstFalloff);
  422. }
  423. }
  424. void DirectionalLightComponentController::SetFullscreenBlurDepthFalloffStrength(float blurDepthFalloffStrength)
  425. {
  426. m_configuration.m_fullscreenBlurDepthFalloffStrength = blurDepthFalloffStrength;
  427. if (m_featureProcessor)
  428. {
  429. m_featureProcessor->SetFullscreenBlurDepthFalloffStrength(m_lightHandle, blurDepthFalloffStrength);
  430. }
  431. }
  432. bool DirectionalLightComponentController::GetAffectsGI() const
  433. {
  434. return m_configuration.m_affectsGI;
  435. }
  436. void DirectionalLightComponentController::SetAffectsGI(bool affectsGI)
  437. {
  438. m_configuration.m_affectsGI = affectsGI;
  439. if (m_featureProcessor)
  440. {
  441. m_featureProcessor->SetAffectsGI(m_lightHandle, m_configuration.m_affectsGI);
  442. }
  443. }
  444. float DirectionalLightComponentController::GetAffectsGIFactor() const
  445. {
  446. return m_configuration.m_affectsGIFactor;
  447. }
  448. void DirectionalLightComponentController::SetAffectsGIFactor(float affectsGIFactor)
  449. {
  450. m_configuration.m_affectsGIFactor = affectsGIFactor;
  451. if (m_featureProcessor)
  452. {
  453. m_featureProcessor->SetAffectsGIFactor(m_lightHandle, m_configuration.m_affectsGIFactor);
  454. }
  455. }
  456. void DirectionalLightComponentController::BindConfigurationChangedEventHandler(DirectionalLightConfigurationChangedEvent::Handler& configurationChangedHandler)
  457. {
  458. configurationChangedHandler.Connect(m_configurationChangedEvent);
  459. }
  460. uint32_t DirectionalLightComponentController::GetLightingChannelMask() const
  461. {
  462. return m_configuration.m_lightingChannelConfig.GetLightingChannelMask();
  463. }
  464. void DirectionalLightComponentController::SetLightingChannelMask(uint32_t lightingChannelMask)
  465. {
  466. m_configuration.m_lightingChannelConfig.SetLightingChannelMask(lightingChannelMask);
  467. m_featureProcessor->SetLightingChannelMask(m_lightHandle, m_configuration.m_lightingChannelConfig.GetLightingChannelMask());
  468. }
  469. const DirectionalLightComponentConfig& DirectionalLightComponentController::GetConfiguration() const
  470. {
  471. return m_configuration;
  472. }
  473. void DirectionalLightComponentController::OnTick(float, ScriptTimePoint)
  474. {
  475. if (!m_configuration.m_cameraEntityId.IsValid())
  476. {
  477. UpdateCameraTransform();
  478. }
  479. if (m_featureProcessor)
  480. {
  481. m_featureProcessor->SetCameraConfiguration(m_lightHandle, GetCameraConfiguration());
  482. }
  483. }
  484. void DirectionalLightComponentController::OnTransformChanged(const Transform&, const Transform&)
  485. {
  486. const EntityId* currentBusId = TransformNotificationBus::GetCurrentBusId();
  487. if (!currentBusId)
  488. {
  489. AZ_Assert(false, "Cannot get current Bus ID.");
  490. return;
  491. }
  492. if (*currentBusId == m_entityId)
  493. {
  494. UpdateLightTransform();
  495. }
  496. else if (*currentBusId == m_configuration.m_cameraEntityId)
  497. {
  498. UpdateCameraTransform();
  499. }
  500. }
  501. void DirectionalLightComponentController::OnCameraAdded(const AZ::EntityId& cameraId)
  502. {
  503. if (cameraId == m_configuration.m_cameraEntityId)
  504. {
  505. TransformNotificationBus::MultiHandler::BusConnect(cameraId);
  506. }
  507. }
  508. void DirectionalLightComponentController::OnCameraRemoved(const AZ::EntityId& cameraId)
  509. {
  510. if (cameraId == m_configuration.m_cameraEntityId)
  511. {
  512. TransformNotificationBus::MultiHandler::BusDisconnect(cameraId);
  513. m_configuration.m_cameraEntityId.SetInvalid();
  514. }
  515. }
  516. void DirectionalLightComponentController::ApplyConfiguration()
  517. {
  518. m_photometricValue = PhotometricValue(m_configuration.m_intensity, m_configuration.m_color, m_configuration.m_intensityMode);
  519. ColorIntensityChanged();
  520. SetAngularDiameter(m_configuration.m_angularDiameter);
  521. SetShadowEnabled(m_configuration.m_shadowEnabled);
  522. SetCameraEntityId(m_configuration.m_cameraEntityId);
  523. SetCascadeCount(m_configuration.m_cascadeCount);
  524. if (m_configuration.m_isShadowmapFrustumSplitAutomatic)
  525. {
  526. SetShadowmapFrustumSplitSchemeRatio(m_configuration.m_shadowmapFrustumSplitSchemeRatio);
  527. }
  528. else
  529. {
  530. SetCascadeFarDepth(m_configuration.m_cascadeFarDepths);
  531. }
  532. SetShadowmapSize(m_configuration.m_shadowmapSize);
  533. UpdateLightTransform();
  534. m_lastCameraTransform = Transform::CreateTranslation(AZ::Vector3(std::numeric_limits<float>::max())); // invalid value
  535. UpdateCameraTransform();
  536. if (m_featureProcessor)
  537. {
  538. m_featureProcessor->SetCameraConfiguration(m_lightHandle, GetCameraConfiguration());
  539. }
  540. SetShadowFarClipDistance(m_configuration.m_shadowFarClipDistance);
  541. SetGroundHeight(m_configuration.m_groundHeight);
  542. SetViewFrustumCorrectionEnabled(m_configuration.m_isCascadeCorrectionEnabled);
  543. SetDebugColoringEnabled(m_configuration.m_isDebugColoringEnabled);
  544. SetShadowFilterMethod(m_configuration.m_shadowFilterMethod);
  545. SetShadowBias(m_configuration.m_shadowBias);
  546. SetNormalShadowBias(m_configuration.m_normalShadowBias);
  547. SetFilteringSampleCount(m_configuration.m_filteringSampleCount);
  548. SetShadowReceiverPlaneBiasEnabled(m_configuration.m_receiverPlaneBiasEnabled);
  549. SetCascadeBlendingEnabled(m_configuration.m_cascadeBlendingEnabled);
  550. SetFullscreenBlurEnabled(m_configuration.m_fullscreenBlurEnabled);
  551. SetFullscreenBlurConstFalloff(m_configuration.m_fullscreenBlurConstFalloff);
  552. SetFullscreenBlurDepthFalloffStrength(m_configuration.m_fullscreenBlurDepthFalloffStrength);
  553. SetAffectsGI(m_configuration.m_affectsGI);
  554. SetAffectsGIFactor(m_configuration.m_affectsGIFactor);
  555. LightingChannelMaskChanged();
  556. // [GFX TODO][ATOM-1726] share config for multiple light (e.g., light ID).
  557. // [GFX TODO][ATOM-2416] adapt to multiple viewports.
  558. }
  559. Camera::Configuration DirectionalLightComponentController::GetCameraConfiguration() const
  560. {
  561. Camera::Configuration config;
  562. if (m_configuration.m_cameraEntityId.IsValid())
  563. {
  564. Camera::CameraRequestBus::EventResult(
  565. config,
  566. m_configuration.m_cameraEntityId,
  567. &Camera::CameraRequestBus::Events::GetCameraConfiguration);
  568. }
  569. else
  570. {
  571. Camera::ActiveCameraRequestBus::BroadcastResult(
  572. config,
  573. &Camera::ActiveCameraRequestBus::Events::GetActiveCameraConfiguration);
  574. }
  575. if (config.m_fovRadians == 0.f)
  576. {
  577. // When the entity does not have a camera component,
  578. // the config. get invalid. Then we use the default config.
  579. config.m_fovRadians = Constants::HalfPi;
  580. config.m_nearClipDistance = 0.1f;
  581. config.m_farClipDistance = 100.f;
  582. config.m_frustumWidth = 100.f;
  583. config.m_frustumHeight = 100.f;
  584. }
  585. return config;
  586. }
  587. void DirectionalLightComponentController::UpdateCameraTransform()
  588. {
  589. auto cameraTransform = Transform::CreateIdentity();
  590. if (m_configuration.m_cameraEntityId.IsValid())
  591. {
  592. TransformBus::EventResult(
  593. cameraTransform,
  594. m_configuration.m_cameraEntityId,
  595. &TransformBus::Events::GetWorldTM);
  596. }
  597. else
  598. {
  599. if (const auto& viewportContext = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get()->GetDefaultViewportContext())
  600. {
  601. cameraTransform = viewportContext->GetCameraTransform();
  602. }
  603. }
  604. if (cameraTransform == m_lastCameraTransform)
  605. {
  606. return;
  607. }
  608. m_lastCameraTransform = cameraTransform;
  609. if (m_featureProcessor)
  610. {
  611. m_featureProcessor->SetCameraTransform(m_lightHandle, cameraTransform);
  612. }
  613. }
  614. void DirectionalLightComponentController::UpdateLightTransform()
  615. {
  616. auto lightTransform = Transform::CreateIdentity();
  617. TransformBus::EventResult(
  618. lightTransform,
  619. m_entityId,
  620. &TransformBus::Events::GetWorldTM);
  621. if (m_featureProcessor)
  622. {
  623. m_featureProcessor->SetDirection(m_lightHandle, lightTransform.GetBasisY());
  624. }
  625. }
  626. void DirectionalLightComponentController::ColorIntensityChanged()
  627. {
  628. m_photometricValue.SetChroma(m_configuration.m_color);
  629. m_photometricValue.SetIntensity(m_configuration.m_intensity);
  630. if (m_featureProcessor)
  631. {
  632. m_featureProcessor->SetRgbIntensity(m_lightHandle, m_photometricValue.GetCombinedRgb<PhotometricUnit::Lux>());
  633. }
  634. }
  635. bool DirectionalLightComponentController::GetShadowEnabled() const
  636. {
  637. return m_configuration.m_shadowEnabled;
  638. }
  639. void DirectionalLightComponentController::SetShadowEnabled(bool enable)
  640. {
  641. m_configuration.m_shadowEnabled = enable;
  642. m_featureProcessor->SetShadowEnabled(m_lightHandle, enable);
  643. }
  644. void DirectionalLightComponentController::LightingChannelMaskChanged()
  645. {
  646. if (m_featureProcessor)
  647. {
  648. m_featureProcessor->SetLightingChannelMask(m_lightHandle, m_configuration.m_lightingChannelConfig.GetLightingChannelMask());
  649. }
  650. }
  651. bool DirectionalLightComponentController::GetShadowReceiverPlaneBiasEnabled() const
  652. {
  653. return m_configuration.m_receiverPlaneBiasEnabled;
  654. }
  655. void DirectionalLightComponentController::SetShadowReceiverPlaneBiasEnabled(bool enable)
  656. {
  657. m_configuration.m_receiverPlaneBiasEnabled = enable;
  658. m_featureProcessor->SetShadowReceiverPlaneBiasEnabled(m_lightHandle, enable);
  659. }
  660. bool DirectionalLightComponentController::GetCascadeBlendingEnabled() const
  661. {
  662. return m_configuration.m_cascadeBlendingEnabled;
  663. }
  664. void DirectionalLightComponentController::SetCascadeBlendingEnabled(bool enable)
  665. {
  666. m_configuration.m_cascadeBlendingEnabled = enable;
  667. m_featureProcessor->SetCascadeBlendingEnabled(m_lightHandle, enable);
  668. }
  669. } // namespace Render
  670. } // namespace AZ