procedural.glslf 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifndef PROCEDURAL_GLSLF
  2. #define PROCEDURAL_GLSLF
  3. vec4 mod289(vec4 x) {
  4. return x - floor(x * (_1_0 / 289.0)) * 289.0;
  5. }
  6. vec3 mod289(vec3 x) {
  7. return x - floor(x * (_1_0 / 289.0)) * 289.0;
  8. }
  9. vec2 mod289(vec2 x) {
  10. return x - floor(x * (_1_0 / 289.0)) * 289.0;
  11. }
  12. vec4 mod7(vec4 x) {
  13. return x - floor(x * (_1_0 / 7.0)) * 7.0;
  14. }
  15. // Permutation polynomial: (34x^2 + 5x) mod 289
  16. vec4 permute(vec4 x) {
  17. return mod289((34.0 * x + 5.0) * x);
  18. }
  19. // Cellular noise ("Worley noise") in 2D in GLSL.
  20. // Copyright (c) Stefan Gustavson 2011-04-19. All rights reserved.
  21. // This code is released under the conditions of the MIT license.
  22. // See LICENSE file for details.
  23. // Cellular noise, returning F1 and F2 in a vec2.
  24. // Speeded up by using 2x2 search window instead of 3x3,
  25. // at the expense of some strong pattern artifacts.
  26. // F2 is often wrong and has sharp discontinuities.
  27. // If you need a smooth F2, use the slower 3x3 version.
  28. // F1 is sometimes wrong, too, but OK for most purposes.
  29. #define K 0.142857142857 // 1/7
  30. #define K2 0.0714285714285 // K/2
  31. #define JITTER 0.7 // JITTER 1.0 makes F1 wrong more often
  32. vec2 cellular2x2(vec2 P) {
  33. vec2 Pi = mod289(floor(P));
  34. vec2 Pf = fract(P);
  35. vec4 Pfx = Pf.x + vec4(-0.5, -1.5, -0.5, -1.5);
  36. vec4 Pfy = Pf.y + vec4(-0.5, -0.5, -1.5, -1.5);
  37. vec4 p = permute(Pi.x + vec4(_0_0, _1_0,
  38. _0_0, _1_0));
  39. p = permute(p + Pi.y + vec4(_0_0, _0_0,
  40. _1_0, _1_0));
  41. vec4 ox = mod7(p)*K+K2;
  42. vec4 oy = mod7(floor(p*K))*K+K2;
  43. vec4 dx = Pfx + JITTER*ox;
  44. vec4 dy = Pfy + JITTER*oy;
  45. vec4 d = dx * dx + dy * dy; // d11, d12, d21 and d22, squared
  46. // Sort out the two smallest distances
  47. #if 1
  48. // Cheat and pick only F1
  49. d.xy = min(d.xy, d.zw);
  50. d.x = min(d.x, d.y);
  51. return d.xx; // F1 duplicated, F2 not computed
  52. #else
  53. // Do it right and find both F1 and F2
  54. d.xy = (d.x < d.y) ? d.xy : d.yx; // Swap if smaller
  55. d.xz = (d.x < d.z) ? d.xz : d.zx;
  56. d.xw = (d.x < d.w) ? d.xw : d.wx;
  57. d.y = min(d.y, d.z);
  58. d.y = min(d.y, d.w);
  59. return sqrt(d.xy);
  60. #endif
  61. }
  62. //Special Voronoi noise for caustics with aberration
  63. vec3 cellular2x2_caust(vec2 P, float aber) {
  64. vec2 Pi = mod289(floor(P));
  65. vec2 Pf = fract(P);
  66. vec4 Pfx = Pf.x + vec4(-0.5, -1.5, -0.5, -1.5);
  67. vec4 Pfy = Pf.y + vec4(-0.5, -0.5, -1.5, -1.5);
  68. vec4 p = permute(Pi.x + vec4(_0_0, _1_0,
  69. _0_0, _1_0));
  70. p = permute(p + Pi.y + vec4(_0_0, _0_0,
  71. _1_0, _1_0));
  72. vec4 ox = mod7(p) * K + K2;
  73. vec4 oy = mod7(floor(p * K)) * K + K2;
  74. vec4 dx = Pfx + JITTER * ox;
  75. vec4 dy = Pfy + JITTER * oy;
  76. vec4 d1 = dx * dx + dy * dy; // d11, d12, d21 and d22, squared
  77. dx += aber;
  78. dy += aber;
  79. vec4 d2 = dx * dx + dy * dy; // d11, d12, d21 and d22, squared
  80. dx += aber;
  81. dy += aber;
  82. vec4 d3 = dx * dx + dy * dy; // d11, d12, d21 and d22, squared
  83. // Sort out the two smallest distances
  84. // Cheat and pick only F1
  85. d1.xy = min(d1.xy, d1.zw);
  86. d1.x = min(d1.x, d1.y);
  87. d2.xy = min(d2.xy, d2.zw);
  88. d2.x = min(d2.x, d2.y);
  89. d3.xy = min(d3.xy, d3.zw);
  90. d3.x = min(d3.x, d3.y);
  91. return vec3(d1.x, d2.x, d3.x); // F1 duplicated, F2 not computed
  92. }
  93. //
  94. // Description : Array and textureless GLSL 2D simplex noise function.
  95. // Author : Ian McEwan, Ashima Arts.
  96. // Maintainer : ijm
  97. // Lastmod : 20110822 (ijm)
  98. // License : Copyright (C) 2011 Ashima Arts. All rights reserved.
  99. // Distributed under the MIT License. See LICENSE file.
  100. // https://github.com/ashima/webgl-noise
  101. //
  102. vec3 permute3(vec3 x) {
  103. return mod289(((x*34.0)+_1_0)*x);
  104. }
  105. float snoise(vec2 v)
  106. {
  107. const vec4 C = vec4(0.211324865405187, // (3.0-sqrt(3.0))/6.0
  108. 0.366025403784439, // 0.5*(sqrt(3.0)-1.0)
  109. -0.577350269189626, // -1.0 + 2.0 * C.x
  110. 0.024390243902439); // 1.0 / 41.0
  111. // First corner
  112. vec2 i = floor(v + dot(v, C.yy) );
  113. vec2 x0 = v - i + dot(i, C.xx);
  114. // Other corners
  115. vec2 i1;
  116. //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
  117. //i1.y = 1.0 - i1.x;
  118. i1 = (x0.x > x0.y) ? vec2(_1_0, _0_0)
  119. : vec2(_0_0, _1_0);
  120. // x0 = x0 - 0.0 + 0.0 * C.xx ;
  121. // x1 = x0 - i1 + 1.0 * C.xx ;
  122. // x2 = x0 - 1.0 + 2.0 * C.xx ;
  123. vec4 x12 = x0.xyxy + C.xxzz;
  124. x12.xy -= i1;
  125. // Permutations
  126. i = mod289(i); // Avoid truncation effects in permutation
  127. vec3 p = permute3( permute3( i.y + vec3(_0_0, i1.y,
  128. _1_0 ))
  129. + i.x + vec3(_0_0, i1.x, _1_0 ));
  130. vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)),
  131. _0_0);
  132. m = m*m ;
  133. m = m*m ;
  134. // Gradients: 41 points uniformly over a line, mapped onto a diamond.
  135. // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
  136. vec3 x = 2.0 * fract(p * C.www) - _1_0;
  137. vec3 h = abs(x) - 0.5;
  138. vec3 ox = floor(x + 0.5);
  139. vec3 a0 = x - ox;
  140. // Normalise gradients implicitly by scaling m
  141. // Approximation of: m *= inversesqrt( a0*a0 + h*h );
  142. m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
  143. // Compute final noise value at P
  144. vec3 g;
  145. g.x = a0.x * x0.x + h.x * x0.y;
  146. g.yz = a0.yz * x12.xz + h.yz * x12.yw;
  147. return 130.0 * dot(m, g);
  148. }
  149. // generating noise/pattern texture for dithering
  150. vec2 generate_dithering_tex(vec2 coord) {
  151. float d1 = dot(coord, vec2(12.9898, 78.233));
  152. float d2 = dot(coord, vec2(12.9898, 78.233) * 2.0);
  153. float noiseX = fract(sin(d1) * 43758.5453) * 2.0 - _1_0;
  154. float noiseY = fract(sin(d2) * 43758.5453) * 2.0 - _1_0;
  155. return vec2(noiseX, noiseY);
  156. }
  157. #endif