GradientSignalTest.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 <Tests/GradientSignalTestFixtures.h>
  9. #include <GradientSignal/PerlinImprovedNoise.h>
  10. #include <GradientSignal/Ebuses/GradientRequestBus.h>
  11. #include <GradientSignal/Components/PerlinGradientComponent.h>
  12. #include <GradientSignal/Components/RandomGradientComponent.h>
  13. #include <GradientSignal/Components/LevelsGradientComponent.h>
  14. #include <GradientSignal/Components/PosterizeGradientComponent.h>
  15. #include <GradientSignal/Components/SmoothStepGradientComponent.h>
  16. #include <GradientSignal/Components/ThresholdGradientComponent.h>
  17. #include <GradientSignal/Components/GradientTransformComponent.h>
  18. namespace UnitTest
  19. {
  20. struct GradientSignalTestGeneratorFixture
  21. : public GradientSignalTest
  22. {
  23. void TestLevelsGradientComponent(int dataSize, const AZStd::vector<float>& inputData, const AZStd::vector<float>& expectedOutput,
  24. float inputMin, float inputMid, float inputMax, float outputMin, float outputMax)
  25. {
  26. auto entityMock = CreateTestEntity(1.0f);
  27. const AZ::EntityId id = entityMock->GetId();
  28. UnitTest::MockGradientArrayRequestsBus mockGradientRequestsBus(id, inputData, dataSize);
  29. GradientSignal::GradientTransformConfig gradientTransformConfig;
  30. entityMock->CreateComponent<GradientSignal::GradientTransformComponent>(gradientTransformConfig);
  31. ActivateEntity(entityMock.get());
  32. GradientSignal::LevelsGradientConfig config;
  33. config.m_gradientSampler.m_gradientId = entityMock->GetId();
  34. config.m_inputMin = inputMin;
  35. config.m_inputMid = inputMid;
  36. config.m_inputMax = inputMax;
  37. config.m_outputMin = outputMin;
  38. config.m_outputMax = outputMax;
  39. auto entity = CreateEntity();
  40. entity->CreateComponent<GradientSignal::LevelsGradientComponent>(config);
  41. ActivateEntity(entity.get());
  42. TestFixedDataSampler(expectedOutput, dataSize, entity->GetId());
  43. }
  44. void TestPosterizeGradientComponent(int dataSize, const AZStd::vector<float>& inputData, const AZStd::vector<float>& expectedOutput,
  45. GradientSignal::PosterizeGradientConfig::ModeType posterizeMode, int bands)
  46. {
  47. auto entityMock = CreateTestEntity(0.5f);
  48. const AZ::EntityId id = entityMock->GetId();
  49. UnitTest::MockGradientArrayRequestsBus mockGradientRequestsBus(id, inputData, dataSize);
  50. GradientSignal::GradientTransformConfig gradientTransformConfig;
  51. entityMock->CreateComponent<GradientSignal::GradientTransformComponent>(gradientTransformConfig);
  52. ActivateEntity(entityMock.get());
  53. GradientSignal::PosterizeGradientConfig config;
  54. config.m_gradientSampler.m_gradientId = entityMock->GetId();
  55. config.m_mode = posterizeMode;
  56. config.m_bands = bands;
  57. auto entity = CreateEntity();
  58. entity->CreateComponent<GradientSignal::PosterizeGradientComponent>(config);
  59. ActivateEntity(entity.get());
  60. TestFixedDataSampler(expectedOutput, dataSize, entity->GetId());
  61. }
  62. void TestSmoothStepGradientComponent(int dataSize, const AZStd::vector<float>& inputData, const AZStd::vector<float>& expectedOutput,
  63. float midpoint, float range, float softness)
  64. {
  65. auto entityMock = CreateTestEntity(0.5f);
  66. const AZ::EntityId id = entityMock->GetId();
  67. UnitTest::MockGradientArrayRequestsBus mockGradientRequestsBus(id, inputData, dataSize);
  68. GradientSignal::GradientTransformConfig gradientTransformConfig;
  69. entityMock->CreateComponent<GradientSignal::GradientTransformComponent>(gradientTransformConfig);
  70. ActivateEntity(entityMock.get());
  71. GradientSignal::SmoothStepGradientConfig config;
  72. config.m_gradientSampler.m_gradientId = entityMock->GetId();
  73. config.m_smoothStep.m_falloffMidpoint = midpoint;
  74. config.m_smoothStep.m_falloffRange = range;
  75. config.m_smoothStep.m_falloffStrength = softness;
  76. auto entity = CreateEntity();
  77. entity->CreateComponent<GradientSignal::SmoothStepGradientComponent>(config);
  78. ActivateEntity(entity.get());
  79. TestFixedDataSampler(expectedOutput, dataSize, entity->GetId());
  80. }
  81. void TestThresholdGradientComponent(int dataSize, const AZStd::vector<float>& inputData, const AZStd::vector<float>& expectedOutput, float threshold)
  82. {
  83. auto entityMock = CreateTestEntity(0.5f);
  84. const AZ::EntityId id = entityMock->GetId();
  85. UnitTest::MockGradientArrayRequestsBus mockGradientRequestsBus(id, inputData, dataSize);
  86. GradientSignal::GradientTransformConfig gradientTransformConfig;
  87. entityMock->CreateComponent<GradientSignal::GradientTransformComponent>(gradientTransformConfig);
  88. ActivateEntity(entityMock.get());
  89. GradientSignal::ThresholdGradientConfig config;
  90. config.m_gradientSampler.m_gradientId = entityMock->GetId();
  91. config.m_threshold = threshold;
  92. auto entity = CreateEntity();
  93. entity->CreateComponent<GradientSignal::ThresholdGradientComponent>(config);
  94. ActivateEntity(entity.get());
  95. TestFixedDataSampler(expectedOutput, dataSize, entity->GetId());
  96. }
  97. };
  98. TEST_F(GradientSignalTestGeneratorFixture, GradientSampler_BasicFunctionality)
  99. {
  100. // Verify that a GradientSampler correctly handles requests and returns the mocked value.
  101. const float expectedOutput = 159.0f;
  102. auto entity = CreateEntity();
  103. const AZ::EntityId id = entity->GetId();
  104. MockGradientRequestsBus mockGradientRequestsBus(id);
  105. mockGradientRequestsBus.m_GetValue = expectedOutput;
  106. ActivateEntity(entity.get());
  107. GradientSignal::GradientSampler gradientSampler;
  108. gradientSampler.m_gradientId = entity->GetId();
  109. EXPECT_EQ(expectedOutput, gradientSampler.GetValue({}));
  110. }
  111. TEST_F(GradientSignalTestGeneratorFixture, PerlinGradientComponent_GoldenTest)
  112. {
  113. // Make sure PerlinGradientComponent generates a set of values that
  114. // matches a previously-calculated "golden" set of values.
  115. constexpr int dataSize = 4;
  116. GradientSignal::PerlinGradientConfig config;
  117. config.m_randomSeed = 7878;
  118. config.m_octave = 4;
  119. config.m_amplitude = 3.0f;
  120. config.m_frequency = 1.13f;
  121. // The random seed to generate the input for the permutation table is platform independent, but
  122. // is not deterministic per platform due to the inconsistent implementaion of the mersenne twister
  123. // engine in different standard libraries. This will lead to deterministic results by platform,
  124. // so the values cannot be relied upon per platform. In order to generate consistent values, we
  125. // will pregenerate the permutation table using the above 7878 seed and the results of the
  126. // permutation generation based on the windows implementation of the RNG, so we can have a fixed
  127. // value to compare against the results to validate the perlin component. The values below represent
  128. // the original permutation table that was based on the seed and windows environment.
  129. AZStd::array<int, 512> testPerlinPermutationTable =
  130. {
  131. 0x5e, 0xdd, 0x95, 0xf6, 0x43, 0x0f, 0x7e, 0x20, 0xf7, 0xb7, 0x82, 0x98, 0x73, 0x58, 0xf5, 0xa0,
  132. 0xa7, 0x12, 0xbf, 0x9c, 0xba, 0x88, 0x08, 0x2d, 0xd6, 0x1f, 0xd0, 0x4f, 0x0e, 0x9e, 0x4a, 0xe4,
  133. 0x93, 0xac, 0x5a, 0x89, 0x13, 0x8b, 0x62, 0x3c, 0x69, 0x78, 0xda, 0xcd, 0x57, 0xa6, 0x0d, 0xde,
  134. 0xb5, 0xb2, 0x70, 0x04, 0x16, 0x2a, 0x91, 0x2c, 0x07, 0x6a, 0x81, 0x4c, 0x9d, 0xad, 0xe1, 0x2b,
  135. 0x30, 0x3b, 0x83, 0x9b, 0x31, 0x38, 0x9f, 0xaf, 0x3e, 0x1c, 0x06, 0x97, 0x46, 0x00, 0xae, 0x90,
  136. 0xc3, 0xd9, 0xf2, 0xd2, 0xcf, 0x11, 0x10, 0xe7, 0x56, 0xfa, 0x87, 0x09, 0x1b, 0xb4, 0x61, 0x25,
  137. 0xcc, 0x7c, 0x50, 0x94, 0xc6, 0x0c, 0xe3, 0xc1, 0x26, 0x96, 0xdc, 0x02, 0xa8, 0x19, 0xe9, 0x68,
  138. 0xf4, 0xb3, 0x4b, 0x33, 0x52, 0xb1, 0x6f, 0xec, 0x51, 0x1e, 0x24, 0xc7, 0xaa, 0xc8, 0xc9, 0x15,
  139. 0x18, 0x48, 0x0a, 0xa3, 0xdf, 0x59, 0xf8, 0x92, 0x64, 0xd5, 0xfb, 0x8f, 0x99, 0xca, 0xea, 0x79,
  140. 0x63, 0x84, 0x6b, 0x67, 0x2e, 0x28, 0xab, 0xcb, 0xf1, 0x2f, 0x71, 0x5c, 0x27, 0x72, 0xdb, 0x03,
  141. 0xd1, 0x36, 0x65, 0x14, 0x7a, 0x23, 0xf3, 0x5f, 0xb0, 0x86, 0xe6, 0x8c, 0xa4, 0x6d, 0xf9, 0x22,
  142. 0xce, 0x40, 0x01, 0x8e, 0xbd, 0x17, 0x7b, 0x66, 0xa1, 0x5b, 0xa9, 0xa2, 0xe5, 0x1a, 0xee, 0x3f,
  143. 0x85, 0xeb, 0xef, 0xff, 0x4d, 0xfc, 0xb9, 0xd3, 0x5d, 0x53, 0xd4, 0x76, 0x49, 0xbc, 0x41, 0xc0,
  144. 0x39, 0x21, 0x74, 0xed, 0x54, 0xd7, 0xc5, 0x8a, 0xd8, 0xc4, 0xfe, 0x29, 0x9a, 0x6e, 0x7d, 0xb8,
  145. 0xc2, 0x55, 0x1d, 0xfd, 0x05, 0x42, 0x4e, 0x3d, 0xe8, 0x60, 0xe2, 0x75, 0x6c, 0x7f, 0x45, 0xbe,
  146. 0x47, 0x44, 0xbb, 0xe0, 0x3a, 0xb6, 0xa5, 0x77, 0x34, 0x0b, 0x37, 0x32, 0x8d, 0x35, 0xf0, 0x80,
  147. 0x5e, 0xdd, 0x95, 0xf6, 0x43, 0x0f, 0x7e, 0x20, 0xf7, 0xb7, 0x82, 0x98, 0x73, 0x58, 0xf5, 0xa0,
  148. 0xa7, 0x12, 0xbf, 0x9c, 0xba, 0x88, 0x08, 0x2d, 0xd6, 0x1f, 0xd0, 0x4f, 0x0e, 0x9e, 0x4a, 0xe4,
  149. 0x93, 0xac, 0x5a, 0x89, 0x13, 0x8b, 0x62, 0x3c, 0x69, 0x78, 0xda, 0xcd, 0x57, 0xa6, 0x0d, 0xde,
  150. 0xb5, 0xb2, 0x70, 0x04, 0x16, 0x2a, 0x91, 0x2c, 0x07, 0x6a, 0x81, 0x4c, 0x9d, 0xad, 0xe1, 0x2b,
  151. 0x30, 0x3b, 0x83, 0x9b, 0x31, 0x38, 0x9f, 0xaf, 0x3e, 0x1c, 0x06, 0x97, 0x46, 0x00, 0xae, 0x90,
  152. 0xc3, 0xd9, 0xf2, 0xd2, 0xcf, 0x11, 0x10, 0xe7, 0x56, 0xfa, 0x87, 0x09, 0x1b, 0xb4, 0x61, 0x25,
  153. 0xcc, 0x7c, 0x50, 0x94, 0xc6, 0x0c, 0xe3, 0xc1, 0x26, 0x96, 0xdc, 0x02, 0xa8, 0x19, 0xe9, 0x68,
  154. 0xf4, 0xb3, 0x4b, 0x33, 0x52, 0xb1, 0x6f, 0xec, 0x51, 0x1e, 0x24, 0xc7, 0xaa, 0xc8, 0xc9, 0x15,
  155. 0x18, 0x48, 0x0a, 0xa3, 0xdf, 0x59, 0xf8, 0x92, 0x64, 0xd5, 0xfb, 0x8f, 0x99, 0xca, 0xea, 0x79,
  156. 0x63, 0x84, 0x6b, 0x67, 0x2e, 0x28, 0xab, 0xcb, 0xf1, 0x2f, 0x71, 0x5c, 0x27, 0x72, 0xdb, 0x03,
  157. 0xd1, 0x36, 0x65, 0x14, 0x7a, 0x23, 0xf3, 0x5f, 0xb0, 0x86, 0xe6, 0x8c, 0xa4, 0x6d, 0xf9, 0x22,
  158. 0xce, 0x40, 0x01, 0x8e, 0xbd, 0x17, 0x7b, 0x66, 0xa1, 0x5b, 0xa9, 0xa2, 0xe5, 0x1a, 0xee, 0x3f,
  159. 0x85, 0xeb, 0xef, 0xff, 0x4d, 0xfc, 0xb9, 0xd3, 0x5d, 0x53, 0xd4, 0x76, 0x49, 0xbc, 0x41, 0xc0,
  160. 0x39, 0x21, 0x74, 0xed, 0x54, 0xd7, 0xc5, 0x8a, 0xd8, 0xc4, 0xfe, 0x29, 0x9a, 0x6e, 0x7d, 0xb8,
  161. 0xc2, 0x55, 0x1d, 0xfd, 0x05, 0x42, 0x4e, 0x3d, 0xe8, 0x60, 0xe2, 0x75, 0x6c, 0x7f, 0x45, 0xbe,
  162. 0x47, 0x44, 0xbb, 0xe0, 0x3a, 0xb6, 0xa5, 0x77, 0x34, 0x0b, 0x37, 0x32, 0x8d, 0x35, 0xf0, 0x80
  163. };
  164. // The 'golden' expected value based on the seed 7878 (see above comment)
  165. AZStd::vector<float> expectedOutput =
  166. {
  167. 0.50000f, 0.54557f, 0.51378f, 0.48007f,
  168. 0.41741f, 0.49420f, 0.54927f, 0.54314f,
  169. 0.49841f, 0.52041f, 0.55258f, 0.58404f,
  170. 0.52507f, 0.50288f, 0.61527f, 0.58024f
  171. };
  172. auto entity = CreateEntity();
  173. auto* mockGradientSignal = entity->CreateComponent<MockGradientSignal>(config);
  174. mockGradientSignal->SetPerlinNoisePermutationTableForTest(testPerlinPermutationTable);
  175. GradientSignal::GradientTransformConfig gradientTransformConfig;
  176. entity->CreateComponent<GradientSignal::GradientTransformComponent>(gradientTransformConfig);
  177. entity->CreateComponent<MockShapeComponent>();
  178. MockShapeComponentHandler mockShapeHandler(entity->GetId());
  179. ActivateEntity(entity.get());
  180. TestFixedDataSampler(expectedOutput, dataSize, entity->GetId());
  181. }
  182. TEST_F(GradientSignalTestGeneratorFixture, RandomGradientComponent_GoldenTest)
  183. {
  184. // Make sure RandomGradientComponent returns back a "golden" set
  185. // of values for a given random seed.
  186. constexpr int dataSize = 4;
  187. AZStd::vector<float> expectedOutput =
  188. {
  189. 0.5059f, 0.4902f, 0.6000f, 0.7372f,
  190. 0.9490f, 0.2823f, 0.6588f, 0.5804f,
  191. 0.1490f, 0.3294f, 0.1451f, 0.6627f,
  192. 0.2980f, 0.1608f, 0.9098f, 0.9804f,
  193. };
  194. GradientSignal::RandomGradientConfig config;
  195. config.m_randomSeed = 5656;
  196. auto entity = CreateEntity();
  197. entity->CreateComponent<GradientSignal::RandomGradientComponent>(config);
  198. GradientSignal::GradientTransformConfig gradientTransformConfig;
  199. entity->CreateComponent<GradientSignal::GradientTransformComponent>(gradientTransformConfig);
  200. entity->CreateComponent<MockShapeComponent>();
  201. MockShapeComponentHandler mockShapeHandler(entity->GetId());
  202. ActivateEntity(entity.get());
  203. TestFixedDataSampler(expectedOutput, dataSize, entity->GetId());
  204. }
  205. TEST_F(GradientSignalTestGeneratorFixture, LevelsGradientComponent_DefaultValues)
  206. {
  207. // Verify that with the default config values, our outputs equal our inputs.
  208. constexpr int dataSize = 3;
  209. AZStd::vector<float> inputData =
  210. {
  211. 0.0f, 0.1f, 0.2f,
  212. 0.4f, 0.5f, 0.6f,
  213. 0.8f, 0.9f, 1.0f
  214. };
  215. AZStd::vector<float> expectedOutput =
  216. {
  217. 0.0f, 0.1f, 0.2f,
  218. 0.4f, 0.5f, 0.6f,
  219. 0.8f, 0.9f, 1.0f
  220. };
  221. // default values: input min/mid/max of 0-1-1, and output min/max of 0-1
  222. TestLevelsGradientComponent(dataSize, inputData, expectedOutput, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f);
  223. }
  224. TEST_F(GradientSignalTestGeneratorFixture, LevelsGradientComponent_ScaleToMinMax)
  225. {
  226. // Verify that setting the output min/max correctly scales the inputs into the output range.
  227. constexpr int dataSize = 3;
  228. AZStd::vector<float> inputData =
  229. {
  230. 0.0f, 0.1f, 0.2f,
  231. 0.4f, 0.5f, 0.6f,
  232. 0.8f, 0.9f, 1.0f
  233. };
  234. AZStd::vector<float> expectedOutput;
  235. constexpr float outputMin = 0.25f;
  236. constexpr float outputMax = 0.75f;
  237. // We expect our inputs to be linearly scaled into the range defined by outputMin / outputMax.
  238. for (auto input : inputData)
  239. {
  240. expectedOutput.push_back(AZ::Lerp(outputMin, outputMax, input));
  241. }
  242. // Set input min/mid/max to 0-1-1 for no input remapping, so we only test the output params.
  243. TestLevelsGradientComponent(dataSize, inputData, expectedOutput, 0.0f, 1.0f, 1.0f, outputMin, outputMax);
  244. }
  245. TEST_F(GradientSignalTestGeneratorFixture, LevelsGradientComponent_BelowMinIsZero)
  246. {
  247. // Inputs at or below the min produces an output of 0.
  248. constexpr int dataSize = 3;
  249. AZStd::vector<float> inputData =
  250. {
  251. 0.0f, 0.1f, 0.2f,
  252. 0.4f, 0.5f, 0.6f,
  253. 0.8f, 0.9f, 1.0f
  254. };
  255. // Because we're adjusting our input range to 0.5 - 1, it means that values above 0.5 get lerped
  256. AZStd::vector<float> expectedOutput =
  257. {
  258. 0.0f, 0.0f, 0.0f,
  259. 0.0f, 0.0f, 0.2f,
  260. 0.6f, 0.8f, 1.0f
  261. };
  262. // Set output min/max to 0-1 for no remapping, so we only test the input params.
  263. TestLevelsGradientComponent(dataSize, inputData, expectedOutput, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f);
  264. }
  265. TEST_F(GradientSignalTestGeneratorFixture, LevelsGradientComponent_AboveMaxIsOne)
  266. {
  267. // Inputs above the max produces an output of 1.
  268. constexpr int dataSize = 3;
  269. AZStd::vector<float> inputData =
  270. {
  271. 0.0f, 0.1f, 0.2f,
  272. 0.4f, 0.5f, 0.6f,
  273. 0.8f, 0.9f, 1.0f
  274. };
  275. // Because we're adjusting our input range to 0.0 - 0.5, it means that values below 0.5 get lerped
  276. AZStd::vector<float> expectedOutput =
  277. {
  278. 0.0f, 0.2f, 0.4f,
  279. 0.8f, 1.0f, 1.0f,
  280. 1.0f, 1.0f, 1.0f
  281. };
  282. // Set output min/max to 0-1 for no remapping, so we only test the input params.
  283. TestLevelsGradientComponent(dataSize, inputData, expectedOutput, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f);
  284. }
  285. TEST_F(GradientSignalTestGeneratorFixture, LevelsGradientComponent_AdjustedMidpoint)
  286. {
  287. // Verify that a midpoint adjusted to 0.5 correctly squares the inputs for the outputs.
  288. // (We're using 0.5 for verification because it's an easy value to test)
  289. constexpr int dataSize = 3;
  290. AZStd::vector<float> inputData =
  291. {
  292. 0.0f, 0.1f, 0.2f,
  293. 0.4f, 0.5f, 0.6f,
  294. 0.8f, 0.9f, 1.0f
  295. };
  296. AZStd::vector<float> expectedOutput;
  297. // With a midpoint of 0.5, we expect our outputs to be the inputs squared (input ^ (1/0.5))
  298. for (auto input : inputData)
  299. {
  300. expectedOutput.push_back(input * input);
  301. }
  302. // Set the input midpoint to 0.5 to adjust all the values
  303. TestLevelsGradientComponent(dataSize, inputData, expectedOutput, 0.0f, 0.5f, 1.0f, 0.0f, 1.0f);
  304. }
  305. TEST_F(GradientSignalTestGeneratorFixture, PosterizeGradientComponent_ModeFloor)
  306. {
  307. // Verify that the "floor mode" divides into equal bands and uses the floored value for each band.
  308. // Ex: For 3 bands, input bands of 0.0-0.33 / 0.33-.67 / 0.67-1.0 should map to 0.00 / 0.33 / 0.67
  309. constexpr int dataSize = 3;
  310. AZStd::vector<float> inputData =
  311. {
  312. 0.0f, 0.1f, 0.2f,
  313. 0.4f, 0.5f, 0.6f,
  314. 0.8f, 0.9f, 1.0f
  315. };
  316. // We have 3 bands, choose the lowest value from each band.
  317. constexpr float lowBand = 0.0f / 3.0f;
  318. constexpr float midBand = 1.0f / 3.0f;
  319. constexpr float highBand = 2.0f / 3.0f;
  320. AZStd::vector<float> expectedOutput =
  321. {
  322. lowBand, lowBand, lowBand,
  323. midBand, midBand, midBand,
  324. highBand, highBand, highBand
  325. };
  326. TestPosterizeGradientComponent(dataSize, inputData, expectedOutput, GradientSignal::PosterizeGradientConfig::ModeType::Floor, 3);
  327. }
  328. TEST_F(GradientSignalTestGeneratorFixture, PosterizeGradientComponent_ModeRound)
  329. {
  330. // Verify that the "round mode" divides into equal bands and uses the midpoint value for each band.
  331. // Ex: For 3 bands, input bands of 0.0-0.33 / 0.33-.67 / 0.67-1.0 should map to 0.17 / 0.5 / 0.84
  332. constexpr int dataSize = 3;
  333. AZStd::vector<float> inputData =
  334. {
  335. 0.0f, 0.1f, 0.2f,
  336. 0.4f, 0.5f, 0.6f,
  337. 0.8f, 0.9f, 1.0f
  338. };
  339. // We have 3 bands, choose the middle value from each band.
  340. constexpr float lowBand = 0.5f / 3.0f;
  341. constexpr float midBand = 1.5f / 3.0f;
  342. constexpr float highBand = 2.5f / 3.0f;
  343. AZStd::vector<float> expectedOutput =
  344. {
  345. lowBand, lowBand, lowBand,
  346. midBand, midBand, midBand,
  347. highBand, highBand, highBand
  348. };
  349. TestPosterizeGradientComponent(dataSize, inputData, expectedOutput, GradientSignal::PosterizeGradientConfig::ModeType::Round, 3);
  350. }
  351. TEST_F(GradientSignalTestGeneratorFixture, PosterizeGradientComponent_ModeCeiling)
  352. {
  353. // Verify that the "ceiling mode" divides into equal bands and uses the high value for each band.
  354. // Ex: For 3 bands, input bands of 0.0-0.33 / 0.33-.67 / 0.67-1.0 should map to 0.33 / 0.67 / 1.0
  355. constexpr int dataSize = 3;
  356. AZStd::vector<float> inputData =
  357. {
  358. 0.0f, 0.1f, 0.2f,
  359. 0.4f, 0.5f, 0.6f,
  360. 0.8f, 0.9f, 1.0f
  361. };
  362. // We have 3 bands, choose the highest value from each band.
  363. constexpr float lowBand = 1.0f / 3.0f;
  364. constexpr float midBand = 2.0f / 3.0f;
  365. constexpr float highBand = 3.0f / 3.0f;
  366. AZStd::vector<float> expectedOutput =
  367. {
  368. lowBand, lowBand, lowBand,
  369. midBand, midBand, midBand,
  370. highBand, highBand, highBand
  371. };
  372. TestPosterizeGradientComponent(dataSize, inputData, expectedOutput, GradientSignal::PosterizeGradientConfig::ModeType::Ceiling, 3);
  373. }
  374. TEST_F(GradientSignalTestGeneratorFixture, PosterizeGradientComponent_ModePs)
  375. {
  376. // Verify that the "Ps mode" divides into equal bands which always have 0 for the lowest band, 1 for
  377. // the highest band, and equally spaced ranges for every band in-between.
  378. // Ex: For 3 bands, input bands of 0.0-0.33 / 0.33-.67 / 0.67-1.0 should map to 0.0 / 0.5 / 1.0
  379. constexpr int dataSize = 3;
  380. AZStd::vector<float> inputData =
  381. {
  382. 0.0f, 0.1f, 0.2f,
  383. 0.4f, 0.5f, 0.6f,
  384. 0.8f, 0.9f, 1.0f
  385. };
  386. // Ps mode has equally-spaced value ranges that always start with 0 and end with 1.
  387. constexpr float lowBand = 0.0f;
  388. constexpr float midBand = 0.5f;
  389. constexpr float highBand = 1.0f;
  390. AZStd::vector<float> expectedOutput =
  391. {
  392. lowBand, lowBand, lowBand,
  393. midBand, midBand, midBand,
  394. highBand, highBand, highBand
  395. };
  396. TestPosterizeGradientComponent(dataSize, inputData, expectedOutput, GradientSignal::PosterizeGradientConfig::ModeType::Ps, 3);
  397. }
  398. TEST_F(GradientSignalTestGeneratorFixture, SmoothStepGradientComponent)
  399. {
  400. // Smooth step creates a ramp up and down. We expect the following:
  401. // inputs 0 to (midpoint - range/2): 0
  402. // inputs (midpoint - range/2) to (midpoint - range/2)+softness: ramp up
  403. // inputs (midpoint - range/2)+softness to (midpoint + range/2)-softness: 1
  404. // inputs (midpoint + range/2)-softness) to (midpoint + range/2): ramp down
  405. // inputs (midpoint + range/2) to 1: 0
  406. // We'll test with midpoint = 0.5, range = 0.6, softness = 0.1 so that we have easy ranges to verify.
  407. constexpr int dataSize = 5;
  408. AZStd::vector<float> inputData =
  409. {
  410. 0.00f, 0.05f, 0.10f, 0.15f, 0.20f, // Should all be 0
  411. 0.21f, 0.23f, 0.25f, 0.27f, 0.29f, // Should ramp up
  412. 0.30f, 0.40f, 0.50f, 0.60f, 0.70f, // Should all be 1
  413. 0.71f, 0.73f, 0.75f, 0.77f, 0.79f, // Should ramp down
  414. 0.80f, 0.85f, 0.90f, 0.95f, 1.00f // Should all be 0
  415. };
  416. // For smoothstep ramp curves, we expect the values to be symmetric between the up and down ramp,
  417. // hit 0.5 at the middle of the ramp, and be symmetric on both sides of the midpoint of the ramp.
  418. AZStd::vector<float> expectedOutput =
  419. {
  420. 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, // 0.00 - 0.20 input -> 0.0 output
  421. 0.028f, 0.216f, 0.500f, 0.784f, 0.972f, // 0.21 - 0.29 input -> pre-verified ramp up values
  422. 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, // 0.30 - 0.70 input -> 1.0 output
  423. 0.972f, 0.784f, 0.500f, 0.216f, 0.028f, // 0.71 - 0.79 input -> pre-verified ramp down values
  424. 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, // 0.80 - 1.00 input -> 0.0 output
  425. };
  426. TestSmoothStepGradientComponent(dataSize, inputData, expectedOutput, 0.5f, 0.6f, 0.1f);
  427. }
  428. TEST_F(GradientSignalTestGeneratorFixture, ThresholdGradientComponent_ZeroThreshold)
  429. {
  430. // A threshold of 0 should make (input <= 0) go to 0, and (input > 0) go to 1.
  431. constexpr int dataSize = 3;
  432. AZStd::vector<float> inputData =
  433. {
  434. 0.0f, 0.1f, 0.2f,
  435. 0.4f, 0.5f, 0.6f,
  436. 0.8f, 0.9f, 1.0f
  437. };
  438. AZStd::vector<float> expectedOutput =
  439. {
  440. 0.0f, 1.0f, 1.0f,
  441. 1.0f, 1.0f, 1.0f,
  442. 1.0f, 1.0f, 1.0f
  443. };
  444. TestThresholdGradientComponent(dataSize, inputData, expectedOutput, 0.0f);
  445. }
  446. TEST_F(GradientSignalTestGeneratorFixture, ThresholdGradientComponent_MidpointThreshold)
  447. {
  448. // A threshold of 0.5 should make (input <= 0.5) go to 0, and (input > 0.5) go to 1.
  449. constexpr int dataSize = 3;
  450. AZStd::vector<float> inputData =
  451. {
  452. 0.0f, 0.1f, 0.2f,
  453. 0.4f, 0.5f, 0.6f,
  454. 0.8f, 0.9f, 1.0f
  455. };
  456. AZStd::vector<float> expectedOutput =
  457. {
  458. 0.0f, 0.0f, 0.0f,
  459. 0.0f, 0.0f, 1.0f,
  460. 1.0f, 1.0f, 1.0f
  461. };
  462. TestThresholdGradientComponent(dataSize, inputData, expectedOutput, 0.5f);
  463. }
  464. TEST_F(GradientSignalTestGeneratorFixture, ThresholdGradientComponent_OneThreshold)
  465. {
  466. // A threshold of 1.0 should make every value (input <= 1.0) drop to 0.0.
  467. constexpr int dataSize = 3;
  468. AZStd::vector<float> inputData =
  469. {
  470. 0.0f, 0.1f, 0.2f,
  471. 0.4f, 0.5f, 0.6f,
  472. 0.8f, 0.9f, 1.0f
  473. };
  474. AZStd::vector<float> expectedOutput =
  475. {
  476. 0.0f, 0.0f, 0.0f,
  477. 0.0f, 0.0f, 0.0f,
  478. 0.0f, 0.0f, 0.0f
  479. };
  480. TestThresholdGradientComponent(dataSize, inputData, expectedOutput, 1.0f);
  481. }
  482. }
  483. // This uses custom test / benchmark hooks so that we can load LmbrCentral and use Shape components in our unit tests and benchmarks.
  484. AZ_UNIT_TEST_HOOK(new UnitTest::GradientSignalTestEnvironment, UnitTest::GradientSignalBenchmarkEnvironment);