No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

psrdnoise2D.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. //
  2. // float3 psrdnoise(float2 pos, float2 per, float rot)
  3. // float3 psrdnoise(float2 pos, float2 per)
  4. // float psrnoise(float2 pos, float2 per, float rot)
  5. // float psrnoise(float2 pos, float2 per)
  6. // float3 srdnoise(float2 pos, float rot)
  7. // float3 srdnoise(float2 pos)
  8. // float srnoise(float2 pos, float rot)
  9. // float srnoise(float2 pos)
  10. //
  11. // Periodic (tiling) 2-D simplex noise (hexagonal lattice gradient noise)
  12. // with rotating gradients and analytic derivatives.
  13. // Variants also without the derivative (no "d" in the name), without
  14. // the tiling property (no "p" in the name) and without the rotating
  15. // gradients (no "r" in the name).
  16. //
  17. // This is (yet) another variation on simplex noise. It's similar to the
  18. // version presented by Ken Perlin, but the grid is axis-aligned and
  19. // slightly stretched in the y direction to permit rectangular tiling.
  20. //
  21. // The noise can be made to tile seamlessly to any integer period in x and
  22. // any even integer period in y. Odd periods may be specified for y, but
  23. // then the actual tiling period will be twice that number.
  24. //
  25. // The rotating gradients give the appearance of a swirling motion, and can
  26. // serve a similar purpose for animation as motion along z in 3-D noise.
  27. // The rotating gradients in conjunction with the analytic derivatives
  28. // can make "flow noise" effects as presented by Perlin and Neyret.
  29. //
  30. // float3 {p}s{r}dnoise(float2 pos {, float2 per} {, float rot})
  31. // "pos" is the input (x,y) coordinate
  32. // "per" is the x and y period, where per.x is a positive integer
  33. // and per.y is a positive even integer
  34. // "rot" is the angle to rotate the gradients (any float value,
  35. // where 0.0 is no rotation and 1.0 is one full turn)
  36. // The first component of the 3-element return vector is the noise value.
  37. // The second and third components are the x and y partial derivatives.
  38. //
  39. // float {p}s{r}noise(float2 pos {, float2 per} {, float rot})
  40. // "pos" is the input (x,y) coordinate
  41. // "per" is the x and y period, where per.x is a positive integer
  42. // and per.y is a positive even integer
  43. // "rot" is the angle to rotate the gradients (any float value,
  44. // where 0.0 is no rotation and 1.0 is one full turn)
  45. // The return value is the noise value.
  46. // Partial derivatives are not computed, making these functions faster.
  47. //
  48. // Author: Stefan Gustavson (stefan.gustavson@gmail.com)
  49. // Version 2016-05-10.
  50. //
  51. // Many thanks to Ian McEwan of Ashima Arts for the
  52. // idea of umath.sing a permutation polynomial.
  53. //
  54. // Copyright (c) 2016 Stefan Gustavson. All rights reserved.
  55. // Distributed under the MIT license. See LICENSE file.
  56. // https://github.com/stegu/webgl-noise
  57. //
  58. //
  59. // TODO: One-pixel wide artefacts used to occur due to precision issues with
  60. // the gradient indexing. This is specific to this variant of noise, because
  61. // one axis of the simplex grid is perfectly aligned with the input x axis.
  62. // The errors were rare, and they are now very unlikely to ever be visible
  63. // after a quick fix was introduced: a small offset is added to the y coordinate.
  64. // A proper fix would involve umath.sing round() instead of math.floor() in selected
  65. // places, but the quick fix works fine.
  66. // (If you run into problems with this, please let me know.)
  67. //
  68. using static Unity.Mathematics.math;
  69. namespace Unity.Mathematics
  70. {
  71. public static partial class noise
  72. {
  73. /// <summary>
  74. /// 2-D tiling simplex noise with rotating gradients and analytical derivative.
  75. /// </summary>
  76. /// <param name="pos">Input (x,y) coordinate.</param>
  77. /// <param name="per">The x and y period, where per.x is a positive integer and per.y is a positive even integer.</param>
  78. /// <param name="rot">Angle to rotate the gradients.</param>
  79. /// <returns>The first component of the 3-element return vector is the noise value, and the second and third components are the x and y partial derivatives.</returns>
  80. public static float3 psrdnoise(float2 pos, float2 per, float rot)
  81. {
  82. // Hack: offset y slightly to hide some rare artifacts
  83. pos.y += 0.01f;
  84. // Skew to hexagonal grid
  85. float2 uv = float2(pos.x + pos.y * 0.5f, pos.y);
  86. float2 i0 = floor(uv);
  87. float2 f0 = frac(uv);
  88. // Traversal order
  89. float2 i1 = (f0.x > f0.y) ? float2(1.0f, 0.0f) : float2(0.0f, 1.0f);
  90. // Unskewed grid points in (x,y) space
  91. float2 p0 = float2(i0.x - i0.y * 0.5f, i0.y);
  92. float2 p1 = float2(p0.x + i1.x - i1.y * 0.5f, p0.y + i1.y);
  93. float2 p2 = float2(p0.x + 0.5f, p0.y + 1.0f);
  94. // Vectors in unskewed (x,y) coordinates from
  95. // each of the simplex corners to the evaluation point
  96. float2 d0 = pos - p0;
  97. float2 d1 = pos - p1;
  98. float2 d2 = pos - p2;
  99. // Wrap p0, p1 and p2 to the desired period before gradient hashing:
  100. // wrap points in (x,y), map to (u,v)
  101. float3 xw = fmod(float3(p0.x, p1.x, p2.x), per.x);
  102. float3 yw = fmod(float3(p0.y, p1.y, p2.y), per.y);
  103. float3 iuw = xw + 0.5f * yw;
  104. float3 ivw = yw;
  105. // Create gradients from indices
  106. float2 g0 = rgrad2(float2(iuw.x, ivw.x), rot);
  107. float2 g1 = rgrad2(float2(iuw.y, ivw.y), rot);
  108. float2 g2 = rgrad2(float2(iuw.z, ivw.z), rot);
  109. // Gradients math.dot vectors to corresponding corners
  110. // (The derivatives of this are simply the gradients)
  111. float3 w = float3(dot(g0, d0), dot(g1, d1), dot(g2, d2));
  112. // Radial weights from corners
  113. // 0.8 is the square of 2/math.sqrt(5), the distance from
  114. // a grid point to the nearest simplex boundary
  115. float3 t = 0.8f - float3(dot(d0, d0), dot(d1, d1), dot(d2, d2));
  116. // Partial derivatives for analytical gradient computation
  117. float3 dtdx = -2.0f * float3(d0.x, d1.x, d2.x);
  118. float3 dtdy = -2.0f * float3(d0.y, d1.y, d2.y);
  119. // Set influence of each surflet to zero outside radius math.sqrt(0.8)
  120. if (t.x < 0.0f)
  121. {
  122. dtdx.x = 0.0f;
  123. dtdy.x = 0.0f;
  124. t.x = 0.0f;
  125. }
  126. if (t.y < 0.0f)
  127. {
  128. dtdx.y = 0.0f;
  129. dtdy.y = 0.0f;
  130. t.y = 0.0f;
  131. }
  132. if (t.z < 0.0f)
  133. {
  134. dtdx.z = 0.0f;
  135. dtdy.z = 0.0f;
  136. t.z = 0.0f;
  137. }
  138. // Fourth power of t (and third power for derivative)
  139. float3 t2 = t * t;
  140. float3 t4 = t2 * t2;
  141. float3 t3 = t2 * t;
  142. // Final noise value is:
  143. // sum of ((radial weights) times (gradient math.dot vector from corner))
  144. float n = dot(t4, w);
  145. // Final analytical derivative (gradient of a sum of scalar products)
  146. float2 dt0 = float2(dtdx.x, dtdy.x) * 4.0f * t3.x;
  147. float2 dn0 = t4.x * g0 + dt0 * w.x;
  148. float2 dt1 = float2(dtdx.y, dtdy.y) * 4.0f * t3.y;
  149. float2 dn1 = t4.y * g1 + dt1 * w.y;
  150. float2 dt2 = float2(dtdx.z, dtdy.z) * 4.0f * t3.z;
  151. float2 dn2 = t4.z * g2 + dt2 * w.z;
  152. return 11.0f * float3(n, dn0 + dn1 + dn2);
  153. }
  154. /// <summary>
  155. /// 2-D tiling simplex noise with fixed gradients and analytical derivative.
  156. /// </summary>
  157. /// <param name="pos">Input (x,y) coordinate.</param>
  158. /// <param name="per">The x and y period, where per.x is a positive integer and per.y is a positive even integer.</param>
  159. /// <returns>The first component of the 3-element return vector is the noise value, and the second and third components are the x and y partial derivatives.</returns>
  160. public static float3 psrdnoise(float2 pos, float2 per)
  161. {
  162. return psrdnoise(pos, per, 0.0f);
  163. }
  164. /// <summary>
  165. /// 2-D tiling simplex noise with rotating gradients, but without the analytical derivative.
  166. /// </summary>
  167. /// <param name="pos">Input (x,y) coordinate.</param>
  168. /// <param name="per">The x and y period, where per.x is a positive integer and per.y is a positive even integer.</param>
  169. /// <param name="rot">Angle to rotate the gradients.</param>
  170. /// <returns>Noise value.</returns>
  171. public static float psrnoise(float2 pos, float2 per, float rot)
  172. {
  173. // Offset y slightly to hide some rare artifacts
  174. pos.y += 0.001f;
  175. // Skew to hexagonal grid
  176. float2 uv = float2(pos.x + pos.y * 0.5f, pos.y);
  177. float2 i0 = floor(uv);
  178. float2 f0 = frac(uv);
  179. // Traversal order
  180. float2 i1 = (f0.x > f0.y) ? float2(1.0f, 0.0f) : float2(0.0f, 1.0f);
  181. // Unskewed grid points in (x,y) space
  182. float2 p0 = float2(i0.x - i0.y * 0.5f, i0.y);
  183. float2 p1 = float2(p0.x + i1.x - i1.y * 0.5f, p0.y + i1.y);
  184. float2 p2 = float2(p0.x + 0.5f, p0.y + 1.0f);
  185. // Vectors in unskewed (x,y) coordinates from
  186. // each of the simplex corners to the evaluation point
  187. float2 d0 = pos - p0;
  188. float2 d1 = pos - p1;
  189. float2 d2 = pos - p2;
  190. // Wrap p0, p1 and p2 to the desired period before gradient hashing:
  191. // wrap points in (x,y), map to (u,v)
  192. float3 xw = fmod(float3(p0.x, p1.x, p2.x), per.x);
  193. float3 yw = fmod(float3(p0.y, p1.y, p2.y), per.y);
  194. float3 iuw = xw + 0.5f * yw;
  195. float3 ivw = yw;
  196. // Create gradients from indices
  197. float2 g0 = rgrad2(float2(iuw.x, ivw.x), rot);
  198. float2 g1 = rgrad2(float2(iuw.y, ivw.y), rot);
  199. float2 g2 = rgrad2(float2(iuw.z, ivw.z), rot);
  200. // Gradients math.dot vectors to corresponding corners
  201. // (The derivatives of this are simply the gradients)
  202. float3 w = float3(dot(g0, d0), dot(g1, d1), dot(g2, d2));
  203. // Radial weights from corners
  204. // 0.8 is the square of 2/math.sqrt(5), the distance from
  205. // a grid point to the nearest simplex boundary
  206. float3 t = 0.8f - float3(dot(d0, d0), dot(d1, d1), dot(d2, d2));
  207. // Set influence of each surflet to zero outside radius math.sqrt(0.8)
  208. t = max(t, 0.0f);
  209. // Fourth power of t
  210. float3 t2 = t * t;
  211. float3 t4 = t2 * t2;
  212. // Final noise value is:
  213. // sum of ((radial weights) times (gradient math.dot vector from corner))
  214. float n = dot(t4, w);
  215. // Rescale to cover the range [-1,1] reasonably well
  216. return 11.0f * n;
  217. }
  218. /// <summary>
  219. /// 2-D tiling simplex noise with fixed gradients, without the analytical derivative.
  220. /// </summary>
  221. /// <param name="pos">Input (x,y) coordinate.</param>
  222. /// <param name="per">The x and y period, where per.x is a positive integer and per.y is a positive even integer.</param>
  223. /// <returns>Noise value.</returns>
  224. public static float psrnoise(float2 pos, float2 per)
  225. {
  226. return psrnoise(pos, per, 0.0f);
  227. }
  228. /// <summary>
  229. /// 2-D non-tiling simplex noise with rotating gradients and analytical derivative.
  230. /// </summary>
  231. /// <param name="pos">Input (x,y) coordinate.</param>
  232. /// <param name="rot">Angle to rotate the gradients.</param>
  233. /// <returns>The first component of the 3-element return vector is the noise value, and the second and third components are the x and y partial derivatives.</returns>
  234. public static float3 srdnoise(float2 pos, float rot)
  235. {
  236. // Offset y slightly to hide some rare artifacts
  237. pos.y += 0.001f;
  238. // Skew to hexagonal grid
  239. float2 uv = float2(pos.x + pos.y * 0.5f, pos.y);
  240. float2 i0 = floor(uv);
  241. float2 f0 = frac(uv);
  242. // Traversal order
  243. float2 i1 = (f0.x > f0.y) ? float2(1.0f, 0.0f) : float2(0.0f, 1.0f);
  244. // Unskewed grid points in (x,y) space
  245. float2 p0 = float2(i0.x - i0.y * 0.5f, i0.y);
  246. float2 p1 = float2(p0.x + i1.x - i1.y * 0.5f, p0.y + i1.y);
  247. float2 p2 = float2(p0.x + 0.5f, p0.y + 1.0f);
  248. // Vectors in unskewed (x,y) coordinates from
  249. // each of the simplex corners to the evaluation point
  250. float2 d0 = pos - p0;
  251. float2 d1 = pos - p1;
  252. float2 d2 = pos - p2;
  253. float3 x = float3(p0.x, p1.x, p2.x);
  254. float3 y = float3(p0.y, p1.y, p2.y);
  255. float3 iuw = x + 0.5f * y;
  256. float3 ivw = y;
  257. // Avoid precision issues in permutation
  258. iuw = mod289(iuw);
  259. ivw = mod289(ivw);
  260. // Create gradients from indices
  261. float2 g0 = rgrad2(float2(iuw.x, ivw.x), rot);
  262. float2 g1 = rgrad2(float2(iuw.y, ivw.y), rot);
  263. float2 g2 = rgrad2(float2(iuw.z, ivw.z), rot);
  264. // Gradients math.dot vectors to corresponding corners
  265. // (The derivatives of this are simply the gradients)
  266. float3 w = float3(dot(g0, d0), dot(g1, d1), dot(g2, d2));
  267. // Radial weights from corners
  268. // 0.8 is the square of 2/math.sqrt(5), the distance from
  269. // a grid point to the nearest simplex boundary
  270. float3 t = 0.8f - float3(dot(d0, d0), dot(d1, d1), dot(d2, d2));
  271. // Partial derivatives for analytical gradient computation
  272. float3 dtdx = -2.0f * float3(d0.x, d1.x, d2.x);
  273. float3 dtdy = -2.0f * float3(d0.y, d1.y, d2.y);
  274. // Set influence of each surflet to zero outside radius math.sqrt(0.8)
  275. if (t.x < 0.0f)
  276. {
  277. dtdx.x = 0.0f;
  278. dtdy.x = 0.0f;
  279. t.x = 0.0f;
  280. }
  281. if (t.y < 0.0f)
  282. {
  283. dtdx.y = 0.0f;
  284. dtdy.y = 0.0f;
  285. t.y = 0.0f;
  286. }
  287. if (t.z < 0.0f)
  288. {
  289. dtdx.z = 0.0f;
  290. dtdy.z = 0.0f;
  291. t.z = 0.0f;
  292. }
  293. // Fourth power of t (and third power for derivative)
  294. float3 t2 = t * t;
  295. float3 t4 = t2 * t2;
  296. float3 t3 = t2 * t;
  297. // Final noise value is:
  298. // sum of ((radial weights) times (gradient math.dot vector from corner))
  299. float n = dot(t4, w);
  300. // Final analytical derivative (gradient of a sum of scalar products)
  301. float2 dt0 = float2(dtdx.x, dtdy.x) * 4.0f * t3.x;
  302. float2 dn0 = t4.x * g0 + dt0 * w.x;
  303. float2 dt1 = float2(dtdx.y, dtdy.y) * 4.0f * t3.y;
  304. float2 dn1 = t4.y * g1 + dt1 * w.y;
  305. float2 dt2 = float2(dtdx.z, dtdy.z) * 4.0f * t3.z;
  306. float2 dn2 = t4.z * g2 + dt2 * w.z;
  307. return 11.0f * float3(n, dn0 + dn1 + dn2);
  308. }
  309. /// <summary>
  310. /// 2-D non-tiling simplex noise with fixed gradients and analytical derivative.
  311. /// </summary>
  312. /// <param name="pos">Input (x,y) coordinate.</param>
  313. /// <returns>The first component of the 3-element return vector is the noise value, and the second and third components are the x and y partial derivatives.</returns>
  314. public static float3 srdnoise(float2 pos)
  315. {
  316. return srdnoise(pos, 0.0f);
  317. }
  318. /// <summary>
  319. /// 2-D non-tiling simplex noise with rotating gradients, without the analytical derivative.
  320. /// </summary>
  321. /// <param name="pos">Input (x,y) coordinate.</param>
  322. /// <param name="rot">Angle to rotate the gradients.</param>
  323. /// <returns>Noise value.</returns>
  324. public static float srnoise(float2 pos, float rot)
  325. {
  326. // Offset y slightly to hide some rare artifacts
  327. pos.y += 0.001f;
  328. // Skew to hexagonal grid
  329. float2 uv = float2(pos.x + pos.y * 0.5f, pos.y);
  330. float2 i0 = floor(uv);
  331. float2 f0 = frac(uv);
  332. // Traversal order
  333. float2 i1 = (f0.x > f0.y) ? float2(1.0f, 0.0f) : float2(0.0f, 1.0f);
  334. // Unskewed grid points in (x,y) space
  335. float2 p0 = float2(i0.x - i0.y * 0.5f, i0.y);
  336. float2 p1 = float2(p0.x + i1.x - i1.y * 0.5f, p0.y + i1.y);
  337. float2 p2 = float2(p0.x + 0.5f, p0.y + 1.0f);
  338. // Vectors in unskewed (x,y) coordinates from
  339. // each of the simplex corners to the evaluation point
  340. float2 d0 = pos - p0;
  341. float2 d1 = pos - p1;
  342. float2 d2 = pos - p2;
  343. float3 x = float3(p0.x, p1.x, p2.x);
  344. float3 y = float3(p0.y, p1.y, p2.y);
  345. float3 iuw = x + 0.5f * y;
  346. float3 ivw = y;
  347. // Avoid precision issues in permutation
  348. iuw = mod289(iuw);
  349. ivw = mod289(ivw);
  350. // Create gradients from indices
  351. float2 g0 = rgrad2(float2(iuw.x, ivw.x), rot);
  352. float2 g1 = rgrad2(float2(iuw.y, ivw.y), rot);
  353. float2 g2 = rgrad2(float2(iuw.z, ivw.z), rot);
  354. // Gradients math.dot vectors to corresponding corners
  355. // (The derivatives of this are simply the gradients)
  356. float3 w = float3(dot(g0, d0), dot(g1, d1), dot(g2, d2));
  357. // Radial weights from corners
  358. // 0.8 is the square of 2/math.sqrt(5), the distance from
  359. // a grid point to the nearest simplex boundary
  360. float3 t = 0.8f - float3(dot(d0, d0), dot(d1, d1), dot(d2, d2));
  361. // Set influence of each surflet to zero outside radius math.sqrt(0.8)
  362. t = max(t, 0.0f);
  363. // Fourth power of t
  364. float3 t2 = t * t;
  365. float3 t4 = t2 * t2;
  366. // Final noise value is:
  367. // sum of ((radial weights) times (gradient math.dot vector from corner))
  368. float n = dot(t4, w);
  369. // Rescale to cover the range [-1,1] reasonably well
  370. return 11.0f * n;
  371. }
  372. /// <summary>
  373. /// 2-D non-tiling simplex noise with fixed gradients, without the analytical derivative.
  374. /// </summary>
  375. /// <param name="pos">Input (x,y) coordinate.</param>
  376. /// <returns>Noise value.</returns>
  377. public static float srnoise(float2 pos)
  378. {
  379. return srnoise(pos, 0.0f);
  380. }
  381. }
  382. }