SHARE
TWEET

Shader Noise Functions - Unity Shaderlab port

yankooliveira Jun 18th, 2016 204 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // All shader code originally from
  4. //  https://github.com/stegu/webgl-noise/wiki
  5. //  I've blindly ported them to further experiment with later on,
  6. //  but I've tested all except the gradient and 4d ones and they
  7. //  seem to be working. Excuse any errors :)
  8. //
  9. //  Have fun!
  10. //
  11. //        - @yankooliveira
  12. //
  13. //////////////////////////////////////////////////////////////////////////////////
  14.  
  15. //////////////////////////////////////////////////////////////////////////////////
  16. // Helper functions
  17. //////////////////////////////////////////////////////////////////////////////////
  18.  
  19. float2 fade(float2 t) {
  20.     return t*t*t*(t*(t*6.0 - 15.0) + 10.0);
  21. }
  22.  
  23. float3 fade(float3 t) {
  24.     return t*t*t*(t*(t*6.0 - 15.0) + 10.0);
  25. }
  26.  
  27. float4 fade(float4 t) {
  28.     return t*t*t*(t*(t*6.0 - 15.0) + 10.0);
  29. }
  30.  
  31. // Modulo 7 without a division
  32. float4 mod7(float4 x) {
  33.     return x - floor(x * (1.0 / 7.0)) * 7.0;
  34. }
  35.  
  36. // Modulo 7 without a division
  37. float3 mod7(float3 x) {
  38.     return x - floor(x * (1.0 / 7.0)) * 7.0;
  39. }
  40.  
  41. float2 mod289(float2 x) {
  42.     return x - floor(x * (1.0 / 289.0)) * 289.0;
  43. }
  44.  
  45. // Modulo 289 without a division (only multiplications)
  46. float3 mod289(float3 x) {
  47.     return x - floor(x * (1.0 / 289.0)) * 289.0;
  48. }
  49.  
  50. float4 mod289(float4 x) {
  51.     return x - floor(x * (1.0 / 289.0)) * 289.0;
  52. }
  53.  
  54. float mod289(float x) {
  55.     return x - floor(x * (1.0 / 289.0)) * 289.0;
  56. }
  57.  
  58. // Permutation polynomial: (34x^2 + x) mod 289
  59. float3 permute(float3 x) {
  60.     return mod289((34.0 * x + 1.0) * x);
  61. }
  62.  
  63. float4 permute(float4 x) {
  64.     return mod289(((x*34.0) + 1.0)*x);
  65. }
  66.  
  67. float permute(float x) {
  68.     return mod289(((x*34.0) + 1.0)*x);
  69. }
  70.  
  71. float4 taylorInvSqrt(float4 r)
  72. {
  73.     return 1.79284291400159 - 0.85373472095314 * r;
  74. }
  75.  
  76. float4 lessThan(float4 x, float4 y) {
  77.     return 1 - step(y, x);
  78. }
  79.  
  80. float4 grad4(float j, float4 ip)
  81. {
  82.     const float4 ones = float4(1.0, 1.0, 1.0, -1.0);
  83.     float4 p, s;
  84.  
  85.     p.xyz = floor(frac(float3(j,j,j)* ip.xyz) * 7.0) * ip.z - 1.0;
  86.     p.w = 1.5 - dot(abs(p.xyz), ones.xyz);
  87.     s = float4(lessThan(p, float4(0,0,0,0)));
  88.     p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www;
  89.  
  90.     return p;
  91. }
  92.  
  93. ///////////////////////////////////////////////////////////////////////////
  94. //3d noise
  95. //
  96. // Description : Array and textureless GLSL 2D/3D/4D simplex
  97. //               noise functions.
  98. //      Author : Ian McEwan, Ashima Arts.
  99. //  Maintainer : stegu
  100. //     Lastmod : 20110822 (ijm)
  101. //     License : Copyright (C) 2011 Ashima Arts. All rights reserved.
  102. //               Distributed under the MIT License. See LICENSE file.
  103. //               https://github.com/ashima/webgl-noise
  104. //               https://github.com/stegu/webgl-noise
  105. //
  106. ///////////////////////////////////////////////////////////////////////////
  107.  
  108. float simplex3d(float3 v)
  109. {
  110.     const float2  C = float2(1.0 / 6.0, 1.0 / 3.0);
  111.     const float4  D = float4(0.0, 0.5, 1.0, 2.0);
  112.  
  113.     // First corner
  114.     float3 i = floor(v + dot(v, C.yyy));
  115.     float3 x0 = v - i + dot(i, C.xxx);
  116.  
  117.     // Other corners
  118.     float3 g = step(x0.yzx, x0.xyz);
  119.     float3 l = 1.0 - g;
  120.     float3 i1 = min(g.xyz, l.zxy);
  121.     float3 i2 = max(g.xyz, l.zxy);
  122.  
  123.     //   x0 = x0 - 0.0 + 0.0 * C.xxx;
  124.     //   x1 = x0 - i1  + 1.0 * C.xxx;
  125.     //   x2 = x0 - i2  + 2.0 * C.xxx;
  126.     //   x3 = x0 - 1.0 + 3.0 * C.xxx;
  127.     float3 x1 = x0 - i1 + C.xxx;
  128.     float3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
  129.     float3 x3 = x0 - D.yyy;      // -1.0+3.0*C.x = -0.5 = -D.y
  130.  
  131.                                  // Permutations
  132.     i = mod289(i);
  133.     float4 p = permute(permute(permute(
  134.         i.z + float4(0.0, i1.z, i2.z, 1.0))
  135.         + i.y + float4(0.0, i1.y, i2.y, 1.0))
  136.         + i.x + float4(0.0, i1.x, i2.x, 1.0));
  137.  
  138.     // Gradients: 7x7 points over a square, mapped onto an octahedron.
  139.     // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
  140.     float n_ = 0.142857142857; // 1.0/7.0
  141.     float3  ns = n_ * D.wyz - D.xzx;
  142.  
  143.     float4 j = p - 49.0 * floor(p * ns.z * ns.z);  //  mod(p,7*7)
  144.  
  145.     float4 x_ = floor(j * ns.z);
  146.     float4 y_ = floor(j - 7.0 * x_);    // mod(j,N)
  147.  
  148.     float4 x = x_ *ns.x + ns.yyyy;
  149.     float4 y = y_ *ns.x + ns.yyyy;
  150.     float4 h = 1.0 - abs(x) - abs(y);
  151.  
  152.     float4 b0 = float4(x.xy, y.xy);
  153.     float4 b1 = float4(x.zw, y.zw);
  154.  
  155.     //float4 s0 = float4(lessThan(b0,0.0))*2.0 - 1.0;
  156.     //float4 s1 = float4(lessThan(b1,0.0))*2.0 - 1.0;
  157.     float4 s0 = floor(b0)*2.0 + 1.0;
  158.     float4 s1 = floor(b1)*2.0 + 1.0;
  159.     float4 sh = -step(h, float4(0, 0, 0, 0));
  160.  
  161.     float4 a0 = b0.xzyw + s0.xzyw*sh.xxyy;
  162.     float4 a1 = b1.xzyw + s1.xzyw*sh.zzww;
  163.  
  164.     float3 p0 = float3(a0.xy, h.x);
  165.     float3 p1 = float3(a0.zw, h.y);
  166.     float3 p2 = float3(a1.xy, h.z);
  167.     float3 p3 = float3(a1.zw, h.w);
  168.  
  169.     //Normalise gradients
  170.     float4 norm = taylorInvSqrt(float4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
  171.     p0 *= norm.x;
  172.     p1 *= norm.y;
  173.     p2 *= norm.z;
  174.     p3 *= norm.w;
  175.  
  176.     // Mix final noise value
  177.     float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
  178.     m = m * m;
  179.     return 42.0 * dot(m*m, float4(dot(p0, x0), dot(p1, x1),
  180.         dot(p2, x2), dot(p3, x3)));
  181. }
  182.  
  183. ///////////////////////////////////////////////////////////////////////////
  184. // Cellular noise ("Worley noise") in 2D in GLSL.
  185. // Copyright (c) Stefan Gustavson 2011-04-19. All rights reserved.
  186. // This code is released under the conditions of the MIT license.
  187. // See LICENSE file for details.
  188. // https://github.com/stegu/webgl-noise
  189. ///////////////////////////////////////////////////////////////////////////
  190.  
  191. // Cellular noise, returning F1 and F2 in a float2.
  192. // Standard 3x3 search window for good F1 and F2 values
  193. float2 cellular(float2 P) {
  194. #define K 0.142857142857 // 1/7
  195. #define Ko 0.428571428571 // 3/7
  196. #define jitter 1.0 // Less gives more regular pattern
  197.     float2 Pi = mod289(floor(P));
  198.     float2 Pf = frac(P);
  199.     float3 oi = float3(-1.0, 0.0, 1.0);
  200.     float3 of = float3(-0.5, 0.5, 1.5);
  201.     float3 px = permute(Pi.x + oi);
  202.     float3 p = permute(px.x + Pi.y + oi); // p11, p12, p13
  203.     float3 ox = frac(p*K) - Ko;
  204.     float3 oy = mod7(floor(p*K))*K - Ko;
  205.     float3 dx = Pf.x + 0.5 + jitter*ox;
  206.     float3 dy = Pf.y - of + jitter*oy;
  207.     float3 d1 = dx * dx + dy * dy; // d11, d12 and d13, squared
  208.     p = permute(px.y + Pi.y + oi); // p21, p22, p23
  209.     ox = frac(p*K) - Ko;
  210.     oy = mod7(floor(p*K))*K - Ko;
  211.     dx = Pf.x - 0.5 + jitter*ox;
  212.     dy = Pf.y - of + jitter*oy;
  213.     float3 d2 = dx * dx + dy * dy; // d21, d22 and d23, squared
  214.     p = permute(px.z + Pi.y + oi); // p31, p32, p33
  215.     ox = frac(p*K) - Ko;
  216.     oy = mod7(floor(p*K))*K - Ko;
  217.     dx = Pf.x - 1.5 + jitter*ox;
  218.     dy = Pf.y - of + jitter*oy;
  219.     float3 d3 = dx * dx + dy * dy; // d31, d32 and d33, squared
  220.                                  // Sort out the two smallest distances (F1, F2)
  221.     float3 d1a = min(d1, d2);
  222.     d2 = max(d1, d2); // Swap to keep candidates for F2
  223.     d2 = min(d2, d3); // neither F1 nor F2 are now in d3
  224.     d1 = min(d1a, d2); // F1 is now in d1
  225.     d2 = max(d1a, d2); // Swap to keep candidates for F2
  226.     d1.xy = (d1.x < d1.y) ? d1.xy : d1.yx; // Swap if smaller
  227.     d1.xz = (d1.x < d1.z) ? d1.xz : d1.zx; // F1 is in d1.x
  228.     d1.yz = min(d1.yz, d2.yz); // F2 is now not in d2.yz
  229.     d1.y = min(d1.y, d1.z); // nor in  d1.z
  230.     d1.y = min(d1.y, d2.x); // F2 is in d1.y, we're done.
  231.     return sqrt(d1.xy);
  232. }
  233.  
  234. ///////////////////////////////////////////////////////////////////////////
  235. // Cellular noise, returning F1 and F2 in a float2.
  236. // Speeded up by using 2x2 search window instead of 3x3,
  237. // at the expense of some strong pattern artifacts.
  238. // F2 is often wrong and has sharp discontinuities.
  239. // If you need a smooth F2, use the slower 3x3 version.
  240. // F1 is sometimes wrong, too, but OK for most purposes.
  241. ///////////////////////////////////////////////////////////////////////////
  242.  
  243. float2 cellular2x2(float2 P) {
  244. #define K 0.142857142857 // 1/7
  245. #define K2 0.0714285714285 // K/2
  246. #define jitter2x2 0.8 // jitter 1.0 makes F1 wrong more often
  247.     float2 Pi = mod289(floor(P));
  248.     float2 Pf = frac(P);
  249.     float4 Pfx = Pf.x + float4(-0.5, -1.5, -0.5, -1.5);
  250.     float4 Pfy = Pf.y + float4(-0.5, -0.5, -1.5, -1.5);
  251.     float4 p = permute(Pi.x + float4(0.0, 1.0, 0.0, 1.0));
  252.     p = permute(p + Pi.y + float4(0.0, 0.0, 1.0, 1.0));
  253.     float4 ox = mod7(p)*K + K2;
  254.     float4 oy = mod7(floor(p*K))*K + K2;
  255.     float4 dx = Pfx + jitter2x2*ox;
  256.     float4 dy = Pfy + jitter2x2*oy;
  257.     float4 d = dx * dx + dy * dy; // d11, d12, d21 and d22, squared
  258.                                 // Sort out the two smallest distances
  259. #if 0
  260.                                 // Cheat and pick only F1
  261.     d.xy = min(d.xy, d.zw);
  262.     d.x = min(d.x, d.y);
  263.     return float2(sqrt(d.x)); // F1 duplicated, F2 not computed
  264. #else
  265.                                 // Do it right and find both F1 and F2
  266.     d.xy = (d.x < d.y) ? d.xy : d.yx; // Swap if smaller
  267.     d.xz = (d.x < d.z) ? d.xz : d.zx;
  268.     d.xw = (d.x < d.w) ? d.xw : d.wx;
  269.     d.y = min(d.y, d.z);
  270.     d.y = min(d.y, d.w);
  271.     return sqrt(d.xy);
  272. #endif
  273. }
  274.  
  275. ///////////////////////////////////////////////////////////////////////////
  276. // Cellular noise, returning F1 and F2 in a float2.
  277. // Speeded up by using 2x2x2 search window instead of 3x3x3,
  278. // at the expense of some pattern artifacts.
  279. // F2 is often wrong and has sharp discontinuities.
  280. // If you need a good F2, use the slower 3x3x3 version.
  281. ///////////////////////////////////////////////////////////////////////////
  282. float2 cellular2x2x2(float3 P) {
  283. #define K 0.142857142857 // 1/7
  284. #define Ko 0.428571428571 // 1/2-K/2
  285. #define K2_2x2x2 0.020408163265306 // 1/(7*7)
  286. #define Kz 0.166666666667 // 1/6
  287. #define Kzo 0.416666666667 // 1/2-1/6*2
  288. #define jitter2x2x2 0.8 // smaller jitter gives less errors in F2
  289.     float3 Pi = mod289(floor(P));
  290.     float3 Pf = frac(P);
  291.     float4 Pfx = Pf.x + float4(0.0, -1.0, 0.0, -1.0);
  292.     float4 Pfy = Pf.y + float4(0.0, 0.0, -1.0, -1.0);
  293.     float4 p = permute(Pi.x + float4(0.0, 1.0, 0.0, 1.0));
  294.     p = permute(p + Pi.y + float4(0.0, 0.0, 1.0, 1.0));
  295.     float4 p1 = permute(p + Pi.z); // z+0
  296.     float4 p2 = permute(p + Pi.z + float4(1,1,1,1)); // z+1
  297.     float4 ox1 = frac(p1*K) - Ko;
  298.     float4 oy1 = mod7(floor(p1*K))*K - Ko;
  299.     float4 oz1 = floor(p1*K2_2x2x2)*Kz - Kzo; // p1 < 289 guaranteed
  300.     float4 ox2 = frac(p2*K) - Ko;
  301.     float4 oy2 = mod7(floor(p2*K))*K - Ko;
  302.     float4 oz2 = floor(p2*K2_2x2x2)*Kz - Kzo;
  303.     float4 dx1 = Pfx + jitter2x2x2*ox1;
  304.     float4 dy1 = Pfy + jitter2x2x2*oy1;
  305.     float4 dz1 = Pf.z + jitter2x2x2*oz1;
  306.     float4 dx2 = Pfx + jitter2x2x2*ox2;
  307.     float4 dy2 = Pfy + jitter2x2x2*oy2;
  308.     float4 dz2 = Pf.z - 1.0 + jitter2x2x2*oz2;
  309.     float4 d1 = dx1 * dx1 + dy1 * dy1 + dz1 * dz1; // z+0
  310.     float4 d2 = dx2 * dx2 + dy2 * dy2 + dz2 * dz2; // z+1
  311.  
  312.                                                  // Sort out the two smallest distances (F1, F2)
  313. #if 0
  314.                                                  // Cheat and sort out only F1
  315.     d1 = min(d1, d2);
  316.     d1.xy = min(d1.xy, d1.wz);
  317.     d1.x = min(d1.x, d1.y);
  318.     return float2(sqrt(d1.x));
  319. #else
  320.                                                  // Do it right and sort out both F1 and F2
  321.     float4 d = min(d1, d2); // F1 is now in d
  322.     d2 = max(d1, d2); // Make sure we keep all candidates for F2
  323.     d.xy = (d.x < d.y) ? d.xy : d.yx; // Swap smallest to d.x
  324.     d.xz = (d.x < d.z) ? d.xz : d.zx;
  325.     d.xw = (d.x < d.w) ? d.xw : d.wx; // F1 is now in d.x
  326.     d.yzw = min(d.yzw, d2.yzw); // F2 now not in d2.yzw
  327.     d.y = min(d.y, d.z); // nor in d.z
  328.     d.y = min(d.y, d.w); // nor in d.w
  329.     d.y = min(d.y, d2.x); // F2 is now in d.y
  330.     return sqrt(d.xy); // F1 and F2
  331. #endif
  332. }
  333.  
  334. float2 cellular(float3 P) {
  335. #define K 0.142857142857 // 1/7
  336. #define Ko 0.428571428571 // 1/2-K/2
  337. #define K2_3d 0.020408163265306 // 1/(7*7)
  338. #define Kz 0.166666666667 // 1/6
  339. #define Kzo 0.416666666667 // 1/2-1/6*2
  340. #define jitter 1.0 // smaller jitter gives more regular pattern
  341.  
  342.     float3 Pi = mod289(floor(P));
  343.     float3 Pf = frac(P) - 0.5;
  344.  
  345.     float3 Pfx = Pf.x + float3(1.0, 0.0, -1.0);
  346.     float3 Pfy = Pf.y + float3(1.0, 0.0, -1.0);
  347.     float3 Pfz = Pf.z + float3(1.0, 0.0, -1.0);
  348.  
  349.     float3 p = permute(Pi.x + float3(-1.0, 0.0, 1.0));
  350.     float3 p1 = permute(p + Pi.y - 1.0);
  351.     float3 p2 = permute(p + Pi.y);
  352.     float3 p3 = permute(p + Pi.y + 1.0);
  353.  
  354.     float3 p11 = permute(p1 + Pi.z - 1.0);
  355.     float3 p12 = permute(p1 + Pi.z);
  356.     float3 p13 = permute(p1 + Pi.z + 1.0);
  357.  
  358.     float3 p21 = permute(p2 + Pi.z - 1.0);
  359.     float3 p22 = permute(p2 + Pi.z);
  360.     float3 p23 = permute(p2 + Pi.z + 1.0);
  361.  
  362.     float3 p31 = permute(p3 + Pi.z - 1.0);
  363.     float3 p32 = permute(p3 + Pi.z);
  364.     float3 p33 = permute(p3 + Pi.z + 1.0);
  365.  
  366.     float3 ox11 = frac(p11*K) - Ko;
  367.     float3 oy11 = mod7(floor(p11*K))*K - Ko;
  368.     float3 oz11 = floor(p11*K2_3d)*Kz - Kzo; // p11 < 289 guaranteed
  369.  
  370.     float3 ox12 = frac(p12*K) - Ko;
  371.     float3 oy12 = mod7(floor(p12*K))*K - Ko;
  372.     float3 oz12 = floor(p12*K2_3d)*Kz - Kzo;
  373.  
  374.     float3 ox13 = frac(p13*K) - Ko;
  375.     float3 oy13 = mod7(floor(p13*K))*K - Ko;
  376.     float3 oz13 = floor(p13*K2_3d)*Kz - Kzo;
  377.  
  378.     float3 ox21 = frac(p21*K) - Ko;
  379.     float3 oy21 = mod7(floor(p21*K))*K - Ko;
  380.     float3 oz21 = floor(p21*K2_3d)*Kz - Kzo;
  381.  
  382.     float3 ox22 = frac(p22*K) - Ko;
  383.     float3 oy22 = mod7(floor(p22*K))*K - Ko;
  384.     float3 oz22 = floor(p22*K2_3d)*Kz - Kzo;
  385.  
  386.     float3 ox23 = frac(p23*K) - Ko;
  387.     float3 oy23 = mod7(floor(p23*K))*K - Ko;
  388.     float3 oz23 = floor(p23*K2_3d)*Kz - Kzo;
  389.  
  390.     float3 ox31 = frac(p31*K) - Ko;
  391.     float3 oy31 = mod7(floor(p31*K))*K - Ko;
  392.     float3 oz31 = floor(p31*K2_3d)*Kz - Kzo;
  393.  
  394.     float3 ox32 = frac(p32*K) - Ko;
  395.     float3 oy32 = mod7(floor(p32*K))*K - Ko;
  396.     float3 oz32 = floor(p32*K2_3d)*Kz - Kzo;
  397.  
  398.     float3 ox33 = frac(p33*K) - Ko;
  399.     float3 oy33 = mod7(floor(p33*K))*K - Ko;
  400.     float3 oz33 = floor(p33*K2_3d)*Kz - Kzo;
  401.  
  402.     float3 dx11 = Pfx + jitter*ox11;
  403.     float3 dy11 = Pfy.x + jitter*oy11;
  404.     float3 dz11 = Pfz.x + jitter*oz11;
  405.  
  406.     float3 dx12 = Pfx + jitter*ox12;
  407.     float3 dy12 = Pfy.x + jitter*oy12;
  408.     float3 dz12 = Pfz.y + jitter*oz12;
  409.  
  410.     float3 dx13 = Pfx + jitter*ox13;
  411.     float3 dy13 = Pfy.x + jitter*oy13;
  412.     float3 dz13 = Pfz.z + jitter*oz13;
  413.  
  414.     float3 dx21 = Pfx + jitter*ox21;
  415.     float3 dy21 = Pfy.y + jitter*oy21;
  416.     float3 dz21 = Pfz.x + jitter*oz21;
  417.  
  418.     float3 dx22 = Pfx + jitter*ox22;
  419.     float3 dy22 = Pfy.y + jitter*oy22;
  420.     float3 dz22 = Pfz.y + jitter*oz22;
  421.  
  422.     float3 dx23 = Pfx + jitter*ox23;
  423.     float3 dy23 = Pfy.y + jitter*oy23;
  424.     float3 dz23 = Pfz.z + jitter*oz23;
  425.  
  426.     float3 dx31 = Pfx + jitter*ox31;
  427.     float3 dy31 = Pfy.z + jitter*oy31;
  428.     float3 dz31 = Pfz.x + jitter*oz31;
  429.  
  430.     float3 dx32 = Pfx + jitter*ox32;
  431.     float3 dy32 = Pfy.z + jitter*oy32;
  432.     float3 dz32 = Pfz.y + jitter*oz32;
  433.  
  434.     float3 dx33 = Pfx + jitter*ox33;
  435.     float3 dy33 = Pfy.z + jitter*oy33;
  436.     float3 dz33 = Pfz.z + jitter*oz33;
  437.  
  438.     float3 d11 = dx11 * dx11 + dy11 * dy11 + dz11 * dz11;
  439.     float3 d12 = dx12 * dx12 + dy12 * dy12 + dz12 * dz12;
  440.     float3 d13 = dx13 * dx13 + dy13 * dy13 + dz13 * dz13;
  441.     float3 d21 = dx21 * dx21 + dy21 * dy21 + dz21 * dz21;
  442.     float3 d22 = dx22 * dx22 + dy22 * dy22 + dz22 * dz22;
  443.     float3 d23 = dx23 * dx23 + dy23 * dy23 + dz23 * dz23;
  444.     float3 d31 = dx31 * dx31 + dy31 * dy31 + dz31 * dz31;
  445.     float3 d32 = dx32 * dx32 + dy32 * dy32 + dz32 * dz32;
  446.     float3 d33 = dx33 * dx33 + dy33 * dy33 + dz33 * dz33;
  447.  
  448.     // Sort out the two smallest distances (F1, F2)
  449. #if 0
  450.     // Cheat and sort out only F1
  451.     float3 d1 = min(min(d11, d12), d13);
  452.     float3 d2 = min(min(d21, d22), d23);
  453.     float3 d3 = min(min(d31, d32), d33);
  454.     float3 d = min(min(d1, d2), d3);
  455.     d.x = min(min(d.x, d.y), d.z);
  456.     return float2(sqrt(d.x)); // F1 duplicated, no F2 computed
  457. #else
  458.     // Do it right and sort out both F1 and F2
  459.     float3 d1a = min(d11, d12);
  460.     d12 = max(d11, d12);
  461.     d11 = min(d1a, d13); // Smallest now not in d12 or d13
  462.     d13 = max(d1a, d13);
  463.     d12 = min(d12, d13); // 2nd smallest now not in d13
  464.     float3 d2a = min(d21, d22);
  465.     d22 = max(d21, d22);
  466.     d21 = min(d2a, d23); // Smallest now not in d22 or d23
  467.     d23 = max(d2a, d23);
  468.     d22 = min(d22, d23); // 2nd smallest now not in d23
  469.     float3 d3a = min(d31, d32);
  470.     d32 = max(d31, d32);
  471.     d31 = min(d3a, d33); // Smallest now not in d32 or d33
  472.     d33 = max(d3a, d33);
  473.     d32 = min(d32, d33); // 2nd smallest now not in d33
  474.     float3 da = min(d11, d21);
  475.     d21 = max(d11, d21);
  476.     d11 = min(da, d31); // Smallest now in d11
  477.     d31 = max(da, d31); // 2nd smallest now not in d31
  478.     d11.xy = (d11.x < d11.y) ? d11.xy : d11.yx;
  479.     d11.xz = (d11.x < d11.z) ? d11.xz : d11.zx; // d11.x now smallest
  480.     d12 = min(d12, d21); // 2nd smallest now not in d21
  481.     d12 = min(d12, d22); // nor in d22
  482.     d12 = min(d12, d31); // nor in d31
  483.     d12 = min(d12, d32); // nor in d32
  484.     d11.yz = min(d11.yz, d12.xy); // nor in d12.yz
  485.     d11.y = min(d11.y, d12.z); // Only two more to go
  486.     d11.y = min(d11.y, d11.z); // Done! (Phew!)
  487.     return sqrt(d11.xy); // F1, F2
  488. #endif
  489. }
  490.  
  491. // Classic Perlin noise
  492. float perlin(float2 P)
  493. {
  494.     float4 Pi = floor(P.xyxy) + float4(0.0, 0.0, 1.0, 1.0);
  495.     float4 Pf = frac(P.xyxy) - float4(0.0, 0.0, 1.0, 1.0);
  496.     Pi = mod289(Pi); // To avoid truncation effects in permutation
  497.     float4 ix = Pi.xzxz;
  498.     float4 iy = Pi.yyww;
  499.     float4 fx = Pf.xzxz;
  500.     float4 fy = Pf.yyww;
  501.  
  502.     float4 i = permute(permute(ix) + iy);
  503.  
  504.     float4 gx = frac(i * (1.0 / 41.0)) * 2.0 - 1.0;
  505.     float4 gy = abs(gx) - 0.5;
  506.     float4 tx = floor(gx + 0.5);
  507.     gx = gx - tx;
  508.  
  509.     float2 g00 = float2(gx.x, gy.x);
  510.     float2 g10 = float2(gx.y, gy.y);
  511.     float2 g01 = float2(gx.z, gy.z);
  512.     float2 g11 = float2(gx.w, gy.w);
  513.  
  514.     float4 norm = taylorInvSqrt(float4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
  515.     g00 *= norm.x;
  516.     g01 *= norm.y;
  517.     g10 *= norm.z;
  518.     g11 *= norm.w;
  519.  
  520.     float n00 = dot(g00, float2(fx.x, fy.x));
  521.     float n10 = dot(g10, float2(fx.y, fy.y));
  522.     float n01 = dot(g01, float2(fx.z, fy.z));
  523.     float n11 = dot(g11, float2(fx.w, fy.w));
  524.  
  525.     float2 fade_xy = fade(Pf.xy);
  526.     float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x);
  527.     float n_xy = lerp(n_x.x, n_x.y, fade_xy.y);
  528.     return 2.3 * n_xy;
  529. }
  530.  
  531. // Classic Perlin noise, periodic variant
  532. float perlinPeriodic(float2 P, float2 rep)
  533. {
  534.     float4 Pi = floor(P.xyxy) + float4(0.0, 0.0, 1.0, 1.0);
  535.     float4 Pf = frac(P.xyxy) - float4(0.0, 0.0, 1.0, 1.0);
  536.     Pi = fmod(Pi, rep.xyxy); // To create noise with explicit period
  537.     Pi = mod289(Pi);        // To avoid truncation effects in permutation
  538.     float4 ix = Pi.xzxz;
  539.     float4 iy = Pi.yyww;
  540.     float4 fx = Pf.xzxz;
  541.     float4 fy = Pf.yyww;
  542.  
  543.     float4 i = permute(permute(ix) + iy);
  544.  
  545.     float4 gx = frac(i * (1.0 / 41.0)) * 2.0 - 1.0;
  546.     float4 gy = abs(gx) - 0.5;
  547.     float4 tx = floor(gx + 0.5);
  548.     gx = gx - tx;
  549.  
  550.     float2 g00 = float2(gx.x, gy.x);
  551.     float2 g10 = float2(gx.y, gy.y);
  552.     float2 g01 = float2(gx.z, gy.z);
  553.     float2 g11 = float2(gx.w, gy.w);
  554.  
  555.     float4 norm = taylorInvSqrt(float4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
  556.     g00 *= norm.x;
  557.     g01 *= norm.y;
  558.     g10 *= norm.z;
  559.     g11 *= norm.w;
  560.  
  561.     float n00 = dot(g00, float2(fx.x, fy.x));
  562.     float n10 = dot(g10, float2(fx.y, fy.y));
  563.     float n01 = dot(g01, float2(fx.z, fy.z));
  564.     float n11 = dot(g11, float2(fx.w, fy.w));
  565.  
  566.     float2 fade_xy = fade(Pf.xy);
  567.     float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x);
  568.     float n_xy = lerp(n_x.x, n_x.y, fade_xy.y);
  569.     return 2.3 * n_xy;
  570. }
  571.  
  572. float perlin3d(float3 P)
  573. {
  574.     float3 Pi0 = floor(P); // Integer part for indexing
  575.     float3 Pi1 = Pi0 + float3(1,1,1); // Integer part + 1
  576.     Pi0 = mod289(Pi0);
  577.     Pi1 = mod289(Pi1);
  578.     float3 Pf0 = frac(P); // Fractional part for interpolation
  579.     float3 Pf1 = Pf0 - float3(1,1,1); // Fractional part - 1.0
  580.     float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  581.     float4 iy = float4(Pi0.yy, Pi1.yy);
  582.     float4 iz0 = Pi0.zzzz;
  583.     float4 iz1 = Pi1.zzzz;
  584.  
  585.     float4 ixy = permute(permute(ix) + iy);
  586.     float4 ixy0 = permute(ixy + iz0);
  587.     float4 ixy1 = permute(ixy + iz1);
  588.  
  589.     float4 gx0 = ixy0 * (1.0 / 7.0);
  590.     float4 gy0 = frac(floor(gx0) * (1.0 / 7.0)) - 0.5;
  591.     gx0 = frac(gx0);
  592.     float4 gz0 = float4(0.5, 0.5, 0.5, 0.5) - abs(gx0) - abs(gy0);
  593.     float4 sz0 = step(gz0, float4(0,0,0,0));
  594.     gx0 -= sz0 * (step(0.0, gx0) - 0.5);
  595.     gy0 -= sz0 * (step(0.0, gy0) - 0.5);
  596.  
  597.     float4 gx1 = ixy1 * (1.0 / 7.0);
  598.     float4 gy1 = frac(floor(gx1) * (1.0 / 7.0)) - 0.5;
  599.     gx1 = frac(gx1);
  600.     float4 gz1 = float4(0.5, 0.5, 0.5, 0.5) - abs(gx1) - abs(gy1);
  601.     float4 sz1 = step(gz1, float4(0,0,0,0));
  602.     gx1 -= sz1 * (step(0.0, gx1) - 0.5);
  603.     gy1 -= sz1 * (step(0.0, gy1) - 0.5);
  604.  
  605.     float3 g000 = float3(gx0.x, gy0.x, gz0.x);
  606.     float3 g100 = float3(gx0.y, gy0.y, gz0.y);
  607.     float3 g010 = float3(gx0.z, gy0.z, gz0.z);
  608.     float3 g110 = float3(gx0.w, gy0.w, gz0.w);
  609.     float3 g001 = float3(gx1.x, gy1.x, gz1.x);
  610.     float3 g101 = float3(gx1.y, gy1.y, gz1.y);
  611.     float3 g011 = float3(gx1.z, gy1.z, gz1.z);
  612.     float3 g111 = float3(gx1.w, gy1.w, gz1.w);
  613.  
  614.     float4 norm0 = taylorInvSqrt(float4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
  615.     g000 *= norm0.x;
  616.     g010 *= norm0.y;
  617.     g100 *= norm0.z;
  618.     g110 *= norm0.w;
  619.     float4 norm1 = taylorInvSqrt(float4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
  620.     g001 *= norm1.x;
  621.     g011 *= norm1.y;
  622.     g101 *= norm1.z;
  623.     g111 *= norm1.w;
  624.  
  625.     float n000 = dot(g000, Pf0);
  626.     float n100 = dot(g100, float3(Pf1.x, Pf0.yz));
  627.     float n010 = dot(g010, float3(Pf0.x, Pf1.y, Pf0.z));
  628.     float n110 = dot(g110, float3(Pf1.xy, Pf0.z));
  629.     float n001 = dot(g001, float3(Pf0.xy, Pf1.z));
  630.     float n101 = dot(g101, float3(Pf1.x, Pf0.y, Pf1.z));
  631.     float n011 = dot(g011, float3(Pf0.x, Pf1.yz));
  632.     float n111 = dot(g111, Pf1);
  633.  
  634.     float3 fade_xyz = fade(Pf0);
  635.     float4 n_z = lerp(float4(n000, n100, n010, n110), float4(n001, n101, n011, n111), fade_xyz.z);
  636.     float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y);
  637.     float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x);
  638.     return 2.2 * n_xyz;
  639. }
  640.  
  641. // Classic Perlin noise, periodic variant
  642. float perlin3dPeriodic(float3 P, float3 rep)
  643. {
  644.     float3 Pi0 = fmod(floor(P), rep); // Integer part, modulo period
  645.     float3 Pi1 = fmod(Pi0 + float3(1,1,1), rep); // Integer part + 1, mod period
  646.     Pi0 = mod289(Pi0);
  647.     Pi1 = mod289(Pi1);
  648.     float3 Pf0 = frac(P); // Fractional part for interpolation
  649.     float3 Pf1 = Pf0 - float3(1,1,1); // Fractional part - 1.0
  650.     float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  651.     float4 iy = float4(Pi0.yy, Pi1.yy);
  652.     float4 iz0 = Pi0.zzzz;
  653.     float4 iz1 = Pi1.zzzz;
  654.  
  655.     float4 ixy = permute(permute(ix) + iy);
  656.     float4 ixy0 = permute(ixy + iz0);
  657.     float4 ixy1 = permute(ixy + iz1);
  658.  
  659.     float4 gx0 = ixy0 * (1.0 / 7.0);
  660.     float4 gy0 = frac(floor(gx0) * (1.0 / 7.0)) - 0.5;
  661.     gx0 = frac(gx0);
  662.     float4 gz0 = float4(0.5, 0.5, 0.5, 0.5) - abs(gx0) - abs(gy0);
  663.     float4 sz0 = step(gz0, float4(0,0,0,0));
  664.     gx0 -= sz0 * (step(0.0, gx0) - 0.5);
  665.     gy0 -= sz0 * (step(0.0, gy0) - 0.5);
  666.  
  667.     float4 gx1 = ixy1 * (1.0 / 7.0);
  668.     float4 gy1 = frac(floor(gx1) * (1.0 / 7.0)) - 0.5;
  669.     gx1 = frac(gx1);
  670.     float4 gz1 = float4(0.5, 0.5, 0.5, 0.5) - abs(gx1) - abs(gy1);
  671.     float4 sz1 = step(gz1, float4(0,0,0,0));
  672.     gx1 -= sz1 * (step(0.0, gx1) - 0.5);
  673.     gy1 -= sz1 * (step(0.0, gy1) - 0.5);
  674.  
  675.     float3 g000 = float3(gx0.x, gy0.x, gz0.x);
  676.     float3 g100 = float3(gx0.y, gy0.y, gz0.y);
  677.     float3 g010 = float3(gx0.z, gy0.z, gz0.z);
  678.     float3 g110 = float3(gx0.w, gy0.w, gz0.w);
  679.     float3 g001 = float3(gx1.x, gy1.x, gz1.x);
  680.     float3 g101 = float3(gx1.y, gy1.y, gz1.y);
  681.     float3 g011 = float3(gx1.z, gy1.z, gz1.z);
  682.     float3 g111 = float3(gx1.w, gy1.w, gz1.w);
  683.  
  684.     float4 norm0 = taylorInvSqrt(float4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
  685.     g000 *= norm0.x;
  686.     g010 *= norm0.y;
  687.     g100 *= norm0.z;
  688.     g110 *= norm0.w;
  689.     float4 norm1 = taylorInvSqrt(float4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
  690.     g001 *= norm1.x;
  691.     g011 *= norm1.y;
  692.     g101 *= norm1.z;
  693.     g111 *= norm1.w;
  694.  
  695.     float n000 = dot(g000, Pf0);
  696.     float n100 = dot(g100, float3(Pf1.x, Pf0.yz));
  697.     float n010 = dot(g010, float3(Pf0.x, Pf1.y, Pf0.z));
  698.     float n110 = dot(g110, float3(Pf1.xy, Pf0.z));
  699.     float n001 = dot(g001, float3(Pf0.xy, Pf1.z));
  700.     float n101 = dot(g101, float3(Pf1.x, Pf0.y, Pf1.z));
  701.     float n011 = dot(g011, float3(Pf0.x, Pf1.yz));
  702.     float n111 = dot(g111, Pf1);
  703.  
  704.     float3 fade_xyz = fade(Pf0);
  705.     float4 n_z = lerp(float4(n000, n100, n010, n110), float4(n001, n101, n011, n111), fade_xyz.z);
  706.     float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y);
  707.     float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x);
  708.     return 2.2 * n_xyz;
  709. }
  710.  
  711. //
  712. // GLSL textureless classic 4D noise "cnoise",
  713. // with an RSL-style periodic variant "pnoise".
  714. // Author:  Stefan Gustavson (stefan.gustavson@liu.se)
  715. // Version: 2011-08-22
  716. //
  717. // Many thanks to Ian McEwan of Ashima Arts for the
  718. // ideas for permutation and gradient selection.
  719. //
  720. // Copyright (c) 2011 Stefan Gustavson. All rights reserved.
  721. // Distributed under the MIT license. See LICENSE file.
  722. // https://github.com/stegu/webgl-noise
  723. //
  724.  
  725. // Classic Perlin noise
  726. float perlin4d(float4 P)
  727. {
  728.     float4 Pi0 = floor(P); // Integer part for indexing
  729.     float4 Pi1 = Pi0 + 1.0; // Integer part + 1
  730.     Pi0 = mod289(Pi0);
  731.     Pi1 = mod289(Pi1);
  732.     float4 Pf0 = frac(P); // Fractional part for interpolation
  733.     float4 Pf1 = Pf0 - 1.0; // Fractional part - 1.0
  734.     float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  735.     float4 iy = float4(Pi0.yy, Pi1.yy);
  736.     float4 iz0 = float4(Pi0.zzzz);
  737.     float4 iz1 = float4(Pi1.zzzz);
  738.     float4 iw0 = float4(Pi0.wwww);
  739.     float4 iw1 = float4(Pi1.wwww);
  740.  
  741.     float4 ixy = permute(permute(ix) + iy);
  742.     float4 ixy0 = permute(ixy + iz0);
  743.     float4 ixy1 = permute(ixy + iz1);
  744.     float4 ixy00 = permute(ixy0 + iw0);
  745.     float4 ixy01 = permute(ixy0 + iw1);
  746.     float4 ixy10 = permute(ixy1 + iw0);
  747.     float4 ixy11 = permute(ixy1 + iw1);
  748.  
  749.     float4 gx00 = ixy00 * (1.0 / 7.0);
  750.     float4 gy00 = floor(gx00) * (1.0 / 7.0);
  751.     float4 gz00 = floor(gy00) * (1.0 / 6.0);
  752.     gx00 = frac(gx00) - 0.5;
  753.     gy00 = frac(gy00) - 0.5;
  754.     gz00 = frac(gz00) - 0.5;
  755.     float4 gw00 = float4(0.75, 0.75, 0.75, 0.75) - abs(gx00) - abs(gy00) - abs(gz00);
  756.     float4 sw00 = step(gw00, float4(0, 0, 0, 0));
  757.     gx00 -= sw00 * (step(0.0, gx00) - 0.5);
  758.     gy00 -= sw00 * (step(0.0, gy00) - 0.5);
  759.  
  760.     float4 gx01 = ixy01 * (1.0 / 7.0);
  761.     float4 gy01 = floor(gx01) * (1.0 / 7.0);
  762.     float4 gz01 = floor(gy01) * (1.0 / 6.0);
  763.     gx01 = frac(gx01) - 0.5;
  764.     gy01 = frac(gy01) - 0.5;
  765.     gz01 = frac(gz01) - 0.5;
  766.     float4 gw01 = float4(0.75, 0.75, 0.75, 0.75) - abs(gx01) - abs(gy01) - abs(gz01);
  767.     float4 sw01 = step(gw01, float4(0, 0, 0, 0));
  768.     gx01 -= sw01 * (step(0.0, gx01) - 0.5);
  769.     gy01 -= sw01 * (step(0.0, gy01) - 0.5);
  770.  
  771.     float4 gx10 = ixy10 * (1.0 / 7.0);
  772.     float4 gy10 = floor(gx10) * (1.0 / 7.0);
  773.     float4 gz10 = floor(gy10) * (1.0 / 6.0);
  774.     gx10 = frac(gx10) - 0.5;
  775.     gy10 = frac(gy10) - 0.5;
  776.     gz10 = frac(gz10) - 0.5;
  777.     float4 gw10 = float4(0.75, 0.75, 0.75, 0.75) - abs(gx10) - abs(gy10) - abs(gz10);
  778.     float4 sw10 = step(gw10, float4(0, 0, 0, 0));
  779.     gx10 -= sw10 * (step(0.0, gx10) - 0.5);
  780.     gy10 -= sw10 * (step(0.0, gy10) - 0.5);
  781.  
  782.     float4 gx11 = ixy11 * (1.0 / 7.0);
  783.     float4 gy11 = floor(gx11) * (1.0 / 7.0);
  784.     float4 gz11 = floor(gy11) * (1.0 / 6.0);
  785.     gx11 = frac(gx11) - 0.5;
  786.     gy11 = frac(gy11) - 0.5;
  787.     gz11 = frac(gz11) - 0.5;
  788.     float4 gw11 = float4(0.75, 0.75, 0.75, 0.75) - abs(gx11) - abs(gy11) - abs(gz11);
  789.     float4 sw11 = step(gw11, float4(0, 0, 0, 0));
  790.     gx11 -= sw11 * (step(0.0, gx11) - 0.5);
  791.     gy11 -= sw11 * (step(0.0, gy11) - 0.5);
  792.  
  793.     float4 g0000 = float4(gx00.x, gy00.x, gz00.x, gw00.x);
  794.     float4 g1000 = float4(gx00.y, gy00.y, gz00.y, gw00.y);
  795.     float4 g0100 = float4(gx00.z, gy00.z, gz00.z, gw00.z);
  796.     float4 g1100 = float4(gx00.w, gy00.w, gz00.w, gw00.w);
  797.     float4 g0010 = float4(gx10.x, gy10.x, gz10.x, gw10.x);
  798.     float4 g1010 = float4(gx10.y, gy10.y, gz10.y, gw10.y);
  799.     float4 g0110 = float4(gx10.z, gy10.z, gz10.z, gw10.z);
  800.     float4 g1110 = float4(gx10.w, gy10.w, gz10.w, gw10.w);
  801.     float4 g0001 = float4(gx01.x, gy01.x, gz01.x, gw01.x);
  802.     float4 g1001 = float4(gx01.y, gy01.y, gz01.y, gw01.y);
  803.     float4 g0101 = float4(gx01.z, gy01.z, gz01.z, gw01.z);
  804.     float4 g1101 = float4(gx01.w, gy01.w, gz01.w, gw01.w);
  805.     float4 g0011 = float4(gx11.x, gy11.x, gz11.x, gw11.x);
  806.     float4 g1011 = float4(gx11.y, gy11.y, gz11.y, gw11.y);
  807.     float4 g0111 = float4(gx11.z, gy11.z, gz11.z, gw11.z);
  808.     float4 g1111 = float4(gx11.w, gy11.w, gz11.w, gw11.w);
  809.  
  810.     float4 norm00 = taylorInvSqrt(float4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100)));
  811.     g0000 *= norm00.x;
  812.     g0100 *= norm00.y;
  813.     g1000 *= norm00.z;
  814.     g1100 *= norm00.w;
  815.  
  816.     float4 norm01 = taylorInvSqrt(float4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101)));
  817.     g0001 *= norm01.x;
  818.     g0101 *= norm01.y;
  819.     g1001 *= norm01.z;
  820.     g1101 *= norm01.w;
  821.  
  822.     float4 norm10 = taylorInvSqrt(float4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110)));
  823.     g0010 *= norm10.x;
  824.     g0110 *= norm10.y;
  825.     g1010 *= norm10.z;
  826.     g1110 *= norm10.w;
  827.  
  828.     float4 norm11 = taylorInvSqrt(float4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111)));
  829.     g0011 *= norm11.x;
  830.     g0111 *= norm11.y;
  831.     g1011 *= norm11.z;
  832.     g1111 *= norm11.w;
  833.  
  834.     float n0000 = dot(g0000, Pf0);
  835.     float n1000 = dot(g1000, float4(Pf1.x, Pf0.yzw));
  836.     float n0100 = dot(g0100, float4(Pf0.x, Pf1.y, Pf0.zw));
  837.     float n1100 = dot(g1100, float4(Pf1.xy, Pf0.zw));
  838.     float n0010 = dot(g0010, float4(Pf0.xy, Pf1.z, Pf0.w));
  839.     float n1010 = dot(g1010, float4(Pf1.x, Pf0.y, Pf1.z, Pf0.w));
  840.     float n0110 = dot(g0110, float4(Pf0.x, Pf1.yz, Pf0.w));
  841.     float n1110 = dot(g1110, float4(Pf1.xyz, Pf0.w));
  842.     float n0001 = dot(g0001, float4(Pf0.xyz, Pf1.w));
  843.     float n1001 = dot(g1001, float4(Pf1.x, Pf0.yz, Pf1.w));
  844.     float n0101 = dot(g0101, float4(Pf0.x, Pf1.y, Pf0.z, Pf1.w));
  845.     float n1101 = dot(g1101, float4(Pf1.xy, Pf0.z, Pf1.w));
  846.     float n0011 = dot(g0011, float4(Pf0.xy, Pf1.zw));
  847.     float n1011 = dot(g1011, float4(Pf1.x, Pf0.y, Pf1.zw));
  848.     float n0111 = dot(g0111, float4(Pf0.x, Pf1.yzw));
  849.     float n1111 = dot(g1111, Pf1);
  850.  
  851.     float4 fade_xyzw = fade(Pf0);
  852.     float4 n_0w = lerp(float4(n0000, n1000, n0100, n1100), float4(n0001, n1001, n0101, n1101), fade_xyzw.w);
  853.     float4 n_1w = lerp(float4(n0010, n1010, n0110, n1110), float4(n0011, n1011, n0111, n1111), fade_xyzw.w);
  854.     float4 n_zw = lerp(n_0w, n_1w, fade_xyzw.z);
  855.     float2 n_yzw = lerp(n_zw.xy, n_zw.zw, fade_xyzw.y);
  856.     float n_xyzw = lerp(n_yzw.x, n_yzw.y, fade_xyzw.x);
  857.     return 2.2 * n_xyzw;
  858. }
  859.  
  860. // Classic Perlin noise, periodic version
  861. float perlin4dPeriodic(float4 P, float4 rep)
  862. {
  863.     float4 Pi0 = fmod(floor(P), rep); // Integer part modulo rep
  864.     float4 Pi1 = fmod(Pi0 + 1.0, rep); // Integer part + 1 mod rep
  865.     Pi0 = mod289(Pi0);
  866.     Pi1 = mod289(Pi1);
  867.     float4 Pf0 = frac(P); // Fractional part for interpolation
  868.     float4 Pf1 = Pf0 - 1.0; // Fractional part - 1.0
  869.     float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  870.     float4 iy = float4(Pi0.yy, Pi1.yy);
  871.     float4 iz0 = float4(Pi0.zzzz);
  872.     float4 iz1 = float4(Pi1.zzzz);
  873.     float4 iw0 = float4(Pi0.wwww);
  874.     float4 iw1 = float4(Pi1.wwww);
  875.  
  876.     float4 ixy = permute(permute(ix) + iy);
  877.     float4 ixy0 = permute(ixy + iz0);
  878.     float4 ixy1 = permute(ixy + iz1);
  879.     float4 ixy00 = permute(ixy0 + iw0);
  880.     float4 ixy01 = permute(ixy0 + iw1);
  881.     float4 ixy10 = permute(ixy1 + iw0);
  882.     float4 ixy11 = permute(ixy1 + iw1);
  883.  
  884.     float4 gx00 = ixy00 * (1.0 / 7.0);
  885.     float4 gy00 = floor(gx00) * (1.0 / 7.0);
  886.     float4 gz00 = floor(gy00) * (1.0 / 6.0);
  887.     gx00 = frac(gx00) - 0.5;
  888.     gy00 = frac(gy00) - 0.5;
  889.     gz00 = frac(gz00) - 0.5;
  890.     float4 gw00 = float4(0.75, 0.75, 0.75, 0.75) - abs(gx00) - abs(gy00) - abs(gz00);
  891.     float4 sw00 = step(gw00, float4(0, 0, 0, 0));
  892.     gx00 -= sw00 * (step(0.0, gx00) - 0.5);
  893.     gy00 -= sw00 * (step(0.0, gy00) - 0.5);
  894.  
  895.     float4 gx01 = ixy01 * (1.0 / 7.0);
  896.     float4 gy01 = floor(gx01) * (1.0 / 7.0);
  897.     float4 gz01 = floor(gy01) * (1.0 / 6.0);
  898.     gx01 = frac(gx01) - 0.5;
  899.     gy01 = frac(gy01) - 0.5;
  900.     gz01 = frac(gz01) - 0.5;
  901.     float4 gw01 = float4(0.75, 0.75, 0.75, 0.75) - abs(gx01) - abs(gy01) - abs(gz01);
  902.     float4 sw01 = step(gw01, float4(0, 0, 0, 0));
  903.     gx01 -= sw01 * (step(0.0, gx01) - 0.5);
  904.     gy01 -= sw01 * (step(0.0, gy01) - 0.5);
  905.  
  906.     float4 gx10 = ixy10 * (1.0 / 7.0);
  907.     float4 gy10 = floor(gx10) * (1.0 / 7.0);
  908.     float4 gz10 = floor(gy10) * (1.0 / 6.0);
  909.     gx10 = frac(gx10) - 0.5;
  910.     gy10 = frac(gy10) - 0.5;
  911.     gz10 = frac(gz10) - 0.5;
  912.     float4 gw10 = float4(0.75, 0.75, 0.75, 0.75) - abs(gx10) - abs(gy10) - abs(gz10);
  913.     float4 sw10 = step(gw10, float4(0, 0, 0, 0));
  914.     gx10 -= sw10 * (step(0.0, gx10) - 0.5);
  915.     gy10 -= sw10 * (step(0.0, gy10) - 0.5);
  916.  
  917.     float4 gx11 = ixy11 * (1.0 / 7.0);
  918.     float4 gy11 = floor(gx11) * (1.0 / 7.0);
  919.     float4 gz11 = floor(gy11) * (1.0 / 6.0);
  920.     gx11 = frac(gx11) - 0.5;
  921.     gy11 = frac(gy11) - 0.5;
  922.     gz11 = frac(gz11) - 0.5;
  923.     float4 gw11 = float4(0.75, 0.75, 0.75, 0.75) - abs(gx11) - abs(gy11) - abs(gz11);
  924.     float4 sw11 = step(gw11, float4(0, 0, 0, 0));
  925.     gx11 -= sw11 * (step(0.0, gx11) - 0.5);
  926.     gy11 -= sw11 * (step(0.0, gy11) - 0.5);
  927.  
  928.     float4 g0000 = float4(gx00.x, gy00.x, gz00.x, gw00.x);
  929.     float4 g1000 = float4(gx00.y, gy00.y, gz00.y, gw00.y);
  930.     float4 g0100 = float4(gx00.z, gy00.z, gz00.z, gw00.z);
  931.     float4 g1100 = float4(gx00.w, gy00.w, gz00.w, gw00.w);
  932.     float4 g0010 = float4(gx10.x, gy10.x, gz10.x, gw10.x);
  933.     float4 g1010 = float4(gx10.y, gy10.y, gz10.y, gw10.y);
  934.     float4 g0110 = float4(gx10.z, gy10.z, gz10.z, gw10.z);
  935.     float4 g1110 = float4(gx10.w, gy10.w, gz10.w, gw10.w);
  936.     float4 g0001 = float4(gx01.x, gy01.x, gz01.x, gw01.x);
  937.     float4 g1001 = float4(gx01.y, gy01.y, gz01.y, gw01.y);
  938.     float4 g0101 = float4(gx01.z, gy01.z, gz01.z, gw01.z);
  939.     float4 g1101 = float4(gx01.w, gy01.w, gz01.w, gw01.w);
  940.     float4 g0011 = float4(gx11.x, gy11.x, gz11.x, gw11.x);
  941.     float4 g1011 = float4(gx11.y, gy11.y, gz11.y, gw11.y);
  942.     float4 g0111 = float4(gx11.z, gy11.z, gz11.z, gw11.z);
  943.     float4 g1111 = float4(gx11.w, gy11.w, gz11.w, gw11.w);
  944.  
  945.     float4 norm00 = taylorInvSqrt(float4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100)));
  946.     g0000 *= norm00.x;
  947.     g0100 *= norm00.y;
  948.     g1000 *= norm00.z;
  949.     g1100 *= norm00.w;
  950.  
  951.     float4 norm01 = taylorInvSqrt(float4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101)));
  952.     g0001 *= norm01.x;
  953.     g0101 *= norm01.y;
  954.     g1001 *= norm01.z;
  955.     g1101 *= norm01.w;
  956.  
  957.     float4 norm10 = taylorInvSqrt(float4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110)));
  958.     g0010 *= norm10.x;
  959.     g0110 *= norm10.y;
  960.     g1010 *= norm10.z;
  961.     g1110 *= norm10.w;
  962.  
  963.     float4 norm11 = taylorInvSqrt(float4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111)));
  964.     g0011 *= norm11.x;
  965.     g0111 *= norm11.y;
  966.     g1011 *= norm11.z;
  967.     g1111 *= norm11.w;
  968.  
  969.     float n0000 = dot(g0000, Pf0);
  970.     float n1000 = dot(g1000, float4(Pf1.x, Pf0.yzw));
  971.     float n0100 = dot(g0100, float4(Pf0.x, Pf1.y, Pf0.zw));
  972.     float n1100 = dot(g1100, float4(Pf1.xy, Pf0.zw));
  973.     float n0010 = dot(g0010, float4(Pf0.xy, Pf1.z, Pf0.w));
  974.     float n1010 = dot(g1010, float4(Pf1.x, Pf0.y, Pf1.z, Pf0.w));
  975.     float n0110 = dot(g0110, float4(Pf0.x, Pf1.yz, Pf0.w));
  976.     float n1110 = dot(g1110, float4(Pf1.xyz, Pf0.w));
  977.     float n0001 = dot(g0001, float4(Pf0.xyz, Pf1.w));
  978.     float n1001 = dot(g1001, float4(Pf1.x, Pf0.yz, Pf1.w));
  979.     float n0101 = dot(g0101, float4(Pf0.x, Pf1.y, Pf0.z, Pf1.w));
  980.     float n1101 = dot(g1101, float4(Pf1.xy, Pf0.z, Pf1.w));
  981.     float n0011 = dot(g0011, float4(Pf0.xy, Pf1.zw));
  982.     float n1011 = dot(g1011, float4(Pf1.x, Pf0.y, Pf1.zw));
  983.     float n0111 = dot(g0111, float4(Pf0.x, Pf1.yzw));
  984.     float n1111 = dot(g1111, Pf1);
  985.  
  986.     float4 fade_xyzw = fade(Pf0);
  987.     float4 n_0w = lerp(float4(n0000, n1000, n0100, n1100), float4(n0001, n1001, n0101, n1101), fade_xyzw.w);
  988.     float4 n_1w = lerp(float4(n0010, n1010, n0110, n1110), float4(n0011, n1011, n0111, n1111), fade_xyzw.w);
  989.     float4 n_zw = lerp(n_0w, n_1w, fade_xyzw.z);
  990.     float2 n_yzw = lerp(n_zw.xy, n_zw.zw, fade_xyzw.y);
  991.     float n_xyzw = lerp(n_yzw.x, n_yzw.y, fade_xyzw.x);
  992.     return 2.2 * n_xyzw;
  993. }
  994.  
  995. //
  996. // Description : Array and textureless GLSL 2D simplex noise function.
  997. //      Author : Ian McEwan, Ashima Arts.
  998. //  Maintainer : stegu
  999. //     Lastmod : 20110822 (ijm)
  1000. //     License : Copyright (C) 2011 Ashima Arts. All rights reserved.
  1001. //               Distributed under the MIT License. See LICENSE file.
  1002. //               https://github.com/ashima/webgl-noise
  1003. //               https://github.com/stegu/webgl-noise
  1004. //
  1005.  
  1006. float simplex(float2 v)
  1007. {
  1008.     const float4 C = float4(0.211324865405187,  // (3.0-sqrt(3.0))/6.0
  1009.         0.366025403784439,  // 0.5*(sqrt(3.0)-1.0)
  1010.         -0.577350269189626,  // -1.0 + 2.0 * C.x
  1011.         0.024390243902439); // 1.0 / 41.0
  1012.                             // First corner
  1013.     float2 i = floor(v + dot(v, C.yy));
  1014.     float2 x0 = v - i + dot(i, C.xx);
  1015.  
  1016.     // Other corners
  1017.     float2 i1;
  1018.     //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
  1019.     //i1.y = 1.0 - i1.x;
  1020.     i1 = (x0.x > x0.y) ? float2(1.0, 0.0) : float2(0.0, 1.0);
  1021.     // x0 = x0 - 0.0 + 0.0 * C.xx ;
  1022.     // x1 = x0 - i1 + 1.0 * C.xx ;
  1023.     // x2 = x0 - 1.0 + 2.0 * C.xx ;
  1024.     float4 x12 = x0.xyxy + C.xxzz;
  1025.     x12.xy -= i1;
  1026.  
  1027.     // Permutations
  1028.     i = mod289(i); // Avoid truncation effects in permutation
  1029.     float3 p = permute(permute(i.y + float3(0.0, i1.y, 1.0))
  1030.         + i.x + float3(0.0, i1.x, 1.0));
  1031.  
  1032.     float3 m = max(0.5 - float3(dot(x0, x0), dot(x12.xy, x12.xy), dot(x12.zw, x12.zw)), 0.0);
  1033.     m = m*m;
  1034.     m = m*m;
  1035.  
  1036.     // Gradients: 41 points uniformly over a line, mapped onto a diamond.
  1037.     // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
  1038.  
  1039.     float3 x = 2.0 * frac(p * C.www) - 1.0;
  1040.     float3 h = abs(x) - 0.5;
  1041.     float3 ox = floor(x + 0.5);
  1042.     float3 a0 = x - ox;
  1043.  
  1044.     // Normalise gradients implicitly by scaling m
  1045.     // Approximation of: m *= inversesqrt( a0*a0 + h*h );
  1046.     m *= 1.79284291400159 - 0.85373472095314 * (a0*a0 + h*h);
  1047.  
  1048.     // Compute final noise value at P
  1049.     float3 g;
  1050.     g.x = a0.x  * x0.x + h.x  * x0.y;
  1051.     g.yz = a0.yz * x12.xz + h.yz * x12.yw;
  1052.     return 130.0 * dot(m, g);
  1053. }
  1054.  
  1055.  
  1056.  
  1057. float simplex3dGradient(float3 v, out float3 gradient)
  1058. {
  1059.     const float2  C = float2(1.0 / 6.0, 1.0 / 3.0);
  1060.     const float4  D = float4(0.0, 0.5, 1.0, 2.0);
  1061.  
  1062.     // First corner
  1063.     float3 i = floor(v + dot(v, C.yyy));
  1064.     float3 x0 = v - i + dot(i, C.xxx);
  1065.  
  1066.     // Other corners
  1067.     float3 g = step(x0.yzx, x0.xyz);
  1068.     float3 l = 1.0 - g;
  1069.     float3 i1 = min(g.xyz, l.zxy);
  1070.     float3 i2 = max(g.xyz, l.zxy);
  1071.  
  1072.     //   x0 = x0 - 0.0 + 0.0 * C.xxx;
  1073.     //   x1 = x0 - i1  + 1.0 * C.xxx;
  1074.     //   x2 = x0 - i2  + 2.0 * C.xxx;
  1075.     //   x3 = x0 - 1.0 + 3.0 * C.xxx;
  1076.     float3 x1 = x0 - i1 + C.xxx;
  1077.     float3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
  1078.     float3 x3 = x0 - D.yyy;      // -1.0+3.0*C.x = -0.5 = -D.y
  1079.  
  1080.                                // Permutations
  1081.     i = mod289(i);
  1082.     float4 p = permute(permute(permute(
  1083.         i.z + float4(0.0, i1.z, i2.z, 1.0))
  1084.         + i.y + float4(0.0, i1.y, i2.y, 1.0))
  1085.         + i.x + float4(0.0, i1.x, i2.x, 1.0));
  1086.  
  1087.     // Gradients: 7x7 points over a square, mapped onto an octahedron.
  1088.     // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
  1089.     float n_ = 0.142857142857; // 1.0/7.0
  1090.     float3  ns = n_ * D.wyz - D.xzx;
  1091.  
  1092.     float4 j = p - 49.0 * floor(p * ns.z * ns.z);  //  mod(p,7*7)
  1093.  
  1094.     float4 x_ = floor(j * ns.z);
  1095.     float4 y_ = floor(j - 7.0 * x_);    // mod(j,N)
  1096.  
  1097.     float4 x = x_ *ns.x + ns.yyyy;
  1098.     float4 y = y_ *ns.x + ns.yyyy;
  1099.     float4 h = 1.0 - abs(x) - abs(y);
  1100.  
  1101.     float4 b0 = float4(x.xy, y.xy);
  1102.     float4 b1 = float4(x.zw, y.zw);
  1103.  
  1104.     //float4 s0 = float4(lessThan(b0,0.0))*2.0 - 1.0;
  1105.     //float4 s1 = float4(lessThan(b1,0.0))*2.0 - 1.0;
  1106.     float4 s0 = floor(b0)*2.0 + 1.0;
  1107.     float4 s1 = floor(b1)*2.0 + 1.0;
  1108.     float4 sh = -step(h, float4(0,0,0,0));
  1109.  
  1110.     float4 a0 = b0.xzyw + s0.xzyw*sh.xxyy;
  1111.     float4 a1 = b1.xzyw + s1.xzyw*sh.zzww;
  1112.  
  1113.     float3 p0 = float3(a0.xy, h.x);
  1114.     float3 p1 = float3(a0.zw, h.y);
  1115.     float3 p2 = float3(a1.xy, h.z);
  1116.     float3 p3 = float3(a1.zw, h.w);
  1117.  
  1118.     //Normalise gradients
  1119.     float4 norm = taylorInvSqrt(float4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
  1120.     p0 *= norm.x;
  1121.     p1 *= norm.y;
  1122.     p2 *= norm.z;
  1123.     p3 *= norm.w;
  1124.  
  1125.     // Mix final noise value
  1126.     float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
  1127.     float4 m2 = m * m;
  1128.     float4 m4 = m2 * m2;
  1129.     float4 pdotx = float4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3));
  1130.  
  1131.     // Determine noise gradient
  1132.     float4 temp = m2 * m * pdotx;
  1133.     gradient = -8.0 * (temp.x * x0 + temp.y * x1 + temp.z * x2 + temp.w * x3);
  1134.     gradient += m4.x * p0 + m4.y * p1 + m4.z * p2 + m4.w * p3;
  1135.     gradient *= 42.0;
  1136.  
  1137.     return 42.0 * dot(m4, pdotx);
  1138. }
  1139.  
  1140.  
  1141. // (sqrt(5) - 1)/4 = F4, used once below
  1142. #define F4 0.309016994374947451
  1143.  
  1144. float simplex4dGradient(float4 v)
  1145. {
  1146.     const float4  C = float4(0.138196601125011,  // (5 - sqrt(5))/20  G4
  1147.         0.276393202250021,  // 2 * G4
  1148.         0.414589803375032,  // 3 * G4
  1149.         -0.447213595499958); // -1 + 4 * G4
  1150.  
  1151.                              // First corner
  1152.     float4 i = floor(v + dot(v, float4(F4, F4, F4, F4)));
  1153.     float4 x0 = v - i + dot(i, C.xxxx);
  1154.  
  1155.     // Other corners
  1156.  
  1157.     // Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
  1158.     float4 i0;
  1159.     float3 isX = step(x0.yzw, x0.xxx);
  1160.     float3 isYZ = step(x0.zww, x0.yyz);
  1161.     //  i0.x = dot( isX, float3( 1.0 ) );
  1162.     i0.x = isX.x + isX.y + isX.z;
  1163.     i0.yzw = 1.0 - isX;
  1164.     //  i0.y += dot( isYZ.xy, float2( 1.0 ) );
  1165.     i0.y += isYZ.x + isYZ.y;
  1166.     i0.zw += 1.0 - isYZ.xy;
  1167.     i0.z += isYZ.z;
  1168.     i0.w += 1.0 - isYZ.z;
  1169.  
  1170.     // i0 now contains the unique values 0,1,2,3 in each channel
  1171.     float4 i3 = clamp(i0, 0.0, 1.0);
  1172.     float4 i2 = clamp(i0 - 1.0, 0.0, 1.0);
  1173.     float4 i1 = clamp(i0 - 2.0, 0.0, 1.0);
  1174.  
  1175.     //  x0 = x0 - 0.0 + 0.0 * C.xxxx
  1176.     //  x1 = x0 - i1  + 1.0 * C.xxxx
  1177.     //  x2 = x0 - i2  + 2.0 * C.xxxx
  1178.     //  x3 = x0 - i3  + 3.0 * C.xxxx
  1179.     //  x4 = x0 - 1.0 + 4.0 * C.xxxx
  1180.     float4 x1 = x0 - i1 + C.xxxx;
  1181.     float4 x2 = x0 - i2 + C.yyyy;
  1182.     float4 x3 = x0 - i3 + C.zzzz;
  1183.     float4 x4 = x0 + C.wwww;
  1184.  
  1185.     // Permutations
  1186.     i = mod289(i);
  1187.     float j0 = permute(permute(permute(permute(i.w) + i.z) + i.y) + i.x);
  1188.     float4 j1 = permute(permute(permute(permute(
  1189.         i.w + float4(i1.w, i2.w, i3.w, 1.0))
  1190.         + i.z + float4(i1.z, i2.z, i3.z, 1.0))
  1191.         + i.y + float4(i1.y, i2.y, i3.y, 1.0))
  1192.         + i.x + float4(i1.x, i2.x, i3.x, 1.0));
  1193.  
  1194.     // Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
  1195.     // 7*7*6 = 294, which is close to the ring size 17*17 = 289.
  1196.     float4 ip = float4(1.0 / 294.0, 1.0 / 49.0, 1.0 / 7.0, 0.0);
  1197.  
  1198.     float4 p0 = grad4(j0, ip);
  1199.     float4 p1 = grad4(j1.x, ip);
  1200.     float4 p2 = grad4(j1.y, ip);
  1201.     float4 p3 = grad4(j1.z, ip);
  1202.     float4 p4 = grad4(j1.w, ip);
  1203.  
  1204.     // Normalise gradients
  1205.     float4 norm = taylorInvSqrt(float4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
  1206.     p0 *= norm.x;
  1207.     p1 *= norm.y;
  1208.     p2 *= norm.z;
  1209.     p3 *= norm.w;
  1210.     p4 *= taylorInvSqrt(dot(p4, p4));
  1211.  
  1212.     // Mix contributions from the five corners
  1213.     float3 m0 = max(0.6 - float3(dot(x0, x0), dot(x1, x1), dot(x2, x2)), 0.0);
  1214.     float2 m1 = max(0.6 - float2(dot(x3, x3), dot(x4, x4)), 0.0);
  1215.     m0 = m0 * m0;
  1216.     m1 = m1 * m1;
  1217.     return 49.0 * (dot(m0*m0, float3(dot(p0, x0), dot(p1, x1), dot(p2, x2)))
  1218.         + dot(m1*m1, float2(dot(p3, x3), dot(p4, x4))));
  1219.  
  1220. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top