simplex.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. --- Simplex Noise
  2. -- @module simplex
  3. --
  4. -- Based on code in "Simplex noise demystified", by Stefan Gustavson
  5. -- www.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
  6. --
  7. -- Thanks to Mike Pall for some cleanup and improvements (and for LuaJIT!)
  8. --
  9. -- Permission is hereby granted, free of charge, to any person obtaining
  10. -- a copy of this software and associated documentation files (the
  11. -- "Software"), to deal in the Software without restriction, including
  12. -- without limitation the rights to use, copy, modify, merge, publish,
  13. -- distribute, sublicense, and/or sell copies of the Software, and to
  14. -- permit persons to whom the Software is furnished to do so, subject to
  15. -- the following conditions:
  16. --
  17. -- The above copyright notice and this permission notice shall be
  18. -- included in all copies or substantial portions of the Software.
  19. --
  20. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24. -- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. -- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. --
  28. -- [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
  29. --
  30. if _G.love and _G.love.math then
  31. return love.math.noise
  32. end
  33. -- Bail out with dummy module if FFI is missing.
  34. local has_ffi, ffi = pcall(require, "ffi")
  35. if not has_ffi then
  36. return function()
  37. return 0
  38. end
  39. end
  40. -- Modules --
  41. local bit = require("bit")
  42. -- Imports --
  43. local band = bit.band
  44. local bor = bit.bor
  45. local floor = math.floor
  46. local lshift = bit.lshift
  47. local max = math.max
  48. local rshift = bit.rshift
  49. -- Permutation of 0-255, replicated to allow easy indexing with sums of two bytes --
  50. local Perms = ffi.new("uint8_t[512]", {
  51. 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225,
  52. 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148,
  53. 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32,
  54. 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175,
  55. 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122,
  56. 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54,
  57. 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169,
  58. 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64,
  59. 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212,
  60. 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213,
  61. 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
  62. 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104,
  63. 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241,
  64. 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157,
  65. 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93,
  66. 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180
  67. })
  68. -- The above, mod 12 for each element --
  69. local Perms12 = ffi.new("uint8_t[512]")
  70. for i = 0, 255 do
  71. local x = Perms[i] % 12
  72. Perms[i + 256], Perms12[i], Perms12[i + 256] = Perms[i], x, x
  73. end
  74. -- Gradients for 2D, 3D case --
  75. local Grads3 = ffi.new("const double[12][3]",
  76. { 1, 1, 0 }, { -1, 1, 0 }, { 1, -1, 0 }, { -1, -1, 0 },
  77. { 1, 0, 1 }, { -1, 0, 1 }, { 1, 0, -1 }, { -1, 0, -1 },
  78. { 0, 1, 1 }, { 0, -1, 1 }, { 0, 1, -1 }, { 0, -1, -1 }
  79. )
  80. -- 2D weight contribution
  81. local function GetN2(bx, by, x, y)
  82. local t = .5 - x * x - y * y
  83. local index = Perms12[bx + Perms[by]]
  84. return max(0, (t * t) * (t * t)) * (Grads3[index][0] * x + Grads3[index][1] * y)
  85. end
  86. local function simplex_2d(x, y)
  87. --[[
  88. 2D skew factors:
  89. F = (math.sqrt(3) - 1) / 2
  90. G = (3 - math.sqrt(3)) / 6
  91. G2 = 2 * G - 1
  92. ]]
  93. -- Skew the input space to determine which simplex cell we are in.
  94. local s = (x + y) * 0.366025403 -- F
  95. local ix, iy = floor(x + s), floor(y + s)
  96. -- Unskew the cell origin back to (x, y) space.
  97. local t = (ix + iy) * 0.211324865 -- G
  98. local x0 = x + t - ix
  99. local y0 = y + t - iy
  100. -- Calculate the contribution from the two fixed corners.
  101. -- A step of (1,0) in (i,j) means a step of (1-G,-G) in (x,y), and
  102. -- A step of (0,1) in (i,j) means a step of (-G,1-G) in (x,y).
  103. ix, iy = band(ix, 255), band(iy, 255)
  104. local n0 = GetN2(ix, iy, x0, y0)
  105. local n2 = GetN2(ix + 1, iy + 1, x0 - 0.577350270, y0 - 0.577350270) -- G2
  106. --[[
  107. Determine other corner based on simplex (equilateral triangle) we are in:
  108. if x0 > y0 then
  109. ix, x1 = ix + 1, x1 - 1
  110. else
  111. iy, y1 = iy + 1, y1 - 1
  112. end
  113. ]]
  114. local xi = rshift(floor(y0 - x0), 31) -- y0 < x0
  115. local n1 = GetN2(ix + xi, iy + (1 - xi), x0 + 0.211324865 - xi, y0 - 0.788675135 + xi) -- x0 + G - xi, y0 + G - (1 - xi)
  116. -- Add contributions from each corner to get the final noise value.
  117. -- The result is scaled to return values in the interval [-1,1].
  118. return 70.1480580019 * (n0 + n1 + n2)
  119. end
  120. -- 3D weight contribution
  121. local function GetN3(ix, iy, iz, x, y, z)
  122. local t = .6 - x * x - y * y - z * z
  123. local index = Perms12[ix + Perms[iy + Perms[iz]]]
  124. return max(0, (t * t) * (t * t)) * (Grads3[index][0] * x + Grads3[index][1] * y + Grads3[index][2] * z)
  125. end
  126. local function simplex_3d(x, y, z)
  127. --[[
  128. 3D skew factors:
  129. F = 1 / 3
  130. G = 1 / 6
  131. G2 = 2 * G
  132. G3 = 3 * G - 1
  133. ]]
  134. -- Skew the input space to determine which simplex cell we are in.
  135. local s = (x + y + z) * 0.333333333 -- F
  136. local ix, iy, iz = floor(x + s), floor(y + s), floor(z + s)
  137. -- Unskew the cell origin back to (x, y, z) space.
  138. local t = (ix + iy + iz) * 0.166666667 -- G
  139. local x0 = x + t - ix
  140. local y0 = y + t - iy
  141. local z0 = z + t - iz
  142. -- Calculate the contribution from the two fixed corners.
  143. -- A step of (1,0,0) in (i,j,k) means a step of (1-G,-G,-G) in (x,y,z);
  144. -- a step of (0,1,0) in (i,j,k) means a step of (-G,1-G,-G) in (x,y,z);
  145. -- a step of (0,0,1) in (i,j,k) means a step of (-G,-G,1-G) in (x,y,z).
  146. ix, iy, iz = band(ix, 255), band(iy, 255), band(iz, 255)
  147. local n0 = GetN3(ix, iy, iz, x0, y0, z0)
  148. local n3 = GetN3(ix + 1, iy + 1, iz + 1, x0 - 0.5, y0 - 0.5, z0 - 0.5) -- G3
  149. --[[
  150. Determine other corners based on simplex (skewed tetrahedron) we are in:
  151. if x0 >= y0 then -- ~A
  152. if y0 >= z0 then -- ~A and ~B
  153. i1, j1, k1, i2, j2, k2 = 1, 0, 0, 1, 1, 0
  154. elseif x0 >= z0 then -- ~A and B and ~C
  155. i1, j1, k1, i2, j2, k2 = 1, 0, 0, 1, 0, 1
  156. else -- ~A and B and C
  157. i1, j1, k1, i2, j2, k2 = 0, 0, 1, 1, 0, 1
  158. end
  159. else -- A
  160. if y0 < z0 then -- A and B
  161. i1, j1, k1, i2, j2, k2 = 0, 0, 1, 0, 1, 1
  162. elseif x0 < z0 then -- A and ~B and C
  163. i1, j1, k1, i2, j2, k2 = 0, 1, 0, 0, 1, 1
  164. else -- A and ~B and ~C
  165. i1, j1, k1, i2, j2, k2 = 0, 1, 0, 1, 1, 0
  166. end
  167. end
  168. ]]
  169. local xLy = rshift(floor(x0 - y0), 31) -- x0 < y0
  170. local yLz = rshift(floor(y0 - z0), 31) -- y0 < z0
  171. local xLz = rshift(floor(x0 - z0), 31) -- x0 < z0
  172. local i1 = band(1 - xLy, bor(1 - yLz, 1 - xLz)) -- x0 >= y0 and (y0 >= z0 or x0 >= z0)
  173. local j1 = band(xLy, 1 - yLz) -- x0 < y0 and y0 >= z0
  174. local k1 = band(yLz, bor(xLy, xLz)) -- y0 < z0 and (x0 < y0 or x0 < z0)
  175. local i2 = bor(1 - xLy, band(1 - yLz, 1 - xLz)) -- x0 >= y0 or (y0 >= z0 and x0 >= z0)
  176. local j2 = bor(xLy, 1 - yLz) -- x0 < y0 or y0 >= z0
  177. local k2 = bor(band(1 - xLy, yLz), band(xLy, bor(yLz, xLz))) -- (x0 >= y0 and y0 < z0) or (x0 < y0 and (y0 < z0 or x0 < z0))
  178. local n1 = GetN3(ix + i1, iy + j1, iz + k1, x0 + 0.166666667 - i1, y0 + 0.166666667 - j1, z0 + 0.166666667 - k1) -- G
  179. local n2 = GetN3(ix + i2, iy + j2, iz + k2, x0 + 0.333333333 - i2, y0 + 0.333333333 - j2, z0 + 0.333333333 - k2) -- G2
  180. -- Add contributions from each corner to get the final noise value.
  181. -- The result is scaled to stay just inside [-1,1]
  182. return 28.452842 * (n0 + n1 + n2 + n3)
  183. end
  184. -- Gradients for 4D case --
  185. local Grads4 = ffi.new("const double[32][4]",
  186. { 0, 1, 1, 1 }, { 0, 1, 1, -1 }, { 0, 1, -1, 1 }, { 0, 1, -1, -1 },
  187. { 0, -1, 1, 1 }, { 0, -1, 1, -1 }, { 0, -1, -1, 1 }, { 0, -1, -1, -1 },
  188. { 1, 0, 1, 1 }, { 1, 0, 1, -1 }, { 1, 0, -1, 1 }, { 1, 0, -1, -1 },
  189. { -1, 0, 1, 1 }, { -1, 0, 1, -1 }, { -1, 0, -1, 1 }, { -1, 0, -1, -1 },
  190. { 1, 1, 0, 1 }, { 1, 1, 0, -1 }, { 1, -1, 0, 1 }, { 1, -1, 0, -1 },
  191. { -1, 1, 0, 1 }, { -1, 1, 0, -1 }, { -1, -1, 0, 1 }, { -1, -1, 0, -1 },
  192. { 1, 1, 1, 0 }, { 1, 1, -1, 0 }, { 1, -1, 1, 0 }, { 1, -1, -1, 0 },
  193. { -1, 1, 1, 0 }, { -1, 1, -1, 0 }, { -1, -1, 1, 0 }, { -1, -1, -1, 0 }
  194. )
  195. -- 4D weight contribution
  196. local function GetN4(ix, iy, iz, iw, x, y, z, w)
  197. local t = .6 - x * x - y * y - z * z - w * w
  198. local index = band(Perms[ix + Perms[iy + Perms[iz + Perms[iw]]]], 0x1F)
  199. return max(0, (t * t) * (t * t)) * (Grads4[index][0] * x + Grads4[index][1] * y + Grads4[index][2] * z + Grads4[index][3] * w)
  200. end
  201. -- A lookup table to traverse the simplex around a given point in 4D.
  202. -- Details can be found where this table is used, in the 4D noise method.
  203. local Simplex = ffi.new("uint8_t[64][4]",
  204. { 0, 1, 2, 3 }, { 0, 1, 3, 2 }, {}, { 0, 2, 3, 1 }, {}, {}, {}, { 1, 2, 3 },
  205. { 0, 2, 1, 3 }, {}, { 0, 3, 1, 2 }, { 0, 3, 2, 1 }, {}, {}, {}, { 1, 3, 2 },
  206. {}, {}, {}, {}, {}, {}, {}, {},
  207. { 1, 2, 0, 3 }, {}, { 1, 3, 0, 2 }, {}, {}, {}, { 2, 3, 0, 1 }, { 2, 3, 1 },
  208. { 1, 0, 2, 3 }, { 1, 0, 3, 2 }, {}, {}, {}, { 2, 0, 3, 1 }, {}, { 2, 1, 3 },
  209. {}, {}, {}, {}, {}, {}, {}, {},
  210. { 2, 0, 1, 3 }, {}, {}, {}, { 3, 0, 1, 2 }, { 3, 0, 2, 1 }, {}, { 3, 1, 2 },
  211. { 2, 1, 0, 3 }, {}, {}, {}, { 3, 1, 0, 2 }, {}, { 3, 2, 0, 1 }, { 3, 2, 1 }
  212. )
  213. -- Convert the above indices to masks that can be shifted / anded into offsets --
  214. for i = 0, 63 do
  215. Simplex[i][0] = lshift(1, Simplex[i][0]) - 1
  216. Simplex[i][1] = lshift(1, Simplex[i][1]) - 1
  217. Simplex[i][2] = lshift(1, Simplex[i][2]) - 1
  218. Simplex[i][3] = lshift(1, Simplex[i][3]) - 1
  219. end
  220. local function simplex_4d(x, y, z, w)
  221. --[[
  222. 4D skew factors:
  223. F = (math.sqrt(5) - 1) / 4
  224. G = (5 - math.sqrt(5)) / 20
  225. G2 = 2 * G
  226. G3 = 3 * G
  227. G4 = 4 * G - 1
  228. ]]
  229. -- Skew the input space to determine which simplex cell we are in.
  230. local s = (x + y + z + w) * 0.309016994 -- F
  231. local ix, iy, iz, iw = floor(x + s), floor(y + s), floor(z + s), floor(w + s)
  232. -- Unskew the cell origin back to (x, y, z) space.
  233. local t = (ix + iy + iz + iw) * 0.138196601 -- G
  234. local x0 = x + t - ix
  235. local y0 = y + t - iy
  236. local z0 = z + t - iz
  237. local w0 = w + t - iw
  238. -- For the 4D case, the simplex is a 4D shape I won't even try to describe.
  239. -- To find out which of the 24 possible simplices we're in, we need to
  240. -- determine the magnitude ordering of x0, y0, z0 and w0.
  241. -- The method below is a good way of finding the ordering of x,y,z,w and
  242. -- then find the correct traversal order for the simplex we�re in.
  243. -- First, six pair-wise comparisons are performed between each possible pair
  244. -- of the four coordinates, and the results are used to add up binary bits
  245. -- for an integer index.
  246. local c1 = band(rshift(floor(y0 - x0), 26), 32)
  247. local c2 = band(rshift(floor(z0 - x0), 27), 16)
  248. local c3 = band(rshift(floor(z0 - y0), 28), 8)
  249. local c4 = band(rshift(floor(w0 - x0), 29), 4)
  250. local c5 = band(rshift(floor(w0 - y0), 30), 2)
  251. local c6 = rshift(floor(w0 - z0), 31)
  252. -- Simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
  253. -- Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
  254. -- impossible. Only the 24 indices which have non-zero entries make any sense.
  255. -- We use a thresholding to set the coordinates in turn from the largest magnitude.
  256. local c = c1 + c2 + c3 + c4 + c5 + c6
  257. -- The number 3 (i.e. bit 2) in the "simplex" array is at the position of the largest coordinate.
  258. local i1 = rshift(Simplex[c][0], 2)
  259. local j1 = rshift(Simplex[c][1], 2)
  260. local k1 = rshift(Simplex[c][2], 2)
  261. local l1 = rshift(Simplex[c][3], 2)
  262. -- The number 2 (i.e. bit 1) in the "simplex" array is at the second largest coordinate.
  263. local i2 = band(rshift(Simplex[c][0], 1), 1)
  264. local j2 = band(rshift(Simplex[c][1], 1), 1)
  265. local k2 = band(rshift(Simplex[c][2], 1), 1)
  266. local l2 = band(rshift(Simplex[c][3], 1), 1)
  267. -- The number 1 (i.e. bit 0) in the "simplex" array is at the second smallest coordinate.
  268. local i3 = band(Simplex[c][0], 1)
  269. local j3 = band(Simplex[c][1], 1)
  270. local k3 = band(Simplex[c][2], 1)
  271. local l3 = band(Simplex[c][3], 1)
  272. -- Work out the hashed gradient indices of the five simplex corners
  273. -- Sum up and scale the result to cover the range [-1,1]
  274. ix, iy, iz, iw = band(ix, 255), band(iy, 255), band(iz, 255), band(iw, 255)
  275. local n0 = GetN4(ix, iy, iz, iw, x0, y0, z0, w0)
  276. local n1 = GetN4(ix + i1, iy + j1, iz + k1, iw + l1, x0 + 0.138196601 - i1, y0 + 0.138196601 - j1, z0 + 0.138196601 - k1, w0 + 0.138196601 - l1) -- G
  277. local n2 = GetN4(ix + i2, iy + j2, iz + k2, iw + l2, x0 + 0.276393202 - i2, y0 + 0.276393202 - j2, z0 + 0.276393202 - k2, w0 + 0.276393202 - l2) -- G2
  278. local n3 = GetN4(ix + i3, iy + j3, iz + k3, iw + l3, x0 + 0.414589803 - i3, y0 + 0.414589803 - j3, z0 + 0.414589803 - k3, w0 + 0.414589803 - l3) -- G3
  279. local n4 = GetN4(ix + 1, iy + 1, iz + 1, iw + 1, x0 - 0.447213595, y0 - 0.447213595, z0 - 0.447213595, w0 - 0.447213595) -- G4
  280. return 2.210600293 * (n0 + n1 + n2 + n3 + n4)
  281. end
  282. --- Simplex Noise
  283. -- @param x
  284. -- @param y
  285. -- @param z optional
  286. -- @param w optional
  287. -- @return Noise value in the range [-1, +1]
  288. return function(x, y, z, w)
  289. if w then
  290. return simplex_4d(x, y, z, w)
  291. end
  292. if z then
  293. return simplex_3d(x, y, z)
  294. end
  295. if y then
  296. return simplex_2d(x, y)
  297. end
  298. error "Simplex requires at least two arguments"
  299. end