VegetationComponentModifierTests.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/UnitTest/TestTypes.h>
  12. #include <Source/Components/PositionModifierComponent.h>
  13. #include <Source/Components/RotationModifierComponent.h>
  14. #include <Source/Components/ScaleModifierComponent.h>
  15. #include <Source/Components/SlopeAlignmentModifierComponent.h>
  16. namespace UnitTest
  17. {
  18. struct VegetationComponentModifierTests
  19. : public VegetationComponentTests
  20. {
  21. Vegetation::InstanceData m_instanceData;
  22. void RegisterComponentDescriptors() override
  23. {
  24. m_app.RegisterComponentDescriptor(MockShapeServiceComponent::CreateDescriptor());
  25. m_app.RegisterComponentDescriptor(MockVegetationAreaServiceComponent::CreateDescriptor());
  26. m_app.RegisterComponentDescriptor(MockMeshServiceComponent::CreateDescriptor());
  27. }
  28. };
  29. TEST_F(VegetationComponentModifierTests, PositionModifierComponent)
  30. {
  31. const auto crcMask = AZ_CRC("mock-mask", 0xfdf99e32);
  32. Vegetation::InstanceData vegInstance;
  33. vegInstance.m_position = AZ::Vector3(2.0f, 4.0f, 0.0f);
  34. MockGradientRequestHandler gradient;
  35. gradient.m_defaultValue = 0.99f;
  36. Vegetation::PositionModifierConfig config;
  37. config.m_autoSnapToSurface = false;
  38. config.m_rangeMinX = -0.3f;
  39. config.m_rangeMaxX = 0.3f;
  40. config.m_gradientSamplerX.m_gradientId = gradient.m_entity.GetId();
  41. config.m_rangeMinY = -0.3f;
  42. config.m_rangeMaxY = 0.3f;
  43. config.m_gradientSamplerY.m_gradientId = gradient.m_entity.GetId();
  44. config.m_rangeMinZ = 0.0f;
  45. config.m_rangeMaxZ = 0.0f;
  46. config.m_gradientSamplerZ.m_gradientId = gradient.m_entity.GetId();
  47. Vegetation::PositionModifierComponent* component = nullptr;
  48. auto entity = CreateEntity(config, &component, [](AZ::Entity* e)
  49. {
  50. e->CreateComponent<MockVegetationAreaServiceComponent>();
  51. });
  52. Vegetation::ModifierRequestBus::Event(entity->GetId(), &Vegetation::ModifierRequestBus::Events::Execute, vegInstance);
  53. EXPECT_NEAR(vegInstance.m_position.GetX(), 2.294f, AZ::Constants::Tolerance);
  54. EXPECT_NEAR(vegInstance.m_position.GetY(), 4.294f, AZ::Constants::Tolerance);
  55. EXPECT_NEAR(vegInstance.m_position.GetZ(), 0.0f, AZ::Constants::Tolerance);
  56. // with the surface handler
  57. MockSurfaceHandler mockSurfaceHandler;
  58. mockSurfaceHandler.m_outPosition = AZ::Vector3(vegInstance.m_position.GetX(), vegInstance.m_position.GetY(), 6.0f);
  59. mockSurfaceHandler.m_outNormal = AZ::Vector3(0.0f, 0.0f, 1.0f);
  60. mockSurfaceHandler.m_outMasks.AddSurfaceTagWeight(crcMask, 1.0f);
  61. entity->Deactivate();
  62. config.m_autoSnapToSurface = true;
  63. component->ReadInConfig(&config);
  64. entity->Activate();
  65. Vegetation::ModifierRequestBus::Event(entity->GetId(), &Vegetation::ModifierRequestBus::Events::Execute, vegInstance);
  66. EXPECT_EQ(mockSurfaceHandler.m_outNormal, vegInstance.m_normal);
  67. EXPECT_EQ(mockSurfaceHandler.m_outMasks, vegInstance.m_masks);
  68. }
  69. TEST_F(VegetationComponentModifierTests, RotationModifierComponent)
  70. {
  71. m_instanceData.m_rotation = AZ::Quaternion::CreateIdentity();
  72. MockGradientRequestHandler gradientX;
  73. gradientX.m_defaultValue = 0.15f;
  74. MockGradientRequestHandler gradientY;
  75. gradientY.m_defaultValue = 0.25f;
  76. MockGradientRequestHandler gradientZ;
  77. gradientZ.m_defaultValue = 0.35f;
  78. Vegetation::RotationModifierConfig config;
  79. config.m_rangeMinX = -100.0f;
  80. config.m_rangeMaxX = 100.0f;
  81. config.m_gradientSamplerX.m_gradientId = gradientX.m_entity.GetId();
  82. config.m_rangeMinY = -80.0f;
  83. config.m_rangeMaxY = 80.0f;
  84. config.m_gradientSamplerY.m_gradientId = gradientY.m_entity.GetId();
  85. config.m_rangeMinZ = -180.0f;
  86. config.m_rangeMaxZ = 180.0f;
  87. config.m_gradientSamplerZ.m_gradientId = gradientZ.m_entity.GetId();
  88. Vegetation::RotationModifierComponent* component = nullptr;
  89. auto entity = CreateEntity(config, &component, [](AZ::Entity* e)
  90. {
  91. e->CreateComponent<MockVegetationAreaServiceComponent>();
  92. });
  93. Vegetation::ModifierRequestBus::Event(entity->GetId(), &Vegetation::ModifierRequestBus::Events::Execute, m_instanceData);
  94. EXPECT_NEAR(m_instanceData.m_rotation.GetW(), 0.777f, 0.003f);
  95. EXPECT_NEAR(m_instanceData.m_rotation.GetX(), -0.353f, 0.003f);
  96. EXPECT_NEAR(m_instanceData.m_rotation.GetY(), -0.495f, 0.003f);
  97. EXPECT_NEAR(m_instanceData.m_rotation.GetZ(), -0.175f, 0.003f);
  98. }
  99. TEST_F(VegetationComponentModifierTests, ScaleModifierComponent)
  100. {
  101. MockGradientRequestHandler gradient;
  102. gradient.m_defaultValue = 0.1234f;
  103. Vegetation::ScaleModifierConfig config;
  104. config.m_gradientSampler.m_gradientId = gradient.m_entity.GetId();
  105. config.m_rangeMin = 0.1f;
  106. config.m_rangeMax = 0.9f;
  107. m_instanceData.m_scale = 1.0f;
  108. Vegetation::ScaleModifierComponent* component = nullptr;
  109. auto entity = CreateEntity(config, &component, [](AZ::Entity* e)
  110. {
  111. e->CreateComponent<MockVegetationAreaServiceComponent>();
  112. });
  113. Vegetation::ModifierRequestBus::Event(entity->GetId(), &Vegetation::ModifierRequestBus::Events::Execute, m_instanceData);
  114. EXPECT_TRUE(AZ::IsClose(m_instanceData.m_scale, 0.19872f, std::numeric_limits<decltype(m_instanceData.m_scale)>::epsilon()));
  115. }
  116. #if AZ_TRAIT_DISABLE_FAILED_VEGETATION_TESTS
  117. TEST_F(VegetationComponentModifierTests, DISABLED_SlopeAlignmentModifierComponent)
  118. #else
  119. TEST_F(VegetationComponentModifierTests, SlopeAlignmentModifierComponent)
  120. #endif // AZ_TRAIT_DISABLE_FAILED_VEGETATION_TESTS
  121. {
  122. MockGradientRequestHandler gradient;
  123. gradient.m_defaultValue = 0.87654f;
  124. Vegetation::SlopeAlignmentModifierConfig config;
  125. config.m_gradientSampler.m_gradientId = gradient.m_entity.GetId();
  126. config.m_rangeMin = 0.1f;
  127. config.m_rangeMax = 0.9f;
  128. m_instanceData.m_normal = AZ::Vector3(0.12f, 0.34f, 0.56f);
  129. m_instanceData.m_alignment = AZ::Quaternion::CreateFromAxisAngle(AZ::Vector3::CreateAxisY(), AZ::DegToRad(42)).GetNormalized();
  130. Vegetation::SlopeAlignmentModifierComponent* component = nullptr;
  131. auto entity = CreateEntity(config, &component, [](AZ::Entity* e)
  132. {
  133. e->CreateComponent<MockVegetationAreaServiceComponent>();
  134. });
  135. Vegetation::ModifierRequestBus::Event(entity->GetId(), &Vegetation::ModifierRequestBus::Events::Execute, m_instanceData);
  136. EXPECT_NEAR(m_instanceData.m_alignment.GetX(), -0.1973f, AZ::Constants::Tolerance);
  137. EXPECT_NEAR(m_instanceData.m_alignment.GetY(), 0.0666f, AZ::Constants::Tolerance);
  138. EXPECT_NEAR(m_instanceData.m_alignment.GetZ(), -0.0134f, AZ::Constants::Tolerance);
  139. EXPECT_NEAR(m_instanceData.m_alignment.GetW(), 0.9779f, AZ::Constants::Tolerance);
  140. }
  141. }