noise.glsl 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Virtually all of these were taken from: https://www.shadertoy.com/view/ttc3zr
  2. uvec4 murmurHash42(uvec2 src) {
  3. const uint M = 0x5bd1e995u;
  4. uvec4 h = uvec4(1190494759u, 2147483647u, 3559788179u, 179424673u);
  5. src *= M; src ^= src>>24u; src *= M;
  6. h *= M; h ^= src.x; h *= M; h ^= src.y;
  7. h ^= h>>13u; h *= M; h ^= h>>15u;
  8. return h;
  9. }
  10. uint murmurHash11(uint src) {
  11. const uint M = 0x5bd1e995u;
  12. uint h = 1190494759u;
  13. src *= M; src ^= src>>24u; src *= M;
  14. h *= M; h ^= src;
  15. h ^= h>>13u; h *= M; h ^= h>>15u;
  16. return h;
  17. }
  18. uint murmurHash12(uvec2 src) {
  19. const uint M = 0x5bd1e995u;
  20. uint h = 1190494759u;
  21. src *= M; src ^= src>>24u; src *= M;
  22. h *= M; h ^= src.x; h *= M; h ^= src.y;
  23. h ^= h>>13u; h *= M; h ^= h>>15u;
  24. return h;
  25. }
  26. uint murmurHash13(uvec3 src) {
  27. const uint M = 0x5bd1e995u;
  28. uint h = 1190494759u;
  29. src *= M; src ^= src>>24u; src *= M;
  30. h *= M; h ^= src.x; h *= M; h ^= src.y; h *= M; h ^= src.z;
  31. h ^= h>>13u; h *= M; h ^= h>>15u;
  32. return h;
  33. }
  34. uvec2 murmurHash22(uvec2 src) {
  35. const uint M = 0x5bd1e995u;
  36. uvec2 h = uvec2(1190494759u, 2147483647u);
  37. src *= M; src ^= src>>24u; src *= M;
  38. h *= M; h ^= src.x; h *= M; h ^= src.y;
  39. h ^= h>>13u; h *= M; h ^= h>>15u;
  40. return h;
  41. }
  42. uvec2 murmurHash21(uint src) {
  43. const uint M = 0x5bd1e995u;
  44. uvec2 h = uvec2(1190494759u, 2147483647u);
  45. src *= M; src ^= src>>24u; src *= M;
  46. h *= M; h ^= src;
  47. h ^= h>>13u; h *= M; h ^= h>>15u;
  48. return h;
  49. }
  50. uvec2 murmurHash23(uvec3 src) {
  51. const uint M = 0x5bd1e995u;
  52. uvec2 h = uvec2(1190494759u, 2147483647u);
  53. src *= M; src ^= src>>24u; src *= M;
  54. h *= M; h ^= src.x; h *= M; h ^= src.y; h *= M; h ^= src.z;
  55. h ^= h>>13u; h *= M; h ^= h>>15u;
  56. return h;
  57. }
  58. uvec3 murmurHash31(uint src) {
  59. const uint M = 0x5bd1e995u;
  60. uvec3 h = uvec3(1190494759u, 2147483647u, 3559788179u);
  61. src *= M; src ^= src>>24u; src *= M;
  62. h *= M; h ^= src;
  63. h ^= h>>13u; h *= M; h ^= h>>15u;
  64. return h;
  65. }
  66. uvec3 murmurHash33(uvec3 src) {
  67. const uint M = 0x5bd1e995u;
  68. uvec3 h = uvec3(1190494759u, 2147483647u, 3559788179u);
  69. src *= M; src ^= src>>24u; src *= M;
  70. h *= M; h ^= src.x; h *= M; h ^= src.y; h *= M; h ^= src.z;
  71. h ^= h>>13u; h *= M; h ^= h>>15u;
  72. return h;
  73. }
  74. // 3 outputs, 3 inputs
  75. vec3 hash33(vec3 src) {
  76. uvec3 h = murmurHash33(floatBitsToUint(src));
  77. return uintBitsToFloat(h & 0x007fffffu | 0x3f800000u) - 1.0;
  78. }
  79. // 1 output, 1 input
  80. float hash11(float src) {
  81. uint h = murmurHash11(floatBitsToUint(src));
  82. return uintBitsToFloat(h & 0x007fffffu | 0x3f800000u) - 1.0;
  83. }
  84. // 1 output, 2 inputs
  85. float hash12(vec2 src) {
  86. uint h = murmurHash12(floatBitsToUint(src));
  87. return uintBitsToFloat(h & 0x007fffffu | 0x3f800000u) - 1.0;
  88. }
  89. // 1 output, 3 inputs
  90. float hash13(vec3 src) {
  91. uint h = murmurHash13(floatBitsToUint(src));
  92. return uintBitsToFloat(h & 0x007fffffu | 0x3f800000u) - 1.0;
  93. }
  94. // 2 outputs, 1 input
  95. vec2 hash21(float src) {
  96. uvec2 h = murmurHash21(floatBitsToUint(src));
  97. return uintBitsToFloat(h & 0x007fffffu | 0x3f800000u) - 1.0;
  98. }
  99. // 3 outputs, 1 input
  100. vec3 hash31(float src) {
  101. uvec3 h = murmurHash31(floatBitsToUint(src));
  102. return uintBitsToFloat(h & 0x007fffffu | 0x3f800000u) - 1.0;
  103. }
  104. // 2 outputs, 2 inputs
  105. vec2 hash22(vec2 src) {
  106. uvec2 h = murmurHash22(floatBitsToUint(src));
  107. return uintBitsToFloat(h & 0x007fffffu | 0x3f800000u) - 1.0;
  108. }
  109. // 4 outputs, 2 inputs
  110. vec4 hash42(vec2 src) {
  111. uvec4 h = murmurHash42(floatBitsToUint(src));
  112. return uintBitsToFloat(h & 0x007fffffu | 0x3f800000u) - 1.0;
  113. }
  114. // 2 outputs, 3 inputs
  115. vec2 hash23(vec3 src) {
  116. uvec2 h = murmurHash23(floatBitsToUint(src));
  117. return uintBitsToFloat(h & 0x007fffffu | 0x3f800000u) - 1.0;
  118. }
  119. float noise11(float p) {
  120. float i = floor(p);
  121. float f = fract(p);
  122. float u = smoothstep(0.0, 1.0, f);
  123. float val = mix( hash11(i + 0.0),
  124. hash11(i + 1.0), u);
  125. return val * 2.0 - 1.0;
  126. }
  127. float noise12(vec2 p) {
  128. vec2 i = floor(p);
  129. vec2 f = fract(p);
  130. vec2 u = smoothstep(vec2(0.0), vec2(1.0), f);
  131. float val = mix( mix( hash12( i + vec2(0.0, 0.0) ),
  132. hash12( i + vec2(1.0, 0.0) ), u.x),
  133. mix( hash12( i + vec2(0.0, 1.0) ),
  134. hash12( i + vec2(1.0, 1.0) ), u.x), u.y);
  135. return val * 2.0 - 1.0;
  136. }
  137. float noise13(vec3 x) {
  138. vec3 i = floor(x);
  139. vec3 f = fract(x);
  140. f = f*f*(3.0-2.0*f);
  141. return mix(mix(mix( hash13(i+vec3(0.0, 0.0, 0.0)),
  142. hash13(i+vec3(1.0, 0.0, 0.0)),f.x),
  143. mix( hash13(i+vec3(0.0, 1.0, 0.0)),
  144. hash13(i+vec3(1.0, 1.0, 0.0)),f.x),f.y),
  145. mix(mix( hash13(i+vec3(0.0, 0.0, 1.0)),
  146. hash13(i+vec3(1.0, 0.0, 1.0)),f.x),
  147. mix( hash13(i+vec3(0.0, 1.0, 1.0)),
  148. hash13(i+vec3(1.0, 1.0, 1.0)),f.x),f.y),f.z);
  149. }
  150. vec2 noise23(vec3 x) {
  151. vec3 i = floor(x);
  152. vec3 f = fract(x);
  153. f = f*f*(3.0-2.0*f);
  154. return mix(mix(mix( hash23(i+vec3(0.0, 0.0, 0.0)),
  155. hash23(i+vec3(1.0, 0.0, 0.0)),f.x),
  156. mix( hash23(i+vec3(0.0, 1.0, 0.0)),
  157. hash23(i+vec3(1.0, 1.0, 0.0)),f.x),f.y),
  158. mix(mix( hash23(i+vec3(0.0, 0.0, 1.0)),
  159. hash23(i+vec3(1.0, 0.0, 1.0)),f.x),
  160. mix( hash23(i+vec3(0.0, 1.0, 1.0)),
  161. hash23(i+vec3(1.0, 1.0, 1.0)),f.x),f.y),f.z);
  162. }
  163. vec2 noise22(vec2 p) {
  164. vec2 i = floor(p);
  165. vec2 f = fract(p);
  166. vec2 u = smoothstep(vec2(0.0), vec2(1.0), f);
  167. vec2 val = mix( mix( hash22( i + vec2(0.0, 0.0) ),
  168. hash22( i + vec2(1.0, 0.0) ), u.x),
  169. mix( hash22( i + vec2(0.0, 1.0) ),
  170. hash22( i + vec2(1.0, 1.0) ), u.x), u.y);
  171. return val * 2.0 - 1.0;
  172. }
  173. // The MIT License
  174. // Copyright © 2017 Inigo Quilez
  175. // 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.
  176. // https://www.youtube.com/c/InigoQuilez
  177. // https://iquilezles.org/
  178. vec4 noised_1_3(vec3 x) {
  179. vec3 i = floor(x);
  180. vec3 f = fract(x);
  181. // quintic interpolant
  182. vec3 u = f*f*f*(f*(f*6.0-15.0)+10.0);
  183. vec3 du = 30.0*f*f*(f*(f-2.0)+1.0);
  184. // gradients
  185. vec3 ga = hash33( i+vec3(0.0,0.0,0.0) ) * 2.0 - 1.0;
  186. vec3 gb = hash33( i+vec3(1.0,0.0,0.0) ) * 2.0 - 1.0;
  187. vec3 gc = hash33( i+vec3(0.0,1.0,0.0) ) * 2.0 - 1.0;
  188. vec3 gd = hash33( i+vec3(1.0,1.0,0.0) ) * 2.0 - 1.0;
  189. vec3 ge = hash33( i+vec3(0.0,0.0,1.0) ) * 2.0 - 1.0;
  190. vec3 gf = hash33( i+vec3(1.0,0.0,1.0) ) * 2.0 - 1.0;
  191. vec3 gg = hash33( i+vec3(0.0,1.0,1.0) ) * 2.0 - 1.0;
  192. vec3 gh = hash33( i+vec3(1.0,1.0,1.0) ) * 2.0 - 1.0;
  193. // projections
  194. float va = dot( ga, f-vec3(0.0,0.0,0.0) );
  195. float vb = dot( gb, f-vec3(1.0,0.0,0.0) );
  196. float vc = dot( gc, f-vec3(0.0,1.0,0.0) );
  197. float vd = dot( gd, f-vec3(1.0,1.0,0.0) );
  198. float ve = dot( ge, f-vec3(0.0,0.0,1.0) );
  199. float vf = dot( gf, f-vec3(1.0,0.0,1.0) );
  200. float vg = dot( gg, f-vec3(0.0,1.0,1.0) );
  201. float vh = dot( gh, f-vec3(1.0,1.0,1.0) );
  202. // interpolations
  203. return vec4( 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) + u.z*u.x*(va-vb-ve+vf) + (-va+vb+vc-vd+ve-vf-vg+vh)*u.x*u.y*u.z, // value
  204. 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) + u.z*u.x*(ga-gb-ge+gf) + (-ga+gb+gc-gd+ge-gf-gg+gh)*u.x*u.y*u.z + // derivatives
  205. du * (vec3(vb,vc,ve) - va + u.yzx*vec3(va-vb-vc+vd,va-vc-ve+vg,va-vb-ve+vf) + 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) ));
  206. }
  207. float FBM_1_2(vec2 p, int octaves, float persistence, float lacunarity) {
  208. float amplitude = 1.0;
  209. float frequency = 1.0;
  210. float total = 0.0;
  211. float normalization = 0.0;
  212. for (int i = 0; i < octaves; ++i) {
  213. float noiseValue = noise12(p * frequency);
  214. total += noiseValue * amplitude;
  215. normalization += amplitude;
  216. amplitude *= persistence;
  217. frequency *= lacunarity;
  218. }
  219. total /= normalization;
  220. total = total * 0.5 + 0.5;
  221. return total;
  222. }
  223. float FBM_1_3(vec3 p, int octaves, float persistence, float lacunarity) {
  224. float amplitude = 1.0;
  225. float frequency = 1.0;
  226. float total = 0.0;
  227. float normalization = 0.0;
  228. for (int i = 0; i < octaves; ++i) {
  229. float noiseValue = noise13(p * frequency);
  230. total += noiseValue * amplitude;
  231. normalization += amplitude;
  232. amplitude *= persistence;
  233. frequency *= lacunarity;
  234. }
  235. total /= normalization;
  236. total = total * 0.5 + 0.5;
  237. return total;
  238. }
  239. const mat3 m3 = mat3( 0.00, 0.80, 0.60,
  240. -0.80, 0.36, -0.48,
  241. -0.60, -0.48, 0.64 );
  242. const mat3 m3i = mat3( 0.00, -0.80, -0.60,
  243. 0.80, 0.36, -0.48,
  244. 0.60, -0.48, 0.64 );
  245. vec4 FBM_D_1_4(in vec3 x, int octaves) {
  246. float f = 1.98; // could be 2.0
  247. float s = 0.49; // could be 0.5
  248. float a = 0.0;
  249. float b = 0.5;
  250. vec3 d = vec3(0.0);
  251. mat3 m = mat3(
  252. 1.0,0.0,0.0,
  253. 0.0,1.0,0.0,
  254. 0.0,0.0,1.0);
  255. for( int i=0; i < octaves; i++ )
  256. {
  257. vec4 n = noised_1_3(x);
  258. a += b*n.x; // accumulate values
  259. d += b*m*n.yzw; // accumulate derivatives
  260. b *= s;
  261. x = f*m3*x;
  262. m = f*m3i*m;
  263. }
  264. return vec4( a, d );
  265. }