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.

snoise.glsl 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * @license
  3. * Description : Array and textureless GLSL 2D/3D/4D simplex
  4. * noise functions.
  5. * Author : Ian McEwan, Ashima Arts.
  6. * Maintainer : ijm
  7. * Lastmod : 20110822 (ijm)
  8. * License : Copyright (C) 2011 Ashima Arts. All rights reserved.
  9. * Distributed under the MIT License. See LICENSE file.
  10. * https://github.com/ashima/webgl-noise
  11. */
  12. vec4 _czm_mod289(vec4 x)
  13. {
  14. return x - floor(x * (1.0 / 289.0)) * 289.0;
  15. }
  16. vec3 _czm_mod289(vec3 x)
  17. {
  18. return x - floor(x * (1.0 / 289.0)) * 289.0;
  19. }
  20. vec2 _czm_mod289(vec2 x)
  21. {
  22. return x - floor(x * (1.0 / 289.0)) * 289.0;
  23. }
  24. float _czm_mod289(float x)
  25. {
  26. return x - floor(x * (1.0 / 289.0)) * 289.0;
  27. }
  28. vec4 _czm_permute(vec4 x)
  29. {
  30. return _czm_mod289(((x*34.0)+1.0)*x);
  31. }
  32. vec3 _czm_permute(vec3 x)
  33. {
  34. return _czm_mod289(((x*34.0)+1.0)*x);
  35. }
  36. float _czm_permute(float x)
  37. {
  38. return _czm_mod289(((x*34.0)+1.0)*x);
  39. }
  40. vec4 _czm_taylorInvSqrt(vec4 r)
  41. {
  42. return 1.79284291400159 - 0.85373472095314 * r;
  43. }
  44. float _czm_taylorInvSqrt(float r)
  45. {
  46. return 1.79284291400159 - 0.85373472095314 * r;
  47. }
  48. vec4 _czm_grad4(float j, vec4 ip)
  49. {
  50. const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0);
  51. vec4 p,s;
  52. p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0;
  53. p.w = 1.5 - dot(abs(p.xyz), ones.xyz);
  54. s = vec4(lessThan(p, vec4(0.0)));
  55. p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www;
  56. return p;
  57. }
  58. /**
  59. * DOC_TBA
  60. *
  61. * Implemented by Ian McEwan, Ashima Arts, and distributed under the MIT License. {@link https://github.com/ashima/webgl-noise}
  62. *
  63. * @name czm_snoise
  64. * @glslFunction
  65. *
  66. * @see <a href="https://github.com/ashima/webgl-noise">https://github.com/ashima/webgl-noise</a>
  67. * @see Stefan Gustavson's paper <a href="http://www.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf">Simplex noise demystified</a>
  68. */
  69. float czm_snoise(vec2 v)
  70. {
  71. const vec4 C = vec4(0.211324865405187, // (3.0-sqrt(3.0))/6.0
  72. 0.366025403784439, // 0.5*(sqrt(3.0)-1.0)
  73. -0.577350269189626, // -1.0 + 2.0 * C.x
  74. 0.024390243902439); // 1.0 / 41.0
  75. // First corner
  76. vec2 i = floor(v + dot(v, C.yy) );
  77. vec2 x0 = v - i + dot(i, C.xx);
  78. // Other corners
  79. vec2 i1;
  80. //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
  81. //i1.y = 1.0 - i1.x;
  82. i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
  83. // x0 = x0 - 0.0 + 0.0 * C.xx ;
  84. // x1 = x0 - i1 + 1.0 * C.xx ;
  85. // x2 = x0 - 1.0 + 2.0 * C.xx ;
  86. vec4 x12 = x0.xyxy + C.xxzz;
  87. x12.xy -= i1;
  88. // Permutations
  89. i = _czm_mod289(i); // Avoid truncation effect in permutation
  90. vec3 p = _czm_permute( _czm_permute( i.y + vec3(0.0, i1.y, 1.0 )) + i.x + vec3(0.0, i1.x, 1.0 ));
  91. vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
  92. m = m*m ;
  93. m = m*m ;
  94. // Gradients: 41 points uniformly over a line, mapped onto a diamond.
  95. // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
  96. vec3 x = 2.0 * fract(p * C.www) - 1.0;
  97. vec3 h = abs(x) - 0.5;
  98. vec3 ox = floor(x + 0.5);
  99. vec3 a0 = x - ox;
  100. // Normalise gradients implicitly by scaling m
  101. // Approximation of: m *= inversesqrt( a0*a0 + h*h );
  102. m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
  103. // Compute final noise value at P
  104. vec3 g;
  105. g.x = a0.x * x0.x + h.x * x0.y;
  106. g.yz = a0.yz * x12.xz + h.yz * x12.yw;
  107. return 130.0 * dot(m, g);
  108. }
  109. float czm_snoise(vec3 v)
  110. {
  111. const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
  112. const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
  113. // First corner
  114. vec3 i = floor(v + dot(v, C.yyy) );
  115. vec3 x0 = v - i + dot(i, C.xxx) ;
  116. // Other corners
  117. vec3 g = step(x0.yzx, x0.xyz);
  118. vec3 l = 1.0 - g;
  119. vec3 i1 = min( g.xyz, l.zxy );
  120. vec3 i2 = max( g.xyz, l.zxy );
  121. // x0 = x0 - 0.0 + 0.0 * C.xxx;
  122. // x1 = x0 - i1 + 1.0 * C.xxx;
  123. // x2 = x0 - i2 + 2.0 * C.xxx;
  124. // x3 = x0 - 1.0 + 3.0 * C.xxx;
  125. vec3 x1 = x0 - i1 + C.xxx;
  126. vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
  127. vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
  128. // Permutations
  129. i = _czm_mod289(i);
  130. vec4 p = _czm_permute( _czm_permute( _czm_permute(
  131. i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
  132. + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
  133. + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
  134. // Gradients: 7x7 points over a square, mapped onto an octahedron.
  135. // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
  136. float n_ = 0.142857142857; // 1.0/7.0
  137. vec3 ns = n_ * D.wyz - D.xzx;
  138. vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
  139. vec4 x_ = floor(j * ns.z);
  140. vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
  141. vec4 x = x_ *ns.x + ns.yyyy;
  142. vec4 y = y_ *ns.x + ns.yyyy;
  143. vec4 h = 1.0 - abs(x) - abs(y);
  144. vec4 b0 = vec4( x.xy, y.xy );
  145. vec4 b1 = vec4( x.zw, y.zw );
  146. //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
  147. //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
  148. vec4 s0 = floor(b0)*2.0 + 1.0;
  149. vec4 s1 = floor(b1)*2.0 + 1.0;
  150. vec4 sh = -step(h, vec4(0.0));
  151. vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
  152. vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
  153. vec3 p0 = vec3(a0.xy,h.x);
  154. vec3 p1 = vec3(a0.zw,h.y);
  155. vec3 p2 = vec3(a1.xy,h.z);
  156. vec3 p3 = vec3(a1.zw,h.w);
  157. //Normalise gradients
  158. vec4 norm = _czm_taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
  159. p0 *= norm.x;
  160. p1 *= norm.y;
  161. p2 *= norm.z;
  162. p3 *= norm.w;
  163. // Mix final noise value
  164. vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
  165. m = m * m;
  166. return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
  167. dot(p2,x2), dot(p3,x3) ) );
  168. }
  169. float czm_snoise(vec4 v)
  170. {
  171. const vec4 C = vec4( 0.138196601125011, // (5 - sqrt(5))/20 G4
  172. 0.276393202250021, // 2 * G4
  173. 0.414589803375032, // 3 * G4
  174. -0.447213595499958); // -1 + 4 * G4
  175. // (sqrt(5) - 1)/4 = F4, used once below
  176. #define F4 0.309016994374947451
  177. // First corner
  178. vec4 i = floor(v + dot(v, vec4(F4)) );
  179. vec4 x0 = v - i + dot(i, C.xxxx);
  180. // Other corners
  181. // Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
  182. vec4 i0;
  183. vec3 isX = step( x0.yzw, x0.xxx );
  184. vec3 isYZ = step( x0.zww, x0.yyz );
  185. // i0.x = dot( isX, vec3( 1.0 ) );
  186. i0.x = isX.x + isX.y + isX.z;
  187. i0.yzw = 1.0 - isX;
  188. // i0.y += dot( isYZ.xy, vec2( 1.0 ) );
  189. i0.y += isYZ.x + isYZ.y;
  190. i0.zw += 1.0 - isYZ.xy;
  191. i0.z += isYZ.z;
  192. i0.w += 1.0 - isYZ.z;
  193. // i0 now contains the unique values 0,1,2,3 in each channel
  194. vec4 i3 = clamp( i0, 0.0, 1.0 );
  195. vec4 i2 = clamp( i0-1.0, 0.0, 1.0 );
  196. vec4 i1 = clamp( i0-2.0, 0.0, 1.0 );
  197. // x0 = x0 - 0.0 + 0.0 * C.xxxx
  198. // x1 = x0 - i1 + 1.0 * C.xxxx
  199. // x2 = x0 - i2 + 2.0 * C.xxxx
  200. // x3 = x0 - i3 + 3.0 * C.xxxx
  201. // x4 = x0 - 1.0 + 4.0 * C.xxxx
  202. vec4 x1 = x0 - i1 + C.xxxx;
  203. vec4 x2 = x0 - i2 + C.yyyy;
  204. vec4 x3 = x0 - i3 + C.zzzz;
  205. vec4 x4 = x0 + C.wwww;
  206. // Permutations
  207. i = _czm_mod289(i);
  208. float j0 = _czm_permute( _czm_permute( _czm_permute( _czm_permute(i.w) + i.z) + i.y) + i.x);
  209. vec4 j1 = _czm_permute( _czm_permute( _czm_permute( _czm_permute (
  210. i.w + vec4(i1.w, i2.w, i3.w, 1.0 ))
  211. + i.z + vec4(i1.z, i2.z, i3.z, 1.0 ))
  212. + i.y + vec4(i1.y, i2.y, i3.y, 1.0 ))
  213. + i.x + vec4(i1.x, i2.x, i3.x, 1.0 ));
  214. // Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
  215. // 7*7*6 = 294, which is close to the ring size 17*17 = 289.
  216. vec4 ip = vec4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ;
  217. vec4 p0 = _czm_grad4(j0, ip);
  218. vec4 p1 = _czm_grad4(j1.x, ip);
  219. vec4 p2 = _czm_grad4(j1.y, ip);
  220. vec4 p3 = _czm_grad4(j1.z, ip);
  221. vec4 p4 = _czm_grad4(j1.w, ip);
  222. // Normalise gradients
  223. vec4 norm = _czm_taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
  224. p0 *= norm.x;
  225. p1 *= norm.y;
  226. p2 *= norm.z;
  227. p3 *= norm.w;
  228. p4 *= _czm_taylorInvSqrt(dot(p4,p4));
  229. // Mix contributions from the five corners
  230. vec3 m0 = max(0.6 - vec3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0);
  231. vec2 m1 = max(0.6 - vec2(dot(x3,x3), dot(x4,x4) ), 0.0);
  232. m0 = m0 * m0;
  233. m1 = m1 * m1;
  234. return 49.0 * ( dot(m0*m0, vec3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 )))
  235. + dot(m1*m1, vec2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ;
  236. }