InvertGradientComponent.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/InvertGradientComponent.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 InvertGradientConfig::Reflect(AZ::ReflectContext* context)
  16. {
  17. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  18. if (serialize)
  19. {
  20. serialize->Class<InvertGradientConfig, AZ::ComponentConfig>()
  21. ->Version(0)
  22. ->Field("Gradient", &InvertGradientConfig::m_gradientSampler)
  23. ;
  24. AZ::EditContext* edit = serialize->GetEditContext();
  25. if (edit)
  26. {
  27. edit->Class<InvertGradientConfig>(
  28. "Invert Gradient", "")
  29. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  30. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  31. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  32. ->DataElement(0, &InvertGradientConfig::m_gradientSampler, "Gradient", "Input gradient whose values will be inverted.")
  33. ;
  34. }
  35. }
  36. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  37. {
  38. behaviorContext->Class<InvertGradientConfig>()
  39. ->Constructor()
  40. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  41. ->Property("gradientSampler", BehaviorValueProperty(&InvertGradientConfig::m_gradientSampler))
  42. ;
  43. }
  44. }
  45. void InvertGradientComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  46. {
  47. services.push_back(AZ_CRC_CE("GradientService"));
  48. }
  49. void InvertGradientComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  50. {
  51. services.push_back(AZ_CRC_CE("GradientService"));
  52. }
  53. void InvertGradientComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& services)
  54. {
  55. }
  56. void InvertGradientComponent::Reflect(AZ::ReflectContext* context)
  57. {
  58. InvertGradientConfig::Reflect(context);
  59. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  60. if (serialize)
  61. {
  62. serialize->Class<InvertGradientComponent, AZ::Component>()
  63. ->Version(0)
  64. ->Field("Configuration", &InvertGradientComponent::m_configuration)
  65. ;
  66. }
  67. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  68. {
  69. behaviorContext->Constant("InvertGradientComponentTypeId", BehaviorConstant(InvertGradientComponentTypeId));
  70. behaviorContext->Class<InvertGradientComponent>()->RequestBus("InvertGradientRequestBus");
  71. behaviorContext->EBus<InvertGradientRequestBus>("InvertGradientRequestBus")
  72. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  73. ->Event("GetGradientSampler", &InvertGradientRequestBus::Events::GetGradientSampler)
  74. ;
  75. }
  76. }
  77. InvertGradientComponent::InvertGradientComponent(const InvertGradientConfig& configuration)
  78. : m_configuration(configuration)
  79. {
  80. }
  81. void InvertGradientComponent::Activate()
  82. {
  83. m_dependencyMonitor.Reset();
  84. m_dependencyMonitor.ConnectOwner(GetEntityId());
  85. m_dependencyMonitor.ConnectDependency(m_configuration.m_gradientSampler.m_gradientId);
  86. InvertGradientRequestBus::Handler::BusConnect(GetEntityId());
  87. // Connect to GradientRequestBus last so that everything is initialized before listening for gradient queries.
  88. GradientRequestBus::Handler::BusConnect(GetEntityId());
  89. }
  90. void InvertGradientComponent::Deactivate()
  91. {
  92. // Disconnect from GradientRequestBus first to ensure no queries are in process when deactivating.
  93. GradientRequestBus::Handler::BusDisconnect();
  94. m_dependencyMonitor.Reset();
  95. InvertGradientRequestBus::Handler::BusDisconnect();
  96. }
  97. bool InvertGradientComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  98. {
  99. if (auto config = azrtti_cast<const InvertGradientConfig*>(baseConfig))
  100. {
  101. m_configuration = *config;
  102. return true;
  103. }
  104. return false;
  105. }
  106. bool InvertGradientComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  107. {
  108. if (auto config = azrtti_cast<InvertGradientConfig*>(outBaseConfig))
  109. {
  110. *config = m_configuration;
  111. return true;
  112. }
  113. return false;
  114. }
  115. float InvertGradientComponent::GetValue(const GradientSampleParams& sampleParams) const
  116. {
  117. float output = 0.0f;
  118. output = 1.0f - AZ::GetClamp(m_configuration.m_gradientSampler.GetValue(sampleParams), 0.0f, 1.0f);
  119. return output;
  120. }
  121. void InvertGradientComponent::GetValues(AZStd::span<const AZ::Vector3> positions, AZStd::span<float> outValues) const
  122. {
  123. if (positions.size() != outValues.size())
  124. {
  125. AZ_Assert(false, "input and output lists are different sizes (%zu vs %zu).", positions.size(), outValues.size());
  126. return;
  127. }
  128. m_configuration.m_gradientSampler.GetValues(positions, outValues);
  129. for (auto& outValue : outValues)
  130. {
  131. outValue = 1.0f - AZ::GetClamp(outValue, 0.0f, 1.0f);
  132. }
  133. }
  134. bool InvertGradientComponent::IsEntityInHierarchy(const AZ::EntityId& entityId) const
  135. {
  136. return m_configuration.m_gradientSampler.IsEntityInHierarchy(entityId);
  137. }
  138. GradientSampler& InvertGradientComponent::GetGradientSampler()
  139. {
  140. return m_configuration.m_gradientSampler;
  141. }
  142. }