classicnoise2D.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // GLSL textureless classic 2D 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(float2 P)
  21. {
  22. float4 Pi = floor(P.xyxy) + float4(0.0f, 0.0f, 1.0f, 1.0f);
  23. float4 Pf = frac(P.xyxy) - float4(0.0f, 0.0f, 1.0f, 1.0f);
  24. Pi = mod289(Pi); // To avoid truncation effects in permutation
  25. float4 ix = Pi.xzxz;
  26. float4 iy = Pi.yyww;
  27. float4 fx = Pf.xzxz;
  28. float4 fy = Pf.yyww;
  29. float4 i = permute(permute(ix) + iy);
  30. float4 gx = frac(i * (1.0f / 41.0f)) * 2.0f - 1.0f;
  31. float4 gy = abs(gx) - 0.5f;
  32. float4 tx = floor(gx + 0.5f);
  33. gx = gx - tx;
  34. float2 g00 = float2(gx.x, gy.x);
  35. float2 g10 = float2(gx.y, gy.y);
  36. float2 g01 = float2(gx.z, gy.z);
  37. float2 g11 = float2(gx.w, gy.w);
  38. float4 norm = taylorInvSqrt(float4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
  39. g00 *= norm.x;
  40. g01 *= norm.y;
  41. g10 *= norm.z;
  42. g11 *= norm.w;
  43. float n00 = dot(g00, float2(fx.x, fy.x));
  44. float n10 = dot(g10, float2(fx.y, fy.y));
  45. float n01 = dot(g01, float2(fx.z, fy.z));
  46. float n11 = dot(g11, float2(fx.w, fy.w));
  47. float2 fade_xy = fade(Pf.xy);
  48. float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x);
  49. float n_xy = lerp(n_x.x, n_x.y, fade_xy.y);
  50. return 2.3f * n_xy;
  51. }
  52. // Classic Perlin noise, periodic variant
  53. public static float pnoise(float2 P, float2 rep)
  54. {
  55. float4 Pi = floor(P.xyxy) + float4(0.0f, 0.0f, 1.0f, 1.0f);
  56. float4 Pf = frac(P.xyxy) - float4(0.0f, 0.0f, 1.0f, 1.0f);
  57. Pi = fmod(Pi, rep.xyxy); // To create noise with explicit period
  58. Pi = mod289(Pi); // To avoid truncation effects in permutation
  59. float4 ix = Pi.xzxz;
  60. float4 iy = Pi.yyww;
  61. float4 fx = Pf.xzxz;
  62. float4 fy = Pf.yyww;
  63. float4 i = permute(permute(ix) + iy);
  64. float4 gx = frac(i * (1.0f / 41.0f)) * 2.0f - 1.0f;
  65. float4 gy = abs(gx) - 0.5f;
  66. float4 tx = floor(gx + 0.5f);
  67. gx = gx - tx;
  68. float2 g00 = float2(gx.x, gy.x);
  69. float2 g10 = float2(gx.y, gy.y);
  70. float2 g01 = float2(gx.z, gy.z);
  71. float2 g11 = float2(gx.w, gy.w);
  72. float4 norm = taylorInvSqrt(float4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
  73. g00 *= norm.x;
  74. g01 *= norm.y;
  75. g10 *= norm.z;
  76. g11 *= norm.w;
  77. float n00 = dot(g00, float2(fx.x, fy.x));
  78. float n10 = dot(g10, float2(fx.y, fy.y));
  79. float n01 = dot(g01, float2(fx.z, fy.z));
  80. float n11 = dot(g11, float2(fx.w, fy.w));
  81. float2 fade_xy = fade(Pf.xy);
  82. float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x);
  83. float n_xy = lerp(n_x.x, n_x.y, fade_xy.y);
  84. return 2.3f * n_xy;
  85. }
  86. }
  87. }