DitherGradientComponent.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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/DitherGradientComponent.h>
  9. #include <AzCore/Debug/Profiler.h>
  10. #include <AzCore/Math/MathUtils.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <GradientSignal/Util.h>
  15. #include <GradientSignal/Ebuses/SectorDataRequestBus.h>
  16. #include <LmbrCentral/Dependency/DependencyNotificationBus.h>
  17. namespace GradientSignal
  18. {
  19. void DitherGradientConfig::Reflect(AZ::ReflectContext* context)
  20. {
  21. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  22. if (serialize)
  23. {
  24. serialize->Class<DitherGradientConfig, AZ::ComponentConfig>()
  25. ->Version(1)
  26. ->Field("PatternOffset", &DitherGradientConfig::m_patternOffset)
  27. ->Field("PatternType", &DitherGradientConfig::m_patternType)
  28. ->Field("UseSystemPointsPerUnit", &DitherGradientConfig::m_useSystemPointsPerUnit)
  29. ->Field("PointsPerUnit", &DitherGradientConfig::m_pointsPerUnit)
  30. ->Field("Gradient", &DitherGradientConfig::m_gradientSampler)
  31. ;
  32. AZ::EditContext* edit = serialize->GetEditContext();
  33. if (edit)
  34. {
  35. edit->Class<DitherGradientConfig>(
  36. "Dither Gradient", "")
  37. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  38. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  39. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  40. ->DataElement(0, &DitherGradientConfig::m_patternOffset, "Pattern Offset", "Shift pattern lookup indices")
  41. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &DitherGradientConfig::m_patternType, "Pattern Type", "")
  42. ->EnumAttribute(DitherGradientConfig::BayerPatternType::PATTERN_SIZE_4x4, "4x4")
  43. ->EnumAttribute(DitherGradientConfig::BayerPatternType::PATTERN_SIZE_8x8, "8x8")
  44. ->ClassElement(AZ::Edit::ClassElements::Group, "Sample Settings")
  45. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &DitherGradientConfig::m_useSystemPointsPerUnit, "Use System Points Per Unit", "Automatically sets points per unit. Value is equal to Sector Density / Sector Size")
  46. ->DataElement(AZ::Edit::UIHandlers::Slider, &DitherGradientConfig::m_pointsPerUnit, "Points Per Unit", "Scales input position before sampling")
  47. ->Attribute(AZ::Edit::Attributes::ReadOnly, &DitherGradientConfig::IsPointsPerUnitResdOnly)
  48. ->Attribute(AZ::Edit::Attributes::Min, 0.001f)
  49. ->Attribute(AZ::Edit::Attributes::Max, std::numeric_limits<float>::max())
  50. ->Attribute(AZ::Edit::Attributes::SoftMax, 100.0f)
  51. ->EndGroup()
  52. ->DataElement(0, &DitherGradientConfig::m_gradientSampler, "Gradient", "Input gradient whose values will be dithered.")
  53. ;
  54. }
  55. }
  56. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  57. {
  58. behaviorContext->Class<DitherGradientConfig>()
  59. ->Constructor()
  60. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  61. ->Property("useSystemPointsPerUnit", BehaviorValueProperty(&DitherGradientConfig::m_useSystemPointsPerUnit))
  62. ->Property("pointsPerUnit", BehaviorValueProperty(&DitherGradientConfig::m_pointsPerUnit))
  63. ->Property("patternOffset", BehaviorValueProperty(&DitherGradientConfig::m_patternOffset))
  64. ->Property("patternType",
  65. [](DitherGradientConfig* config) { return (AZ::u8&)(config->m_patternType); },
  66. [](DitherGradientConfig* config, const AZ::u8& i) { config->m_patternType = (DitherGradientConfig::BayerPatternType)i; })
  67. ->Property("gradientSampler", BehaviorValueProperty(&DitherGradientConfig::m_gradientSampler))
  68. ;
  69. }
  70. }
  71. bool DitherGradientConfig::IsPointsPerUnitResdOnly() const
  72. {
  73. return m_useSystemPointsPerUnit;
  74. }
  75. void DitherGradientComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  76. {
  77. services.push_back(AZ_CRC_CE("GradientService"));
  78. }
  79. void DitherGradientComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  80. {
  81. services.push_back(AZ_CRC_CE("GradientService"));
  82. }
  83. void DitherGradientComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& services)
  84. {
  85. }
  86. void DitherGradientComponent::Reflect(AZ::ReflectContext* context)
  87. {
  88. DitherGradientConfig::Reflect(context);
  89. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  90. if (serialize)
  91. {
  92. serialize->Class<DitherGradientComponent, AZ::Component>()
  93. ->Version(0)
  94. ->Field("Configuration", &DitherGradientComponent::m_configuration)
  95. ;
  96. }
  97. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  98. {
  99. behaviorContext->Constant("DitherGradientComponentTypeId", BehaviorConstant(DitherGradientComponentTypeId));
  100. behaviorContext->Class<DitherGradientComponent>()->RequestBus("DitherGradientRequestBus");
  101. behaviorContext->EBus<DitherGradientRequestBus>("DitherGradientRequestBus")
  102. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  103. ->Event("GetUseSystemPointsPerUnit", &DitherGradientRequestBus::Events::GetUseSystemPointsPerUnit)
  104. ->Event("SetUseSystemPointsPerUnit", &DitherGradientRequestBus::Events::SetUseSystemPointsPerUnit)
  105. ->VirtualProperty("UseSystemPointsPerUnit", "GetUseSystemPointsPerUnit", "SetUseSystemPointsPerUnit")
  106. ->Event("GetPointsPerUnit", &DitherGradientRequestBus::Events::GetPointsPerUnit)
  107. ->Event("SetPointsPerUnit", &DitherGradientRequestBus::Events::SetPointsPerUnit)
  108. ->VirtualProperty("PointsPerUnit", "GetPointsPerUnit", "SetPointsPerUnit")
  109. ->Event("GetPatternOffset", &DitherGradientRequestBus::Events::GetPatternOffset)
  110. ->Event("SetPatternOffset", &DitherGradientRequestBus::Events::SetPatternOffset)
  111. ->VirtualProperty("PatternOffset", "GetPatternOffset", "SetPatternOffset")
  112. ->Event("GetPatternType", &DitherGradientRequestBus::Events::GetPatternType)
  113. ->Event("SetPatternType", &DitherGradientRequestBus::Events::SetPatternType)
  114. ->VirtualProperty("PatternType", "GetPatternType", "SetPatternType")
  115. ->Event("GetGradientSampler", &DitherGradientRequestBus::Events::GetGradientSampler)
  116. ;
  117. }
  118. }
  119. DitherGradientComponent::DitherGradientComponent(const DitherGradientConfig& configuration)
  120. : m_configuration(configuration)
  121. {
  122. }
  123. void DitherGradientComponent::Activate()
  124. {
  125. m_dependencyMonitor.Reset();
  126. m_dependencyMonitor.ConnectOwner(GetEntityId());
  127. m_dependencyMonitor.ConnectDependency(m_configuration.m_gradientSampler.m_gradientId);
  128. DitherGradientRequestBus::Handler::BusConnect(GetEntityId());
  129. SectorDataNotificationBus::Handler::BusConnect();
  130. // Connect to GradientRequestBus last so that everything is initialized before listening for gradient queries.
  131. GradientRequestBus::Handler::BusConnect(GetEntityId());
  132. }
  133. void DitherGradientComponent::Deactivate()
  134. {
  135. // Disconnect from GradientRequestBus first to ensure no queries are in process when deactivating.
  136. GradientRequestBus::Handler::BusDisconnect();
  137. m_dependencyMonitor.Reset();
  138. DitherGradientRequestBus::Handler::BusDisconnect();
  139. SectorDataNotificationBus::Handler::BusDisconnect();
  140. }
  141. bool DitherGradientComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  142. {
  143. if (auto config = azrtti_cast<const DitherGradientConfig*>(baseConfig))
  144. {
  145. m_configuration = *config;
  146. return true;
  147. }
  148. return false;
  149. }
  150. bool DitherGradientComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  151. {
  152. if (auto config = azrtti_cast<DitherGradientConfig*>(outBaseConfig))
  153. {
  154. *config = m_configuration;
  155. return true;
  156. }
  157. return false;
  158. }
  159. int DitherGradientComponent::ScaledPositionToPatternIndex(const AZ::Vector3& scaledPosition, int patternSize)
  160. {
  161. // The input position is expected to be scaled up so that each integer value is a unique point in our dither pattern, and
  162. // the fractional value is just the amount within the point. The output is the specific index into an NxN pattern to use
  163. // for the dither comparison value.
  164. // Get the floor before casting to int because we want fractional negative values to go "down" to the next negative value.
  165. AZ::Vector3 flooredScaledPosition = scaledPosition.GetFloor();
  166. // For a pattern of 4, we want our indices to go 0, 1, 2, 3, 0, 1, 2, 3, etc. However, we want it continuous across
  167. // negative and positive positions so we can't just use mod with abs(). Instead, we use a double-mod which gives us
  168. // a result that's continuous across all coordinate space.
  169. const int x = ((static_cast<int>(flooredScaledPosition.GetX()) % patternSize) + patternSize) % patternSize;
  170. const int y = ((static_cast<int>(flooredScaledPosition.GetY()) % patternSize) + patternSize) % patternSize;
  171. return (patternSize * y + x);
  172. }
  173. float DitherGradientComponent::GetDitherValue4x4(const AZ::Vector3& scaledPosition)
  174. {
  175. constexpr int patternSize = 4;
  176. constexpr float indexMatrix[] = {
  177. 0.0f / 16.0f, 8.0f / 16.0f, 2.0f / 16.0f, 10.0f / 16.0f,
  178. 12.0f / 16.0f, 4.0f / 16.0f, 14.0f / 16.0f, 6.0f / 16.0f,
  179. 3.0f / 16.0f, 11.0f / 16.0f, 1.0f / 16.0f, 9.0f / 16.0f,
  180. 15.0f / 16.0f, 7.0f / 16.0f, 13.0f / 16.0f, 5.0f / 16.0f };
  181. return indexMatrix[ScaledPositionToPatternIndex(scaledPosition, patternSize)];
  182. }
  183. float DitherGradientComponent::GetDitherValue8x8(const AZ::Vector3& scaledPosition)
  184. {
  185. constexpr int patternSize = 8;
  186. constexpr float indexMatrix[] = {
  187. 0.0f / 64.0f, 32.0f / 64.0f, 8.0f / 64.0f, 40.0f / 64.0f, 2.0f / 64.0f, 34.0f / 64.0f, 10.0f / 64.0f, 42.0f / 64.0f,
  188. 48.0f / 64.0f, 16.0f / 64.0f, 56.0f / 64.0f, 24.0f / 64.0f, 50.0f / 64.0f, 18.0f / 64.0f, 58.0f / 64.0f, 26.0f / 64.0f,
  189. 12.0f / 64.0f, 44.0f / 64.0f, 4.0f / 64.0f, 36.0f / 64.0f, 14.0f / 64.0f, 46.0f / 64.0f, 6.0f / 64.0f, 38.0f / 64.0f,
  190. 60.0f / 64.0f, 28.0f / 64.0f, 52.0f / 64.0f, 20.0f / 64.0f, 62.0f / 64.0f, 30.0f / 64.0f, 54.0f / 64.0f, 22.0f / 64.0f,
  191. 3.0f / 64.0f, 35.0f / 64.0f, 11.0f / 64.0f, 43.0f / 64.0f, 1.0f / 64.0f, 33.0f / 64.0f, 9.0f / 64.0f, 41.0f / 64.0f,
  192. 51.0f / 64.0f, 19.0f / 64.0f, 59.0f / 64.0f, 27.0f / 64.0f, 49.0f / 64.0f, 17.0f / 64.0f, 57.0f / 64.0f, 25.0f / 64.0f,
  193. 15.0f / 64.0f, 47.0f / 64.0f, 7.0f / 64.0f, 39.0f / 64.0f, 13.0f / 64.0f, 45.0f / 64.0f, 5.0f / 64.0f, 37.0f / 64.0f,
  194. 63.0f / 64.0f, 31.0f / 64.0f, 55.0f / 64.0f, 23.0f / 64.0f, 61.0f / 64.0f, 29.0f / 64.0f, 53.0f / 64.0f, 21.0f / 64.0f
  195. };
  196. return indexMatrix[ScaledPositionToPatternIndex(scaledPosition, patternSize)];
  197. }
  198. float DitherGradientComponent::GetCalculatedPointsPerUnit() const
  199. {
  200. float pointsPerUnit = m_configuration.m_pointsPerUnit;
  201. if (m_configuration.m_useSystemPointsPerUnit)
  202. {
  203. SectorDataRequestBus::Broadcast(&SectorDataRequestBus::Events::GetPointsPerMeter, pointsPerUnit);
  204. }
  205. return AZ::GetMax(pointsPerUnit, 0.0001f);
  206. }
  207. float DitherGradientComponent::GetDitherValue(const AZ::Vector3& scaledPosition, float value) const
  208. {
  209. float d = 0.0f;
  210. switch (m_configuration.m_patternType)
  211. {
  212. default:
  213. case DitherGradientConfig::BayerPatternType::PATTERN_SIZE_4x4:
  214. d = GetDitherValue4x4(scaledPosition + m_configuration.m_patternOffset);
  215. break;
  216. case DitherGradientConfig::BayerPatternType::PATTERN_SIZE_8x8:
  217. d = GetDitherValue8x8(scaledPosition + m_configuration.m_patternOffset);
  218. break;
  219. }
  220. return (value > d) ? 1.0f : 0.0f;
  221. }
  222. float DitherGradientComponent::GetValue(const GradientSampleParams& sampleParams) const
  223. {
  224. AZStd::shared_lock lock(m_queryMutex);
  225. const AZ::Vector3& coordinate = sampleParams.m_position;
  226. const float pointsPerUnit = GetCalculatedPointsPerUnit();
  227. AZ::Vector3 scaledCoordinate = coordinate * pointsPerUnit;
  228. AZ::Vector3 flooredCoordinate = scaledCoordinate.GetFloor() / pointsPerUnit;
  229. GradientSampleParams adjustedSampleParams = sampleParams;
  230. adjustedSampleParams.m_position = flooredCoordinate;
  231. float value = m_configuration.m_gradientSampler.GetValue(adjustedSampleParams);
  232. return GetDitherValue(scaledCoordinate, value);
  233. }
  234. void DitherGradientComponent::GetValues(AZStd::span<const AZ::Vector3> positions, AZStd::span<float> outValues) const
  235. {
  236. if (positions.size() != outValues.size())
  237. {
  238. AZ_Assert(false, "input and output lists are different sizes (%zu vs %zu).", positions.size(), outValues.size());
  239. return;
  240. }
  241. AZStd::shared_lock lock(m_queryMutex);
  242. const float pointsPerUnit = GetCalculatedPointsPerUnit();
  243. // Create the entire set of floored coordinates to use in the gradient value lookups.
  244. AZStd::vector<AZ::Vector3> flooredCoordinates(positions.size());
  245. for (size_t index = 0; index < positions.size(); index++)
  246. {
  247. AZ::Vector3 scaledCoordinate = positions[index] * pointsPerUnit;
  248. flooredCoordinates[index] = scaledCoordinate.GetFloor() / pointsPerUnit;
  249. }
  250. m_configuration.m_gradientSampler.GetValues(flooredCoordinates, outValues);
  251. // For each gradient value, turn it into a 0 or 1 based on the location and the dither pattern.
  252. for (size_t index = 0; index < positions.size(); index++)
  253. {
  254. AZ::Vector3 scaledCoordinate = positions[index] * pointsPerUnit;
  255. outValues[index] = GetDitherValue(scaledCoordinate, outValues[index]);
  256. }
  257. }
  258. bool DitherGradientComponent::IsEntityInHierarchy(const AZ::EntityId& entityId) const
  259. {
  260. return m_configuration.m_gradientSampler.IsEntityInHierarchy(entityId);
  261. }
  262. void DitherGradientComponent::OnSectorDataConfigurationUpdated() const
  263. {
  264. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  265. }
  266. bool DitherGradientComponent::GetUseSystemPointsPerUnit() const
  267. {
  268. return m_configuration.m_useSystemPointsPerUnit;
  269. }
  270. void DitherGradientComponent::SetUseSystemPointsPerUnit(bool value)
  271. {
  272. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  273. // execute an arbitrary amount of logic, including calls back to this component.
  274. {
  275. AZStd::unique_lock lock(m_queryMutex);
  276. m_configuration.m_useSystemPointsPerUnit = value;
  277. }
  278. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  279. }
  280. float DitherGradientComponent::GetPointsPerUnit() const
  281. {
  282. return m_configuration.m_pointsPerUnit;
  283. }
  284. void DitherGradientComponent::SetPointsPerUnit(float points)
  285. {
  286. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  287. // execute an arbitrary amount of logic, including calls back to this component.
  288. {
  289. AZStd::unique_lock lock(m_queryMutex);
  290. m_configuration.m_pointsPerUnit = points;
  291. }
  292. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  293. }
  294. AZ::Vector3 DitherGradientComponent::GetPatternOffset() const
  295. {
  296. return m_configuration.m_patternOffset;
  297. }
  298. void DitherGradientComponent::SetPatternOffset(AZ::Vector3 offset)
  299. {
  300. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  301. // execute an arbitrary amount of logic, including calls back to this component.
  302. {
  303. AZStd::unique_lock lock(m_queryMutex);
  304. m_configuration.m_patternOffset = offset;
  305. }
  306. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  307. }
  308. AZ::u8 DitherGradientComponent::GetPatternType() const
  309. {
  310. return (AZ::u8)m_configuration.m_patternType;
  311. }
  312. void DitherGradientComponent::SetPatternType(AZ::u8 type)
  313. {
  314. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  315. // execute an arbitrary amount of logic, including calls back to this component.
  316. {
  317. AZStd::unique_lock lock(m_queryMutex);
  318. m_configuration.m_patternType = (DitherGradientConfig::BayerPatternType)type;
  319. }
  320. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  321. }
  322. GradientSampler& DitherGradientComponent::GetGradientSampler()
  323. {
  324. return m_configuration.m_gradientSampler;
  325. }
  326. }