Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Cellular noise ("Worley noise") in 3D in GLSL.
  2. // Copyright (c) Stefan Gustavson 2011-04-19. All rights reserved.
  3. // This code is released under the conditions of the MIT license.
  4. // See LICENSE file for details.
  5. // https://github.com/stegu/webgl-noise
  6. using static Unity.Mathematics.math;
  7. namespace Unity.Mathematics
  8. {
  9. public static partial class noise
  10. {
  11. /// <summary>
  12. /// 3D Cellular noise ("Worley noise") with a 2x2x2 search window.
  13. /// </summary>
  14. /// <remarks>
  15. /// Faster than using 3x3x3, at the expense of some pattern artifacts. F2 is often wrong and has sharp discontinuities. If you need a smooth F2, use the slower 3x3x3 version.
  16. /// </remarks>
  17. /// <param name="P">A point in 3D space.</param>
  18. /// <returns>Feature points. F1 is in the x component, F2 in the y component.</returns>
  19. public static float2 cellular2x2x2(float3 P)
  20. {
  21. const float K = 0.142857142857f; // 1/7
  22. const float Ko = 0.428571428571f; // 1/2-K/2
  23. const float K2 = 0.020408163265306f; // 1/(7*7)
  24. const float Kz = 0.166666666667f; // 1/6
  25. const float Kzo = 0.416666666667f; // 1/2-1/6*2
  26. const float jitter = 0.8f; // smaller jitter gives less errors in F2
  27. float3 Pi = mod289(floor(P));
  28. float3 Pf = frac(P);
  29. float4 Pfx = Pf.x + float4(0.0f, -1.0f, 0.0f, -1.0f);
  30. float4 Pfy = Pf.y + float4(0.0f, 0.0f, -1.0f, -1.0f);
  31. float4 p = permute(Pi.x + float4(0.0f, 1.0f, 0.0f, 1.0f));
  32. p = permute(p + Pi.y + float4(0.0f, 0.0f, 1.0f, 1.0f));
  33. float4 p1 = permute(p + Pi.z); // z+0
  34. float4 p2 = permute(p + Pi.z + float4(1.0f,1.0f,1.0f,1.0f)); // z+1
  35. float4 ox1 = frac(p1 * K) - Ko;
  36. float4 oy1 = mod7(floor(p1 * K)) * K - Ko;
  37. float4 oz1 = floor(p1 * K2) * Kz - Kzo; // p1 < 289 guaranteed
  38. float4 ox2 = frac(p2 * K) - Ko;
  39. float4 oy2 = mod7(floor(p2 * K)) * K - Ko;
  40. float4 oz2 = floor(p2 * K2) * Kz - Kzo;
  41. float4 dx1 = Pfx + jitter * ox1;
  42. float4 dy1 = Pfy + jitter * oy1;
  43. float4 dz1 = Pf.z + jitter * oz1;
  44. float4 dx2 = Pfx + jitter * ox2;
  45. float4 dy2 = Pfy + jitter * oy2;
  46. float4 dz2 = Pf.z - 1.0f + jitter * oz2;
  47. float4 d1 = dx1 * dx1 + dy1 * dy1 + dz1 * dz1; // z+0
  48. float4 d2 = dx2 * dx2 + dy2 * dy2 + dz2 * dz2; // z+1
  49. // Sort out the two smallest distances (F1, F2)
  50. // Do it right and sort out both F1 and F2
  51. float4 d = min(d1,d2); // F1 is now in d
  52. d2 = max(d1,d2); // Make sure we keep all candidates for F2
  53. d.xy = (d.x < d.y) ? d.xy : d.yx; // Swap smallest to d.x
  54. d.xz = (d.x < d.z) ? d.xz : d.zx;
  55. d.xw = (d.x < d.w) ? d.xw : d.wx; // F1 is now in d.x
  56. d.yzw = min(d.yzw, d2.yzw); // F2 now not in d2.yzw
  57. d.y = min(d.y, d.z); // nor in d.z
  58. d.y = min(d.y, d.w); // nor in d.w
  59. d.y = min(d.y, d2.x); // F2 is now in d.y
  60. return sqrt(d.xy); // F1 and F2
  61. }
  62. }
  63. }