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.

noise2D.cs 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // Description : Array and textureless GLSL 2D simplex noise function.
  3. // Author : Ian McEwan, Ashima Arts.
  4. // Maintainer : stegu
  5. // Lastmath.mod : 20110822 (ijm)
  6. // License : Copyright (C) 2011 Ashima Arts. All rights reserved.
  7. // Distributed under the MIT License. See LICENSE file.
  8. // https://github.com/ashima/webgl-noise
  9. // https://github.com/stegu/webgl-noise
  10. //
  11. using static Unity.Mathematics.math;
  12. namespace Unity.Mathematics
  13. {
  14. public static partial class noise
  15. {
  16. /// <summary>
  17. /// Simplex noise.
  18. /// </summary>
  19. /// <param name="v">Input coordinate.</param>
  20. /// <returns>Noise value.</returns>
  21. public static float snoise(float2 v)
  22. {
  23. float4 C = float4(0.211324865405187f, // (3.0-math.sqrt(3.0))/6.0
  24. 0.366025403784439f, // 0.5*(math.sqrt(3.0)-1.0)
  25. -0.577350269189626f, // -1.0 + 2.0 * C.x
  26. 0.024390243902439f); // 1.0 / 41.0
  27. // First corner
  28. float2 i = floor(v + dot(v, C.yy));
  29. float2 x0 = v - i + dot(i, C.xx);
  30. // Other corners
  31. float2 i1;
  32. //i1.x = math.step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
  33. //i1.y = 1.0 - i1.x;
  34. i1 = (x0.x > x0.y) ? float2(1.0f, 0.0f) : float2(0.0f, 1.0f);
  35. // x0 = x0 - 0.0 + 0.0 * C.xx ;
  36. // x1 = x0 - i1 + 1.0 * C.xx ;
  37. // x2 = x0 - 1.0 + 2.0 * C.xx ;
  38. float4 x12 = x0.xyxy + C.xxzz;
  39. x12.xy -= i1;
  40. // Permutations
  41. i = mod289(i); // Avoid truncation effects in permutation
  42. float3 p = permute(permute(i.y + float3(0.0f, i1.y, 1.0f)) + i.x + float3(0.0f, i1.x, 1.0f));
  43. float3 m = max(0.5f - float3(dot(x0, x0), dot(x12.xy, x12.xy), dot(x12.zw, x12.zw)), 0.0f);
  44. m = m * m;
  45. m = m * m;
  46. // Gradients: 41 points uniformly over a line, mapped onto a diamond.
  47. // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
  48. float3 x = 2.0f * frac(p * C.www) - 1.0f;
  49. float3 h = abs(x) - 0.5f;
  50. float3 ox = floor(x + 0.5f);
  51. float3 a0 = x - ox;
  52. // Normalise gradients implicitly by scaling m
  53. // Approximation of: m *= inversemath.sqrt( a0*a0 + h*h );
  54. m *= 1.79284291400159f - 0.85373472095314f * (a0 * a0 + h * h);
  55. // Compute final noise value at P
  56. float gx = a0.x * x0.x + h.x * x0.y;
  57. float2 gyz = a0.yz * x12.xz + h.yz * x12.yw;
  58. float3 g = float3(gx,gyz);
  59. return 130.0f * dot(m, g);
  60. }
  61. }
  62. }