PhysicalSkyComponentController.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 <SkyBox/PhysicalSkyComponentController.h>
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <Atom/RPI.Public/Scene.h>
  11. namespace AZ
  12. {
  13. namespace Render
  14. {
  15. void PhysicalSkyComponentController::Reflect(ReflectContext* context)
  16. {
  17. PhysicalSkyComponentConfig::Reflect(context);
  18. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  19. {
  20. serializeContext->Class<PhysicalSkyComponentController>()
  21. ->Version(0)
  22. ->Field("Configuration", &PhysicalSkyComponentController::m_configuration);
  23. }
  24. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  25. {
  26. behaviorContext->EBus<PhysicalSkyRequestBus>("PhysicalSkyRequestBus")
  27. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  28. ->Attribute(AZ::Script::Attributes::Category, "render")
  29. ->Attribute(AZ::Script::Attributes::Module, "render")
  30. ->Event("SetTurbidity", &PhysicalSkyRequestBus::Events::SetTurbidity)
  31. ->Event("GetTurbidity", &PhysicalSkyRequestBus::Events::GetTurbidity)
  32. ->Event("SetSunRadiusFactor", &PhysicalSkyRequestBus::Events::SetSunRadiusFactor)
  33. ->Event("GetSunRadiusFactor", &PhysicalSkyRequestBus::Events::GetSunRadiusFactor)
  34. ->Event("SetSkyIntensity", static_cast<void(PhysicalSkyRequestBus::Events::*)(float)>(&PhysicalSkyRequestBus::Events::SetSkyIntensity))
  35. ->Event("GetSkyIntensity", static_cast<float(PhysicalSkyRequestBus::Events::*)()>(&PhysicalSkyRequestBus::Events::GetSkyIntensity))
  36. ->Event("SetSunIntensity", static_cast<void(PhysicalSkyRequestBus::Events::*)(float)>(&PhysicalSkyRequestBus::Events::SetSunIntensity))
  37. ->Event("GetSunIntensity", static_cast<float(PhysicalSkyRequestBus::Events::*)()>(&PhysicalSkyRequestBus::Events::GetSunIntensity))
  38. ->VirtualProperty("Turbidity", "GetTurbidity", "SetTurbidity")
  39. ->VirtualProperty("SunRadiusFactor", "GetSunRadiusFactor", "SetSunRadiusFactor")
  40. ->VirtualProperty("SkyIntensity", "GetSkyIntensity", "SetSkyIntensity")
  41. ->VirtualProperty("SunIntensity", "GetSunIntensity", "SetSunIntensity")
  42. ;
  43. }
  44. }
  45. void PhysicalSkyComponentController::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  46. {
  47. provided.push_back(AZ_CRC_CE("SkyBoxService"));
  48. }
  49. void PhysicalSkyComponentController::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  50. {
  51. incompatible.push_back(AZ_CRC_CE("SkyBoxService"));
  52. incompatible.push_back(AZ_CRC_CE("NonUniformScaleService"));
  53. }
  54. void PhysicalSkyComponentController::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  55. {
  56. required.push_back(AZ_CRC_CE("TransformService"));
  57. }
  58. PhysicalSkyComponentController::PhysicalSkyComponentController(const PhysicalSkyComponentConfig& config)
  59. : m_configuration(config)
  60. {
  61. }
  62. void PhysicalSkyComponentController::Activate(EntityId entityId)
  63. {
  64. m_featureProcessorInterface = RPI::Scene::GetFeatureProcessorForEntity<SkyBoxFeatureProcessorInterface>(entityId);
  65. // only activate if there is no other skybox activate
  66. if (!m_featureProcessorInterface->IsEnabled())
  67. {
  68. m_featureProcessorInterface->SetSkyboxMode(SkyBoxMode::PhysicalSky);
  69. m_featureProcessorInterface->Enable(true);
  70. m_entityId = entityId;
  71. m_skyPhotometricValue = PhotometricValue(m_configuration.m_skyIntensity, Color::CreateOne(), m_configuration.m_intensityMode);
  72. m_sunPhotometricValue = PhotometricValue(m_configuration.m_sunIntensity, Color::CreateOne(), m_configuration.m_intensityMode);
  73. SetTurbidity(m_configuration.m_turbidity);
  74. SetSunRadiusFactor(m_configuration.m_sunRadiusFactor);
  75. SetSkyIntensity(m_configuration.m_skyIntensity, m_configuration.m_intensityMode);
  76. SetSunIntensity(m_configuration.m_sunIntensity, m_configuration.m_intensityMode);
  77. m_transformInterface = TransformBus::FindFirstHandler(m_entityId);
  78. AZ_Assert(m_transformInterface, "Unable to attach to a TransformBus handler. Entity transform will not affect to skybox.");
  79. const AZ::Transform& transform = m_transformInterface ? m_transformInterface->GetWorldTM() : Transform::Identity();
  80. m_featureProcessorInterface->SetSunPosition(GetSunTransform(transform));
  81. m_featureProcessorInterface->SetFogSettings(m_configuration.m_skyBoxFogSettings);
  82. PhysicalSkyRequestBus::Handler::BusConnect(m_entityId);
  83. SkyBoxFogRequestBus::Handler::BusConnect(m_entityId);
  84. TransformNotificationBus::Handler::BusConnect(m_entityId);
  85. m_isActive = true;
  86. }
  87. else
  88. {
  89. m_featureProcessorInterface = nullptr;
  90. AZ_Warning("PhysicalSkyComponentController", false, "There is already another HDRi Skybox or Physical Sky component in the scene!");
  91. }
  92. }
  93. void PhysicalSkyComponentController::Deactivate()
  94. {
  95. // Run deactivate if this skybox is activate
  96. if (m_isActive)
  97. {
  98. PhysicalSkyRequestBus::Handler::BusDisconnect();
  99. SkyBoxFogRequestBus::Handler::BusDisconnect();
  100. TransformNotificationBus::Handler::BusDisconnect();
  101. m_featureProcessorInterface->Enable(false);
  102. m_featureProcessorInterface = nullptr;
  103. m_transformInterface = nullptr;
  104. m_isActive = false;
  105. }
  106. }
  107. void PhysicalSkyComponentController::SetConfiguration(const PhysicalSkyComponentConfig& config)
  108. {
  109. m_configuration = config;
  110. }
  111. const PhysicalSkyComponentConfig& PhysicalSkyComponentController::GetConfiguration() const
  112. {
  113. return m_configuration;
  114. }
  115. void PhysicalSkyComponentController::OnTransformChanged([[maybe_unused]] const AZ::Transform& local, const AZ::Transform& world)
  116. {
  117. m_featureProcessorInterface->SetSunPosition(GetSunTransform(world));
  118. }
  119. void PhysicalSkyComponentController::SetTurbidity(int turbidity)
  120. {
  121. m_configuration.m_turbidity = turbidity;
  122. m_featureProcessorInterface->SetTurbidity(turbidity);
  123. }
  124. int PhysicalSkyComponentController::GetTurbidity()
  125. {
  126. return m_configuration.m_turbidity;
  127. }
  128. void PhysicalSkyComponentController::SetSunRadiusFactor(float factor)
  129. {
  130. m_configuration.m_sunRadiusFactor = factor;
  131. m_featureProcessorInterface->SetSunRadiusFactor(factor);
  132. }
  133. float PhysicalSkyComponentController::GetSunRadiusFactor()
  134. {
  135. return m_configuration.m_sunRadiusFactor;
  136. }
  137. void PhysicalSkyComponentController::SetSkyIntensity(float intensity, PhotometricUnit unit)
  138. {
  139. m_skyPhotometricValue.ConvertToPhotometricUnit(unit);
  140. m_skyPhotometricValue.SetIntensity(intensity);
  141. m_configuration.m_skyIntensity = intensity;
  142. m_featureProcessorInterface->SetSkyIntensity(intensity, unit);
  143. }
  144. void PhysicalSkyComponentController::SetSkyIntensity(float intensity)
  145. {
  146. m_skyPhotometricValue.SetIntensity(intensity);
  147. m_configuration.m_skyIntensity = intensity;
  148. m_featureProcessorInterface->SetSkyIntensity(intensity, m_skyPhotometricValue.GetType());
  149. }
  150. float PhysicalSkyComponentController::GetSkyIntensity(PhotometricUnit unit)
  151. {
  152. m_skyPhotometricValue.ConvertToPhotometricUnit(unit);
  153. m_configuration.m_skyIntensity = m_skyPhotometricValue.GetIntensity();
  154. return m_configuration.m_skyIntensity;
  155. }
  156. float PhysicalSkyComponentController::GetSkyIntensity()
  157. {
  158. return m_configuration.m_skyIntensity;
  159. }
  160. void PhysicalSkyComponentController::SetSunIntensity(float intensity, PhotometricUnit unit)
  161. {
  162. m_sunPhotometricValue.ConvertToPhotometricUnit(unit);
  163. m_sunPhotometricValue.SetIntensity(intensity);
  164. m_configuration.m_sunIntensity = intensity;
  165. m_featureProcessorInterface->SetSunIntensity(intensity, unit);
  166. }
  167. void PhysicalSkyComponentController::SetSunIntensity(float intensity)
  168. {
  169. m_sunPhotometricValue.SetIntensity(intensity);
  170. m_configuration.m_sunIntensity = intensity;
  171. m_featureProcessorInterface->SetSunIntensity(intensity, m_sunPhotometricValue.GetType());
  172. }
  173. float PhysicalSkyComponentController::GetSunIntensity(PhotometricUnit unit)
  174. {
  175. m_sunPhotometricValue.ConvertToPhotometricUnit(unit);
  176. m_configuration.m_sunIntensity = m_sunPhotometricValue.GetIntensity();
  177. return m_configuration.m_sunIntensity;
  178. }
  179. float PhysicalSkyComponentController::GetSunIntensity()
  180. {
  181. return m_configuration.m_sunIntensity;
  182. }
  183. SunPosition PhysicalSkyComponentController::GetSunTransform(const AZ::Transform& world)
  184. {
  185. Transform worldNoScale = world;
  186. worldNoScale.ExtractUniformScale();
  187. AZ::Vector3 sunPositionAtom = worldNoScale.TransformVector(AZ::Vector3(0, -1, 0)); // transform Sun from default position
  188. // Convert sun position to Y-up coordinate
  189. AZ::Vector3 sunPosition;
  190. sunPosition.SetX(-sunPositionAtom.GetY());
  191. sunPosition.SetY(sunPositionAtom.GetZ());
  192. sunPosition.SetZ(sunPositionAtom.GetX());
  193. return SunPosition(atan2(sunPosition.GetZ(), sunPosition.GetX()), asin(sunPosition.GetY()));
  194. }
  195. void PhysicalSkyComponentController::SetEnabled(bool enable)
  196. {
  197. m_configuration.m_skyBoxFogSettings.m_enable = enable;
  198. m_featureProcessorInterface->SetFogEnabled(enable);
  199. }
  200. bool PhysicalSkyComponentController::IsEnabled() const
  201. {
  202. return m_configuration.m_skyBoxFogSettings.m_enable;
  203. }
  204. void PhysicalSkyComponentController::SetColor(const AZ::Color& color)
  205. {
  206. m_configuration.m_skyBoxFogSettings.m_color = color;
  207. m_featureProcessorInterface->SetFogColor(color);
  208. }
  209. const AZ::Color& PhysicalSkyComponentController::GetColor() const
  210. {
  211. return m_configuration.m_skyBoxFogSettings.m_color;
  212. }
  213. void PhysicalSkyComponentController::SetTopHeight(float topHeight)
  214. {
  215. m_configuration.m_skyBoxFogSettings.m_topHeight = topHeight;
  216. m_featureProcessorInterface->SetFogTopHeight(topHeight);
  217. }
  218. float PhysicalSkyComponentController::GetTopHeight() const
  219. {
  220. return m_configuration.m_skyBoxFogSettings.m_topHeight;
  221. }
  222. void PhysicalSkyComponentController::SetBottomHeight(float bottomHeight)
  223. {
  224. m_configuration.m_skyBoxFogSettings.m_bottomHeight = bottomHeight;
  225. m_featureProcessorInterface->SetFogBottomHeight(bottomHeight);
  226. }
  227. float PhysicalSkyComponentController::GetBottomHeight() const
  228. {
  229. return m_configuration.m_skyBoxFogSettings.m_bottomHeight;
  230. }
  231. }
  232. }