AreaBlenderComponent.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 <VegetationProfiler.h>
  9. #include "AreaBlenderComponent.h"
  10. #include <AzCore/Debug/Profiler.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <Vegetation/InstanceData.h>
  15. #include <Vegetation/Ebuses/AreaSystemRequestBus.h>
  16. #include <Vegetation/Ebuses/DebugNotificationBus.h>
  17. #include <VegetationProfiler.h>
  18. namespace Vegetation
  19. {
  20. void AreaBlenderConfig::Reflect(AZ::ReflectContext* context)
  21. {
  22. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  23. if (serialize)
  24. {
  25. serialize->Class<AreaBlenderConfig, AreaConfig>()
  26. ->Version(0)
  27. ->Field("InheritBehavior", &AreaBlenderConfig::m_inheritBehavior)
  28. ->Field("PropagateBehavior", &AreaBlenderConfig::m_propagateBehavior)
  29. ->Field("Operations", &AreaBlenderConfig::m_vegetationAreaIds)
  30. ;
  31. AZ::EditContext* edit = serialize->GetEditContext();
  32. if (edit)
  33. {
  34. edit->Class<AreaBlenderConfig>(
  35. "Vegetation Layer Blender", "")
  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(0, &AreaBlenderConfig::m_inheritBehavior, "Inherit Behavior", "Allow shapes, modifiers, filters of a parent to affect this area.")
  40. ->DataElement(0, &AreaBlenderConfig::m_propagateBehavior, "Propagate Behavior", "Allow shapes, modifiers, filters to affect referenced areas.")
  41. ->DataElement(0, &AreaBlenderConfig::m_vegetationAreaIds, "Vegetation Areas", "Ordered list of vegetation areas.")
  42. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  43. ->Attribute(AZ::Edit::Attributes::ContainerCanBeModified, true)
  44. ->ElementAttribute(AZ::Edit::Attributes::RequiredService, AZ_CRC_CE("VegetationAreaService"));
  45. ;
  46. }
  47. }
  48. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  49. {
  50. behaviorContext->Class<AreaBlenderConfig>()
  51. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  52. ->Constructor()
  53. ->Property("inheritBehavior", BehaviorValueProperty(&AreaBlenderConfig::m_inheritBehavior))
  54. ->Property("propogateBehavior", BehaviorValueProperty(&AreaBlenderConfig::m_propagateBehavior))
  55. ->Method("GetNumAreas", &AreaBlenderConfig::GetNumAreas)
  56. ->Method("GetAreaEntityId", &AreaBlenderConfig::GetAreaEntityId)
  57. ->Method("RemoveAreaEntityId", &AreaBlenderConfig::RemoveAreaEntityId)
  58. ->Method("AddAreaEntityId", &AreaBlenderConfig::AddAreaEntityId)
  59. ;
  60. }
  61. }
  62. size_t AreaBlenderConfig::GetNumAreas() const
  63. {
  64. return m_vegetationAreaIds.size();
  65. }
  66. AZ::EntityId AreaBlenderConfig::GetAreaEntityId(int index) const
  67. {
  68. if (index < m_vegetationAreaIds.size() && index >= 0)
  69. {
  70. return m_vegetationAreaIds[index];
  71. }
  72. return AZ::EntityId();
  73. }
  74. void AreaBlenderConfig::RemoveAreaEntityId(int index)
  75. {
  76. if (index < m_vegetationAreaIds.size() && index >= 0)
  77. {
  78. m_vegetationAreaIds.erase(m_vegetationAreaIds.begin() + index);
  79. }
  80. }
  81. void AreaBlenderConfig::AddAreaEntityId(AZ::EntityId entityId)
  82. {
  83. m_vegetationAreaIds.push_back(entityId);
  84. }
  85. void AreaBlenderComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  86. {
  87. AreaComponentBase::GetProvidedServices(services);
  88. }
  89. void AreaBlenderComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  90. {
  91. AreaComponentBase::GetIncompatibleServices(services);
  92. }
  93. void AreaBlenderComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  94. {
  95. AreaComponentBase::GetRequiredServices(services);
  96. }
  97. void AreaBlenderComponent::Reflect(AZ::ReflectContext* context)
  98. {
  99. AreaBlenderConfig::Reflect(context);
  100. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  101. if (serialize)
  102. {
  103. serialize->Class<AreaBlenderComponent, AreaComponentBase>()
  104. ->Version(0)
  105. ->Field("Configuration", &AreaBlenderComponent::m_configuration)
  106. ;
  107. }
  108. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  109. {
  110. behaviorContext->Constant("AreaBlenderComponentTypeId", BehaviorConstant(AreaBlenderComponentTypeId));
  111. behaviorContext->Class<AreaBlenderComponent>()->RequestBus("AreaBlenderRequestBus");
  112. behaviorContext->EBus<AreaBlenderRequestBus>("AreaBlenderRequestBus")
  113. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  114. ->Event("GetAreaPriority", &AreaBlenderRequestBus::Events::GetAreaPriority)
  115. ->Event("SetAreaPriority", &AreaBlenderRequestBus::Events::SetAreaPriority)
  116. ->VirtualProperty("AreaPriority", "GetAreaPriority", "SetAreaPriority")
  117. ->Event("GetAreaLayer", &AreaBlenderRequestBus::Events::GetAreaLayer)
  118. ->Event("SetAreaLayer", &AreaBlenderRequestBus::Events::SetAreaLayer)
  119. ->VirtualProperty("AreaLayer", "GetAreaLayer", "SetAreaLayer")
  120. ->Event("GetAreaProductCount", &AreaBlenderRequestBus::Events::GetAreaProductCount)
  121. ->Event("GetInheritBehavior", &AreaBlenderRequestBus::Events::GetInheritBehavior)
  122. ->Event("SetInheritBehavior", &AreaBlenderRequestBus::Events::SetInheritBehavior)
  123. ->VirtualProperty("InheritBehavior", "GetInheritBehavior", "SetInheritBehavior")
  124. ->Event("GetPropagateBehavior", &AreaBlenderRequestBus::Events::GetPropagateBehavior)
  125. ->Event("SetPropagateBehavior", &AreaBlenderRequestBus::Events::SetPropagateBehavior)
  126. ->VirtualProperty("PropagateBehavior", "GetPropagateBehavior", "SetPropagateBehavior")
  127. ->Event("GetNumAreas", &AreaBlenderRequestBus::Events::GetNumAreas)
  128. ->Event("GetAreaEntityId", &AreaBlenderRequestBus::Events::GetAreaEntityId)
  129. ->Event("RemoveAreaEntityId", &AreaBlenderRequestBus::Events::RemoveAreaEntityId)
  130. ->Event("AddAreaEntityId", &AreaBlenderRequestBus::Events::AddAreaEntityId)
  131. ;
  132. }
  133. }
  134. AreaBlenderComponent::AreaBlenderComponent(const AreaBlenderConfig& configuration)
  135. : AreaComponentBase(configuration)
  136. , m_configuration(configuration)
  137. {
  138. }
  139. void AreaBlenderComponent::SetupDependencies()
  140. {
  141. m_dependencyMonitor.Reset();
  142. m_dependencyMonitor.ConnectOwner(GetEntityId());
  143. m_dependencyMonitor.ConnectDependencies(m_configuration.m_vegetationAreaIds);
  144. }
  145. void AreaBlenderComponent::Activate()
  146. {
  147. // remove all invalid area IDs or self from the operations list
  148. m_configuration.m_vegetationAreaIds.erase(
  149. AZStd::remove_if(
  150. m_configuration.m_vegetationAreaIds.begin(),
  151. m_configuration.m_vegetationAreaIds.end(),
  152. [this](const auto& id) {return !id.IsValid() || id == GetEntityId();}),
  153. m_configuration.m_vegetationAreaIds.end());
  154. for (const auto& id : m_configuration.m_vegetationAreaIds)
  155. {
  156. AreaSystemRequestBus::Broadcast(&AreaSystemRequestBus::Events::MuteArea, id);
  157. }
  158. SetupDependencies();
  159. AreaComponentBase::Activate(); //must activate base last to connect AreaRequestBus once everything else is setup
  160. AreaBlenderRequestBus::Handler::BusConnect(GetEntityId());
  161. }
  162. void AreaBlenderComponent::Deactivate()
  163. {
  164. AreaComponentBase::Deactivate(); //must deactivate base first to ensure AreaRequestBus disconnect waits for other threads
  165. m_dependencyMonitor.Reset();
  166. for (const auto& id : m_configuration.m_vegetationAreaIds)
  167. {
  168. AreaSystemRequestBus::Broadcast(&AreaSystemRequestBus::Events::UnmuteArea, id);
  169. }
  170. AreaBlenderRequestBus::Handler::BusDisconnect();
  171. }
  172. bool AreaBlenderComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  173. {
  174. AreaComponentBase::ReadInConfig(baseConfig);
  175. if (auto config = azrtti_cast<const AreaBlenderConfig*>(baseConfig))
  176. {
  177. m_configuration = *config;
  178. return true;
  179. }
  180. return false;
  181. }
  182. bool AreaBlenderComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  183. {
  184. AreaComponentBase::WriteOutConfig(outBaseConfig);
  185. if (auto config = azrtti_cast<AreaBlenderConfig*>(outBaseConfig))
  186. {
  187. *config = m_configuration;
  188. return true;
  189. }
  190. return false;
  191. }
  192. bool AreaBlenderComponent::PrepareToClaim(EntityIdStack& stackIds)
  193. {
  194. AZ_PROFILE_FUNCTION(Vegetation);
  195. bool result = true;
  196. AZ_ErrorOnce("Vegetation", !AreaRequestBus::HasReentrantEBusUseThisThread(),
  197. "Detected cyclic dependencies with vegetation entity references on entity '%s' (%s)", GetEntity()->GetName().c_str(),
  198. GetEntityId().ToString().c_str());
  199. if (!AreaRequestBus::HasReentrantEBusUseThisThread())
  200. {
  201. //build a "modifier stack" of contributing entity ids considering inherit and propagate flags
  202. EntityIdStack emptyIds;
  203. EntityIdStack& processedIds = m_configuration.m_inheritBehavior && m_configuration.m_propagateBehavior ? stackIds : emptyIds;
  204. EntityIdStackPusher stackPusher(processedIds, m_configuration.m_propagateBehavior ? GetEntityId() : AZ::EntityId());
  205. for (const auto& entityId : m_configuration.m_vegetationAreaIds)
  206. {
  207. if (AreaNotificationBus::GetNumOfEventHandlers(entityId) == 0) // hidden areas are deactivated -> disconnected from the bus.
  208. {
  209. continue;
  210. }
  211. bool prepared = false;
  212. AreaNotificationBus::Event(entityId, &AreaNotificationBus::Events::OnAreaConnect);
  213. AreaRequestBus::EventResult(prepared, entityId, &AreaRequestBus::Events::PrepareToClaim, processedIds);
  214. AreaNotificationBus::Event(entityId, &AreaNotificationBus::Events::OnAreaDisconnect);
  215. if (!prepared)
  216. {
  217. result = false;
  218. break;
  219. }
  220. }
  221. }
  222. return result;
  223. }
  224. void AreaBlenderComponent::ClaimPositions(EntityIdStack& stackIds, ClaimContext& context)
  225. {
  226. AZ_PROFILE_FUNCTION(Vegetation);
  227. if (context.m_availablePoints.empty())
  228. {
  229. return;
  230. }
  231. AZ_ErrorOnce(
  232. "Vegetation", !AreaRequestBus::HasReentrantEBusUseThisThread(),
  233. "Detected cyclic dependencies with vegetation entity references on entity '%s' (%s)", GetEntity()->GetName().c_str(),
  234. GetEntityId().ToString().c_str());
  235. if (!AreaRequestBus::HasReentrantEBusUseThisThread())
  236. {
  237. //build a "modifier stack" of contributing entity ids considering inherit and propagate flags
  238. EntityIdStack emptyIds;
  239. EntityIdStack& processedIds = m_configuration.m_inheritBehavior && m_configuration.m_propagateBehavior ? stackIds : emptyIds;
  240. EntityIdStackPusher stackPusher(processedIds, m_configuration.m_propagateBehavior ? GetEntityId() : AZ::EntityId());
  241. for (const auto& entityId : m_configuration.m_vegetationAreaIds)
  242. {
  243. VEG_PROFILE_METHOD(DebugNotificationBus::TryQueueBroadcast(&DebugNotificationBus::Events::FillAreaStart, entityId, AZStd::chrono::steady_clock::now()));
  244. if (context.m_availablePoints.empty())
  245. {
  246. break;
  247. }
  248. AreaNotificationBus::Event(entityId, &AreaNotificationBus::Events::OnAreaConnect);
  249. AreaRequestBus::Event(entityId, &AreaRequestBus::Events::ClaimPositions, processedIds, context);
  250. AreaNotificationBus::Event(entityId, &AreaNotificationBus::Events::OnAreaDisconnect);
  251. VEG_PROFILE_METHOD(DebugNotificationBus::TryQueueBroadcast(&DebugNotificationBus::Events::FillAreaEnd, entityId, AZStd::chrono::steady_clock::now(), aznumeric_cast<AZ::u32>(context.m_availablePoints.size())));
  252. }
  253. }
  254. }
  255. void AreaBlenderComponent::UnclaimPosition(const ClaimHandle handle)
  256. {
  257. AZ_PROFILE_FUNCTION(Vegetation);
  258. AZ_ErrorOnce(
  259. "Vegetation", !AreaRequestBus::HasReentrantEBusUseThisThread(),
  260. "Detected cyclic dependencies with vegetation entity references on entity '%s' (%s)", GetEntity()->GetName().c_str(),
  261. GetEntityId().ToString().c_str());
  262. if (!AreaRequestBus::HasReentrantEBusUseThisThread())
  263. {
  264. for (const auto& entityId : m_configuration.m_vegetationAreaIds)
  265. {
  266. AreaNotificationBus::Event(entityId, &AreaNotificationBus::Events::OnAreaConnect);
  267. AreaRequestBus::Event(entityId, &AreaRequestBus::Events::UnclaimPosition, handle);
  268. AreaNotificationBus::Event(entityId, &AreaNotificationBus::Events::OnAreaDisconnect);
  269. }
  270. }
  271. }
  272. AZ::Aabb AreaBlenderComponent::GetEncompassingAabb() const
  273. {
  274. AZ_PROFILE_FUNCTION(Vegetation);
  275. AZ::Aabb bounds = AZ::Aabb::CreateNull();
  276. if (m_configuration.m_propagateBehavior)
  277. {
  278. LmbrCentral::ShapeComponentRequestsBus::EventResult(bounds, GetEntityId(), &LmbrCentral::ShapeComponentRequestsBus::Events::GetEncompassingAabb);
  279. }
  280. AZ_ErrorOnce(
  281. "Vegetation", !AreaInfoBus::HasReentrantEBusUseThisThread(),
  282. "Detected cyclic dependencies with vegetation entity references on entity '%s' (%s)", GetEntity()->GetName().c_str(),
  283. GetEntityId().ToString().c_str());
  284. if (!AreaInfoBus::HasReentrantEBusUseThisThread())
  285. {
  286. for (const auto& entityId : m_configuration.m_vegetationAreaIds)
  287. {
  288. if (entityId != GetEntityId())
  289. {
  290. AZ::Aabb operationBounds = AZ::Aabb::CreateNull();
  291. AreaInfoBus::EventResult(operationBounds, entityId, &AreaInfoBus::Events::GetEncompassingAabb);
  292. bounds.AddAabb(operationBounds);
  293. }
  294. }
  295. }
  296. return bounds;
  297. }
  298. AZ::u32 AreaBlenderComponent::GetProductCount() const
  299. {
  300. AZ_PROFILE_FUNCTION(Vegetation);
  301. AZ::u32 count = 0;
  302. AZ_ErrorOnce(
  303. "Vegetation", !AreaInfoBus::HasReentrantEBusUseThisThread(),
  304. "Detected cyclic dependencies with vegetation entity references on entity '%s' (%s)", GetEntity()->GetName().c_str(),
  305. GetEntityId().ToString().c_str());
  306. if (!AreaInfoBus::HasReentrantEBusUseThisThread())
  307. {
  308. for (const auto& entityId : m_configuration.m_vegetationAreaIds)
  309. {
  310. if (entityId != GetEntityId())
  311. {
  312. AZ::u32 operationCount = 0;
  313. AreaInfoBus::EventResult(operationCount, entityId, &AreaInfoBus::Events::GetProductCount);
  314. count += operationCount;
  315. }
  316. }
  317. }
  318. return count;
  319. }
  320. AZ::u32 AreaBlenderComponent::GetAreaPriority() const
  321. {
  322. return m_configuration.m_priority;
  323. }
  324. void AreaBlenderComponent::SetAreaPriority(AZ::u32 priority)
  325. {
  326. m_configuration.m_priority = priority;
  327. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  328. }
  329. AZ::u32 AreaBlenderComponent::GetAreaLayer() const
  330. {
  331. return m_configuration.m_layer;
  332. }
  333. void AreaBlenderComponent::SetAreaLayer(AZ::u32 layer)
  334. {
  335. m_configuration.m_layer = layer;
  336. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  337. }
  338. AZ::u32 AreaBlenderComponent::GetAreaProductCount() const
  339. {
  340. return GetProductCount();
  341. }
  342. bool AreaBlenderComponent::GetInheritBehavior() const
  343. {
  344. return m_configuration.m_inheritBehavior;
  345. }
  346. void AreaBlenderComponent::SetInheritBehavior(bool value)
  347. {
  348. m_configuration.m_inheritBehavior = value;
  349. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  350. }
  351. bool AreaBlenderComponent::GetPropagateBehavior() const
  352. {
  353. return m_configuration.m_propagateBehavior;
  354. }
  355. void AreaBlenderComponent::SetPropagateBehavior(bool value)
  356. {
  357. m_configuration.m_propagateBehavior = value;
  358. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  359. }
  360. size_t AreaBlenderComponent::GetNumAreas() const
  361. {
  362. return m_configuration.GetNumAreas();
  363. }
  364. AZ::EntityId AreaBlenderComponent::GetAreaEntityId(int index) const
  365. {
  366. return m_configuration.GetAreaEntityId(index);
  367. }
  368. void AreaBlenderComponent::RemoveAreaEntityId(int index)
  369. {
  370. m_configuration.RemoveAreaEntityId(index);
  371. SetupDependencies();
  372. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  373. }
  374. void AreaBlenderComponent::AddAreaEntityId(AZ::EntityId entityId)
  375. {
  376. m_configuration.AddAreaEntityId(entityId);
  377. SetupDependencies();
  378. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  379. }
  380. }