classicnoise4D.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. // Classic Perlin noise
  20. public static float cnoise(float4 P)
  21. {
  22. float4 Pi0 = floor(P); // Integer part for indexing
  23. float4 Pi1 = Pi0 + 1.0f; // Integer part + 1
  24. Pi0 = mod289(Pi0);
  25. Pi1 = mod289(Pi1);
  26. float4 Pf0 = frac(P); // Fractional part for interpolation
  27. float4 Pf1 = Pf0 - 1.0f; // Fractional part - 1.0
  28. float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  29. float4 iy = float4(Pi0.yy, Pi1.yy);
  30. float4 iz0 = float4(Pi0.zzzz);
  31. float4 iz1 = float4(Pi1.zzzz);
  32. float4 iw0 = float4(Pi0.wwww);
  33. float4 iw1 = float4(Pi1.wwww);
  34. float4 ixy = permute(permute(ix) + iy);
  35. float4 ixy0 = permute(ixy + iz0);
  36. float4 ixy1 = permute(ixy + iz1);
  37. float4 ixy00 = permute(ixy0 + iw0);
  38. float4 ixy01 = permute(ixy0 + iw1);
  39. float4 ixy10 = permute(ixy1 + iw0);
  40. float4 ixy11 = permute(ixy1 + iw1);
  41. float4 gx00 = ixy00 * (1.0f / 7.0f);
  42. float4 gy00 = floor(gx00) * (1.0f / 7.0f);
  43. float4 gz00 = floor(gy00) * (1.0f / 6.0f);
  44. gx00 = frac(gx00) - 0.5f;
  45. gy00 = frac(gy00) - 0.5f;
  46. gz00 = frac(gz00) - 0.5f;
  47. float4 gw00 = float4(0.75f) - abs(gx00) - abs(gy00) - abs(gz00);
  48. float4 sw00 = step(gw00, float4(0.0f));
  49. gx00 -= sw00 * (step(0.0f, gx00) - 0.5f);
  50. gy00 -= sw00 * (step(0.0f, gy00) - 0.5f);
  51. float4 gx01 = ixy01 * (1.0f / 7.0f);
  52. float4 gy01 = floor(gx01) * (1.0f / 7.0f);
  53. float4 gz01 = floor(gy01) * (1.0f / 6.0f);
  54. gx01 = frac(gx01) - 0.5f;
  55. gy01 = frac(gy01) - 0.5f;
  56. gz01 = frac(gz01) - 0.5f;
  57. float4 gw01 = float4(0.75f) - abs(gx01) - abs(gy01) - abs(gz01);
  58. float4 sw01 = step(gw01, float4(0.0f));
  59. gx01 -= sw01 * (step(0.0f, gx01) - 0.5f);
  60. gy01 -= sw01 * (step(0.0f, gy01) - 0.5f);
  61. float4 gx10 = ixy10 * (1.0f / 7.0f);
  62. float4 gy10 = floor(gx10) * (1.0f / 7.0f);
  63. float4 gz10 = floor(gy10) * (1.0f / 6.0f);
  64. gx10 = frac(gx10) - 0.5f;
  65. gy10 = frac(gy10) - 0.5f;
  66. gz10 = frac(gz10) - 0.5f;
  67. float4 gw10 = float4(0.75f) - abs(gx10) - abs(gy10) - abs(gz10);
  68. float4 sw10 = step(gw10, float4(0.0f));
  69. gx10 -= sw10 * (step(0.0f, gx10) - 0.5f);
  70. gy10 -= sw10 * (step(0.0f, gy10) - 0.5f);
  71. float4 gx11 = ixy11 * (1.0f / 7.0f);
  72. float4 gy11 = floor(gx11) * (1.0f / 7.0f);
  73. float4 gz11 = floor(gy11) * (1.0f / 6.0f);
  74. gx11 = frac(gx11) - 0.5f;
  75. gy11 = frac(gy11) - 0.5f;
  76. gz11 = frac(gz11) - 0.5f;
  77. float4 gw11 = float4(0.75f) - abs(gx11) - abs(gy11) - abs(gz11);
  78. float4 sw11 = step(gw11, float4(0.0f));
  79. gx11 -= sw11 * (step(0.0f, gx11) - 0.5f);
  80. gy11 -= sw11 * (step(0.0f, gy11) - 0.5f);
  81. float4 g0000 = float4(gx00.x, gy00.x, gz00.x, gw00.x);
  82. float4 g1000 = float4(gx00.y, gy00.y, gz00.y, gw00.y);
  83. float4 g0100 = float4(gx00.z, gy00.z, gz00.z, gw00.z);
  84. float4 g1100 = float4(gx00.w, gy00.w, gz00.w, gw00.w);
  85. float4 g0010 = float4(gx10.x, gy10.x, gz10.x, gw10.x);
  86. float4 g1010 = float4(gx10.y, gy10.y, gz10.y, gw10.y);
  87. float4 g0110 = float4(gx10.z, gy10.z, gz10.z, gw10.z);
  88. float4 g1110 = float4(gx10.w, gy10.w, gz10.w, gw10.w);
  89. float4 g0001 = float4(gx01.x, gy01.x, gz01.x, gw01.x);
  90. float4 g1001 = float4(gx01.y, gy01.y, gz01.y, gw01.y);
  91. float4 g0101 = float4(gx01.z, gy01.z, gz01.z, gw01.z);
  92. float4 g1101 = float4(gx01.w, gy01.w, gz01.w, gw01.w);
  93. float4 g0011 = float4(gx11.x, gy11.x, gz11.x, gw11.x);
  94. float4 g1011 = float4(gx11.y, gy11.y, gz11.y, gw11.y);
  95. float4 g0111 = float4(gx11.z, gy11.z, gz11.z, gw11.z);
  96. float4 g1111 = float4(gx11.w, gy11.w, gz11.w, gw11.w);
  97. float4 norm00 = taylorInvSqrt(float4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100)));
  98. g0000 *= norm00.x;
  99. g0100 *= norm00.y;
  100. g1000 *= norm00.z;
  101. g1100 *= norm00.w;
  102. float4 norm01 = taylorInvSqrt(float4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101)));
  103. g0001 *= norm01.x;
  104. g0101 *= norm01.y;
  105. g1001 *= norm01.z;
  106. g1101 *= norm01.w;
  107. float4 norm10 = taylorInvSqrt(float4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110)));
  108. g0010 *= norm10.x;
  109. g0110 *= norm10.y;
  110. g1010 *= norm10.z;
  111. g1110 *= norm10.w;
  112. float4 norm11 = taylorInvSqrt(float4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111)));
  113. g0011 *= norm11.x;
  114. g0111 *= norm11.y;
  115. g1011 *= norm11.z;
  116. g1111 *= norm11.w;
  117. float n0000 = dot(g0000, Pf0);
  118. float n1000 = dot(g1000, float4(Pf1.x, Pf0.yzw));
  119. float n0100 = dot(g0100, float4(Pf0.x, Pf1.y, Pf0.zw));
  120. float n1100 = dot(g1100, float4(Pf1.xy, Pf0.zw));
  121. float n0010 = dot(g0010, float4(Pf0.xy, Pf1.z, Pf0.w));
  122. float n1010 = dot(g1010, float4(Pf1.x, Pf0.y, Pf1.z, Pf0.w));
  123. float n0110 = dot(g0110, float4(Pf0.x, Pf1.yz, Pf0.w));
  124. float n1110 = dot(g1110, float4(Pf1.xyz, Pf0.w));
  125. float n0001 = dot(g0001, float4(Pf0.xyz, Pf1.w));
  126. float n1001 = dot(g1001, float4(Pf1.x, Pf0.yz, Pf1.w));
  127. float n0101 = dot(g0101, float4(Pf0.x, Pf1.y, Pf0.z, Pf1.w));
  128. float n1101 = dot(g1101, float4(Pf1.xy, Pf0.z, Pf1.w));
  129. float n0011 = dot(g0011, float4(Pf0.xy, Pf1.zw));
  130. float n1011 = dot(g1011, float4(Pf1.x, Pf0.y, Pf1.zw));
  131. float n0111 = dot(g0111, float4(Pf0.x, Pf1.yzw));
  132. float n1111 = dot(g1111, Pf1);
  133. float4 fade_xyzw = fade(Pf0);
  134. float4 n_0w = lerp(float4(n0000, n1000, n0100, n1100), float4(n0001, n1001, n0101, n1101), fade_xyzw.w);
  135. float4 n_1w = lerp(float4(n0010, n1010, n0110, n1110), float4(n0011, n1011, n0111, n1111), fade_xyzw.w);
  136. float4 n_zw = lerp(n_0w, n_1w, fade_xyzw.z);
  137. float2 n_yzw = lerp(n_zw.xy, n_zw.zw, fade_xyzw.y);
  138. float n_xyzw = lerp(n_yzw.x, n_yzw.y, fade_xyzw.x);
  139. return 2.2f * n_xyzw;
  140. }
  141. // Classic Perlin noise, periodic version
  142. public static float pnoise(float4 P, float4 rep)
  143. {
  144. float4 Pi0 = fmod(floor(P), rep); // Integer part math.modulo rep
  145. float4 Pi1 = fmod(Pi0 + 1.0f, rep); // Integer part + 1 math.mod rep
  146. Pi0 = mod289(Pi0);
  147. Pi1 = mod289(Pi1);
  148. float4 Pf0 = frac(P); // Fractional part for interpolation
  149. float4 Pf1 = Pf0 - 1.0f; // Fractional part - 1.0
  150. float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  151. float4 iy = float4(Pi0.yy, Pi1.yy);
  152. float4 iz0 = float4(Pi0.zzzz);
  153. float4 iz1 = float4(Pi1.zzzz);
  154. float4 iw0 = float4(Pi0.wwww);
  155. float4 iw1 = float4(Pi1.wwww);
  156. float4 ixy = permute(permute(ix) + iy);
  157. float4 ixy0 = permute(ixy + iz0);
  158. float4 ixy1 = permute(ixy + iz1);
  159. float4 ixy00 = permute(ixy0 + iw0);
  160. float4 ixy01 = permute(ixy0 + iw1);
  161. float4 ixy10 = permute(ixy1 + iw0);
  162. float4 ixy11 = permute(ixy1 + iw1);
  163. float4 gx00 = ixy00 * (1.0f / 7.0f);
  164. float4 gy00 = floor(gx00) * (1.0f / 7.0f);
  165. float4 gz00 = floor(gy00) * (1.0f / 6.0f);
  166. gx00 = frac(gx00) - 0.5f;
  167. gy00 = frac(gy00) - 0.5f;
  168. gz00 = frac(gz00) - 0.5f;
  169. float4 gw00 = float4(0.75f) - abs(gx00) - abs(gy00) - abs(gz00);
  170. float4 sw00 = step(gw00, float4(0.0f));
  171. gx00 -= sw00 * (step(0.0f, gx00) - 0.5f);
  172. gy00 -= sw00 * (step(0.0f, gy00) - 0.5f);
  173. float4 gx01 = ixy01 * (1.0f / 7.0f);
  174. float4 gy01 = floor(gx01) * (1.0f / 7.0f);
  175. float4 gz01 = floor(gy01) * (1.0f / 6.0f);
  176. gx01 = frac(gx01) - 0.5f;
  177. gy01 = frac(gy01) - 0.5f;
  178. gz01 = frac(gz01) - 0.5f;
  179. float4 gw01 = float4(0.75f) - abs(gx01) - abs(gy01) - abs(gz01);
  180. float4 sw01 = step(gw01, float4(0.0f));
  181. gx01 -= sw01 * (step(0.0f, gx01) - 0.5f);
  182. gy01 -= sw01 * (step(0.0f, gy01) - 0.5f);
  183. float4 gx10 = ixy10 * (1.0f / 7.0f);
  184. float4 gy10 = floor(gx10) * (1.0f / 7.0f);
  185. float4 gz10 = floor(gy10) * (1.0f / 6.0f);
  186. gx10 = frac(gx10) - 0.5f;
  187. gy10 = frac(gy10) - 0.5f;
  188. gz10 = frac(gz10) - 0.5f;
  189. float4 gw10 = float4(0.75f) - abs(gx10) - abs(gy10) - abs(gz10);
  190. float4 sw10 = step(gw10, float4(0.0f));
  191. gx10 -= sw10 * (step(0.0f, gx10) - 0.5f);
  192. gy10 -= sw10 * (step(0.0f, gy10) - 0.5f);
  193. float4 gx11 = ixy11 * (1.0f / 7.0f);
  194. float4 gy11 = floor(gx11) * (1.0f / 7.0f);
  195. float4 gz11 = floor(gy11) * (1.0f / 6.0f);
  196. gx11 = frac(gx11) - 0.5f;
  197. gy11 = frac(gy11) - 0.5f;
  198. gz11 = frac(gz11) - 0.5f;
  199. float4 gw11 = float4(0.75f) - abs(gx11) - abs(gy11) - abs(gz11);
  200. float4 sw11 = step(gw11, float4(0.0f));
  201. gx11 -= sw11 * (step(0.0f, gx11) - 0.5f);
  202. gy11 -= sw11 * (step(0.0f, gy11) - 0.5f);
  203. float4 g0000 = float4(gx00.x, gy00.x, gz00.x, gw00.x);
  204. float4 g1000 = float4(gx00.y, gy00.y, gz00.y, gw00.y);
  205. float4 g0100 = float4(gx00.z, gy00.z, gz00.z, gw00.z);
  206. float4 g1100 = float4(gx00.w, gy00.w, gz00.w, gw00.w);
  207. float4 g0010 = float4(gx10.x, gy10.x, gz10.x, gw10.x);
  208. float4 g1010 = float4(gx10.y, gy10.y, gz10.y, gw10.y);
  209. float4 g0110 = float4(gx10.z, gy10.z, gz10.z, gw10.z);
  210. float4 g1110 = float4(gx10.w, gy10.w, gz10.w, gw10.w);
  211. float4 g0001 = float4(gx01.x, gy01.x, gz01.x, gw01.x);
  212. float4 g1001 = float4(gx01.y, gy01.y, gz01.y, gw01.y);
  213. float4 g0101 = float4(gx01.z, gy01.z, gz01.z, gw01.z);
  214. float4 g1101 = float4(gx01.w, gy01.w, gz01.w, gw01.w);
  215. float4 g0011 = float4(gx11.x, gy11.x, gz11.x, gw11.x);
  216. float4 g1011 = float4(gx11.y, gy11.y, gz11.y, gw11.y);
  217. float4 g0111 = float4(gx11.z, gy11.z, gz11.z, gw11.z);
  218. float4 g1111 = float4(gx11.w, gy11.w, gz11.w, gw11.w);
  219. float4 norm00 = taylorInvSqrt(float4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100)));
  220. g0000 *= norm00.x;
  221. g0100 *= norm00.y;
  222. g1000 *= norm00.z;
  223. g1100 *= norm00.w;
  224. float4 norm01 = taylorInvSqrt(float4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101)));
  225. g0001 *= norm01.x;
  226. g0101 *= norm01.y;
  227. g1001 *= norm01.z;
  228. g1101 *= norm01.w;
  229. float4 norm10 = taylorInvSqrt(float4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110)));
  230. g0010 *= norm10.x;
  231. g0110 *= norm10.y;
  232. g1010 *= norm10.z;
  233. g1110 *= norm10.w;
  234. float4 norm11 = taylorInvSqrt(float4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111)));
  235. g0011 *= norm11.x;
  236. g0111 *= norm11.y;
  237. g1011 *= norm11.z;
  238. g1111 *= norm11.w;
  239. float n0000 = dot(g0000, Pf0);
  240. float n1000 = dot(g1000, float4(Pf1.x, Pf0.yzw));
  241. float n0100 = dot(g0100, float4(Pf0.x, Pf1.y, Pf0.zw));
  242. float n1100 = dot(g1100, float4(Pf1.xy, Pf0.zw));
  243. float n0010 = dot(g0010, float4(Pf0.xy, Pf1.z, Pf0.w));
  244. float n1010 = dot(g1010, float4(Pf1.x, Pf0.y, Pf1.z, Pf0.w));
  245. float n0110 = dot(g0110, float4(Pf0.x, Pf1.yz, Pf0.w));
  246. float n1110 = dot(g1110, float4(Pf1.xyz, Pf0.w));
  247. float n0001 = dot(g0001, float4(Pf0.xyz, Pf1.w));
  248. float n1001 = dot(g1001, float4(Pf1.x, Pf0.yz, Pf1.w));
  249. float n0101 = dot(g0101, float4(Pf0.x, Pf1.y, Pf0.z, Pf1.w));
  250. float n1101 = dot(g1101, float4(Pf1.xy, Pf0.z, Pf1.w));
  251. float n0011 = dot(g0011, float4(Pf0.xy, Pf1.zw));
  252. float n1011 = dot(g1011, float4(Pf1.x, Pf0.y, Pf1.zw));
  253. float n0111 = dot(g0111, float4(Pf0.x, Pf1.yzw));
  254. float n1111 = dot(g1111, Pf1);
  255. float4 fade_xyzw = fade(Pf0);
  256. float4 n_0w = lerp(float4(n0000, n1000, n0100, n1100), float4(n0001, n1001, n0101, n1101), fade_xyzw.w);
  257. float4 n_1w = lerp(float4(n0010, n1010, n0110, n1110), float4(n0011, n1011, n0111, n1111), fade_xyzw.w);
  258. float4 n_zw = lerp(n_0w, n_1w, fade_xyzw.z);
  259. float2 n_yzw = lerp(n_zw.xy, n_zw.zw, fade_xyzw.y);
  260. float n_xyzw = lerp(n_yzw.x, n_yzw.y, fade_xyzw.x);
  261. return 2.2f * n_xyzw;
  262. }
  263. }
  264. }