perlin.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. # Copyright (c) 2008, Casey Duncan (casey dot duncan at gmail dot com)
  2. # see LICENSE.txt for details
  3. """Noise functions for procedural generation of content
  4. Contains native code implementations of Perlin improved noise (with
  5. fBm capabilities) and Perlin simplex noise. Also contains a fast
  6. "fake noise" implementation in GLSL for execution in shaders.
  7. Copyright (c) 2008, Casey Duncan (casey dot duncan at gmail dot com)
  8. """
  9. __version__ = "1.2.1"
  10. from math import floor, fmod, sqrt
  11. from random import randint
  12. # 3D Gradient vectors
  13. _GRAD3 = ((1, 1, 0), (-1, 1, 0), (1, -1, 0), (-1, -1, 0),
  14. (1, 0, 1), (-1, 0, 1), (1, 0, -1), (-1, 0, -1),
  15. (0, 1, 1), (0, -1, 1), (0, 1, -1), (0, -1, -1),
  16. (1, 1, 0), (0, -1, 1), (-1, 1, 0), (0, -1, -1),
  17. )
  18. # 4D Gradient vectors
  19. _GRAD4 = ((0, 1, 1, 1), (0, 1, 1, -1), (0, 1, -1, 1), (0, 1, -1, -1),
  20. (0, -1, 1, 1), (0, -1, 1, -1), (0, -1, -1, 1), (0, -1, -1, -1),
  21. (1, 0, 1, 1), (1, 0, 1, -1), (1, 0, -1, 1), (1, 0, -1, -1),
  22. (-1, 0, 1, 1), (-1, 0, 1, -1), (-1, 0, -1, 1), (-1, 0, -1, -1),
  23. (1, 1, 0, 1), (1, 1, 0, -1), (1, -1, 0, 1), (1, -1, 0, -1),
  24. (-1, 1, 0, 1), (-1, 1, 0, -1), (-1, -1, 0, 1), (-1, -1, 0, -1),
  25. (1, 1, 1, 0), (1, 1, -1, 0), (1, -1, 1, 0), (1, -1, -1, 0),
  26. (-1, 1, 1, 0), (-1, 1, -1, 0), (-1, -1, 1, 0), (-1, -1, -1, 0))
  27. # A lookup table to traverse the simplex around a given point in 4D.
  28. # Details can be found where this table is used, in the 4D noise method.
  29. _SIMPLEX = (
  30. (0, 1, 2, 3), (0, 1, 3, 2), (0, 0, 0, 0), (0, 2, 3, 1), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (1, 2, 3, 0),
  31. (0, 2, 1, 3), (0, 0, 0, 0), (0, 3, 1, 2), (0, 3, 2, 1), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (1, 3, 2, 0),
  32. (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0),
  33. (1, 2, 0, 3), (0, 0, 0, 0), (1, 3, 0, 2), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (2, 3, 0, 1), (2, 3, 1, 0),
  34. (1, 0, 2, 3), (1, 0, 3, 2), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (2, 0, 3, 1), (0, 0, 0, 0), (2, 1, 3, 0),
  35. (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0),
  36. (2, 0, 1, 3), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (3, 0, 1, 2), (3, 0, 2, 1), (0, 0, 0, 0), (3, 1, 2, 0),
  37. (2, 1, 0, 3), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (3, 1, 0, 2), (0, 0, 0, 0), (3, 2, 0, 1), (3, 2, 1, 0))
  38. # Simplex skew constants
  39. _F2 = 0.5 * (sqrt(3.0) - 1.0)
  40. _G2 = (3.0 - sqrt(3.0)) / 6.0
  41. _F3 = 1.0 / 3.0
  42. _G3 = 1.0 / 6.0
  43. class BaseNoise:
  44. """Noise abstract base class"""
  45. permutation = (151, 160, 137, 91, 90, 15,
  46. 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
  47. 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33,
  48. 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,
  49. 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244,
  50. 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196,
  51. 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123,
  52. 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42,
  53. 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
  54. 129, 22, 39, 253, 9, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228,
  55. 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
  56. 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254,
  57. 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180)
  58. period = len(permutation)
  59. # Double permutation array so we don't need to wrap
  60. permutation = permutation * 2
  61. def __init__(self, period=None, permutation_table=None):
  62. """Initialize the noise generator. With no arguments, the default
  63. period and permutation table are used (256). The default permutation
  64. table generates the exact same noise pattern each time.
  65. An integer period can be specified, to generate a random permutation
  66. table with period elements. The period determines the (integer)
  67. interval that the noise repeats, which is useful for creating tiled
  68. textures. period should be a power-of-two, though this is not
  69. enforced. Note that the speed of the noise algorithm is independent of
  70. the period size, though larger periods mean a larger table, which
  71. consume more memory.
  72. A permutation table consisting of an iterable sequence of whole
  73. numbers can be specified directly. This should have a power-of-two
  74. length. Typical permutation tables are a sequnce of unique integers in
  75. the range [0,period) in random order, though other arrangements could
  76. prove useful, they will not be "pure" simplex noise. The largest
  77. element in the sequence must be no larger than period-1.
  78. period and permutation_table may not be specified together.
  79. """
  80. if period is not None and permutation_table is not None:
  81. raise ValueError(
  82. 'Can specify either period or permutation_table, not both')
  83. if period is not None:
  84. self.randomize(period)
  85. elif permutation_table is not None:
  86. self.permutation = tuple(permutation_table) * 2
  87. self.period = len(permutation_table)
  88. def randomize(self, period=None):
  89. """Randomize the permutation table used by the noise functions. This
  90. makes them generate a different noise pattern for the same inputs.
  91. """
  92. if period is not None:
  93. self.period = period
  94. perm = list(range(self.period))
  95. perm_right = self.period - 1
  96. for i in list(perm):
  97. j = randint(0, perm_right)
  98. perm[i], perm[j] = perm[j], perm[i]
  99. self.permutation = tuple(perm) * 2
  100. class SimplexNoise(BaseNoise):
  101. """Perlin simplex noise generator
  102. Adapted from Stefan Gustavson's Java implementation described here:
  103. http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
  104. To summarize:
  105. "In 2001, Ken Perlin presented 'simplex noise', a replacement for his classic
  106. noise algorithm. Classic 'Perlin noise' won him an academy award and has
  107. become an ubiquitous procedural primitive for computer graphics over the
  108. years, but in hindsight it has quite a few limitations. Ken Perlin himself
  109. designed simplex noise specifically to overcome those limitations, and he
  110. spent a lot of good thinking on it. Therefore, it is a better idea than his
  111. original algorithm. A few of the more prominent advantages are:
  112. * Simplex noise has a lower computational complexity and requires fewer
  113. multiplications.
  114. * Simplex noise scales to higher dimensions (4D, 5D and up) with much less
  115. computational cost, the complexity is O(N) for N dimensions instead of
  116. the O(2^N) of classic Noise.
  117. * Simplex noise has no noticeable directional artifacts. Simplex noise has
  118. a well-defined and continuous gradient everywhere that can be computed
  119. quite cheaply.
  120. * Simplex noise is easy to implement in hardware."
  121. """
  122. def noise2(self, x, y):
  123. """2D Perlin simplex noise.
  124. Return a floating point value from -1 to 1 for the given x, y coordinate.
  125. The same value is always returned for a given x, y pair unless the
  126. permutation table changes (see randomize above).
  127. """
  128. # Skew input space to determine which simplex (triangle) we are in
  129. s = (x + y) * _F2
  130. i = floor(x + s)
  131. j = floor(y + s)
  132. t = (i + j) * _G2
  133. x0 = x - (i - t) # "Unskewed" distances from cell origin
  134. y0 = y - (j - t)
  135. if x0 > y0:
  136. i1 = 1
  137. j1 = 0 # Lower triangle, XY order: (0,0)->(1,0)->(1,1)
  138. else:
  139. i1 = 0
  140. j1 = 1 # Upper triangle, YX order: (0,0)->(0,1)->(1,1)
  141. x1 = x0 - i1 + _G2 # Offsets for middle corner in (x,y) unskewed coords
  142. y1 = y0 - j1 + _G2
  143. x2 = x0 + _G2 * 2.0 - 1.0 # Offsets for last corner in (x,y) unskewed coords
  144. y2 = y0 + _G2 * 2.0 - 1.0
  145. # Determine hashed gradient indices of the three simplex corners
  146. perm = self.permutation
  147. ii = int(i) % self.period
  148. jj = int(j) % self.period
  149. gi0 = perm[ii + perm[jj]] % 12
  150. gi1 = perm[ii + i1 + perm[jj + j1]] % 12
  151. gi2 = perm[ii + 1 + perm[jj + 1]] % 12
  152. # Calculate the contribution from the three corners
  153. tt = 0.5 - x0 ** 2 - y0 ** 2
  154. if tt > 0:
  155. g = _GRAD3[gi0]
  156. noise = tt ** 4 * (g[0] * x0 + g[1] * y0)
  157. else:
  158. noise = 0.0
  159. tt = 0.5 - x1 ** 2 - y1 ** 2
  160. if tt > 0:
  161. g = _GRAD3[gi1]
  162. noise += tt ** 4 * (g[0] * x1 + g[1] * y1)
  163. tt = 0.5 - x2 ** 2 - y2 ** 2
  164. if tt > 0:
  165. g = _GRAD3[gi2]
  166. noise += tt ** 4 * (g[0] * x2 + g[1] * y2)
  167. return noise * 70.0 # scale noise to [-1, 1]
  168. def noise3(self, x, y, z):
  169. """3D Perlin simplex noise.
  170. Return a floating point value from -1 to 1 for the given x, y, z coordinate.
  171. The same value is always returned for a given x, y, z pair unless the
  172. permutation table changes (see randomize above).
  173. """
  174. # Skew the input space to determine which simplex cell we're in
  175. s = (x + y + z) * _F3
  176. i = floor(x + s)
  177. j = floor(y + s)
  178. k = floor(z + s)
  179. t = (i + j + k) * _G3
  180. x0 = x - (i - t) # "Unskewed" distances from cell origin
  181. y0 = y - (j - t)
  182. z0 = z - (k - t)
  183. # For the 3D case, the simplex shape is a slightly irregular tetrahedron.
  184. # Determine which simplex we are in.
  185. if x0 >= y0:
  186. if y0 >= z0:
  187. i1 = 1
  188. j1 = 0
  189. k1 = 0
  190. i2 = 1
  191. j2 = 1
  192. k2 = 0
  193. elif x0 >= z0:
  194. i1 = 1
  195. j1 = 0
  196. k1 = 0
  197. i2 = 1
  198. j2 = 0
  199. k2 = 1
  200. else:
  201. i1 = 0
  202. j1 = 0
  203. k1 = 1
  204. i2 = 1
  205. j2 = 0
  206. k2 = 1
  207. else: # x0 < y0
  208. if y0 < z0:
  209. i1 = 0
  210. j1 = 0
  211. k1 = 1
  212. i2 = 0
  213. j2 = 1
  214. k2 = 1
  215. elif x0 < z0:
  216. i1 = 0
  217. j1 = 1
  218. k1 = 0
  219. i2 = 0
  220. j2 = 1
  221. k2 = 1
  222. else:
  223. i1 = 0
  224. j1 = 1
  225. k1 = 0
  226. i2 = 1
  227. j2 = 1
  228. k2 = 0
  229. # Offsets for remaining corners
  230. x1 = x0 - i1 + _G3
  231. y1 = y0 - j1 + _G3
  232. z1 = z0 - k1 + _G3
  233. x2 = x0 - i2 + 2.0 * _G3
  234. y2 = y0 - j2 + 2.0 * _G3
  235. z2 = z0 - k2 + 2.0 * _G3
  236. x3 = x0 - 1.0 + 3.0 * _G3
  237. y3 = y0 - 1.0 + 3.0 * _G3
  238. z3 = z0 - 1.0 + 3.0 * _G3
  239. # Calculate the hashed gradient indices of the four simplex corners
  240. perm = self.permutation
  241. ii = int(i) % self.period
  242. jj = int(j) % self.period
  243. kk = int(k) % self.period
  244. gi0 = perm[ii + perm[jj + perm[kk]]] % 12
  245. gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]] % 12
  246. gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]] % 12
  247. gi3 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]] % 12
  248. # Calculate the contribution from the four corners
  249. noise = 0.0
  250. tt = 0.6 - x0 ** 2 - y0 ** 2 - z0 ** 2
  251. if tt > 0:
  252. g = _GRAD3[gi0]
  253. noise = tt ** 4 * (g[0] * x0 + g[1] * y0 + g[2] * z0)
  254. else:
  255. noise = 0.0
  256. tt = 0.6 - x1 ** 2 - y1 ** 2 - z1 ** 2
  257. if tt > 0:
  258. g = _GRAD3[gi1]
  259. noise += tt ** 4 * (g[0] * x1 + g[1] * y1 + g[2] * z1)
  260. tt = 0.6 - x2 ** 2 - y2 ** 2 - z2 ** 2
  261. if tt > 0:
  262. g = _GRAD3[gi2]
  263. noise += tt ** 4 * (g[0] * x2 + g[1] * y2 + g[2] * z2)
  264. tt = 0.6 - x3 ** 2 - y3 ** 2 - z3 ** 2
  265. if tt > 0:
  266. g = _GRAD3[gi3]
  267. noise += tt ** 4 * (g[0] * x3 + g[1] * y3 + g[2] * z3)
  268. return noise * 32.0
  269. def lerp(t, a, b):
  270. return a + t * (b - a)
  271. def grad3(hash, x, y, z):
  272. g = _GRAD3[hash % 16]
  273. return x * g[0] + y * g[1] + z * g[2]
  274. class TileableNoise(BaseNoise):
  275. """Tileable implementation of Perlin "improved" noise. This
  276. is based on the reference implementation published here:
  277. http://mrl.nyu.edu/~perlin/noise/
  278. """
  279. def noise3(self, x, y, z, repeat, base=0.0):
  280. """Tileable 3D noise.
  281. repeat specifies the integer interval in each dimension
  282. when the noise pattern repeats.
  283. base allows a different texture to be generated for
  284. the same repeat interval.
  285. """
  286. i = int(fmod(floor(x), repeat))
  287. j = int(fmod(floor(y), repeat))
  288. k = int(fmod(floor(z), repeat))
  289. ii = (i + 1) % repeat
  290. jj = (j + 1) % repeat
  291. kk = (k + 1) % repeat
  292. if base:
  293. i += base
  294. j += base
  295. k += base
  296. ii += base
  297. jj += base
  298. kk += base
  299. x -= floor(x)
  300. y -= floor(y)
  301. z -= floor(z)
  302. fx = x ** 3 * (x * (x * 6 - 15) + 10)
  303. fy = y ** 3 * (y * (y * 6 - 15) + 10)
  304. fz = z ** 3 * (z * (z * 6 - 15) + 10)
  305. perm = self.permutation
  306. A = perm[i]
  307. AA = perm[A + j]
  308. AB = perm[A + jj]
  309. B = perm[ii]
  310. BA = perm[B + j]
  311. BB = perm[B + jj]
  312. return lerp(fz, lerp(fy, lerp(fx, grad3(perm[AA + k], x, y, z),
  313. grad3(perm[BA + k], x - 1, y, z)),
  314. lerp(fx, grad3(perm[AB + k], x, y - 1, z),
  315. grad3(perm[BB + k], x - 1, y - 1, z))),
  316. lerp(fy, lerp(fx, grad3(perm[AA + kk], x, y, z - 1),
  317. grad3(perm[BA + kk], x - 1, y, z - 1)),
  318. lerp(fx, grad3(perm[AB + kk], x, y - 1, z - 1),
  319. grad3(perm[BB + kk], x - 1, y - 1, z - 1))))
  320. _simplex = SimplexNoise()
  321. snoise2 = _simplex.noise2
  322. snoise3 = _simplex.noise3
  323. _tileable = TileableNoise()
  324. tnoise3 = _tileable.noise3