ScaleModifierComponent.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "ScaleModifierComponent.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 <Vegetation/Descriptor.h>
  14. #include <GradientSignal/Ebuses/GradientRequestBus.h>
  15. #include <Vegetation/InstanceData.h>
  16. #include <VegetationProfiler.h>
  17. namespace Vegetation
  18. {
  19. void ScaleModifierConfig::Reflect(AZ::ReflectContext* context)
  20. {
  21. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  22. if (serialize)
  23. {
  24. serialize->Class<ScaleModifierConfig, AZ::ComponentConfig>()
  25. ->Version(0)
  26. ->Field("AllowOverrides", &ScaleModifierConfig::m_allowOverrides)
  27. ->Field("RangeMin", &ScaleModifierConfig::m_rangeMin)
  28. ->Field("RangeMax", &ScaleModifierConfig::m_rangeMax)
  29. ->Field("Gradient", &ScaleModifierConfig::m_gradientSampler)
  30. ;
  31. AZ::EditContext* edit = serialize->GetEditContext();
  32. if (edit)
  33. {
  34. edit->Class<ScaleModifierConfig>(
  35. "Vegetation Scale Modifier", "")
  36. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  37. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  38. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  39. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &ScaleModifierConfig::m_allowOverrides, "Allow Per-Item Overrides", "Allow per-descriptor parameters to override component parameters.")
  40. ->DataElement(AZ::Edit::UIHandlers::Slider, &ScaleModifierConfig::m_rangeMin, "Range Min", "Minimum scale.")
  41. ->Attribute(AZ::Edit::Attributes::Min, 0.01f)
  42. ->Attribute(AZ::Edit::Attributes::SoftMax, 10.0f)
  43. ->Attribute(AZ::Edit::Attributes::Step, 0.125f)
  44. ->Attribute(AZ::Edit::Attributes::Max, std::numeric_limits<float>::max())
  45. ->DataElement(AZ::Edit::UIHandlers::Slider, &ScaleModifierConfig::m_rangeMax, "Range Max", "Maximum scale.")
  46. ->Attribute(AZ::Edit::Attributes::Min, 0.01f)
  47. ->Attribute(AZ::Edit::Attributes::SoftMax, 10.0f)
  48. ->Attribute(AZ::Edit::Attributes::Step, 0.125f)
  49. ->Attribute(AZ::Edit::Attributes::Max, std::numeric_limits<float>::max())
  50. ->DataElement(0, &ScaleModifierConfig::m_gradientSampler, "Gradient", "Gradient used as blend factor to lerp between ranges.")
  51. ;
  52. }
  53. }
  54. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  55. {
  56. behaviorContext->Class<ScaleModifierConfig>()
  57. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  58. ->Constructor()
  59. ->Property("allowOverrides", BehaviorValueProperty(&ScaleModifierConfig::m_allowOverrides))
  60. ->Property("rangeMin", BehaviorValueProperty(&ScaleModifierConfig::m_rangeMin))
  61. ->Property("rangeMax", BehaviorValueProperty(&ScaleModifierConfig::m_rangeMax))
  62. ->Property("gradientSampler", BehaviorValueProperty(&ScaleModifierConfig::m_gradientSampler))
  63. ;
  64. }
  65. }
  66. void ScaleModifierComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  67. {
  68. services.push_back(AZ_CRC_CE("VegetationModifierService"));
  69. services.push_back(AZ_CRC_CE("VegetationScaleModifierService"));
  70. }
  71. void ScaleModifierComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  72. {
  73. services.push_back(AZ_CRC_CE("VegetationScaleModifierService"));
  74. }
  75. void ScaleModifierComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  76. {
  77. services.push_back(AZ_CRC_CE("VegetationAreaService"));
  78. }
  79. void ScaleModifierComponent::Reflect(AZ::ReflectContext* context)
  80. {
  81. ScaleModifierConfig::Reflect(context);
  82. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  83. if (serialize)
  84. {
  85. serialize->Class<ScaleModifierComponent, AZ::Component>()
  86. ->Version(0)
  87. ->Field("Configuration", &ScaleModifierComponent::m_configuration)
  88. ;
  89. }
  90. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  91. {
  92. behaviorContext->Constant("ScaleModifierComponentTypeId", BehaviorConstant(ScaleModifierComponentTypeId));
  93. behaviorContext->Class<ScaleModifierComponent>()->RequestBus("ScaleModifierRequestBus");
  94. behaviorContext->EBus<ScaleModifierRequestBus>("ScaleModifierRequestBus")
  95. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  96. ->Event("GetAllowOverrides", &ScaleModifierRequestBus::Events::GetAllowOverrides)
  97. ->Event("SetAllowOverrides", &ScaleModifierRequestBus::Events::SetAllowOverrides)
  98. ->VirtualProperty("AllowOverrides", "GetAllowOverrides", "SetAllowOverrides")
  99. ->Event("GetRangeMin", &ScaleModifierRequestBus::Events::GetRangeMin)
  100. ->Event("SetRangeMin", &ScaleModifierRequestBus::Events::SetRangeMin)
  101. ->VirtualProperty("RangeMin", "GetRangeMin", "SetRangeMin")
  102. ->Event("GetRangeMax", &ScaleModifierRequestBus::Events::GetRangeMax)
  103. ->Event("SetRangeMax", &ScaleModifierRequestBus::Events::SetRangeMax)
  104. ->VirtualProperty("RangeMax", "GetRangeMax", "SetRangeMax")
  105. ->Event("GetGradientSampler", &ScaleModifierRequestBus::Events::GetGradientSampler)
  106. ;
  107. }
  108. }
  109. ScaleModifierComponent::ScaleModifierComponent(const ScaleModifierConfig& configuration)
  110. : m_configuration(configuration)
  111. {
  112. }
  113. void ScaleModifierComponent::Activate()
  114. {
  115. m_dependencyMonitor.Reset();
  116. m_dependencyMonitor.ConnectOwner(GetEntityId());
  117. m_dependencyMonitor.ConnectDependencies({ m_configuration.m_gradientSampler.m_gradientId });
  118. ModifierRequestBus::Handler::BusConnect(GetEntityId());
  119. ScaleModifierRequestBus::Handler::BusConnect(GetEntityId());
  120. }
  121. void ScaleModifierComponent::Deactivate()
  122. {
  123. m_dependencyMonitor.Reset();
  124. ModifierRequestBus::Handler::BusDisconnect();
  125. ScaleModifierRequestBus::Handler::BusDisconnect();
  126. }
  127. bool ScaleModifierComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  128. {
  129. if (auto config = azrtti_cast<const ScaleModifierConfig*>(baseConfig))
  130. {
  131. m_configuration = *config;
  132. return true;
  133. }
  134. return false;
  135. }
  136. bool ScaleModifierComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  137. {
  138. if (auto config = azrtti_cast<ScaleModifierConfig*>(outBaseConfig))
  139. {
  140. *config = m_configuration;
  141. return true;
  142. }
  143. return false;
  144. }
  145. void ScaleModifierComponent::Execute(InstanceData& instanceData) const
  146. {
  147. VEGETATION_PROFILE_FUNCTION_VERBOSE
  148. const GradientSignal::GradientSampleParams sampleParams(instanceData.m_position);
  149. float factor = m_configuration.m_gradientSampler.GetValue(sampleParams);
  150. const bool useOverrides = m_configuration.m_allowOverrides && instanceData.m_descriptorPtr && instanceData.m_descriptorPtr->m_scaleOverrideEnabled;
  151. const float min = useOverrides ? instanceData.m_descriptorPtr->m_scaleMin : m_configuration.m_rangeMin;
  152. const float max = useOverrides ? instanceData.m_descriptorPtr->m_scaleMax : m_configuration.m_rangeMax;
  153. instanceData.m_scale = AZ::GetMax(instanceData.m_scale * (factor * (max - min) + min), 0.01f);
  154. }
  155. bool ScaleModifierComponent::GetAllowOverrides() const
  156. {
  157. return m_configuration.m_allowOverrides;
  158. }
  159. void ScaleModifierComponent::SetAllowOverrides(bool value)
  160. {
  161. m_configuration.m_allowOverrides = value;
  162. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  163. }
  164. float ScaleModifierComponent::GetRangeMin() const
  165. {
  166. return m_configuration.m_rangeMin;
  167. }
  168. void ScaleModifierComponent::SetRangeMin(float rangeMin)
  169. {
  170. m_configuration.m_rangeMin = rangeMin;
  171. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  172. }
  173. float ScaleModifierComponent::GetRangeMax() const
  174. {
  175. return m_configuration.m_rangeMax;
  176. }
  177. void ScaleModifierComponent::SetRangeMax(float rangeMax)
  178. {
  179. m_configuration.m_rangeMax = rangeMax;
  180. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  181. }
  182. GradientSignal::GradientSampler& ScaleModifierComponent::GetGradientSampler()
  183. {
  184. return m_configuration.m_gradientSampler;
  185. }
  186. }