noise.glsl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // The MIT License
  2. // Copyright © 2013 Inigo Quilez
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  4. // https://www.youtube.com/c/InigoQuilez
  5. // https://iquilezles.org/
  6. // https://www.shadertoy.com/view/lsf3WH
  7. float hash(vec2 p) // replace this by something better
  8. {
  9. p = 50.0*fract( p*0.3183099 + vec2(0.71,0.113));
  10. return -1.0+2.0*fract( p.x*p.y*(p.x+p.y) );
  11. }
  12. float hash(vec3 p) // replace this by something better
  13. {
  14. p = 50.0*fract( p*0.3183099 + vec3(0.71, 0.113, 0.5231));
  15. return -1.0+2.0*fract( p.x*p.y*p.z*(p.x+p.y+p.z) );
  16. }
  17. vec2 hash2(vec2 p)
  18. {
  19. return fract(cos(p*mat2(57.3,37.36,83.17,-23.2))*28.9) * 2.0 - 1.0;
  20. }
  21. vec2 hash2( ivec2 z ) // replace this anything that returns a random vector
  22. {
  23. // 2D to 1D (feel free to replace by some other)
  24. int n = z.x+z.y*11111;
  25. // Hugo Elias hash (feel free to replace by another one)
  26. n = (n<<13)^n;
  27. n = (n*(n*n*15731+789221)+1376312589)>>16;
  28. // Perlin style vectors
  29. n &= 7;
  30. vec2 gr = vec2(n&1,n>>1)*2.0-1.0;
  31. return ( n>=6 ) ? vec2(0.0,gr.x) :
  32. ( n>=4 ) ? vec2(gr.x,0.0) :
  33. gr;
  34. }
  35. vec3 hash3(vec2 p ) {
  36. vec3 p3 = vec3( dot(p,vec2(127.1,311.7)),
  37. dot(p,vec2(269.5,183.3)),
  38. dot(p,vec2(113.5,271.9)));
  39. return -1.0 + 2.0*fract(sin(p3)*43758.5453123);
  40. }
  41. vec3 hash3( vec3 p ) // replace this by something better
  42. {
  43. p = vec3( dot(p,vec3(127.1,311.7, 74.7)),
  44. dot(p,vec3(269.5,183.3,246.1)),
  45. dot(p,vec3(113.5,271.9,124.6)));
  46. return -1.0 + 2.0*fract(sin(p)*43758.5453123);
  47. }
  48. vec3 hash3( vec4 p ) // replace this by something better
  49. {
  50. vec3 r = vec3(
  51. dot(p, vec4(127.1, 311.7, 74.7, 93.124)),
  52. dot(p, vec4(269.5, 183.3, 246.1, 55.432)),
  53. dot(p, vec4(113.5, 271.9, 124.6, 6.823)));
  54. return -1.0 + 2.0*fract(sin(r)*43758.5453123);
  55. }
  56. vec4 hash4( vec4 p ) // replace this by something better
  57. {
  58. p = vec4( dot(p, vec4(127.1,311.7,74.7,93.124)),
  59. dot(p, vec4(269.5,183.3,246.1,55.432)),
  60. dot(p, vec4(113.5,271.9,124.6,6.823)),
  61. dot(p, vec4(37.643,83.42,17.531,11.952)));
  62. return -1.0 + 2.0*fract(sin(p)*43758.5453123);
  63. }
  64. uint murmurHash11(uint src) {
  65. const uint M = 0x5bd1e995u;
  66. uint h = 1190494759u;
  67. src *= M; src ^= src>>24u; src *= M;
  68. h *= M; h ^= src;
  69. h ^= h>>13u; h *= M; h ^= h>>15u;
  70. return h;
  71. }
  72. // 1 output, 1 input
  73. // float hash1(float src) {
  74. // uint h = murmurHash11(floatBitsToUint(src));
  75. // return (uintBitsToFloat(h & 0x007fffffu | 0x3f800000u) - 1.0) * 2.0 - 1.0;
  76. // }
  77. float hash1( float p ) // replace this by something better
  78. {
  79. p = p * 321.17;
  80. return -1.0 + 2.0*fract(sin(p)*43758.5453123);
  81. }
  82. // noise definition
  83. float noise( in vec4 p ) {
  84. vec4 i = floor( p );
  85. vec4 f = fract( p );
  86. vec4 u = f * f * (3.0 - 2.0 * f);
  87. float z1 = mix( mix( mix( dot( hash4( i + vec4(0.0,0.0,0.0,0.0) ), f - vec4(0.0,0.0,0.0,0.0) ),
  88. dot( hash4( i + vec4(1.0,0.0,0.0,0.0) ), f - vec4(1.0,0.0,0.0,0.0) ), u.x),
  89. mix( dot( hash4( i + vec4(0.0,1.0,0.0,0.0) ), f - vec4(0.0,1.0,0.0,0.0) ),
  90. dot( hash4( i + vec4(1.0,1.0,0.0,0.0) ), f - vec4(1.0,1.0,0.0,0.0) ), u.x), u.y),
  91. mix( mix( dot( hash4( i + vec4(0.0,0.0,1.0,0.0) ), f - vec4(0.0,0.0,1.0,0.0) ),
  92. dot( hash4( i + vec4(1.0,0.0,1.0,0.0) ), f - vec4(1.0,0.0,1.0,0.0) ), u.x),
  93. mix( dot( hash4( i + vec4(0.0,1.0,1.0,0.0) ), f - vec4(0.0,1.0,1.0,0.0) ),
  94. dot( hash4( i + vec4(1.0,1.0,1.0,0.0) ), f - vec4(1.0,1.0,1.0,0.0) ), u.x), u.y), u.z );
  95. float z2 = mix( mix( mix( dot( hash4( i + vec4(0.0,0.0,0.0,1.0) ), f - vec4(0.0,0.0,0.0,1.0) ),
  96. dot( hash4( i + vec4(1.0,0.0,0.0,1.0) ), f - vec4(1.0,0.0,0.0,1.0) ), u.x),
  97. mix( dot( hash4( i + vec4(0.0,1.0,0.0,1.0) ), f - vec4(0.0,1.0,0.0,1.0) ),
  98. dot( hash4( i + vec4(1.0,1.0,0.0,1.0) ), f - vec4(1.0,1.0,0.0,1.0) ), u.x), u.y),
  99. mix( mix( dot( hash4( i + vec4(0.0,0.0,1.0,1.0) ), f - vec4(0.0,0.0,1.0,1.0) ),
  100. dot( hash4( i + vec4(1.0,0.0,1.0,1.0) ), f - vec4(1.0,0.0,1.0,1.0) ), u.x),
  101. mix( dot( hash4( i + vec4(0.0,1.0,1.0,1.0) ), f - vec4(0.0,1.0,1.0,1.0) ),
  102. dot( hash4( i + vec4(1.0,1.0,1.0,1.0) ), f - vec4(1.0,1.0,1.0,1.0) ), u.x), u.y), u.z );
  103. return mix(z1, z2, u.w);
  104. }
  105. float noise( in vec3 p )
  106. {
  107. vec3 i = floor( p );
  108. vec3 f = fract( p );
  109. vec3 u = f*f*(3.0-2.0*f);
  110. return mix( mix( mix( dot( hash3( i + vec3(0.0,0.0,0.0) ), f - vec3(0.0,0.0,0.0) ),
  111. dot( hash3( i + vec3(1.0,0.0,0.0) ), f - vec3(1.0,0.0,0.0) ), u.x),
  112. mix( dot( hash3( i + vec3(0.0,1.0,0.0) ), f - vec3(0.0,1.0,0.0) ),
  113. dot( hash3( i + vec3(1.0,1.0,0.0) ), f - vec3(1.0,1.0,0.0) ), u.x), u.y),
  114. mix( mix( dot( hash3( i + vec3(0.0,0.0,1.0) ), f - vec3(0.0,0.0,1.0) ),
  115. dot( hash3( i + vec3(1.0,0.0,1.0) ), f - vec3(1.0,0.0,1.0) ), u.x),
  116. mix( dot( hash3( i + vec3(0.0,1.0,1.0) ), f - vec3(0.0,1.0,1.0) ),
  117. dot( hash3( i + vec3(1.0,1.0,1.0) ), f - vec3(1.0,1.0,1.0) ), u.x), u.y), u.z );
  118. }
  119. float noise( in vec2 p )
  120. {
  121. vec2 i = vec2(floor( p ));
  122. vec2 f = fract( p );
  123. vec2 u = f*f*(3.0-2.0*f); // feel free to replace by a quintic smoothstep instead
  124. return mix( mix( dot( hash2( i+vec2(0,0) ), f-vec2(0.0,0.0) ),
  125. dot( hash2( i+vec2(1,0) ), f-vec2(1.0,0.0) ), u.x),
  126. mix( dot( hash2( i+vec2(0,1) ), f-vec2(0.0,1.0) ),
  127. dot( hash2( i+vec2(1,1) ), f-vec2(1.0,1.0) ), u.x), u.y);
  128. }
  129. float noise( float p )
  130. {
  131. float i = floor( p );
  132. float f = fract( p );
  133. float u = f*f*(3.0-2.0*f); // feel free to replace by a quintic smoothstep instead
  134. return mix( hash1( i + 0.0 ) * (f - 0.0),
  135. hash1( i + 1.0 ) * (f - 1.0), u);
  136. }
  137. vec2 noise2(vec2 p) {
  138. return vec2(
  139. noise(p),
  140. noise(p + vec2(243.02935, 743.87439))
  141. );
  142. }
  143. vec3 noise3(vec2 p) {
  144. return vec3(
  145. noise(p),
  146. noise(p + vec2(243.02935, 743.87439)),
  147. noise(p + vec2(731.8735, 912.4724))
  148. );
  149. }
  150. vec3 noise3(vec3 p) {
  151. return vec3(
  152. noise(p),
  153. noise(p + vec3(243.02935, 743.87439, -17.5325)),
  154. noise(p + vec3(731.8735, 912.4724, 1231.43297))
  155. );
  156. }
  157. vec4 noise4(vec2 p) {
  158. return vec4(
  159. noise2(p),
  160. noise2(p.xy + vec2(314.421, -432.32))
  161. );
  162. }
  163. // return value noise (in x) and its derivatives (in yzw)
  164. vec4 noiseD( in vec3 x )
  165. {
  166. // grid
  167. vec3 i = floor(x);
  168. vec3 w = fract(x);
  169. // quintic interpolant
  170. vec3 u = w*w*w*(w*(w*6.0-15.0)+10.0);
  171. vec3 du = 30.0*w*w*(w*(w-2.0)+1.0);
  172. // gradients
  173. vec3 ga = hash3( i+vec3(0.0,0.0,0.0) );
  174. vec3 gb = hash3( i+vec3(1.0,0.0,0.0) );
  175. vec3 gc = hash3( i+vec3(0.0,1.0,0.0) );
  176. vec3 gd = hash3( i+vec3(1.0,1.0,0.0) );
  177. vec3 ge = hash3( i+vec3(0.0,0.0,1.0) );
  178. vec3 gf = hash3( i+vec3(1.0,0.0,1.0) );
  179. vec3 gg = hash3( i+vec3(0.0,1.0,1.0) );
  180. vec3 gh = hash3( i+vec3(1.0,1.0,1.0) );
  181. // projections
  182. float va = dot( ga, w-vec3(0.0,0.0,0.0) );
  183. float vb = dot( gb, w-vec3(1.0,0.0,0.0) );
  184. float vc = dot( gc, w-vec3(0.0,1.0,0.0) );
  185. float vd = dot( gd, w-vec3(1.0,1.0,0.0) );
  186. float ve = dot( ge, w-vec3(0.0,0.0,1.0) );
  187. float vf = dot( gf, w-vec3(1.0,0.0,1.0) );
  188. float vg = dot( gg, w-vec3(0.0,1.0,1.0) );
  189. float vh = dot( gh, w-vec3(1.0,1.0,1.0) );
  190. // interpolations
  191. return vec4(
  192. va + u.x*(vb-va) + u.y*(vc-va) + u.z*(ve-va) + u.x*u.y*(va-vb-vc+vd) + u.y*u.z*(va-vc-ve+vg) +
  193. u.z*u.x*(va-vb-ve+vf) + (-va+vb+vc-vd+ve-vf-vg+vh)*u.x*u.y*u.z, // value
  194. ga + u.x*(gb-ga) + u.y*(gc-ga) + u.z*(ge-ga) + u.x*u.y*(ga-gb-gc+gd) + u.y*u.z*(ga-gc-ge+gg) +
  195. u.z*u.x*(ga-gb-ge+gf) + (-ga+gb+gc-gd+ge-gf-gg+gh)*u.x*u.y*u.z + // derivatives
  196. du * (vec3(vb,vc,ve) - va + u.yzx*vec3(va-vb-vc+vd,va-vc-ve+vg,va-vb-ve+vf) +
  197. u.zxy*vec3(va-vb-ve+vf,va-vb-vc+vd,va-vc-ve+vg) + u.yzx*u.zxy*(-va+vb+vc-vd+ve-vf-vg+vh) ));
  198. }
  199. const mat2 FBM_M = mat2( 0.80, 0.60, -0.60, 0.80 );
  200. const mat3 FBM_M3 = mat3( 0.00, 0.80, 0.60,
  201. -0.80, 0.36, -0.48,
  202. -0.60, -0.48, 0.64 );
  203. float fbm(vec3 p, int octaves, float persistence, float lacunarity, float exponentiation) {
  204. float amplitude = 1.0;
  205. float frequency = 1.0;
  206. float total = 0.0;
  207. float normalization = 0.0;
  208. for (int i = 0; i < octaves; ++i) {
  209. float noiseValue = noise(p * frequency);
  210. total += noiseValue * amplitude;
  211. normalization += amplitude;
  212. amplitude *= persistence;
  213. frequency *= lacunarity;
  214. }
  215. total /= normalization;
  216. total = total * 0.5 + 0.5;
  217. total = pow(total, exponentiation);
  218. // total = 1.0 - abs(total);
  219. return total;
  220. }
  221. // noiseFBM function
  222. float noiseFBM(vec4 p, int octaves, float persistence, float lacunarity) {
  223. float amplitude = 0.5;
  224. float frequency = 1.0;
  225. float total = 0.0;
  226. float normalization = 0.0;
  227. for (int i = 0; i < octaves; ++i) {
  228. float noiseValue = noise(p * frequency);
  229. total += noiseValue * amplitude;
  230. normalization += amplitude;
  231. amplitude *= persistence;
  232. frequency *= lacunarity;
  233. }
  234. return total;
  235. }
  236. float noiseFBM(vec3 p, int octaves, float persistence, float lacunarity) {
  237. float amplitude = 0.5;
  238. float frequency = 1.0;
  239. float total = 0.0;
  240. float normalization = 0.0;
  241. for (int i = 0; i < octaves; ++i) {
  242. float noiseValue = noise(p * frequency);
  243. total += noiseValue * amplitude;
  244. normalization += amplitude;
  245. amplitude *= persistence;
  246. frequency *= lacunarity;
  247. }
  248. return total;
  249. }
  250. float noiseFBM(vec2 p, int octaves, float persistence, float lacunarity) {
  251. float amplitude = 0.5;
  252. float total = 0.0;
  253. float normalization = 0.0;
  254. for (int i = 0; i < octaves; ++i) {
  255. float noiseValue = noise(p);
  256. total += noiseValue * amplitude;
  257. normalization += amplitude;
  258. amplitude *= persistence;
  259. p = FBM_M * p * lacunarity;
  260. }
  261. return total;
  262. }
  263. float noiseFBM(float p, int octaves, float persistence, float lacunarity) {
  264. float amplitude = 0.5;
  265. float total = 0.0;
  266. float normalization = 0.0;
  267. for (int i = 0; i < octaves; ++i) {
  268. float noiseValue = noise(p);
  269. total += noiseValue * amplitude;
  270. normalization += amplitude;
  271. amplitude *= persistence;
  272. p = p * lacunarity;
  273. }
  274. total /= normalization;
  275. return total;
  276. }
  277. // noiseFBM3 definition
  278. vec3 noiseFBM3(vec3 p, int octaves, float persistence, float lacunarity) {
  279. return vec3(
  280. noiseFBM(p, octaves, persistence, lacunarity),
  281. noiseFBM(p + 13.2317, octaves, persistence, lacunarity),
  282. noiseFBM(p + -34.934, octaves, persistence, lacunarity)
  283. );
  284. }
  285. vec3 noiseFBM3(vec4 p, int octaves, float persistence, float lacunarity) {
  286. return vec3(
  287. noiseFBM(p, octaves, persistence, lacunarity),
  288. noiseFBM(p + 13.2317, octaves, persistence, lacunarity),
  289. noiseFBM(p + -34.934, octaves, persistence, lacunarity)
  290. );
  291. }
  292. vec2 noiseFBM2(vec2 p, int octaves, float persistence, float lacunarity) {
  293. return vec2(
  294. noiseFBM(p, octaves, persistence, lacunarity),
  295. noiseFBM(p + 13.2317, octaves, persistence, lacunarity)
  296. );
  297. }
  298. vec2 noiseFBM2(vec3 p, int octaves, float persistence, float lacunarity) {
  299. return vec2(
  300. noiseFBM(p, octaves, persistence, lacunarity),
  301. noiseFBM(p + vec3(432.532, 5326.3245, -95.9043), octaves, persistence, lacunarity)
  302. );
  303. }
  304. float noiseWarp(vec3 coords) {
  305. vec3 offset = vec3(
  306. noiseFBM(coords, 4, 0.5, 2.0),
  307. noiseFBM(coords + vec3(43.235, 23.112, 0.0), 4, 0.5, 2.0), 0.0);
  308. float noiseSample = noiseFBM(coords + offset, 1, 0.5, 2.0);
  309. vec3 offset2 = vec3(
  310. noiseFBM(coords + 4.0 * offset + vec3(5.325, 1.421, 3.235), 4, 0.5, 2.0),
  311. noiseFBM(coords + 4.0 * offset + vec3(4.32, 0.532, 6.324), 4, 0.5, 2.0), 0.0);
  312. noiseSample = noiseFBM(coords + 4.0 * offset2, 1, 0.5, 2.0);
  313. return noiseSample;
  314. }
  315. float noiseWarp(vec2 coords) {
  316. vec2 offset = noiseFBM2(coords, 4, 0.5, 2.0);
  317. float noiseSample = noiseFBM(coords + offset, 1, 0.5, 2.0);
  318. vec2 offset2 = noiseFBM2(coords + 4.0 * offset + vec2(5.325, 1.421), 4, 0.5, 2.0);
  319. noiseSample = noiseFBM(coords + 4.0 * offset2, 1, 0.5, 2.0);
  320. return noiseSample;
  321. }
  322. float voronoi(vec2 coords, float maxOffset) {
  323. vec2 gridBasePosition = floor(coords.xy);
  324. vec2 gridCoordOffset = fract(coords.xy);
  325. float closest = 1.0;
  326. for (float y = -2.0; y <= 2.0; y += 1.0) {
  327. for (float x = -2.0; x <= 2.0; x += 1.0) {
  328. vec2 neighbourCellPosition = vec2(x, y);
  329. vec2 cellWorldPosition = gridBasePosition + neighbourCellPosition;
  330. vec2 cellOffset = hash2(cellWorldPosition);
  331. cellOffset *= maxOffset;
  332. float distToNeighbour = length(
  333. neighbourCellPosition + cellOffset - gridCoordOffset);
  334. closest = min(closest, distToNeighbour);
  335. }
  336. }
  337. return closest;
  338. }