PosterizeGradientComponent.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/PosterizeGradientComponent.h>
  9. #include <AzCore/Math/MathUtils.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. namespace GradientSignal
  14. {
  15. void PosterizeGradientConfig::Reflect(AZ::ReflectContext* context)
  16. {
  17. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  18. if (serialize)
  19. {
  20. serialize->Class<PosterizeGradientConfig, AZ::ComponentConfig>()
  21. ->Version(1)
  22. ->Field("Mode", &PosterizeGradientConfig::m_mode)
  23. ->Field("Bands", &PosterizeGradientConfig::m_bands)
  24. ->Field("Gradient", &PosterizeGradientConfig::m_gradientSampler)
  25. ;
  26. AZ::EditContext* edit = serialize->GetEditContext();
  27. if (edit)
  28. {
  29. edit->Class<PosterizeGradientConfig>(
  30. "Posterize Gradient", "")
  31. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  32. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  33. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  34. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &PosterizeGradientConfig::m_mode, "Mode", "")
  35. ->EnumAttribute(ModeType::Ceiling, "Ceiling")
  36. ->EnumAttribute(ModeType::Floor, "Floor")
  37. ->EnumAttribute(ModeType::Round, "Round")
  38. ->EnumAttribute(ModeType::Ps, "PS")
  39. ->DataElement(AZ::Edit::UIHandlers::Slider, &PosterizeGradientConfig::m_bands, "Bands", "")
  40. ->Attribute(AZ::Edit::Attributes::Min, 2)
  41. ->Attribute(AZ::Edit::Attributes::Max, 255)
  42. ->DataElement(0, &PosterizeGradientConfig::m_gradientSampler, "Gradient", "Input gradient whose values will be transformed in relation to threshold.")
  43. ;
  44. }
  45. }
  46. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  47. {
  48. behaviorContext->Class<PosterizeGradientConfig>()
  49. ->Constructor()
  50. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  51. ->Property("bands", BehaviorValueProperty(&PosterizeGradientConfig::m_bands))
  52. ->Property("mode",
  53. [](PosterizeGradientConfig* config) { return (AZ::u8&)(config->m_mode); },
  54. [](PosterizeGradientConfig* config, const AZ::u8& i) { config->m_mode = (PosterizeGradientConfig::ModeType)i; })
  55. ->Property("gradientSampler", BehaviorValueProperty(&PosterizeGradientConfig::m_gradientSampler))
  56. ;
  57. }
  58. }
  59. void PosterizeGradientComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  60. {
  61. services.push_back(AZ_CRC_CE("GradientService"));
  62. }
  63. void PosterizeGradientComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  64. {
  65. services.push_back(AZ_CRC_CE("GradientService"));
  66. }
  67. void PosterizeGradientComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& services)
  68. {
  69. }
  70. void PosterizeGradientComponent::Reflect(AZ::ReflectContext* context)
  71. {
  72. PosterizeGradientConfig::Reflect(context);
  73. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  74. if (serialize)
  75. {
  76. serialize->Class<PosterizeGradientComponent, AZ::Component>()
  77. ->Version(0)
  78. ->Field("Configuration", &PosterizeGradientComponent::m_configuration)
  79. ;
  80. }
  81. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  82. {
  83. behaviorContext->Constant("PosterizeGradientComponentTypeId", BehaviorConstant(PosterizeGradientComponentTypeId));
  84. behaviorContext->Class<PosterizeGradientComponent>()->RequestBus("PosterizeGradientRequestBus");
  85. behaviorContext->EBus<PosterizeGradientRequestBus>("PosterizeGradientRequestBus")
  86. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  87. ->Event("GetBands", &PosterizeGradientRequestBus::Events::GetBands)
  88. ->Event("SetBands", &PosterizeGradientRequestBus::Events::SetBands)
  89. ->VirtualProperty("Bands", "GetBands", "SetBands")
  90. ->Event("GetModeType", &PosterizeGradientRequestBus::Events::GetModeType)
  91. ->Event("SetModeType", &PosterizeGradientRequestBus::Events::SetModeType)
  92. ->VirtualProperty("Bands", "GetBands", "SetBands")
  93. ->Event("GetGradientSampler", &PosterizeGradientRequestBus::Events::GetGradientSampler)
  94. ;
  95. }
  96. }
  97. PosterizeGradientComponent::PosterizeGradientComponent(const PosterizeGradientConfig& configuration)
  98. : m_configuration(configuration)
  99. {
  100. }
  101. void PosterizeGradientComponent::Activate()
  102. {
  103. m_dependencyMonitor.Reset();
  104. m_dependencyMonitor.ConnectOwner(GetEntityId());
  105. m_dependencyMonitor.ConnectDependency(m_configuration.m_gradientSampler.m_gradientId);
  106. PosterizeGradientRequestBus::Handler::BusConnect(GetEntityId());
  107. // Connect to GradientRequestBus last so that everything is initialized before listening for gradient queries.
  108. GradientRequestBus::Handler::BusConnect(GetEntityId());
  109. }
  110. void PosterizeGradientComponent::Deactivate()
  111. {
  112. // Disconnect from GradientRequestBus first to ensure no queries are in process when deactivating.
  113. GradientRequestBus::Handler::BusDisconnect();
  114. m_dependencyMonitor.Reset();
  115. PosterizeGradientRequestBus::Handler::BusDisconnect();
  116. }
  117. bool PosterizeGradientComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  118. {
  119. if (auto config = azrtti_cast<const PosterizeGradientConfig*>(baseConfig))
  120. {
  121. m_configuration = *config;
  122. return true;
  123. }
  124. return false;
  125. }
  126. bool PosterizeGradientComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  127. {
  128. if (auto config = azrtti_cast<PosterizeGradientConfig*>(outBaseConfig))
  129. {
  130. *config = m_configuration;
  131. return true;
  132. }
  133. return false;
  134. }
  135. float PosterizeGradientComponent::GetValue(const GradientSampleParams& sampleParams) const
  136. {
  137. AZStd::shared_lock lock(m_queryMutex);
  138. const float bands = AZ::GetMax(static_cast<float>(m_configuration.m_bands), 2.0f);
  139. const float input = m_configuration.m_gradientSampler.GetValue(sampleParams);
  140. return PosterizeValue(input, bands, m_configuration.m_mode);
  141. }
  142. void PosterizeGradientComponent::GetValues(AZStd::span<const AZ::Vector3> positions, AZStd::span<float> outValues) const
  143. {
  144. if (positions.size() != outValues.size())
  145. {
  146. AZ_Assert(false, "input and output lists are different sizes (%zu vs %zu).", positions.size(), outValues.size());
  147. return;
  148. }
  149. AZStd::shared_lock lock(m_queryMutex);
  150. const float bands = AZ::GetMax(static_cast<float>(m_configuration.m_bands), 2.0f);
  151. // Fill in the outValues with all of the generated inupt gradient values.
  152. m_configuration.m_gradientSampler.GetValues(positions, outValues);
  153. // Run through all the input values and posterize them.
  154. for (auto& outValue : outValues)
  155. {
  156. outValue = PosterizeValue(outValue, bands, m_configuration.m_mode);
  157. }
  158. }
  159. bool PosterizeGradientComponent::IsEntityInHierarchy(const AZ::EntityId& entityId) const
  160. {
  161. return m_configuration.m_gradientSampler.IsEntityInHierarchy(entityId);
  162. }
  163. AZ::s32 PosterizeGradientComponent::GetBands() const
  164. {
  165. return m_configuration.m_bands;
  166. }
  167. void PosterizeGradientComponent::SetBands(AZ::s32 bands)
  168. {
  169. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  170. // execute an arbitrary amount of logic, including calls back to this component.
  171. {
  172. AZStd::unique_lock lock(m_queryMutex);
  173. m_configuration.m_bands = bands;
  174. }
  175. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  176. }
  177. AZ::u8 PosterizeGradientComponent::GetModeType() const
  178. {
  179. return static_cast<AZ::u8>(m_configuration.m_mode);
  180. }
  181. void PosterizeGradientComponent::SetModeType(AZ::u8 modeType)
  182. {
  183. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  184. // execute an arbitrary amount of logic, including calls back to this component.
  185. {
  186. AZStd::unique_lock lock(m_queryMutex);
  187. m_configuration.m_mode = static_cast<PosterizeGradientConfig::ModeType>(modeType);
  188. }
  189. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  190. }
  191. GradientSampler& PosterizeGradientComponent::GetGradientSampler()
  192. {
  193. return m_configuration.m_gradientSampler;
  194. }
  195. }