VegetationComponentDescriptorTests.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <AzCore/Component/Entity.h>
  11. #include <AzTest/AzTest.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. #include <Source/Components/DescriptorListCombinerComponent.h>
  14. #include <Source/Components/DescriptorListComponent.h>
  15. #include <Source/Components/DescriptorWeightSelectorComponent.h>
  16. namespace UnitTest
  17. {
  18. struct VegetationComponentTestsDescriptor
  19. : public VegetationComponentTests
  20. {
  21. void RegisterComponentDescriptors() override
  22. {
  23. }
  24. AZStd::unique_ptr<AZ::Entity> CreateDescriptorProvider(size_t count)
  25. {
  26. Vegetation::DescriptorListConfig config;
  27. for (int i = 0; i < count; ++i)
  28. {
  29. Vegetation::Descriptor descriptor;
  30. config.m_descriptors.emplace_back(descriptor);
  31. }
  32. Vegetation::DescriptorListComponent* component = nullptr;
  33. auto entity = CreateEntity(config, &component);
  34. return entity;
  35. }
  36. };
  37. TEST_F(VegetationComponentTestsDescriptor, DescriptorListCombinerComponent)
  38. {
  39. MockDescriptorBus mockDescriptorBus;
  40. auto eProvider1 = CreateDescriptorProvider(2);
  41. auto eProvider2 = CreateDescriptorProvider(40);
  42. Vegetation::DescriptorListCombinerConfig config;
  43. config.m_descriptorProviders.push_back(eProvider1->GetId());
  44. config.m_descriptorProviders.push_back(eProvider2->GetId());
  45. Vegetation::DescriptorListCombinerComponent* component = nullptr;
  46. auto entity = CreateEntity(config, &component);
  47. Vegetation::DescriptorPtrVec descriptorCollection;
  48. Vegetation::DescriptorProviderRequestBus::Event(entity->GetId(), &Vegetation::DescriptorProviderRequestBus::Events::GetDescriptors, descriptorCollection);
  49. EXPECT_EQ(42, descriptorCollection.size());
  50. }
  51. TEST_F(VegetationComponentTestsDescriptor, DescriptorListComponent_External)
  52. {
  53. MockDescriptorBus mockDescriptorBus;
  54. Vegetation::Descriptor descriptor;
  55. descriptor.m_weight = 123.0f;
  56. Vegetation::DescriptorListAsset descriptorListAssetData;
  57. descriptorListAssetData.m_descriptors.push_back(descriptor);
  58. reinterpret_cast<MockAssetData*>(&descriptorListAssetData)->SetId(AZ::Uuid::CreateRandom());
  59. AZ::Data::Asset<Vegetation::DescriptorListAsset> descriptorListAsset(&descriptorListAssetData, AZ::Data::AssetLoadBehavior::Default);
  60. Vegetation::DescriptorListConfig config;
  61. config.m_sourceType = Vegetation::DescriptorListSourceType::EXTERNAL;
  62. config.m_descriptorListAsset = descriptorListAsset;
  63. Vegetation::DescriptorListComponent* component = nullptr;
  64. auto entity = CreateEntity(config, &component);
  65. component->OnAssetReady(descriptorListAsset);
  66. Vegetation::DescriptorPtrVec descriptorCollection;
  67. Vegetation::DescriptorProviderRequestBus::Event(entity->GetId(), &Vegetation::DescriptorProviderRequestBus::Events::GetDescriptors, descriptorCollection);
  68. EXPECT_EQ(1, descriptorCollection.size());
  69. // clear up memory before leaving
  70. reinterpret_cast<MockAsset<Vegetation::DescriptorListAsset>&>(descriptorListAsset).ClearData();
  71. mockDescriptorBus.m_descriptorSet.clear();
  72. }
  73. TEST_F(VegetationComponentTestsDescriptor, DescriptorListComponent_Embedded)
  74. {
  75. MockDescriptorBus mockDescriptorBus;
  76. Vegetation::Descriptor descriptor;
  77. descriptor.m_weight = 123.0f;
  78. Vegetation::DescriptorListConfig config;
  79. config.m_sourceType = Vegetation::DescriptorListSourceType::EMBEDDED;
  80. config.m_descriptors.emplace_back(descriptor);
  81. Vegetation::DescriptorListComponent* component = nullptr;
  82. auto entity = CreateEntity(config, &component);
  83. EXPECT_EQ(1, mockDescriptorBus.m_descriptorSet.size());
  84. Vegetation::DescriptorPtrVec descriptorCollection;
  85. Vegetation::DescriptorProviderRequestBus::Event(entity->GetId(), &Vegetation::DescriptorProviderRequestBus::Events::GetDescriptors, descriptorCollection);
  86. ASSERT_EQ(1, descriptorCollection.size());
  87. EXPECT_EQ(123.0f, descriptorCollection[0]->m_weight);
  88. // clear up memory before leaving
  89. mockDescriptorBus.m_descriptorSet.clear();
  90. }
  91. TEST_F(VegetationComponentTestsDescriptor, DescriptorWeightSelectorComponent)
  92. {
  93. MockGradientRequestHandler mockGradientRequestBusHandler;
  94. Vegetation::DescriptorWeightSelectorConfig config;
  95. config.m_sortBehavior = Vegetation::SortBehavior::Descending;
  96. config.m_gradientSampler.m_gradientId = mockGradientRequestBusHandler.m_entity.GetId();
  97. Vegetation::DescriptorWeightSelectorComponent* component = nullptr;
  98. auto entity = CreateEntity(config, &component);
  99. Vegetation::DescriptorSelectorParams descriptorSelectorParams;
  100. Vegetation::DescriptorPtrVec descriptors;
  101. for (int i = 1; i < 5; ++i)
  102. {
  103. auto descriptor = AZStd::make_shared<Vegetation::Descriptor>();
  104. descriptor->m_weight = i / 1000.0f;
  105. descriptors.emplace_back(descriptor);
  106. }
  107. Vegetation::DescriptorSelectorRequestBus::Event(
  108. entity->GetId(),
  109. &Vegetation::DescriptorSelectorRequestBus::Events::SelectDescriptors,
  110. descriptorSelectorParams,
  111. descriptors);
  112. EXPECT_GT(mockGradientRequestBusHandler.m_count, 0);
  113. ASSERT_EQ(4, descriptors.size());
  114. EXPECT_GT(descriptors[0]->m_weight, descriptors[1]->m_weight);
  115. EXPECT_GT(descriptors[1]->m_weight, descriptors[2]->m_weight);
  116. EXPECT_GT(descriptors[2]->m_weight, descriptors[3]->m_weight);
  117. }
  118. }