Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // Description : Array and textureless GLSL 2D/3D/4D simplex
  3. // noise functions.
  4. // Author : Ian McEwan, Ashima Arts.
  5. // Maintainer : stegu
  6. // Lastmath.mod : 20110822 (ijm)
  7. // License : Copyright (C) 2011 Ashima Arts. All rights reserved.
  8. // Distributed under the MIT License. See LICENSE file.
  9. // https://github.com/ashima/webgl-noise
  10. // https://github.com/stegu/webgl-noise
  11. //
  12. using static Unity.Mathematics.math;
  13. namespace Unity.Mathematics
  14. {
  15. public static partial class noise
  16. {
  17. /// <summary>
  18. /// Simplex noise.
  19. /// </summary>
  20. /// <param name="v">Input coordinate.</param>
  21. /// <returns>Noise value.</returns>
  22. public static float snoise(float4 v)
  23. {
  24. // (math.sqrt(5) - 1)/4 = F4, used once below
  25. const float F4 = 0.309016994374947451f;
  26. float4 C = float4( 0.138196601125011f, // (5 - math.sqrt(5))/20 G4
  27. 0.276393202250021f, // 2 * G4
  28. 0.414589803375032f, // 3 * G4
  29. -0.447213595499958f); // -1 + 4 * G4
  30. // First corner
  31. float4 i = floor(v + dot(v, float4(F4)) );
  32. float4 x0 = v - i + dot(i, C.xxxx);
  33. // Other corners
  34. // Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
  35. float4 i0 = float4(0.0f);
  36. float3 isX = step( x0.yzw, x0.xxx );
  37. float3 isYZ = step( x0.zww, x0.yyz );
  38. // i0.x = math.dot( isX, float3( 1.0 ) );
  39. i0.x = isX.x + isX.y + isX.z;
  40. i0.yzw = 1.0f - isX;
  41. // i0.y += math.dot( isYZ.xy, float2( 1.0 ) );
  42. i0.y += isYZ.x + isYZ.y;
  43. i0.zw += 1.0f - isYZ.xy;
  44. i0.z += isYZ.z;
  45. i0.w += 1.0f - isYZ.z;
  46. // i0 now contains the unique values 0,1,2,3 in each channel
  47. float4 i3 = clamp( i0, 0.0f, 1.0f );
  48. float4 i2 = clamp( i0-1.0f, 0.0f, 1.0f );
  49. float4 i1 = clamp( i0-2.0f, 0.0f, 1.0f );
  50. // x0 = x0 - 0.0 + 0.0 * C.xxxx
  51. // x1 = x0 - i1 + 1.0 * C.xxxx
  52. // x2 = x0 - i2 + 2.0 * C.xxxx
  53. // x3 = x0 - i3 + 3.0 * C.xxxx
  54. // x4 = x0 - 1.0 + 4.0 * C.xxxx
  55. float4 x1 = x0 - i1 + C.xxxx;
  56. float4 x2 = x0 - i2 + C.yyyy;
  57. float4 x3 = x0 - i3 + C.zzzz;
  58. float4 x4 = x0 + C.wwww;
  59. // Permutations
  60. i = mod289(i);
  61. float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x);
  62. float4 j1 = permute( permute( permute( permute (
  63. i.w + float4(i1.w, i2.w, i3.w, 1.0f ))
  64. + i.z + float4(i1.z, i2.z, i3.z, 1.0f ))
  65. + i.y + float4(i1.y, i2.y, i3.y, 1.0f ))
  66. + i.x + float4(i1.x, i2.x, i3.x, 1.0f ));
  67. // Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
  68. // 7*7*6 = 294, which is close to the ring size 17*17 = 289.
  69. float4 ip = float4(1.0f/294.0f, 1.0f/49.0f, 1.0f/7.0f, 0.0f) ;
  70. float4 p0 = grad4(j0, ip);
  71. float4 p1 = grad4(j1.x, ip);
  72. float4 p2 = grad4(j1.y, ip);
  73. float4 p3 = grad4(j1.z, ip);
  74. float4 p4 = grad4(j1.w, ip);
  75. // Normalise gradients
  76. float4 norm = taylorInvSqrt(float4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
  77. p0 *= norm.x;
  78. p1 *= norm.y;
  79. p2 *= norm.z;
  80. p3 *= norm.w;
  81. p4 *= taylorInvSqrt(dot(p4,p4));
  82. // Mix contributions from the five corners
  83. float3 m0 = max(0.6f - float3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0f);
  84. float2 m1 = max(0.6f - float2(dot(x3,x3), dot(x4,x4) ), 0.0f);
  85. m0 = m0 * m0;
  86. m1 = m1 * m1;
  87. return 49.0f * ( dot(m0*m0, float3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 )))
  88. + dot(m1*m1, float2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ;
  89. }
  90. }
  91. }