Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

classicnoise2D.cs 4.0KB

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