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.

cellular.glsl 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @license
  3. * Cellular noise ("Worley noise") in 2D in GLSL.
  4. * Copyright (c) Stefan Gustavson 2011-04-19. All rights reserved.
  5. * This code is released under the conditions of the MIT license.
  6. * See LICENSE file for details.
  7. */
  8. //#ifdef GL_OES_standard_derivatives
  9. // #extension GL_OES_standard_derivatives : enable
  10. //#endif
  11. //
  12. //float aastep (float threshold , float value)
  13. //{
  14. // float afwidth = 0.7 * length ( vec2 ( dFdx ( value ), dFdy ( value )));
  15. // return smoothstep ( threshold - afwidth , threshold + afwidth , value );
  16. //}
  17. // Permutation polynomial: (34x^2 + x) mod 289
  18. vec3 _czm_permute289(vec3 x)
  19. {
  20. return mod((34.0 * x + 1.0) * x, 289.0);
  21. }
  22. /**
  23. * DOC_TBA
  24. *
  25. * Implemented by Stefan Gustavson, and distributed under the MIT License. {@link http://openglinsights.git.sourceforge.net/git/gitweb.cgi?p=openglinsights/openglinsights;a=tree;f=proceduraltextures}
  26. *
  27. * @name czm_cellular
  28. * @glslFunction
  29. *
  30. * @see Stefan Gustavson's chapter, <i>Procedural Textures in GLSL</i>, in <a href="http://www.openglinsights.com/">OpenGL Insights</a>.
  31. */
  32. vec2 czm_cellular(vec2 P)
  33. // Cellular noise, returning F1 and F2 in a vec2.
  34. // Standard 3x3 search window for good F1 and F2 values
  35. {
  36. #define K 0.142857142857 // 1/7
  37. #define Ko 0.428571428571 // 3/7
  38. #define jitter 1.0 // Less gives more regular pattern
  39. vec2 Pi = mod(floor(P), 289.0);
  40. vec2 Pf = fract(P);
  41. vec3 oi = vec3(-1.0, 0.0, 1.0);
  42. vec3 of = vec3(-0.5, 0.5, 1.5);
  43. vec3 px = _czm_permute289(Pi.x + oi);
  44. vec3 p = _czm_permute289(px.x + Pi.y + oi); // p11, p12, p13
  45. vec3 ox = fract(p*K) - Ko;
  46. vec3 oy = mod(floor(p*K),7.0)*K - Ko;
  47. vec3 dx = Pf.x + 0.5 + jitter*ox;
  48. vec3 dy = Pf.y - of + jitter*oy;
  49. vec3 d1 = dx * dx + dy * dy; // d11, d12 and d13, squared
  50. p = _czm_permute289(px.y + Pi.y + oi); // p21, p22, p23
  51. ox = fract(p*K) - Ko;
  52. oy = mod(floor(p*K),7.0)*K - Ko;
  53. dx = Pf.x - 0.5 + jitter*ox;
  54. dy = Pf.y - of + jitter*oy;
  55. vec3 d2 = dx * dx + dy * dy; // d21, d22 and d23, squared
  56. p = _czm_permute289(px.z + Pi.y + oi); // p31, p32, p33
  57. ox = fract(p*K) - Ko;
  58. oy = mod(floor(p*K),7.0)*K - Ko;
  59. dx = Pf.x - 1.5 + jitter*ox;
  60. dy = Pf.y - of + jitter*oy;
  61. vec3 d3 = dx * dx + dy * dy; // d31, d32 and d33, squared
  62. // Sort out the two smallest distances (F1, F2)
  63. vec3 d1a = min(d1, d2);
  64. d2 = max(d1, d2); // Swap to keep candidates for F2
  65. d2 = min(d2, d3); // neither F1 nor F2 are now in d3
  66. d1 = min(d1a, d2); // F1 is now in d1
  67. d2 = max(d1a, d2); // Swap to keep candidates for F2
  68. d1.xy = (d1.x < d1.y) ? d1.xy : d1.yx; // Swap if smaller
  69. d1.xz = (d1.x < d1.z) ? d1.xz : d1.zx; // F1 is in d1.x
  70. d1.yz = min(d1.yz, d2.yz); // F2 is now not in d2.yz
  71. d1.y = min(d1.y, d1.z); // nor in d1.z
  72. d1.y = min(d1.y, d2.x); // F2 is in d1.y, we're done.
  73. return sqrt(d1.xy);
  74. }