Noise.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // Copyright(c) 2018, Benjamin Ward
  2. // All rights reserved.
  3. // Redistribution and use in source and binary forms, with or without
  4. // modification, are permitted provided that the following conditions are met:
  5. // * Redistributions of source code must retain the above copyright notice, this
  6. // list of conditions and the following disclaimer.
  7. // * Redistributions in binary form must reproduce the above copyright notice,
  8. // this list of conditions and the following disclaimer in the documentation
  9. // and/or other materials provided with the distribution.
  10. // * Neither the name of the copyright holder nor the names of its
  11. // contributors may be used to endorse or promote products derived from
  12. // this software without specific prior written permission.
  13. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  17. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  21. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  22. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. // SimplexNoise for C#
  24. // Author: Benjamin Ward
  25. // Github Link: https://github.com/WardBenjamin/SimplexNoise
  26. // Originally authored by Heikki Törmälä
  27. using System;
  28. namespace Simplex
  29. {
  30. /// <summary>
  31. /// Implementation of the Perlin simplex noise, an improved Perlin noise algorithm.
  32. /// Based loosely on SimplexNoise1234 by Stefan Gustavson <http://staffwww.itn.liu.se/~stegu/aqsis/aqsis-newnoise/>
  33. /// </summary>
  34. public class Noise
  35. {
  36. public static float[] Calc1D(int width, float scale)
  37. {
  38. float[] values = new float[width];
  39. for (int i = 0; i < width; i++)
  40. values[i] = Generate(i * scale) * 128 + 128;
  41. return values;
  42. }
  43. public static float[,] Calc2D(int width, int height, float scale)
  44. {
  45. float[,] values = new float[width, height];
  46. for (int i = 0; i < width; i++)
  47. for (int j = 0; j < height; j++)
  48. values[i, j] = Generate(i * scale, j * scale) * 128 + 128;
  49. return values;
  50. }
  51. public static float[,,] Calc3D(int width, int height, int length, float scale)
  52. {
  53. float[,,] values = new float[width, height, length];
  54. for (int i = 0; i < width; i++)
  55. for (int j = 0; j < height; j++)
  56. for (int k = 0; k < length; k++)
  57. values[i, j, k] = Generate(i * scale, j * scale, k * scale) * 128 + 128;
  58. return values;
  59. }
  60. public static float CalcPixel1D(int x, float scale)
  61. {
  62. return Generate(x * scale) * 128 + 128;
  63. }
  64. public static float CalcPixel2D(int x, int y, float scale)
  65. {
  66. return Generate(x * scale, y * scale) * 128 + 128;
  67. }
  68. public static float CalcPixel3D(int x, int y, int z, float scale)
  69. {
  70. return Generate(x * scale, y * scale, z * scale) * 128 + 128;
  71. }
  72. static Noise()
  73. {
  74. perm = new byte[permOriginal.Length];
  75. Simplex.Noise.permOriginal.CopyTo(perm, 0);
  76. }
  77. public static int Seed
  78. {
  79. get { return seed; }
  80. set
  81. {
  82. if (value == 0)
  83. {
  84. perm = new byte[permOriginal.Length];
  85. Simplex.Noise.permOriginal.CopyTo(perm, 0);
  86. }
  87. else
  88. {
  89. perm = new byte[512];
  90. Random random = new Random(value);
  91. random.NextBytes(perm);
  92. }
  93. }
  94. }
  95. private static int seed = 0;
  96. /// <summary>
  97. /// 1D simplex noise
  98. /// </summary>
  99. /// <param name="x"></param>
  100. /// <returns></returns>
  101. internal static float Generate(float x)
  102. {
  103. int i0 = FastFloor(x);
  104. int i1 = i0 + 1;
  105. float x0 = x - i0;
  106. float x1 = x0 - 1.0f;
  107. float n0, n1;
  108. float t0 = 1.0f - x0 * x0;
  109. t0 *= t0;
  110. n0 = t0 * t0 * grad(perm[i0 & 0xff], x0);
  111. float t1 = 1.0f - x1 * x1;
  112. t1 *= t1;
  113. n1 = t1 * t1 * grad(perm[i1 & 0xff], x1);
  114. // The maximum value of this noise is 8*(3/4)^4 = 2.53125
  115. // A factor of 0.395 scales to fit exactly within [-1,1]
  116. return 0.395f * (n0 + n1);
  117. }
  118. /// <summary>
  119. /// 2D simplex noise
  120. /// </summary>
  121. /// <param name="x"></param>
  122. /// <param name="y"></param>
  123. /// <returns></returns>
  124. internal static float Generate(float x, float y)
  125. {
  126. const float F2 = 0.366025403f; // F2 = 0.5*(sqrt(3.0)-1.0)
  127. const float G2 = 0.211324865f; // G2 = (3.0-Math.sqrt(3.0))/6.0
  128. float n0, n1, n2; // Noise contributions from the three corners
  129. // Skew the input space to determine which simplex cell we're in
  130. float s = (x + y) * F2; // Hairy factor for 2D
  131. float xs = x + s;
  132. float ys = y + s;
  133. int i = FastFloor(xs);
  134. int j = FastFloor(ys);
  135. float t = (float)(i + j) * G2;
  136. float X0 = i - t; // Unskew the cell origin back to (x,y) space
  137. float Y0 = j - t;
  138. float x0 = x - X0; // The x,y distances from the cell origin
  139. float y0 = y - Y0;
  140. // For the 2D case, the simplex shape is an equilateral triangle.
  141. // Determine which simplex we are in.
  142. int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
  143. if (x0 > y0) { i1 = 1; j1 = 0; } // lower triangle, XY order: (0,0)->(1,0)->(1,1)
  144. else { i1 = 0; j1 = 1; } // upper triangle, YX order: (0,0)->(0,1)->(1,1)
  145. // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
  146. // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
  147. // c = (3-sqrt(3))/6
  148. float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
  149. float y1 = y0 - j1 + G2;
  150. float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords
  151. float y2 = y0 - 1.0f + 2.0f * G2;
  152. // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
  153. int ii = Mod(i, 256);
  154. int jj = Mod(j, 256);
  155. // Calculate the contribution from the three corners
  156. float t0 = 0.5f - x0 * x0 - y0 * y0;
  157. if (t0 < 0.0f) n0 = 0.0f;
  158. else
  159. {
  160. t0 *= t0;
  161. n0 = t0 * t0 * grad(perm[ii + perm[jj]], x0, y0);
  162. }
  163. float t1 = 0.5f - x1 * x1 - y1 * y1;
  164. if (t1 < 0.0f) n1 = 0.0f;
  165. else
  166. {
  167. t1 *= t1;
  168. n1 = t1 * t1 * grad(perm[ii + i1 + perm[jj + j1]], x1, y1);
  169. }
  170. float t2 = 0.5f - x2 * x2 - y2 * y2;
  171. if (t2 < 0.0f) n2 = 0.0f;
  172. else
  173. {
  174. t2 *= t2;
  175. n2 = t2 * t2 * grad(perm[ii + 1 + perm[jj + 1]], x2, y2);
  176. }
  177. // Add contributions from each corner to get the final noise value.
  178. // The result is scaled to return values in the interval [-1,1].
  179. return 40.0f * (n0 + n1 + n2); // TODO: The scale factor is preliminary!
  180. }
  181. internal static float Generate(float x, float y, float z)
  182. {
  183. // Simple skewing factors for the 3D case
  184. const float F3 = 0.333333333f;
  185. const float G3 = 0.166666667f;
  186. float n0, n1, n2, n3; // Noise contributions from the four corners
  187. // Skew the input space to determine which simplex cell we're in
  188. float s = (x + y + z) * F3; // Very nice and simple skew factor for 3D
  189. float xs = x + s;
  190. float ys = y + s;
  191. float zs = z + s;
  192. int i = FastFloor(xs);
  193. int j = FastFloor(ys);
  194. int k = FastFloor(zs);
  195. float t = (float)(i + j + k) * G3;
  196. float X0 = i - t; // Unskew the cell origin back to (x,y,z) space
  197. float Y0 = j - t;
  198. float Z0 = k - t;
  199. float x0 = x - X0; // The x,y,z distances from the cell origin
  200. float y0 = y - Y0;
  201. float z0 = z - Z0;
  202. // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
  203. // Determine which simplex we are in.
  204. int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
  205. int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
  206. /* This code would benefit from a backport from the GLSL version! */
  207. if (x0 >= y0)
  208. {
  209. if (y0 >= z0)
  210. { i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } // X Y Z order
  211. else if (x0 >= z0) { i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 0; k2 = 1; } // X Z Y order
  212. else { i1 = 0; j1 = 0; k1 = 1; i2 = 1; j2 = 0; k2 = 1; } // Z X Y order
  213. }
  214. else
  215. { // x0<y0
  216. if (y0 < z0) { i1 = 0; j1 = 0; k1 = 1; i2 = 0; j2 = 1; k2 = 1; } // Z Y X order
  217. else if (x0 < z0) { i1 = 0; j1 = 1; k1 = 0; i2 = 0; j2 = 1; k2 = 1; } // Y Z X order
  218. else { i1 = 0; j1 = 1; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } // Y X Z order
  219. }
  220. // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
  221. // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
  222. // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
  223. // c = 1/6.
  224. float x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
  225. float y1 = y0 - j1 + G3;
  226. float z1 = z0 - k1 + G3;
  227. float x2 = x0 - i2 + 2.0f * G3; // Offsets for third corner in (x,y,z) coords
  228. float y2 = y0 - j2 + 2.0f * G3;
  229. float z2 = z0 - k2 + 2.0f * G3;
  230. float x3 = x0 - 1.0f + 3.0f * G3; // Offsets for last corner in (x,y,z) coords
  231. float y3 = y0 - 1.0f + 3.0f * G3;
  232. float z3 = z0 - 1.0f + 3.0f * G3;
  233. // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
  234. int ii = Mod(i, 256);
  235. int jj = Mod(j, 256);
  236. int kk = Mod(k, 256);
  237. // Calculate the contribution from the four corners
  238. float t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0;
  239. if (t0 < 0.0f) n0 = 0.0f;
  240. else
  241. {
  242. t0 *= t0;
  243. n0 = t0 * t0 * grad(perm[ii + perm[jj + perm[kk]]], x0, y0, z0);
  244. }
  245. float t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1;
  246. if (t1 < 0.0f) n1 = 0.0f;
  247. else
  248. {
  249. t1 *= t1;
  250. n1 = t1 * t1 * grad(perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]], x1, y1, z1);
  251. }
  252. float t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2;
  253. if (t2 < 0.0f) n2 = 0.0f;
  254. else
  255. {
  256. t2 *= t2;
  257. n2 = t2 * t2 * grad(perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]], x2, y2, z2);
  258. }
  259. float t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3;
  260. if (t3 < 0.0f) n3 = 0.0f;
  261. else
  262. {
  263. t3 *= t3;
  264. n3 = t3 * t3 * grad(perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]], x3, y3, z3);
  265. }
  266. // Add contributions from each corner to get the final noise value.
  267. // The result is scaled to stay just inside [-1,1]
  268. return 32.0f * (n0 + n1 + n2 + n3); // TODO: The scale factor is preliminary!
  269. }
  270. private static byte[] perm;
  271. private static readonly byte[] permOriginal = new byte[]
  272. {
  273. 151,160,137,91,90,15,
  274. 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  275. 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  276. 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  277. 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  278. 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  279. 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  280. 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  281. 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  282. 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  283. 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  284. 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  285. 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
  286. 151,160,137,91,90,15,
  287. 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  288. 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  289. 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  290. 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  291. 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  292. 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  293. 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  294. 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  295. 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  296. 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  297. 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  298. 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
  299. };
  300. private static int FastFloor(float x)
  301. {
  302. return (x > 0) ? ((int)x) : (((int)x) - 1);
  303. }
  304. private static int Mod(int x, int m)
  305. {
  306. int a = x % m;
  307. return a < 0 ? a + m : a;
  308. }
  309. private static float grad(int hash, float x)
  310. {
  311. int h = hash & 15;
  312. float grad = 1.0f + (h & 7); // Gradient value 1.0, 2.0, ..., 8.0
  313. if ((h & 8) != 0) grad = -grad; // Set a random sign for the gradient
  314. return (grad * x); // Multiply the gradient with the distance
  315. }
  316. private static float grad(int hash, float x, float y)
  317. {
  318. int h = hash & 7; // Convert low 3 bits of hash code
  319. float u = h < 4 ? x : y; // into 8 simple gradient directions,
  320. float v = h < 4 ? y : x; // and compute the dot product with (x,y).
  321. return ((h & 1) != 0 ? -u : u) + ((h & 2) != 0 ? -2.0f * v : 2.0f * v);
  322. }
  323. private static float grad(int hash, float x, float y, float z)
  324. {
  325. int h = hash & 15; // Convert low 4 bits of hash code into 12 simple
  326. float u = h < 8 ? x : y; // gradient directions, and compute dot product.
  327. float v = h < 4 ? y : h == 12 || h == 14 ? x : z; // Fix repeats at h = 12 to 15
  328. return ((h & 1) != 0 ? -u : u) + ((h & 2) != 0 ? -v : v);
  329. }
  330. private static float grad(int hash, float x, float y, float z, float t)
  331. {
  332. int h = hash & 31; // Convert low 5 bits of hash code into 32 simple
  333. float u = h < 24 ? x : y; // gradient directions, and compute dot product.
  334. float v = h < 16 ? y : z;
  335. float w = h < 8 ? z : t;
  336. return ((h & 1) != 0 ? -u : u) + ((h & 2) != 0 ? -v : v) + ((h & 4) != 0 ? -w : w);
  337. }
  338. }
  339. }