DecalComponentController.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <Decals/DecalComponentController.h>
  9. #include <AzCore/Asset/AssetSerializer.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <Atom/RPI.Public/Scene.h>
  14. namespace AZ
  15. {
  16. namespace Render
  17. {
  18. void DecalComponentConfig::Reflect(ReflectContext* context)
  19. {
  20. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  21. {
  22. serializeContext->Class<DecalComponentConfig, ComponentConfig>()
  23. ->Version(2)
  24. ->Field("Attenuation Angle", &DecalComponentConfig::m_attenuationAngle)
  25. ->Field("Opacity", &DecalComponentConfig::m_opacity)
  26. ->Field("Normal Map Opacity", &DecalComponentConfig::m_normalMapOpacity)
  27. ->Field("SortKey", &DecalComponentConfig::m_sortKey)
  28. ->Field("Material", &DecalComponentConfig::m_materialAsset)
  29. ;
  30. }
  31. }
  32. void DecalComponentController::Reflect(ReflectContext* context)
  33. {
  34. DecalComponentConfig::Reflect(context);
  35. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  36. {
  37. serializeContext->Class<DecalComponentController>()
  38. ->Version(0)
  39. ->Field("Configuration", &DecalComponentController::m_configuration);
  40. }
  41. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  42. {
  43. behaviorContext->EBus<DecalRequestBus>("DecalRequestBus")
  44. ->Event("GetAttenuationAngle", &DecalRequestBus::Events::GetAttenuationAngle)
  45. ->Event("SetAttenuationAngle", &DecalRequestBus::Events::SetAttenuationAngle)
  46. ->Event("GetOpacity", &DecalRequestBus::Events::GetOpacity)
  47. ->Event("SetOpacity", &DecalRequestBus::Events::SetOpacity)
  48. ->Event("GetNormalMapOpacity", &DecalRequestBus::Events::GetNormalMapOpacity)
  49. ->Event("SetNormalMapOpacity", &DecalRequestBus::Events::SetNormalMapOpacity)
  50. ->Event("SetSortKey", &DecalRequestBus::Events::SetSortKey)
  51. ->Event("GetSortKey", &DecalRequestBus::Events::GetSortKey)
  52. ->VirtualProperty("AttenuationAngle", "GetAttenuationAngle", "SetAttenuationAngle")
  53. ->VirtualProperty("Opacity", "GetOpacity", "SetOpacity")
  54. ->VirtualProperty("NormalMapOpacity", "GetNormalMapOpacity", "SetNormalMapOpacity")
  55. ->VirtualProperty("SortKey", "GetSortKey", "SetSortKey")
  56. ->Event("SetMaterial", &DecalRequestBus::Events::SetMaterialAssetId)
  57. ->Event("GetMaterial", &DecalRequestBus::Events::GetMaterialAssetId)
  58. ->VirtualProperty("Material", "GetMaterial", "SetMaterial")
  59. ;
  60. }
  61. }
  62. void DecalComponentController::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  63. {
  64. provided.push_back(AZ_CRC_CE("DecalService"));
  65. }
  66. void DecalComponentController::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  67. {
  68. incompatible.push_back(AZ_CRC_CE("DecalService"));
  69. }
  70. void DecalComponentController::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  71. {
  72. dependent.push_back(AZ_CRC_CE("TransformService"));
  73. dependent.push_back(AZ_CRC_CE("NonUniformScaleService"));
  74. }
  75. DecalComponentController::DecalComponentController(const DecalComponentConfig& config)
  76. : m_configuration(config)
  77. {
  78. }
  79. void DecalComponentController::Activate(EntityId entityId)
  80. {
  81. m_entityId = entityId;
  82. m_featureProcessor = RPI::Scene::GetFeatureProcessorForEntity<DecalFeatureProcessorInterface>(entityId);
  83. AZ_Assert(m_featureProcessor, "DecalRenderProxy was unable to find a decal FeatureProcessor on the entityId provided.");
  84. if (m_featureProcessor)
  85. {
  86. m_handle = m_featureProcessor->AcquireDecal();
  87. }
  88. m_cachedNonUniformScale = AZ::Vector3::CreateOne();
  89. AZ::NonUniformScaleRequestBus::EventResult(m_cachedNonUniformScale, m_entityId, &AZ::NonUniformScaleRequests::GetScale);
  90. AZ::NonUniformScaleRequestBus::Event(m_entityId, &AZ::NonUniformScaleRequests::RegisterScaleChangedEvent,
  91. m_nonUniformScaleChangedHandler);
  92. AZ::Transform local, world;
  93. AZ::TransformBus::Event(entityId, &AZ::TransformBus::Events::GetLocalAndWorld, local, world);
  94. OnTransformChanged(local, world);
  95. TransformNotificationBus::Handler::BusConnect(m_entityId);
  96. DecalRequestBus::Handler::BusConnect(m_entityId);
  97. ConfigurationChanged();
  98. }
  99. void DecalComponentController::Deactivate()
  100. {
  101. DecalRequestBus::Handler::BusDisconnect(m_entityId);
  102. TransformNotificationBus::Handler::BusDisconnect(m_entityId);
  103. m_nonUniformScaleChangedHandler.Disconnect();
  104. if (m_featureProcessor)
  105. {
  106. m_featureProcessor->ReleaseDecal(m_handle);
  107. m_handle.Reset();
  108. }
  109. }
  110. void DecalComponentController::SetConfiguration(const DecalComponentConfig& config)
  111. {
  112. m_configuration = config;
  113. ConfigurationChanged();
  114. }
  115. const DecalComponentConfig& DecalComponentController::GetConfiguration() const
  116. {
  117. return m_configuration;
  118. }
  119. void DecalComponentController::OnTransformChanged(const AZ::Transform& /*local*/, const AZ::Transform& world)
  120. {
  121. if (m_featureProcessor)
  122. {
  123. m_featureProcessor->SetDecalTransform(m_handle, world, m_cachedNonUniformScale);
  124. }
  125. }
  126. void DecalComponentController::HandleNonUniformScaleChange(const AZ::Vector3& nonUniformScale)
  127. {
  128. m_cachedNonUniformScale = nonUniformScale;
  129. if (m_featureProcessor)
  130. {
  131. AZ::Transform world = AZ::Transform::CreateIdentity();
  132. AZ::TransformBus::EventResult(world, m_entityId, &AZ::TransformBus::Events::GetWorldTM);
  133. m_featureProcessor->SetDecalTransform(m_handle, world, nonUniformScale);
  134. }
  135. }
  136. void DecalComponentController::ConfigurationChanged()
  137. {
  138. AttenuationAngleChanged();
  139. OpacityChanged();
  140. NormalMapOpacityChanged();
  141. SortKeyChanged();
  142. MaterialChanged();
  143. }
  144. float DecalComponentController::GetAttenuationAngle() const
  145. {
  146. return m_configuration.m_attenuationAngle;
  147. }
  148. void DecalComponentController::SetAttenuationAngle(float attenuationAngle)
  149. {
  150. m_configuration.m_attenuationAngle = attenuationAngle;
  151. AttenuationAngleChanged();
  152. }
  153. float DecalComponentController::GetOpacity() const
  154. {
  155. return m_configuration.m_opacity;
  156. }
  157. void DecalComponentController::SetOpacity(float opacity)
  158. {
  159. m_configuration.m_opacity = opacity;
  160. OpacityChanged();
  161. }
  162. float DecalComponentController::GetNormalMapOpacity() const
  163. {
  164. return m_configuration.m_normalMapOpacity;
  165. }
  166. void DecalComponentController::SetNormalMapOpacity(float opacity)
  167. {
  168. m_configuration.m_normalMapOpacity = opacity;
  169. NormalMapOpacityChanged();
  170. }
  171. uint8_t DecalComponentController::GetSortKey() const
  172. {
  173. return m_configuration.m_sortKey;
  174. }
  175. void DecalComponentController::SetSortKey(uint8_t sortKey)
  176. {
  177. m_configuration.m_sortKey = sortKey;
  178. SortKeyChanged();
  179. }
  180. void DecalComponentController::AttenuationAngleChanged()
  181. {
  182. DecalNotificationBus::Event(m_entityId, &DecalNotifications::OnAttenuationAngleChanged, m_configuration.m_attenuationAngle);
  183. if (m_featureProcessor)
  184. {
  185. m_featureProcessor->SetDecalAttenuationAngle(m_handle, m_configuration.m_attenuationAngle);
  186. }
  187. }
  188. void DecalComponentController::OpacityChanged()
  189. {
  190. DecalNotificationBus::Event(m_entityId, &DecalNotifications::OnOpacityChanged, m_configuration.m_opacity);
  191. if (m_featureProcessor)
  192. {
  193. m_featureProcessor->SetDecalOpacity(m_handle, m_configuration.m_opacity);
  194. }
  195. }
  196. void DecalComponentController::NormalMapOpacityChanged()
  197. {
  198. DecalNotificationBus::Event(m_entityId, &DecalNotifications::OnNormalMapOpacityChanged, m_configuration.m_normalMapOpacity);
  199. if (m_featureProcessor)
  200. {
  201. m_featureProcessor->SetDecalNormalMapOpacity(m_handle, m_configuration.m_normalMapOpacity);
  202. }
  203. }
  204. void DecalComponentController::SortKeyChanged()
  205. {
  206. DecalNotificationBus::Event(m_entityId, &DecalNotifications::OnSortKeyChanged, m_configuration.m_sortKey);
  207. if (m_featureProcessor)
  208. {
  209. m_featureProcessor->SetDecalSortKey(m_handle, m_configuration.m_sortKey);
  210. }
  211. }
  212. void DecalComponentController::SetMaterialAssetId(Data::AssetId id)
  213. {
  214. Data::Asset<RPI::MaterialAsset> materialAsset;
  215. materialAsset.Create(id);
  216. m_configuration.m_materialAsset = materialAsset;
  217. MaterialChanged();
  218. }
  219. void DecalComponentController::MaterialChanged()
  220. {
  221. DecalNotificationBus::Event(m_entityId, &DecalNotifications::OnMaterialChanged, m_configuration.m_materialAsset);
  222. if (m_featureProcessor)
  223. {
  224. m_featureProcessor->SetDecalMaterial(m_handle, m_configuration.m_materialAsset.GetId());
  225. }
  226. }
  227. Data::AssetId DecalComponentController::GetMaterialAssetId() const
  228. {
  229. return m_configuration.m_materialAsset.GetId();
  230. }
  231. } // namespace Render
  232. } // namespace AZ