Nessuna descrizione
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.

classicnoise4D.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // GLSL textureless classic 4D noise "cnoise",
  3. // with an RSL-style periodic variant "pnoise".
  4. // Author: Stefan Gustavson (stefan.gustavson@liu.se)
  5. // Version: 2011-08-22
  6. //
  7. // Many thanks to Ian McEwan of Ashima Arts for the
  8. // ideas for permutation and gradient selection.
  9. //
  10. // Copyright (c) 2011 Stefan Gustavson. All rights reserved.
  11. // Distributed under the MIT license. See LICENSE file.
  12. // https://github.com/stegu/webgl-noise
  13. //
  14. using static Unity.Mathematics.math;
  15. namespace Unity.Mathematics
  16. {
  17. public static partial class noise
  18. {
  19. /// <summary>
  20. /// Classic Perlin noise
  21. /// </summary>
  22. /// <param name="P">Point on a 4D grid of gradient vectors.</param>
  23. /// <returns>Noise value.</returns>
  24. public static float cnoise(float4 P)
  25. {
  26. float4 Pi0 = floor(P); // Integer part for indexing
  27. float4 Pi1 = Pi0 + 1.0f; // Integer part + 1
  28. Pi0 = mod289(Pi0);
  29. Pi1 = mod289(Pi1);
  30. float4 Pf0 = frac(P); // Fractional part for interpolation
  31. float4 Pf1 = Pf0 - 1.0f; // Fractional part - 1.0
  32. float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  33. float4 iy = float4(Pi0.yy, Pi1.yy);
  34. float4 iz0 = float4(Pi0.zzzz);
  35. float4 iz1 = float4(Pi1.zzzz);
  36. float4 iw0 = float4(Pi0.wwww);
  37. float4 iw1 = float4(Pi1.wwww);
  38. float4 ixy = permute(permute(ix) + iy);
  39. float4 ixy0 = permute(ixy + iz0);
  40. float4 ixy1 = permute(ixy + iz1);
  41. float4 ixy00 = permute(ixy0 + iw0);
  42. float4 ixy01 = permute(ixy0 + iw1);
  43. float4 ixy10 = permute(ixy1 + iw0);
  44. float4 ixy11 = permute(ixy1 + iw1);
  45. float4 gx00 = ixy00 * (1.0f / 7.0f);
  46. float4 gy00 = floor(gx00) * (1.0f / 7.0f);
  47. float4 gz00 = floor(gy00) * (1.0f / 6.0f);
  48. gx00 = frac(gx00) - 0.5f;
  49. gy00 = frac(gy00) - 0.5f;
  50. gz00 = frac(gz00) - 0.5f;
  51. float4 gw00 = float4(0.75f) - abs(gx00) - abs(gy00) - abs(gz00);
  52. float4 sw00 = step(gw00, float4(0.0f));
  53. gx00 -= sw00 * (step(0.0f, gx00) - 0.5f);
  54. gy00 -= sw00 * (step(0.0f, gy00) - 0.5f);
  55. float4 gx01 = ixy01 * (1.0f / 7.0f);
  56. float4 gy01 = floor(gx01) * (1.0f / 7.0f);
  57. float4 gz01 = floor(gy01) * (1.0f / 6.0f);
  58. gx01 = frac(gx01) - 0.5f;
  59. gy01 = frac(gy01) - 0.5f;
  60. gz01 = frac(gz01) - 0.5f;
  61. float4 gw01 = float4(0.75f) - abs(gx01) - abs(gy01) - abs(gz01);
  62. float4 sw01 = step(gw01, float4(0.0f));
  63. gx01 -= sw01 * (step(0.0f, gx01) - 0.5f);
  64. gy01 -= sw01 * (step(0.0f, gy01) - 0.5f);
  65. float4 gx10 = ixy10 * (1.0f / 7.0f);
  66. float4 gy10 = floor(gx10) * (1.0f / 7.0f);
  67. float4 gz10 = floor(gy10) * (1.0f / 6.0f);
  68. gx10 = frac(gx10) - 0.5f;
  69. gy10 = frac(gy10) - 0.5f;
  70. gz10 = frac(gz10) - 0.5f;
  71. float4 gw10 = float4(0.75f) - abs(gx10) - abs(gy10) - abs(gz10);
  72. float4 sw10 = step(gw10, float4(0.0f));
  73. gx10 -= sw10 * (step(0.0f, gx10) - 0.5f);
  74. gy10 -= sw10 * (step(0.0f, gy10) - 0.5f);
  75. float4 gx11 = ixy11 * (1.0f / 7.0f);
  76. float4 gy11 = floor(gx11) * (1.0f / 7.0f);
  77. float4 gz11 = floor(gy11) * (1.0f / 6.0f);
  78. gx11 = frac(gx11) - 0.5f;
  79. gy11 = frac(gy11) - 0.5f;
  80. gz11 = frac(gz11) - 0.5f;
  81. float4 gw11 = float4(0.75f) - abs(gx11) - abs(gy11) - abs(gz11);
  82. float4 sw11 = step(gw11, float4(0.0f));
  83. gx11 -= sw11 * (step(0.0f, gx11) - 0.5f);
  84. gy11 -= sw11 * (step(0.0f, gy11) - 0.5f);
  85. float4 g0000 = float4(gx00.x, gy00.x, gz00.x, gw00.x);
  86. float4 g1000 = float4(gx00.y, gy00.y, gz00.y, gw00.y);
  87. float4 g0100 = float4(gx00.z, gy00.z, gz00.z, gw00.z);
  88. float4 g1100 = float4(gx00.w, gy00.w, gz00.w, gw00.w);
  89. float4 g0010 = float4(gx10.x, gy10.x, gz10.x, gw10.x);
  90. float4 g1010 = float4(gx10.y, gy10.y, gz10.y, gw10.y);
  91. float4 g0110 = float4(gx10.z, gy10.z, gz10.z, gw10.z);
  92. float4 g1110 = float4(gx10.w, gy10.w, gz10.w, gw10.w);
  93. float4 g0001 = float4(gx01.x, gy01.x, gz01.x, gw01.x);
  94. float4 g1001 = float4(gx01.y, gy01.y, gz01.y, gw01.y);
  95. float4 g0101 = float4(gx01.z, gy01.z, gz01.z, gw01.z);
  96. float4 g1101 = float4(gx01.w, gy01.w, gz01.w, gw01.w);
  97. float4 g0011 = float4(gx11.x, gy11.x, gz11.x, gw11.x);
  98. float4 g1011 = float4(gx11.y, gy11.y, gz11.y, gw11.y);
  99. float4 g0111 = float4(gx11.z, gy11.z, gz11.z, gw11.z);
  100. float4 g1111 = float4(gx11.w, gy11.w, gz11.w, gw11.w);
  101. float4 norm00 = taylorInvSqrt(float4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100)));
  102. g0000 *= norm00.x;
  103. g0100 *= norm00.y;
  104. g1000 *= norm00.z;
  105. g1100 *= norm00.w;
  106. float4 norm01 = taylorInvSqrt(float4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101)));
  107. g0001 *= norm01.x;
  108. g0101 *= norm01.y;
  109. g1001 *= norm01.z;
  110. g1101 *= norm01.w;
  111. float4 norm10 = taylorInvSqrt(float4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110)));
  112. g0010 *= norm10.x;
  113. g0110 *= norm10.y;
  114. g1010 *= norm10.z;
  115. g1110 *= norm10.w;
  116. float4 norm11 = taylorInvSqrt(float4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111)));
  117. g0011 *= norm11.x;
  118. g0111 *= norm11.y;
  119. g1011 *= norm11.z;
  120. g1111 *= norm11.w;
  121. float n0000 = dot(g0000, Pf0);
  122. float n1000 = dot(g1000, float4(Pf1.x, Pf0.yzw));
  123. float n0100 = dot(g0100, float4(Pf0.x, Pf1.y, Pf0.zw));
  124. float n1100 = dot(g1100, float4(Pf1.xy, Pf0.zw));
  125. float n0010 = dot(g0010, float4(Pf0.xy, Pf1.z, Pf0.w));
  126. float n1010 = dot(g1010, float4(Pf1.x, Pf0.y, Pf1.z, Pf0.w));
  127. float n0110 = dot(g0110, float4(Pf0.x, Pf1.yz, Pf0.w));
  128. float n1110 = dot(g1110, float4(Pf1.xyz, Pf0.w));
  129. float n0001 = dot(g0001, float4(Pf0.xyz, Pf1.w));
  130. float n1001 = dot(g1001, float4(Pf1.x, Pf0.yz, Pf1.w));
  131. float n0101 = dot(g0101, float4(Pf0.x, Pf1.y, Pf0.z, Pf1.w));
  132. float n1101 = dot(g1101, float4(Pf1.xy, Pf0.z, Pf1.w));
  133. float n0011 = dot(g0011, float4(Pf0.xy, Pf1.zw));
  134. float n1011 = dot(g1011, float4(Pf1.x, Pf0.y, Pf1.zw));
  135. float n0111 = dot(g0111, float4(Pf0.x, Pf1.yzw));
  136. float n1111 = dot(g1111, Pf1);
  137. float4 fade_xyzw = fade(Pf0);
  138. float4 n_0w = lerp(float4(n0000, n1000, n0100, n1100), float4(n0001, n1001, n0101, n1101), fade_xyzw.w);
  139. float4 n_1w = lerp(float4(n0010, n1010, n0110, n1110), float4(n0011, n1011, n0111, n1111), fade_xyzw.w);
  140. float4 n_zw = lerp(n_0w, n_1w, fade_xyzw.z);
  141. float2 n_yzw = lerp(n_zw.xy, n_zw.zw, fade_xyzw.y);
  142. float n_xyzw = lerp(n_yzw.x, n_yzw.y, fade_xyzw.x);
  143. return 2.2f * n_xyzw;
  144. }
  145. /// <summary>
  146. /// Classic Perlin noise, periodic variant
  147. /// </summary>
  148. /// <param name="P">Point on a 4D grid of gradient vectors.</param>
  149. /// <param name="rep">Period of repetition.</param>
  150. /// <returns>Noise value.</returns>
  151. public static float pnoise(float4 P, float4 rep)
  152. {
  153. float4 Pi0 = fmod(floor(P), rep); // Integer part math.modulo rep
  154. float4 Pi1 = fmod(Pi0 + 1.0f, rep); // Integer part + 1 math.mod rep
  155. Pi0 = mod289(Pi0);
  156. Pi1 = mod289(Pi1);
  157. float4 Pf0 = frac(P); // Fractional part for interpolation
  158. float4 Pf1 = Pf0 - 1.0f; // Fractional part - 1.0
  159. float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  160. float4 iy = float4(Pi0.yy, Pi1.yy);
  161. float4 iz0 = float4(Pi0.zzzz);
  162. float4 iz1 = float4(Pi1.zzzz);
  163. float4 iw0 = float4(Pi0.wwww);
  164. float4 iw1 = float4(Pi1.wwww);
  165. float4 ixy = permute(permute(ix) + iy);
  166. float4 ixy0 = permute(ixy + iz0);
  167. float4 ixy1 = permute(ixy + iz1);
  168. float4 ixy00 = permute(ixy0 + iw0);
  169. float4 ixy01 = permute(ixy0 + iw1);
  170. float4 ixy10 = permute(ixy1 + iw0);
  171. float4 ixy11 = permute(ixy1 + iw1);
  172. float4 gx00 = ixy00 * (1.0f / 7.0f);
  173. float4 gy00 = floor(gx00) * (1.0f / 7.0f);
  174. float4 gz00 = floor(gy00) * (1.0f / 6.0f);
  175. gx00 = frac(gx00) - 0.5f;
  176. gy00 = frac(gy00) - 0.5f;
  177. gz00 = frac(gz00) - 0.5f;
  178. float4 gw00 = float4(0.75f) - abs(gx00) - abs(gy00) - abs(gz00);
  179. float4 sw00 = step(gw00, float4(0.0f));
  180. gx00 -= sw00 * (step(0.0f, gx00) - 0.5f);
  181. gy00 -= sw00 * (step(0.0f, gy00) - 0.5f);
  182. float4 gx01 = ixy01 * (1.0f / 7.0f);
  183. float4 gy01 = floor(gx01) * (1.0f / 7.0f);
  184. float4 gz01 = floor(gy01) * (1.0f / 6.0f);
  185. gx01 = frac(gx01) - 0.5f;
  186. gy01 = frac(gy01) - 0.5f;
  187. gz01 = frac(gz01) - 0.5f;
  188. float4 gw01 = float4(0.75f) - abs(gx01) - abs(gy01) - abs(gz01);
  189. float4 sw01 = step(gw01, float4(0.0f));
  190. gx01 -= sw01 * (step(0.0f, gx01) - 0.5f);
  191. gy01 -= sw01 * (step(0.0f, gy01) - 0.5f);
  192. float4 gx10 = ixy10 * (1.0f / 7.0f);
  193. float4 gy10 = floor(gx10) * (1.0f / 7.0f);
  194. float4 gz10 = floor(gy10) * (1.0f / 6.0f);
  195. gx10 = frac(gx10) - 0.5f;
  196. gy10 = frac(gy10) - 0.5f;
  197. gz10 = frac(gz10) - 0.5f;
  198. float4 gw10 = float4(0.75f) - abs(gx10) - abs(gy10) - abs(gz10);
  199. float4 sw10 = step(gw10, float4(0.0f));
  200. gx10 -= sw10 * (step(0.0f, gx10) - 0.5f);
  201. gy10 -= sw10 * (step(0.0f, gy10) - 0.5f);
  202. float4 gx11 = ixy11 * (1.0f / 7.0f);
  203. float4 gy11 = floor(gx11) * (1.0f / 7.0f);
  204. float4 gz11 = floor(gy11) * (1.0f / 6.0f);
  205. gx11 = frac(gx11) - 0.5f;
  206. gy11 = frac(gy11) - 0.5f;
  207. gz11 = frac(gz11) - 0.5f;
  208. float4 gw11 = float4(0.75f) - abs(gx11) - abs(gy11) - abs(gz11);
  209. float4 sw11 = step(gw11, float4(0.0f));
  210. gx11 -= sw11 * (step(0.0f, gx11) - 0.5f);
  211. gy11 -= sw11 * (step(0.0f, gy11) - 0.5f);
  212. float4 g0000 = float4(gx00.x, gy00.x, gz00.x, gw00.x);
  213. float4 g1000 = float4(gx00.y, gy00.y, gz00.y, gw00.y);
  214. float4 g0100 = float4(gx00.z, gy00.z, gz00.z, gw00.z);
  215. float4 g1100 = float4(gx00.w, gy00.w, gz00.w, gw00.w);
  216. float4 g0010 = float4(gx10.x, gy10.x, gz10.x, gw10.x);
  217. float4 g1010 = float4(gx10.y, gy10.y, gz10.y, gw10.y);
  218. float4 g0110 = float4(gx10.z, gy10.z, gz10.z, gw10.z);
  219. float4 g1110 = float4(gx10.w, gy10.w, gz10.w, gw10.w);
  220. float4 g0001 = float4(gx01.x, gy01.x, gz01.x, gw01.x);
  221. float4 g1001 = float4(gx01.y, gy01.y, gz01.y, gw01.y);
  222. float4 g0101 = float4(gx01.z, gy01.z, gz01.z, gw01.z);
  223. float4 g1101 = float4(gx01.w, gy01.w, gz01.w, gw01.w);
  224. float4 g0011 = float4(gx11.x, gy11.x, gz11.x, gw11.x);
  225. float4 g1011 = float4(gx11.y, gy11.y, gz11.y, gw11.y);
  226. float4 g0111 = float4(gx11.z, gy11.z, gz11.z, gw11.z);
  227. float4 g1111 = float4(gx11.w, gy11.w, gz11.w, gw11.w);
  228. float4 norm00 = taylorInvSqrt(float4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100)));
  229. g0000 *= norm00.x;
  230. g0100 *= norm00.y;
  231. g1000 *= norm00.z;
  232. g1100 *= norm00.w;
  233. float4 norm01 = taylorInvSqrt(float4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101)));
  234. g0001 *= norm01.x;
  235. g0101 *= norm01.y;
  236. g1001 *= norm01.z;
  237. g1101 *= norm01.w;
  238. float4 norm10 = taylorInvSqrt(float4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110)));
  239. g0010 *= norm10.x;
  240. g0110 *= norm10.y;
  241. g1010 *= norm10.z;
  242. g1110 *= norm10.w;
  243. float4 norm11 = taylorInvSqrt(float4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111)));
  244. g0011 *= norm11.x;
  245. g0111 *= norm11.y;
  246. g1011 *= norm11.z;
  247. g1111 *= norm11.w;
  248. float n0000 = dot(g0000, Pf0);
  249. float n1000 = dot(g1000, float4(Pf1.x, Pf0.yzw));
  250. float n0100 = dot(g0100, float4(Pf0.x, Pf1.y, Pf0.zw));
  251. float n1100 = dot(g1100, float4(Pf1.xy, Pf0.zw));
  252. float n0010 = dot(g0010, float4(Pf0.xy, Pf1.z, Pf0.w));
  253. float n1010 = dot(g1010, float4(Pf1.x, Pf0.y, Pf1.z, Pf0.w));
  254. float n0110 = dot(g0110, float4(Pf0.x, Pf1.yz, Pf0.w));
  255. float n1110 = dot(g1110, float4(Pf1.xyz, Pf0.w));
  256. float n0001 = dot(g0001, float4(Pf0.xyz, Pf1.w));
  257. float n1001 = dot(g1001, float4(Pf1.x, Pf0.yz, Pf1.w));
  258. float n0101 = dot(g0101, float4(Pf0.x, Pf1.y, Pf0.z, Pf1.w));
  259. float n1101 = dot(g1101, float4(Pf1.xy, Pf0.z, Pf1.w));
  260. float n0011 = dot(g0011, float4(Pf0.xy, Pf1.zw));
  261. float n1011 = dot(g1011, float4(Pf1.x, Pf0.y, Pf1.zw));
  262. float n0111 = dot(g0111, float4(Pf0.x, Pf1.yzw));
  263. float n1111 = dot(g1111, Pf1);
  264. float4 fade_xyzw = fade(Pf0);
  265. float4 n_0w = lerp(float4(n0000, n1000, n0100, n1100), float4(n0001, n1001, n0101, n1101), fade_xyzw.w);
  266. float4 n_1w = lerp(float4(n0010, n1010, n0110, n1110), float4(n0011, n1011, n0111, n1111), fade_xyzw.w);
  267. float4 n_zw = lerp(n_0w, n_1w, fade_xyzw.z);
  268. float2 n_yzw = lerp(n_zw.xy, n_zw.zw, fade_xyzw.y);
  269. float n_xyzw = lerp(n_yzw.x, n_yzw.y, fade_xyzw.x);
  270. return 2.2f * n_xyzw;
  271. }
  272. }
  273. }