ThresholdGradientComponent.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/ThresholdGradientComponent.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 ThresholdGradientConfig::Reflect(AZ::ReflectContext* context)
  16. {
  17. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  18. if (serialize)
  19. {
  20. serialize->Class<ThresholdGradientConfig, AZ::ComponentConfig>()
  21. ->Version(0)
  22. ->Field("Threshold", &ThresholdGradientConfig::m_threshold)
  23. ->Field("Gradient", &ThresholdGradientConfig::m_gradientSampler)
  24. ;
  25. AZ::EditContext* edit = serialize->GetEditContext();
  26. if (edit)
  27. {
  28. edit->Class<ThresholdGradientConfig>(
  29. "Threshold Gradient", "")
  30. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  31. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  32. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  33. ->DataElement(AZ::Edit::UIHandlers::Slider, &ThresholdGradientConfig::m_threshold, "Threshold", "Specifies the value used to convert lower or higher gradient samples to 0 or 1 respectively.")
  34. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  35. ->Attribute(AZ::Edit::Attributes::Max, 1.0f)
  36. ->DataElement(0, &ThresholdGradientConfig::m_gradientSampler, "Gradient", "Input gradient whose values will be transformed in relation to threshold.")
  37. ;
  38. }
  39. }
  40. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  41. {
  42. behaviorContext->Class<ThresholdGradientConfig>()
  43. ->Constructor()
  44. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  45. ->Property("threshold", BehaviorValueProperty(&ThresholdGradientConfig::m_threshold))
  46. ->Property("gradientSampler", BehaviorValueProperty(&ThresholdGradientConfig::m_gradientSampler))
  47. ;
  48. }
  49. }
  50. void ThresholdGradientComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  51. {
  52. services.push_back(AZ_CRC_CE("GradientService"));
  53. }
  54. void ThresholdGradientComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  55. {
  56. services.push_back(AZ_CRC_CE("GradientService"));
  57. }
  58. void ThresholdGradientComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& services)
  59. {
  60. }
  61. void ThresholdGradientComponent::Reflect(AZ::ReflectContext* context)
  62. {
  63. ThresholdGradientConfig::Reflect(context);
  64. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  65. if (serialize)
  66. {
  67. serialize->Class<ThresholdGradientComponent, AZ::Component>()
  68. ->Version(0)
  69. ->Field("Configuration", &ThresholdGradientComponent::m_configuration)
  70. ;
  71. }
  72. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  73. {
  74. behaviorContext->Constant("ThresholdGradientComponentTypeId", BehaviorConstant(ThresholdGradientComponentTypeId));
  75. behaviorContext->Class<ThresholdGradientComponent>()->RequestBus("ThresholdGradientRequestBus");
  76. behaviorContext->EBus<ThresholdGradientRequestBus>("ThresholdGradientRequestBus")
  77. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  78. ->Event("GetThreshold", &ThresholdGradientRequestBus::Events::GetThreshold)
  79. ->Event("SetThreshold", &ThresholdGradientRequestBus::Events::SetThreshold)
  80. ->VirtualProperty("Threshold", "GetThreshold", "SetThreshold")
  81. ->Event("GetGradientSampler", &ThresholdGradientRequestBus::Events::GetGradientSampler)
  82. ;
  83. }
  84. }
  85. ThresholdGradientComponent::ThresholdGradientComponent(const ThresholdGradientConfig& configuration)
  86. : m_configuration(configuration)
  87. {
  88. }
  89. void ThresholdGradientComponent::Activate()
  90. {
  91. m_dependencyMonitor.Reset();
  92. m_dependencyMonitor.ConnectOwner(GetEntityId());
  93. m_dependencyMonitor.ConnectDependency(m_configuration.m_gradientSampler.m_gradientId);
  94. ThresholdGradientRequestBus::Handler::BusConnect(GetEntityId());
  95. // Connect to GradientRequestBus last so that everything is initialized before listening for gradient queries.
  96. GradientRequestBus::Handler::BusConnect(GetEntityId());
  97. }
  98. void ThresholdGradientComponent::Deactivate()
  99. {
  100. // Disconnect from GradientRequestBus first to ensure no queries are in process when deactivating.
  101. GradientRequestBus::Handler::BusDisconnect();
  102. m_dependencyMonitor.Reset();
  103. ThresholdGradientRequestBus::Handler::BusDisconnect();
  104. }
  105. bool ThresholdGradientComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  106. {
  107. if (auto config = azrtti_cast<const ThresholdGradientConfig*>(baseConfig))
  108. {
  109. m_configuration = *config;
  110. return true;
  111. }
  112. return false;
  113. }
  114. bool ThresholdGradientComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  115. {
  116. if (auto config = azrtti_cast<ThresholdGradientConfig*>(outBaseConfig))
  117. {
  118. *config = m_configuration;
  119. return true;
  120. }
  121. return false;
  122. }
  123. float ThresholdGradientComponent::GetValue(const GradientSampleParams& sampleParams) const
  124. {
  125. AZStd::shared_lock lock(m_queryMutex);
  126. return (m_configuration.m_gradientSampler.GetValue(sampleParams) <= m_configuration.m_threshold) ? 0.0f : 1.0f;
  127. }
  128. void ThresholdGradientComponent::GetValues(AZStd::span<const AZ::Vector3> positions, AZStd::span<float> outValues) const
  129. {
  130. if (positions.size() != outValues.size())
  131. {
  132. AZ_Assert(false, "input and output lists are different sizes (%zu vs %zu).", positions.size(), outValues.size());
  133. return;
  134. }
  135. AZStd::shared_lock lock(m_queryMutex);
  136. m_configuration.m_gradientSampler.GetValues(positions, outValues);
  137. for (auto& outValue : outValues)
  138. {
  139. outValue = (outValue <= m_configuration.m_threshold) ? 0.0f : 1.0f;
  140. }
  141. }
  142. bool ThresholdGradientComponent::IsEntityInHierarchy(const AZ::EntityId& entityId) const
  143. {
  144. return m_configuration.m_gradientSampler.IsEntityInHierarchy(entityId);
  145. }
  146. float ThresholdGradientComponent::GetThreshold() const
  147. {
  148. return m_configuration.m_threshold;
  149. }
  150. void ThresholdGradientComponent::SetThreshold(float threshold)
  151. {
  152. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  153. // execute an arbitrary amount of logic, including calls back to this component.
  154. {
  155. AZStd::unique_lock lock(m_queryMutex);
  156. m_configuration.m_threshold = threshold;
  157. }
  158. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  159. }
  160. GradientSampler& ThresholdGradientComponent::GetGradientSampler()
  161. {
  162. return m_configuration.m_gradientSampler;
  163. }
  164. }