noise.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) The Minecraft Seed Finding Team
  2. //
  3. // MIT License
  4. #ifndef SEEDFINDING_SIMPLEX_H
  5. #define SEEDFINDING_SIMPLEX_H
  6. #include "lcg.h"
  7. #include "cache.h"
  8. // constant definition for simplex
  9. #define F2 0.3660254037844386
  10. #define G2 0.21132486540518713
  11. #define F3 0.3333333333333333
  12. #define G3 0.16666666666666666
  13. namespace noise {
  14. struct Noise {
  15. double xo;
  16. double yo;
  17. double zo;
  18. uint8_t permutations[256];
  19. };
  20. static inline void initOctaves(Noise octaves[], lcg::Random random, int nbOctaves) {
  21. for (int i = 0; i < nbOctaves; ++i) {
  22. octaves[i].xo = lcg::next_double(random) * 256.0;
  23. octaves[i].yo = lcg::next_double(random) * 256.0;
  24. octaves[i].zo = lcg::next_double(random) * 256.0;
  25. uint8_t *permutations = octaves[i].permutations;
  26. uint8_t j = 0;
  27. do {
  28. permutations[j] = j;
  29. } while (j++ != 255);
  30. uint8_t index = 0;
  31. do {
  32. uint32_t randomIndex = lcg::dynamic_next_int(random, 256u - index) + index;
  33. if (randomIndex != index) {
  34. // swap
  35. permutations[index] ^= permutations[randomIndex];
  36. permutations[randomIndex] ^= permutations[index];
  37. permutations[index] ^= permutations[randomIndex];
  38. }
  39. } while (index++ != 255);
  40. }
  41. }
  42. }
  43. namespace simplex {
  44. struct Simplex{
  45. noise::Noise noise;
  46. cache::Cache<
  47. }
  48. DEVICEABLE_CONST int grad2[12][2] = {{1, 1,},
  49. {-1, 1,},
  50. {1, -1,},
  51. {-1, -1,},
  52. {1, 0,},
  53. {-1, 0,},
  54. {1, 0,},
  55. {-1, 0,},
  56. {0, 1,},
  57. {0, -1,},
  58. {0, 1,},
  59. {0, -1,}};
  60. static inline void simplexNoise(double **buffer, double chunkX, double chunkZ, int x, int z, double offsetX, double offsetZ, double octaveFactor, PermutationTable permutationTable) {
  61. int k = 0;
  62. uint8_t *permutations = permutationTable.permutations;
  63. for (int X = 0; X < x; X++) {
  64. double XCoords = (chunkX + (double) X) * offsetX + permutationTable.xo;
  65. for (int Z = 0; Z < z; Z++) {
  66. double ZCoords = (chunkZ + (double) Z) * offsetZ + permutationTable.yo;
  67. // Skew the input space to determine which simplex cell we're in
  68. double hairyFactor = (XCoords + ZCoords) * F2;
  69. auto tempX = static_cast<int32_t>(XCoords + hairyFactor);
  70. auto tempZ = static_cast<int32_t>(ZCoords + hairyFactor);
  71. int32_t xHairy = (XCoords + hairyFactor < tempX) ? (tempX - 1) : (tempX);
  72. int32_t zHairy = (ZCoords + hairyFactor < tempZ) ? (tempZ - 1) : (tempZ);
  73. double d11 = (double) (xHairy + zHairy) * G2;
  74. double X0 = (double) xHairy - d11; // Unskew the cell origin back to (x,y) space
  75. double Y0 = (double) zHairy - d11;
  76. double x0 = XCoords - X0; // The x,y distances from the cell origin
  77. double y0 = ZCoords - Y0;
  78. // For the 2D case, the simplex shape is an equilateral triangle.
  79. // Determine which simplex we are in.
  80. int offsetSecondCornerX, offsetSecondCornerZ; // Offsets for second (middle) corner of simplex in (i,j) coords
  81. if (x0 > y0) { // lower triangle, XY order: (0,0)->(1,0)->(1,1)
  82. offsetSecondCornerX = 1;
  83. offsetSecondCornerZ = 0;
  84. } else { // upper triangle, YX order: (0,0)->(0,1)->(1,1)
  85. offsetSecondCornerX = 0;
  86. offsetSecondCornerZ = 1;
  87. }
  88. double x1 = (x0 - (double) offsetSecondCornerX) + G2; // Offsets for middle corner in (x,y) unskewed coords
  89. double y1 = (y0 - (double) offsetSecondCornerZ) + G2;
  90. double x2 = (x0 - 1.0) + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords
  91. double y2 = (y0 - 1.0) + 2.0 * G2;
  92. // Work out the hashed gradient indices of the three simplex corners
  93. uint32_t ii = (uint32_t) xHairy & 0xffu;
  94. uint32_t jj = (uint32_t) zHairy & 0xffu;
  95. uint8_t gi0 = permutations[ii + permutations[jj]] % 12u;
  96. uint8_t gi1 = permutations[ii + offsetSecondCornerX + permutations[jj + offsetSecondCornerZ]] % 12u;
  97. uint8_t gi2 = permutations[ii + 1 + permutations[jj + 1]] % 12u;
  98. // Calculate the contribution from the three corners
  99. double t0 = 0.5 - x0 * x0 - y0 * y0;
  100. double n0;
  101. if (t0 < 0.0) {
  102. n0 = 0.0;
  103. } else {
  104. t0 *= t0;
  105. n0 = t0 * t0 * ((double) grad2[gi0][0] * x0 + (double) grad2[gi0][1] * y0); // (x,y) of grad2 used for 2D gradient
  106. }
  107. double t1 = 0.5 - x1 * x1 - y1 * y1;
  108. double n1;
  109. if (t1 < 0.0) {
  110. n1 = 0.0;
  111. } else {
  112. t1 *= t1;
  113. n1 = t1 * t1 * ((double) grad2[gi1][0] * x1 + (double) grad2[gi1][1] * y1);
  114. }
  115. double t2 = 0.5 - x2 * x2 - y2 * y2;
  116. double n2;
  117. if (t2 < 0.0) {
  118. n2 = 0.0;
  119. } else {
  120. t2 *= t2;
  121. n2 = t2 * t2 * ((double) grad2[gi2][0] * x2 + (double) grad2[gi2][1] * y2);
  122. }
  123. // Add contributions from each corner to get the final noise value.
  124. // The result is scaled to return values in the interval [-1,1].
  125. (*buffer)[k] = (*buffer)[k] + 70.0 * (n0 + n1 + n2) * octaveFactor;
  126. k++;
  127. }
  128. }
  129. }
  130. }
  131. #endif //SEEDFINDING_SIMPLEX_H