VegetationComponentOperationTests.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 "VegetationTest.h"
  9. #include "VegetationMocks.h"
  10. #include <AzTest/AzTest.h>
  11. #include <AzCore/Component/Entity.h>
  12. #include <Source/Components/AreaBlenderComponent.h>
  13. #include <Source/Components/BlockerComponent.h>
  14. #include <Source/Components/MeshBlockerComponent.h>
  15. #include <Source/Components/SpawnerComponent.h>
  16. #include <Source/InstanceSystemComponent.h>
  17. #include <Source/DebugSystemComponent.h>
  18. #include <Source/Debugger/AreaDebugComponent.h>
  19. #include <Vegetation/EmptyInstanceSpawner.h>
  20. #include <AzCore/Component/TickBus.h>
  21. namespace UnitTest
  22. {
  23. struct MockDescriptorProvider
  24. : public Vegetation::DescriptorProviderRequestBus::Handler
  25. {
  26. AZStd::vector<Vegetation::DescriptorPtr> m_descriptors;
  27. MockMeshAsset mockMeshAssetData;
  28. MockDescriptorProvider(size_t count)
  29. {
  30. for (size_t i = 0; i < count; ++i)
  31. {
  32. m_descriptors.emplace_back(CreateDescriptor(i));
  33. }
  34. }
  35. Vegetation::DescriptorPtr CreateDescriptor([[maybe_unused]] size_t id)
  36. {
  37. Vegetation::Descriptor descriptor;
  38. auto instanceSpawner = AZStd::make_shared<Vegetation::EmptyInstanceSpawner>();
  39. descriptor.SetInstanceSpawner(instanceSpawner);
  40. Vegetation::DescriptorPtr descriptorPtr;
  41. Vegetation::InstanceSystemRequestBus::BroadcastResult(descriptorPtr, &Vegetation::InstanceSystemRequestBus::Events::RegisterUniqueDescriptor, descriptor);
  42. return descriptorPtr;
  43. }
  44. void Clear()
  45. {
  46. for (auto& descriptorPtr : m_descriptors)
  47. {
  48. Vegetation::InstanceSystemRequestBus::Broadcast(&Vegetation::InstanceSystemRequestBus::Events::ReleaseUniqueDescriptor, descriptorPtr);
  49. }
  50. m_descriptors.clear();
  51. }
  52. void GetDescriptors(Vegetation::DescriptorPtrVec& descriptors) const override
  53. {
  54. descriptors = m_descriptors;
  55. }
  56. };
  57. struct VegetationComponentOperationTests
  58. : public VegetationComponentTests
  59. {
  60. void RegisterComponentDescriptors() override
  61. {
  62. m_app.RegisterComponentDescriptor(MockShapeServiceComponent::CreateDescriptor());
  63. m_app.RegisterComponentDescriptor(MockVegetationAreaServiceComponent::CreateDescriptor());
  64. m_app.RegisterComponentDescriptor(MockMeshServiceComponent::CreateDescriptor());
  65. m_app.RegisterComponentDescriptor(Vegetation::InstanceSystemComponent::CreateDescriptor());
  66. m_app.RegisterComponentDescriptor(Vegetation::DebugSystemComponent::CreateDescriptor());
  67. m_app.RegisterComponentDescriptor(Vegetation::AreaDebugComponent::CreateDescriptor());
  68. }
  69. void BasicAreaTests(const AZ::EntityId& areaId)
  70. {
  71. AZ::u32 priority = std::numeric_limits<AZ::u32>::max();
  72. Vegetation::AreaInfoBus::EventResult(priority, areaId, &Vegetation::AreaInfoBus::Events::GetPriority);
  73. EXPECT_NE(priority, std::numeric_limits<AZ::u32>::max());
  74. AZ::u32 layer = std::numeric_limits<AZ::u32>::max();
  75. Vegetation::AreaInfoBus::EventResult(layer, areaId, &Vegetation::AreaInfoBus::Events::GetLayer);
  76. EXPECT_NE(layer, std::numeric_limits<AZ::u32>::max());
  77. auto aabb = AZ::Aabb::CreateNull();
  78. Vegetation::AreaInfoBus::EventResult(aabb, areaId, &Vegetation::AreaInfoBus::Events::GetEncompassingAabb);
  79. EXPECT_TRUE(aabb.IsValid());
  80. // with area not connected
  81. {
  82. Vegetation::AreaNotificationBus::Event(areaId, &Vegetation::AreaNotificationBus::Events::OnAreaDisconnect);
  83. size_t numHandlers = Vegetation::AreaRequestBus::GetNumOfEventHandlers(areaId);
  84. EXPECT_EQ(numHandlers, 0);
  85. }
  86. // now with area connected
  87. {
  88. Vegetation::AreaNotificationBus::Event(areaId, &Vegetation::AreaNotificationBus::Events::OnAreaConnect);
  89. size_t numHandlers = Vegetation::AreaRequestBus::GetNumOfEventHandlers(areaId);
  90. EXPECT_EQ(numHandlers, 1);
  91. }
  92. }
  93. void ConnectToAreaBuses(AZ::Entity& entity)
  94. {
  95. if (!m_connected)
  96. {
  97. entity.Deactivate();
  98. m_mockMeshRequestBus.BusConnect(entity.GetId());
  99. m_mockTransformBus.BusConnect(entity.GetId());
  100. m_mockShapeBus.BusConnect(entity.GetId());
  101. entity.Activate();
  102. BasicAreaTests(entity.GetId());
  103. m_connected = true;
  104. }
  105. }
  106. void ReleaseFromAreaBuses()
  107. {
  108. if (m_connected)
  109. {
  110. m_mockMeshRequestBus.m_GetMeshAssetOutput.Reset();
  111. m_mockMeshRequestBus.BusDisconnect();
  112. m_mockTransformBus.BusDisconnect();
  113. m_mockShapeBus.BusDisconnect();
  114. m_connected = false;
  115. }
  116. }
  117. AZ::Data::Asset<MockMeshAsset> CreateMockMeshAsset()
  118. {
  119. m_mockMeshAssetData = new MockMeshAsset();
  120. AZ::Data::Asset<MockMeshAsset> mockAsset(m_mockMeshAssetData, AZ::Data::AssetLoadBehavior::Default);
  121. return mockAsset;
  122. }
  123. void DestroyMockMeshAsset(AZ::Data::Asset<MockMeshAsset>& mockAsset)
  124. {
  125. if (m_mockMeshAssetData)
  126. {
  127. mockAsset.Reset();
  128. delete m_mockMeshAssetData;
  129. m_mockMeshAssetData = nullptr;
  130. }
  131. }
  132. AZStd::unique_ptr<AZ::Entity> CreateMockMeshEntity(const AZ::Data::Asset<MockMeshAsset>& mockAsset, AZ::Vector3 position,
  133. AZ::Vector3 aabbMin, AZ::Vector3 aabbMax, float meshPercentMin, float meshPercentMax)
  134. {
  135. m_mockMeshRequestBus.m_GetMeshAssetOutput = mockAsset;
  136. m_mockMeshRequestBus.m_GetWorldBoundsOutput = AZ::Aabb::CreateFromMinMax(aabbMin, aabbMax);
  137. m_mockMeshRequestBus.m_GetVisibilityOutput = true;
  138. m_mockTransformBus.m_GetWorldTMOutput = AZ::Transform::CreateTranslation(position);
  139. Vegetation::MeshBlockerConfig config;
  140. config.m_blockWhenInvisible = true;
  141. config.m_priority = 2;
  142. config.m_meshHeightPercentMin = meshPercentMin;
  143. config.m_meshHeightPercentMax = meshPercentMax;
  144. Vegetation::MeshBlockerComponent* component = nullptr;
  145. auto entity = CreateEntity(config, &component, [](AZ::Entity* e)
  146. {
  147. e->CreateComponent<MockMeshServiceComponent>();
  148. });
  149. return entity;
  150. }
  151. void TestMeshBlockerPoint(AZStd::unique_ptr<AZ::Entity>& meshBlockerEntity, AZ::Vector3 testPoint, int numPointsBlocked)
  152. {
  153. AreaBusScope scope(*this, *meshBlockerEntity.get());
  154. Vegetation::AreaNotificationBus::Event(meshBlockerEntity->GetId(), &Vegetation::AreaNotificationBus::Events::OnAreaConnect);
  155. bool prepared = false;
  156. Vegetation::EntityIdStack idStack;
  157. Vegetation::AreaRequestBus::EventResult(prepared, meshBlockerEntity->GetId(), &Vegetation::AreaRequestBus::Events::PrepareToClaim, idStack);
  158. EXPECT_TRUE(prepared);
  159. Vegetation::ClaimContext context = CreateContext<1>({ testPoint });
  160. Vegetation::AreaRequestBus::Event(meshBlockerEntity->GetId(), &Vegetation::AreaRequestBus::Events::ClaimPositions, idStack, context);
  161. EXPECT_EQ(numPointsBlocked, m_createdCallbackCount);
  162. Vegetation::AreaNotificationBus::Event(meshBlockerEntity->GetId(), &Vegetation::AreaNotificationBus::Events::OnAreaDisconnect);
  163. }
  164. struct AreaBusScope
  165. {
  166. VegetationComponentOperationTests& m_block;
  167. AreaBusScope(VegetationComponentOperationTests& block, AZ::Entity& entity)
  168. : m_block(block)
  169. {
  170. m_block.ConnectToAreaBuses(entity);
  171. }
  172. ~AreaBusScope()
  173. {
  174. m_block.ReleaseFromAreaBuses();
  175. }
  176. };
  177. bool m_connected = false;
  178. MockMeshRequestBus m_mockMeshRequestBus;
  179. MockTransformBus m_mockTransformBus;
  180. MockShapeComponentNotificationsBus m_mockShapeBus;
  181. MockMeshAsset* m_mockMeshAssetData;
  182. };
  183. #if AZ_TRAIT_DISABLE_FAILED_VEGETATION_TESTS
  184. TEST_F(VegetationComponentOperationTests, DISABLED_MeshBlockerComponent)
  185. #else
  186. TEST_F(VegetationComponentOperationTests, MeshBlockerComponent)
  187. #endif // AZ_TRAIT_DISABLE_FAILED_VEGETATION_TESTS
  188. {
  189. // Create a mock mesh blocker at (0, 0, 0) that extends from (-1, -1, -1) to (1, 1, 1)
  190. auto mockAsset = CreateMockMeshAsset();
  191. auto entity = CreateMockMeshEntity(mockAsset, AZ::Vector3::CreateZero(),
  192. AZ::Vector3(-1.0f), AZ::Vector3(1.0f), 0.0f, 1.0f);
  193. // Test the point at (0, 0, 0). It should be blocked.
  194. const int numPointsBlocked = 1;
  195. TestMeshBlockerPoint(entity, AZ::Vector3::CreateZero(), numPointsBlocked);
  196. // Have to destroy the entity here before we destroy the mock mesh asset
  197. DestroyEntity(entity.release());
  198. DestroyMockMeshAsset(mockAsset);
  199. }
  200. TEST_F(VegetationComponentOperationTests, LY96037_MeshBlockerIntersectionShouldUseZAxis)
  201. {
  202. // Create a mock mesh blocker at (0, 0, 0) that extends from (-1, -1, -1) to (1, 10, 1)
  203. auto mockAsset = CreateMockMeshAsset();
  204. auto entity = CreateMockMeshEntity(mockAsset, AZ::Vector3::CreateZero(),
  205. AZ::Vector3(-1.0f, -1.0f, -1.0f), AZ::Vector3(1.0f, 10.0f, 1.0f),
  206. 0.0f, 1.0f);
  207. // Test the point at (0.5, 0.5, 2.0). It should *not* be blocked.
  208. // Bug LY96037 was that the Y axis is used for height instead of Z, which would cause the point to be blocked.
  209. // That would make this test fail.
  210. const int numPointsBlocked = 0;
  211. TestMeshBlockerPoint(entity, AZ::Vector3(0.5f, 0.5f, 2.0f), numPointsBlocked);
  212. // Have to destroy the entity here before we destroy the mock mesh asset
  213. DestroyEntity(entity.release());
  214. DestroyMockMeshAsset(mockAsset);
  215. }
  216. #if AZ_TRAIT_DISABLE_FAILED_VEGETATION_TESTS
  217. TEST_F(VegetationComponentOperationTests, DISABLED_LY96068_MeshBlockerHandlesChangedClaimPoints)
  218. #else
  219. TEST_F(VegetationComponentOperationTests, LY96068_MeshBlockerHandlesChangedClaimPoints)
  220. #endif // AZ_TRAIT_DISABLE_FAILED_VEGETATION_TESTS
  221. {
  222. // Create a mock mesh blocker at (0, 0, 0) that extends from (-1, -1, -1) to (1, 1, 1)
  223. auto mockAsset = CreateMockMeshAsset();
  224. auto entity = CreateMockMeshEntity(mockAsset, AZ::Vector3::CreateZero(),
  225. AZ::Vector3(-1.0f, -1.0f, -1.0f), AZ::Vector3(1.0f, 1.0f, 1.0f),
  226. 0.0f, 1.0f);
  227. {
  228. // Putting this into scope so that the AreaBus disconnects before we do any destruction of entities/assets at the end
  229. AreaBusScope scope(*this, *entity.get());
  230. Vegetation::AreaNotificationBus::Event(entity->GetId(), &Vegetation::AreaNotificationBus::Events::OnAreaConnect);
  231. bool prepared = false;
  232. Vegetation::EntityIdStack idStack;
  233. Vegetation::AreaRequestBus::EventResult(prepared, entity->GetId(), &Vegetation::AreaRequestBus::Events::PrepareToClaim, idStack);
  234. EXPECT_TRUE(prepared);
  235. // Create two different contexts that "reuse" the same claim handle for different points.
  236. // The first one has a point at (0, 0, 0) that will be successfully blocked.
  237. // The second one has a point at (2, 2, 2) that should *not* be successfully blocked.
  238. // Bug LY96068 is that claim handles that change location don't refresh correctly in the Mesh Blocker component.
  239. AZ::Vector3 claimPosition1 = AZ::Vector3::CreateZero();
  240. AZ::Vector3 claimPosition2 = AZ::Vector3(2.0f);
  241. Vegetation::ClaimContext context = CreateContext<1>({ claimPosition1 });
  242. Vegetation::ClaimContext contextWithReusedHandle = CreateContext<1>({ claimPosition2 });
  243. contextWithReusedHandle.m_availablePoints[0].m_handle = context.m_availablePoints[0].m_handle;
  244. // The first time we try with this claim handler, the point should be claimed by the MeshBlocker.
  245. Vegetation::AreaRequestBus::Event(entity->GetId(), &Vegetation::AreaRequestBus::Events::ClaimPositions, idStack, context);
  246. EXPECT_EQ(1, m_createdCallbackCount);
  247. // Clear out our results
  248. m_createdCallbackCount = 0;
  249. // Send out a "surface changed" notification, as will as a tick bus tick, to give our mesh blocker a chance to refresh.
  250. SurfaceData::SurfaceDataSystemNotificationBus::Broadcast(&SurfaceData::SurfaceDataSystemNotificationBus::Events::OnSurfaceChanged, entity->GetId(),
  251. AZ::Aabb::CreateFromPoint(claimPosition1), AZ::Aabb::CreateFromPoint(claimPosition2), SurfaceData::SurfaceTagSet());
  252. AZ::TickBus::Broadcast(&AZ::TickBus::Events::OnTick, 0.f, AZ::ScriptTimePoint{});
  253. // Try claiming again, this time with the same claim handle, but a different location.
  254. // This should *not* be claimed by the MeshBlocker.
  255. Vegetation::AreaRequestBus::Event(entity->GetId(), &Vegetation::AreaRequestBus::Events::ClaimPositions, idStack, contextWithReusedHandle);
  256. EXPECT_EQ(0, m_createdCallbackCount);
  257. Vegetation::AreaNotificationBus::Event(entity->GetId(), &Vegetation::AreaNotificationBus::Events::OnAreaDisconnect);
  258. }
  259. // Have to destroy the entity here before we destroy the mock mesh asset
  260. DestroyEntity(entity.release());
  261. DestroyMockMeshAsset(mockAsset);
  262. }
  263. TEST_F(VegetationComponentOperationTests, SpawnerComponent)
  264. {
  265. m_mockShapeBus.m_aabb = AZ::Aabb::CreateCenterRadius(AZ::Vector3::CreateZero(), AZ::Constants::FloatMax);
  266. //need dummy system component to track instance and task stats
  267. Vegetation::InstanceSystemConfig instanceSystemConfig;
  268. Vegetation::InstanceSystemComponent* instanceSystemComponent = nullptr;
  269. auto instanceSystemEntity = CreateEntity(instanceSystemConfig, &instanceSystemComponent, [](AZ::Entity* e)
  270. {
  271. e->CreateComponent<Vegetation::DebugSystemComponent>();
  272. });
  273. //need spawner to generate instances
  274. Vegetation::SpawnerConfig config;
  275. Vegetation::SpawnerComponent* component = nullptr;
  276. auto entity = CreateEntity(config, &component, [](AZ::Entity* e)
  277. {
  278. e->CreateComponent<MockShapeServiceComponent>();
  279. });
  280. AreaBusScope scope(*this, *entity.get());
  281. //need mock descriptor provider to give spawner content to generate
  282. MockDescriptorProvider mockDescriptorProviderBus(8);
  283. mockDescriptorProviderBus.BusConnect(entity->GetId());
  284. //connect the spawner for claim requests
  285. Vegetation::AreaNotificationBus::Event(entity->GetId(), &Vegetation::AreaNotificationBus::Events::OnAreaConnect);
  286. bool prepared = false;
  287. Vegetation::EntityIdStack idStack;
  288. Vegetation::AreaRequestBus::EventResult(prepared, entity->GetId(), &Vegetation::AreaRequestBus::Events::PrepareToClaim, idStack);
  289. EXPECT_TRUE(prepared);
  290. //spawn 32 instances
  291. Vegetation::ClaimContext context = CreateContext<32>({ AZ::Vector3(0, 0, 0) });
  292. const Vegetation::ClaimHandle theClaimHandle = context.m_availablePoints[0].m_handle;
  293. Vegetation::AreaRequestBus::Event(entity->GetId(), &Vegetation::AreaRequestBus::Events::ClaimPositions, idStack, context);
  294. AZ::u32 createTaskCount = 0;
  295. Vegetation::InstanceSystemStatsRequestBus::BroadcastResult(createTaskCount, &Vegetation::InstanceSystemStatsRequestBus::Events::GetCreateTaskCount);
  296. EXPECT_EQ(createTaskCount, 32);
  297. //destroy the first instance
  298. Vegetation::AreaRequestBus::Event(entity->GetId(), &Vegetation::AreaRequestBus::Events::UnclaimPosition, theClaimHandle);
  299. AZ::u32 destroyTaskCount = 0;
  300. Vegetation::InstanceSystemStatsRequestBus::BroadcastResult(destroyTaskCount, &Vegetation::InstanceSystemStatsRequestBus::Events::GetDestroyTaskCount);
  301. EXPECT_EQ(destroyTaskCount, 1);
  302. //disconnect the spawner from claim requests
  303. Vegetation::AreaNotificationBus::Event(entity->GetId(), &Vegetation::AreaNotificationBus::Events::OnAreaDisconnect);
  304. //destroy all instances and queued tasks
  305. Vegetation::InstanceSystemRequestBus::Broadcast(&Vegetation::InstanceSystemRequestBus::Events::DestroyAllInstances);
  306. //verify tasks amd instances are cleared
  307. Vegetation::InstanceSystemStatsRequestBus::BroadcastResult(createTaskCount, &Vegetation::InstanceSystemStatsRequestBus::Events::GetCreateTaskCount);
  308. EXPECT_EQ(createTaskCount, 0);
  309. Vegetation::InstanceSystemStatsRequestBus::BroadcastResult(destroyTaskCount, &Vegetation::InstanceSystemStatsRequestBus::Events::GetDestroyTaskCount);
  310. EXPECT_EQ(destroyTaskCount, 0);
  311. //note: no instances were created because the tick bus did not execute and the test does not setup required engine and renderer systems
  312. AZ::u32 instanceCount = 0;
  313. Vegetation::InstanceSystemStatsRequestBus::BroadcastResult(instanceCount, &Vegetation::InstanceSystemStatsRequestBus::Events::GetInstanceCount);
  314. EXPECT_EQ(instanceCount, 0);
  315. mockDescriptorProviderBus.Clear();
  316. mockDescriptorProviderBus.BusDisconnect();
  317. }
  318. TEST_F(VegetationComponentOperationTests, AreaBlenderComponent)
  319. {
  320. auto entityBlocker = CreateEntity<Vegetation::BlockerComponent>(Vegetation::BlockerConfig(), nullptr, [](AZ::Entity* e)
  321. {
  322. e->CreateComponent<MockShapeServiceComponent>();
  323. });
  324. MockMeshRequestBus mockMeshRequestBusForBlocker;
  325. mockMeshRequestBusForBlocker.m_GetWorldBoundsOutput = AZ::Aabb::CreateCenterRadius(AZ::Vector3::CreateZero(), AZ::Constants::FloatMax);
  326. mockMeshRequestBusForBlocker.m_GetVisibilityOutput = true;
  327. mockMeshRequestBusForBlocker.BusConnect(entityBlocker->GetId());
  328. MockTransformBus mockTransformBusForBlocker;
  329. mockTransformBusForBlocker.m_GetWorldTMOutput = AZ::Transform::CreateTranslation(AZ::Vector3::CreateZero());
  330. mockMeshRequestBusForBlocker.BusConnect(entityBlocker->GetId());
  331. MockShapeComponentNotificationsBus mockShapeBusForBlocker;
  332. mockShapeBusForBlocker.m_aabb = AZ::Aabb::CreateCenterRadius(AZ::Vector3::CreateZero(), AZ::Constants::FloatMax);
  333. mockMeshRequestBusForBlocker.BusConnect(entityBlocker->GetId());
  334. Vegetation::AreaBlenderConfig config;
  335. config.m_vegetationAreaIds.push_back(entityBlocker->GetId());
  336. Vegetation::AreaBlenderComponent* component = nullptr;
  337. auto entity = CreateEntity(config, &component, [](AZ::Entity* e)
  338. {
  339. e->CreateComponent<MockShapeServiceComponent>();
  340. });
  341. AreaBusScope scope(*this, *entity.get());
  342. Vegetation::AreaNotificationBus::Event(entity->GetId(), &Vegetation::AreaNotificationBus::Events::OnAreaConnect);
  343. bool prepared = false;
  344. Vegetation::EntityIdStack idStack;
  345. Vegetation::AreaRequestBus::EventResult(prepared, entity->GetId(), &Vegetation::AreaRequestBus::Events::PrepareToClaim, idStack);
  346. EXPECT_TRUE(prepared);
  347. Vegetation::ClaimContext context = CreateContext<32>({ AZ::Vector3(0, 0, 0) });
  348. const auto previousPointsCount = context.m_availablePoints.size();
  349. Vegetation::AreaRequestBus::Event(entity->GetId(), &Vegetation::AreaRequestBus::Events::ClaimPositions, idStack, context);
  350. EXPECT_NE(previousPointsCount, context.m_availablePoints.size());
  351. Vegetation::AreaNotificationBus::Event(entity->GetId(), &Vegetation::AreaNotificationBus::Events::OnAreaDisconnect);
  352. mockMeshRequestBusForBlocker.BusDisconnect();
  353. mockMeshRequestBusForBlocker.BusDisconnect();
  354. mockMeshRequestBusForBlocker.BusDisconnect();
  355. }
  356. TEST_F(VegetationComponentOperationTests, BlockerComponent)
  357. {
  358. m_mockMeshRequestBus.m_GetWorldBoundsOutput = AZ::Aabb::CreateCenterRadius(AZ::Vector3::CreateZero(), AZ::Constants::FloatMax);
  359. m_mockMeshRequestBus.m_GetVisibilityOutput = true;
  360. m_mockTransformBus.m_GetWorldTMOutput = AZ::Transform::CreateTranslation(AZ::Vector3::CreateZero());
  361. m_mockShapeBus.m_aabb = AZ::Aabb::CreateCenterRadius(AZ::Vector3::CreateZero(), AZ::Constants::FloatMax);
  362. Vegetation::BlockerConfig config;
  363. config.m_inheritBehavior = false;
  364. Vegetation::BlockerComponent* component = nullptr;
  365. auto entity = CreateEntity(config, &component, [](AZ::Entity* e)
  366. {
  367. e->CreateComponent<MockShapeServiceComponent>();
  368. });
  369. AreaBusScope scope(*this, *entity.get());
  370. Vegetation::AreaNotificationBus::Event(entity->GetId(), &Vegetation::AreaNotificationBus::Events::OnAreaConnect);
  371. bool prepared = false;
  372. Vegetation::EntityIdStack idStack;
  373. Vegetation::AreaRequestBus::EventResult(prepared, entity->GetId(), &Vegetation::AreaRequestBus::Events::PrepareToClaim, idStack);
  374. EXPECT_TRUE(prepared);
  375. Vegetation::EntityIdStack stack;
  376. Vegetation::ClaimContext claimContext = CreateContext<32>({ AZ::Vector3(0, 0, 0) });
  377. Vegetation::AreaRequestBus::Event(entity->GetId(), &Vegetation::AreaRequestBus::Events::ClaimPositions, idStack, claimContext);
  378. EXPECT_EQ(32, m_createdCallbackCount);
  379. EXPECT_TRUE(claimContext.m_availablePoints.empty());
  380. Vegetation::AreaNotificationBus::Event(entity->GetId(), &Vegetation::AreaNotificationBus::Events::OnAreaDisconnect);
  381. }
  382. TEST_F(VegetationComponentOperationTests, AreaDebugComponent)
  383. {
  384. m_mockShapeBus.m_aabb = AZ::Aabb::CreateCenterRadius(AZ::Vector3::CreateZero(), AZ::Constants::FloatMax);
  385. //define input and expected output colors
  386. const AZ::Color debugColor1(0.1f, 0.2f, 0.3f, 0.4f);
  387. const AZ::Color debugColor2(0.5f, 0.6f, 0.7f, 0.8f);
  388. const AZ::Color debugColorProduct(debugColor1 * debugColor2);
  389. //create spawner with debug component
  390. Vegetation::SpawnerConfig spawnerConfig;
  391. Vegetation::SpawnerComponent* spawnerComponent = nullptr;
  392. auto spawnerEntity = CreateEntity(spawnerConfig, &spawnerComponent, [&debugColor1](AZ::Entity* e)
  393. {
  394. Vegetation::AreaDebugConfig areaDebugConfig;
  395. areaDebugConfig.m_debugColor = debugColor1;
  396. e->CreateComponent<Vegetation::AreaDebugComponent>(areaDebugConfig);
  397. e->CreateComponent<MockShapeServiceComponent>();
  398. });
  399. AreaBusScope scope(*this, *spawnerEntity.get());
  400. //need mock descriptpor provider to give spawner content to generate
  401. MockDescriptorProvider mockDescriptorProviderBus(8);
  402. mockDescriptorProviderBus.BusConnect(spawnerEntity->GetId());
  403. //create blender that references spawner with debug component
  404. Vegetation::AreaBlenderConfig blenderConfig;
  405. blenderConfig.m_vegetationAreaIds.push_back(spawnerEntity->GetId());
  406. Vegetation::AreaBlenderComponent* blenderComponent = nullptr;
  407. auto blenderEntity = CreateEntity(blenderConfig, &blenderComponent, [&debugColor2](AZ::Entity* e)
  408. {
  409. Vegetation::AreaDebugConfig areaDebugConfig;
  410. areaDebugConfig.m_debugColor = debugColor2;
  411. e->CreateComponent<Vegetation::AreaDebugComponent>(areaDebugConfig);
  412. e->CreateComponent<MockShapeServiceComponent>();
  413. });
  414. //force blender and referenced spawner to prepare for placement, which recomputes blended debug color
  415. bool prepared = false;
  416. Vegetation::EntityIdStack idStack;
  417. Vegetation::AreaNotificationBus::Event(blenderEntity->GetId(), &Vegetation::AreaNotificationBus::Events::OnAreaConnect);
  418. Vegetation::AreaRequestBus::EventResult(prepared, blenderEntity->GetId(), &Vegetation::AreaRequestBus::Events::PrepareToClaim, idStack);
  419. Vegetation::AreaNotificationBus::Event(blenderEntity->GetId(), &Vegetation::AreaNotificationBus::Events::OnAreaDisconnect);
  420. EXPECT_TRUE(prepared);
  421. //verify expected debug data
  422. Vegetation::AreaDebugDisplayData areaDebugDisplayData;
  423. Vegetation::AreaDebugBus::EventResult(areaDebugDisplayData, spawnerEntity->GetId(), &Vegetation::AreaDebugBus::Events::GetBaseDebugDisplayData);
  424. EXPECT_EQ(areaDebugDisplayData.m_instanceColor, debugColor1);
  425. Vegetation::AreaDebugBus::EventResult(areaDebugDisplayData, spawnerEntity->GetId(), &Vegetation::AreaDebugBus::Events::GetBlendedDebugDisplayData);
  426. EXPECT_EQ(areaDebugDisplayData.m_instanceColor, debugColorProduct);
  427. Vegetation::AreaDebugBus::EventResult(areaDebugDisplayData, blenderEntity->GetId(), &Vegetation::AreaDebugBus::Events::GetBaseDebugDisplayData);
  428. EXPECT_EQ(areaDebugDisplayData.m_instanceColor, debugColor2);
  429. Vegetation::AreaDebugBus::EventResult(areaDebugDisplayData, blenderEntity->GetId(), &Vegetation::AreaDebugBus::Events::GetBlendedDebugDisplayData);
  430. EXPECT_EQ(areaDebugDisplayData.m_instanceColor, debugColor2);
  431. mockDescriptorProviderBus.Clear();
  432. mockDescriptorProviderBus.BusDisconnect();
  433. }
  434. }