ReferenceGradientComponent.cpp 6.0 KB

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