SmoothStepGradientComponent.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 <GradientSignal/Components/SmoothStepGradientComponent.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <GradientSignal/Ebuses/GradientRequestBus.h>
  14. #include <GradientSignal/Util.h>
  15. namespace GradientSignal
  16. {
  17. static bool SmoothStepGradientConfigUpdateVersion(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  18. {
  19. // From v0 to v1, The smooth step parameters were moved into a SmoothStep subclass. This reads the old parameters
  20. // into the subclass, removes the old parameters, then write out the subclass.
  21. if (classElement.GetVersion() == 0)
  22. {
  23. SmoothStep convertedSmoothStep;
  24. if (classElement.GetChildData(AZ_CRC_CE("FalloffRange"), convertedSmoothStep.m_falloffRange))
  25. {
  26. classElement.RemoveElementByName(AZ_CRC_CE("FalloffRange"));
  27. }
  28. if (classElement.GetChildData(AZ_CRC_CE("FalloffStrength"), convertedSmoothStep.m_falloffStrength))
  29. {
  30. classElement.RemoveElementByName(AZ_CRC_CE("FalloffStrength"));
  31. }
  32. if (classElement.GetChildData(AZ_CRC_CE("FalloffMidpoint"), convertedSmoothStep.m_falloffMidpoint))
  33. {
  34. classElement.RemoveElementByName(AZ_CRC_CE("FalloffMidpoint"));
  35. }
  36. classElement.AddElementWithData(context, "SmoothStep", convertedSmoothStep);
  37. }
  38. return true;
  39. }
  40. void SmoothStepGradientConfig::Reflect(AZ::ReflectContext* context)
  41. {
  42. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  43. if (serialize)
  44. {
  45. serialize->Class<SmoothStepGradientConfig, AZ::ComponentConfig>()
  46. ->Version(1, &SmoothStepGradientConfigUpdateVersion)
  47. ->Field("SmoothStep", &SmoothStepGradientConfig::m_smoothStep)
  48. ->Field("Gradient", &SmoothStepGradientConfig::m_gradientSampler)
  49. ;
  50. AZ::EditContext* edit = serialize->GetEditContext();
  51. if (edit)
  52. {
  53. edit->Class<SmoothStepGradientConfig>(
  54. "Smooth Step Gradient", "Smooth Step Gradient")
  55. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  56. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  57. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  58. ->DataElement(0, &SmoothStepGradientConfig::m_smoothStep, "Smooth Step", "Parameters for controlling the smooth-step curve.")
  59. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  60. ->DataElement(0, &SmoothStepGradientConfig::m_gradientSampler, "Gradient", "Input gradient whose values will be transformed.")
  61. ;
  62. }
  63. }
  64. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  65. {
  66. behaviorContext->Class<SmoothStepGradientConfig>()
  67. ->Constructor()
  68. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  69. ->Property("smoothStep", BehaviorValueProperty(&SmoothStepGradientConfig::m_smoothStep))
  70. ->Property("gradientSampler", BehaviorValueProperty(&SmoothStepGradientConfig::m_gradientSampler))
  71. ;
  72. }
  73. }
  74. void SmoothStepGradientComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  75. {
  76. services.push_back(AZ_CRC_CE("GradientService"));
  77. }
  78. void SmoothStepGradientComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  79. {
  80. services.push_back(AZ_CRC_CE("GradientService"));
  81. }
  82. void SmoothStepGradientComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& services)
  83. {
  84. }
  85. void SmoothStepGradientComponent::Reflect(AZ::ReflectContext* context)
  86. {
  87. SmoothStepGradientConfig::Reflect(context);
  88. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  89. if (serialize)
  90. {
  91. serialize->Class<SmoothStepGradientComponent, AZ::Component>()
  92. ->Version(0)
  93. ->Field("Configuration", &SmoothStepGradientComponent::m_configuration)
  94. ;
  95. }
  96. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  97. {
  98. behaviorContext->Constant("SmoothStepGradientComponentTypeId", BehaviorConstant(SmoothStepGradientComponentTypeId));
  99. behaviorContext->Class<SmoothStepGradientComponent>()
  100. ->RequestBus("SmoothStepGradientRequestBus")
  101. ->RequestBus("SmoothStepRequestBus")
  102. ;
  103. behaviorContext->EBus<SmoothStepGradientRequestBus>("SmoothStepGradientRequestBus")
  104. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  105. ->Event("GetGradientSampler", &SmoothStepGradientRequestBus::Events::GetGradientSampler)
  106. ;
  107. }
  108. }
  109. SmoothStepGradientComponent::SmoothStepGradientComponent(const SmoothStepGradientConfig& configuration)
  110. : m_configuration(configuration)
  111. {
  112. }
  113. void SmoothStepGradientComponent::Activate()
  114. {
  115. m_dependencyMonitor.Reset();
  116. m_dependencyMonitor.ConnectOwner(GetEntityId());
  117. m_dependencyMonitor.ConnectDependency(m_configuration.m_gradientSampler.m_gradientId);
  118. SmoothStepGradientRequestBus::Handler::BusConnect(GetEntityId());
  119. SmoothStepRequestBus::Handler::BusConnect(GetEntityId());
  120. // Connect to GradientRequestBus last so that everything is initialized before listening for gradient queries.
  121. GradientRequestBus::Handler::BusConnect(GetEntityId());
  122. }
  123. void SmoothStepGradientComponent::Deactivate()
  124. {
  125. // Disconnect from GradientRequestBus first to ensure no queries are in process when deactivating.
  126. GradientRequestBus::Handler::BusDisconnect();
  127. m_dependencyMonitor.Reset();
  128. SmoothStepGradientRequestBus::Handler::BusDisconnect();
  129. SmoothStepRequestBus::Handler::BusDisconnect();
  130. }
  131. bool SmoothStepGradientComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  132. {
  133. if (auto config = azrtti_cast<const SmoothStepGradientConfig*>(baseConfig))
  134. {
  135. m_configuration = *config;
  136. return true;
  137. }
  138. return false;
  139. }
  140. bool SmoothStepGradientComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  141. {
  142. if (auto config = azrtti_cast<SmoothStepGradientConfig*>(outBaseConfig))
  143. {
  144. *config = m_configuration;
  145. return true;
  146. }
  147. return false;
  148. }
  149. float SmoothStepGradientComponent::GetValue(const GradientSampleParams& sampleParams) const
  150. {
  151. AZStd::shared_lock lock(m_queryMutex);
  152. const float value = m_configuration.m_gradientSampler.GetValue(sampleParams);
  153. return m_configuration.m_smoothStep.GetSmoothedValue(value);
  154. }
  155. void SmoothStepGradientComponent::GetValues(AZStd::span<const AZ::Vector3> positions, AZStd::span<float> outValues) const
  156. {
  157. if (positions.size() != outValues.size())
  158. {
  159. AZ_Assert(false, "input and output lists are different sizes (%zu vs %zu).", positions.size(), outValues.size());
  160. return;
  161. }
  162. AZStd::shared_lock lock(m_queryMutex);
  163. m_configuration.m_gradientSampler.GetValues(positions, outValues);
  164. m_configuration.m_smoothStep.GetSmoothedValues(outValues);
  165. }
  166. bool SmoothStepGradientComponent::IsEntityInHierarchy(const AZ::EntityId& entityId) const
  167. {
  168. return m_configuration.m_gradientSampler.IsEntityInHierarchy(entityId);
  169. }
  170. float SmoothStepGradientComponent::GetFallOffRange() const
  171. {
  172. return m_configuration.m_smoothStep.m_falloffRange;
  173. }
  174. void SmoothStepGradientComponent::SetFallOffRange(float range)
  175. {
  176. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  177. // execute an arbitrary amount of logic, including calls back to this component.
  178. {
  179. AZStd::unique_lock lock(m_queryMutex);
  180. m_configuration.m_smoothStep.m_falloffRange = range;
  181. }
  182. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  183. }
  184. float SmoothStepGradientComponent::GetFallOffStrength() const
  185. {
  186. return m_configuration.m_smoothStep.m_falloffStrength;
  187. }
  188. void SmoothStepGradientComponent::SetFallOffStrength(float strength)
  189. {
  190. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  191. // execute an arbitrary amount of logic, including calls back to this component.
  192. {
  193. AZStd::unique_lock lock(m_queryMutex);
  194. m_configuration.m_smoothStep.m_falloffStrength = strength;
  195. }
  196. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  197. }
  198. float SmoothStepGradientComponent::GetFallOffMidpoint() const
  199. {
  200. return m_configuration.m_smoothStep.m_falloffMidpoint;
  201. }
  202. void SmoothStepGradientComponent::SetFallOffMidpoint(float midpoint)
  203. {
  204. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  205. // execute an arbitrary amount of logic, including calls back to this component.
  206. {
  207. AZStd::unique_lock lock(m_queryMutex);
  208. m_configuration.m_smoothStep.m_falloffMidpoint = midpoint;
  209. }
  210. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  211. }
  212. GradientSampler& SmoothStepGradientComponent::GetGradientSampler()
  213. {
  214. return m_configuration.m_gradientSampler;
  215. }
  216. }