PerlinGradientComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/PerlinGradientComponent.h>
  9. #include <AzCore/Debug/Profiler.h>
  10. #include <AzCore/Jobs/Job.h>
  11. #include <AzCore/Jobs/JobFunction.h>
  12. #include <AzCore/Jobs/JobManagerBus.h>
  13. #include <AzCore/Math/Vector3.h>
  14. #include <AzCore/RTTI/BehaviorContext.h>
  15. #include <AzCore/Serialization/EditContext.h>
  16. #include <AzCore/Serialization/SerializeContext.h>
  17. #include <LmbrCentral/Dependency/DependencyNotificationBus.h>
  18. #include <GradientSignal/Ebuses/GradientTransformRequestBus.h>
  19. namespace GradientSignal
  20. {
  21. void PerlinGradientConfig::Reflect(AZ::ReflectContext* context)
  22. {
  23. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  24. if (serialize)
  25. {
  26. serialize->Class<PerlinGradientConfig, AZ::ComponentConfig>()
  27. ->Version(1)
  28. ->Field("randomSeed", &PerlinGradientConfig::m_randomSeed)
  29. ->Field("octave", &PerlinGradientConfig::m_octave)
  30. ->Field("amplitude", &PerlinGradientConfig::m_amplitude)
  31. ->Field("frequency", &PerlinGradientConfig::m_frequency)
  32. ;
  33. AZ::EditContext* edit = serialize->GetEditContext();
  34. if (edit)
  35. {
  36. edit->Class<PerlinGradientConfig>(
  37. "Perlin Noise Gradient", "")
  38. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  39. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  40. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  41. ->DataElement(AZ::Edit::UIHandlers::Slider, &PerlinGradientConfig::m_randomSeed, "Random Seed", "Using different seeds will cause the noise output to change")
  42. ->Attribute(AZ::Edit::Attributes::Min, 1)
  43. ->Attribute(AZ::Edit::Attributes::Max, std::numeric_limits<int>::max())
  44. ->Attribute(AZ::Edit::Attributes::SoftMin, 1)
  45. ->Attribute(AZ::Edit::Attributes::SoftMax, 100)
  46. ->Attribute(AZ::Edit::Attributes::Step, 10)
  47. ->DataElement(AZ::Edit::UIHandlers::Slider, &PerlinGradientConfig::m_octave, "Octaves", "Number of recursions in the pattern generation, higher octaves refine the pattern")
  48. ->Attribute(AZ::Edit::Attributes::Min, 0)
  49. ->Attribute(AZ::Edit::Attributes::Max, 16)
  50. ->Attribute(AZ::Edit::Attributes::SoftMax, 8)
  51. ->DataElement(AZ::Edit::UIHandlers::Slider, &PerlinGradientConfig::m_amplitude, "Amplitude", "Higher amplitudes widen the aperture of the highs (light) and lows (dark)")
  52. ->Attribute(AZ::Edit::Attributes::Min, 0.f)
  53. ->Attribute(AZ::Edit::Attributes::Max, std::numeric_limits<float>::max())
  54. ->Attribute(AZ::Edit::Attributes::SoftMax, 8.0f)
  55. ->DataElement(AZ::Edit::UIHandlers::Slider, &PerlinGradientConfig::m_frequency, "Frequency", "Rescales coordinates based on a multiplied factor")
  56. ->Attribute(AZ::Edit::Attributes::DisplayDecimals, 4)
  57. ->Attribute(AZ::Edit::Attributes::Min, 0.0001f)
  58. ->Attribute(AZ::Edit::Attributes::Max, std::numeric_limits<float>::max())
  59. ->Attribute(AZ::Edit::Attributes::SoftMax, 8.0f)
  60. ->Attribute(AZ::Edit::Attributes::Step, 0.25f)
  61. ->Attribute(AZ::Edit::Attributes::SliderCurveMidpoint, 0.25) // Give the frequency a non-linear scale slider with higher precision at the low end
  62. ;
  63. }
  64. }
  65. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  66. {
  67. behaviorContext->Class<PerlinGradientConfig>()
  68. ->Constructor()
  69. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  70. ->Property("randomSeed", BehaviorValueProperty(&PerlinGradientConfig::m_randomSeed))
  71. ->Property("octave", BehaviorValueProperty(&PerlinGradientConfig::m_octave))
  72. ->Property("amplitude", BehaviorValueProperty(&PerlinGradientConfig::m_amplitude))
  73. ->Property("frequency", BehaviorValueProperty(&PerlinGradientConfig::m_frequency))
  74. ;
  75. }
  76. }
  77. void PerlinGradientComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  78. {
  79. services.push_back(AZ_CRC_CE("GradientService"));
  80. }
  81. void PerlinGradientComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  82. {
  83. services.push_back(AZ_CRC_CE("GradientService"));
  84. }
  85. void PerlinGradientComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  86. {
  87. services.push_back(AZ_CRC_CE("GradientTransformService"));
  88. }
  89. void PerlinGradientComponent::Reflect(AZ::ReflectContext* context)
  90. {
  91. PerlinGradientConfig::Reflect(context);
  92. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  93. if (serialize)
  94. {
  95. serialize->Class<PerlinGradientComponent, AZ::Component>()
  96. ->Version(0)
  97. ->Field("Configuration", &PerlinGradientComponent::m_configuration)
  98. ;
  99. }
  100. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  101. {
  102. behaviorContext->Constant("PerlinGradientComponentTypeId", BehaviorConstant(PerlinGradientComponentTypeId));
  103. behaviorContext->Class<PerlinGradientComponent>()->RequestBus("PerlinGradientRequestBus");
  104. behaviorContext->EBus<PerlinGradientRequestBus>("PerlinGradientRequestBus")
  105. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  106. ->Event("GetRandomSeed", &PerlinGradientRequestBus::Events::GetRandomSeed)
  107. ->Event("SetRandomSeed", &PerlinGradientRequestBus::Events::SetRandomSeed)
  108. ->VirtualProperty("RandomSeed", "GetRandomSeed", "SetRandomSeed")
  109. ->Event("GetAmplitude", &PerlinGradientRequestBus::Events::GetAmplitude)
  110. ->Event("SetAmplitude", &PerlinGradientRequestBus::Events::SetAmplitude)
  111. ->VirtualProperty("Amplitude", "GetAmplitude", "SetAmplitude")
  112. ->Event("GetOctaves", &PerlinGradientRequestBus::Events::GetOctaves)
  113. ->Event("SetOctaves", &PerlinGradientRequestBus::Events::SetOctaves)
  114. ->VirtualProperty("Octaves", "GetOctaves", "SetOctaves")
  115. ->Event("GetFrequency", &PerlinGradientRequestBus::Events::GetFrequency)
  116. ->Event("SetFrequency", &PerlinGradientRequestBus::Events::SetFrequency)
  117. ->VirtualProperty("Frequency", "GetFrequency", "SetFrequency")
  118. ;
  119. }
  120. }
  121. PerlinGradientComponent::PerlinGradientComponent(const PerlinGradientConfig& configuration)
  122. : m_configuration(configuration)
  123. {
  124. }
  125. void PerlinGradientComponent::Activate()
  126. {
  127. // This will immediately call OnGradientTransformChanged and initialize m_gradientTransform.
  128. GradientTransformNotificationBus::Handler::BusConnect(GetEntityId());
  129. m_perlinImprovedNoise.reset(aznew PerlinImprovedNoise(AZ::GetMax(m_configuration.m_randomSeed, 1)));
  130. PerlinGradientRequestBus::Handler::BusConnect(GetEntityId());
  131. // Connect to GradientRequestBus last so that everything is initialized before listening for gradient queries.
  132. GradientRequestBus::Handler::BusConnect(GetEntityId());
  133. }
  134. void PerlinGradientComponent::Deactivate()
  135. {
  136. // Disconnect from GradientRequestBus first to ensure no queries are in process when deactivating.
  137. GradientRequestBus::Handler::BusDisconnect();
  138. PerlinGradientRequestBus::Handler::BusDisconnect();
  139. GradientTransformNotificationBus::Handler::BusDisconnect();
  140. m_perlinImprovedNoise.reset();
  141. }
  142. bool PerlinGradientComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  143. {
  144. if (auto config = azrtti_cast<const PerlinGradientConfig*>(baseConfig))
  145. {
  146. m_configuration = *config;
  147. return true;
  148. }
  149. return false;
  150. }
  151. bool PerlinGradientComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  152. {
  153. if (auto config = azrtti_cast<PerlinGradientConfig*>(outBaseConfig))
  154. {
  155. *config = m_configuration;
  156. return true;
  157. }
  158. return false;
  159. }
  160. void PerlinGradientComponent::OnGradientTransformChanged(const GradientTransform& newTransform)
  161. {
  162. AZStd::unique_lock lock(m_queryMutex);
  163. m_gradientTransform = newTransform;
  164. }
  165. float PerlinGradientComponent::GetValue(const GradientSampleParams& sampleParams) const
  166. {
  167. AZStd::shared_lock lock(m_queryMutex);
  168. if (m_perlinImprovedNoise)
  169. {
  170. AZ::Vector3 uvw = sampleParams.m_position;
  171. bool wasPointRejected = false;
  172. m_gradientTransform.TransformPositionToUVW(sampleParams.m_position, uvw, wasPointRejected);
  173. if (!wasPointRejected)
  174. {
  175. return m_perlinImprovedNoise->GenerateOctaveNoise(
  176. uvw.GetX(), uvw.GetY(), uvw.GetZ(), m_configuration.m_octave, m_configuration.m_amplitude,
  177. m_configuration.m_frequency);
  178. }
  179. }
  180. return 0.0f;
  181. }
  182. void PerlinGradientComponent::GetValues(AZStd::span<const AZ::Vector3> positions, AZStd::span<float> outValues) const
  183. {
  184. if (positions.size() != outValues.size())
  185. {
  186. AZ_Assert(false, "input and output lists are different sizes (%zu vs %zu).", positions.size(), outValues.size());
  187. return;
  188. }
  189. AZ::Vector3 uvw;
  190. bool wasPointRejected = false;
  191. AZStd::shared_lock lock(m_queryMutex);
  192. for (size_t index = 0; index < positions.size(); index++)
  193. {
  194. m_gradientTransform.TransformPositionToUVW(positions[index], uvw, wasPointRejected);
  195. if (!wasPointRejected)
  196. {
  197. outValues[index] = m_perlinImprovedNoise->GenerateOctaveNoise(
  198. uvw.GetX(), uvw.GetY(), uvw.GetZ(), m_configuration.m_octave, m_configuration.m_amplitude,
  199. m_configuration.m_frequency);
  200. }
  201. else
  202. {
  203. outValues[index] = 0.0f;
  204. }
  205. }
  206. }
  207. int PerlinGradientComponent::GetRandomSeed() const
  208. {
  209. return m_configuration.m_randomSeed;
  210. }
  211. void PerlinGradientComponent::SetRandomSeed(int seed)
  212. {
  213. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  214. // execute an arbitrary amount of logic, including calls back to this component.
  215. {
  216. AZStd::unique_lock lock(m_queryMutex);
  217. m_configuration.m_randomSeed = AZStd::GetMax(seed, 1);
  218. m_perlinImprovedNoise.release();
  219. m_perlinImprovedNoise.reset(aznew PerlinImprovedNoise(m_configuration.m_randomSeed));
  220. }
  221. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  222. }
  223. int PerlinGradientComponent::GetOctaves() const
  224. {
  225. return m_configuration.m_octave;
  226. }
  227. void PerlinGradientComponent::SetOctaves(int octaves)
  228. {
  229. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  230. // execute an arbitrary amount of logic, including calls back to this component.
  231. {
  232. AZStd::unique_lock lock(m_queryMutex);
  233. m_configuration.m_octave = octaves;
  234. }
  235. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  236. }
  237. float PerlinGradientComponent::GetAmplitude() const
  238. {
  239. return m_configuration.m_amplitude;
  240. }
  241. void PerlinGradientComponent::SetAmplitude(float amp)
  242. {
  243. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  244. // execute an arbitrary amount of logic, including calls back to this component.
  245. {
  246. AZStd::unique_lock lock(m_queryMutex);
  247. m_configuration.m_amplitude = amp;
  248. }
  249. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  250. }
  251. float PerlinGradientComponent::GetFrequency() const
  252. {
  253. return m_configuration.m_frequency;
  254. }
  255. void PerlinGradientComponent::SetFrequency(float frequency)
  256. {
  257. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  258. // execute an arbitrary amount of logic, including calls back to this component.
  259. {
  260. AZStd::unique_lock lock(m_queryMutex);
  261. m_configuration.m_frequency = frequency;
  262. }
  263. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  264. }
  265. }