ParticleNoise.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <catch2/catch_test_macros.hpp>
  2. #include <cmath>
  3. #include "WallpaperEngine/Render/Utils/NoiseUtils.h"
  4. using WallpaperEngine::Render::Utils::simplexNoise1D;
  5. using WallpaperEngine::Render::Utils::simplexNoise3D;
  6. TEST_CASE ("Simplex noise is zero on lattice points") {
  7. CHECK (simplexNoise1D (0.0f) == 0.0f);
  8. CHECK (simplexNoise1D (7.0f) == 0.0f);
  9. CHECK (std::abs (simplexNoise3D (0.0f, 0.0f, 0.0f)) < 1e-6f);
  10. }
  11. TEST_CASE ("Simplex noise stays in range and is continuous") {
  12. float min1 = 0.0f, max1 = 0.0f, min3 = 0.0f, max3 = 0.0f;
  13. // only the 1D noise is continuous, WE's 3D hash (see simplexNoise3D) jumps at some cell borders
  14. float biggestStep = 0.0f;
  15. float previous = simplexNoise1D (-50.0f);
  16. for (int i = 0; i < 20000; i++) {
  17. const float t = -50.0f + static_cast<float> (i) * 0.005f;
  18. const float one = simplexNoise1D (t);
  19. const float three = simplexNoise3D (t, 3.1f + t * 0.37f, 7.7f - t * 0.21f);
  20. min1 = std::min (min1, one);
  21. max1 = std::max (max1, one);
  22. min3 = std::min (min3, three);
  23. max3 = std::max (max3, three);
  24. biggestStep = std::max (biggestStep, std::abs (one - previous));
  25. previous = one;
  26. }
  27. CHECK (min1 >= -1.1f);
  28. CHECK (max1 <= 1.1f);
  29. CHECK (min3 >= -1.1f);
  30. CHECK (max3 <= 1.1f);
  31. CHECK (max3 - min3 > 1.0f);
  32. CHECK (biggestStep < 0.1f);
  33. }
  34. TEST_CASE ("3D simplex noise matches wallpaper64.exe") {
  35. // sub_1400FD010 run under emulation with these inputs
  36. struct Sample {
  37. float x, y, z, expected;
  38. };
  39. const Sample samples[] = {
  40. { -36.5635757f, 34.7433739f, 26.3774624f, 0.26951617f },
  41. { -24.4930973f, -0.456491292f, -5.05089331f, 0.000486342702f },
  42. { 26.2280083f, -49.7893944f, -5.46128082f, -0.404468179f },
  43. { -28.3400612f, -7.78834248f, -47.0959206f, -0.373473674f },
  44. { 33.7577972f, 5.64543247f, 14.2294359f, -0.48249346f },
  45. };
  46. for (const auto& sample : samples) {
  47. CHECK (std::abs (simplexNoise3D (sample.x, sample.y, sample.z) - sample.expected) < 1e-5f);
  48. }
  49. }