mx_noise.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. import { glsl } from '../../code/CodeNode.js';
  2. import { glslFn } from '../../code/FunctionNode.js';
  3. // Original shader code from:
  4. // https://github.com/AcademySoftwareFoundation/MaterialX/blob/main/libraries/stdlib/genglsl/lib/mx_noise.glsl
  5. export const mx_noise = glsl( `float mx_select(bool b, float t, float f)
  6. {
  7. return b ? t : f;
  8. }
  9. float mx_negate_if(float val, bool b)
  10. {
  11. return b ? -val : val;
  12. }
  13. int mx_floor(float x)
  14. {
  15. return int(floor(x));
  16. }
  17. // return mx_floor as well as the fractional remainder
  18. float mx_floorfrac(float x, out int i)
  19. {
  20. i = mx_floor(x);
  21. return x - float(i);
  22. }
  23. float mx_bilerp(float v0, float v1, float v2, float v3, float s, float t)
  24. {
  25. float s1 = 1.0 - s;
  26. return (1.0 - t) * (v0*s1 + v1*s) + t * (v2*s1 + v3*s);
  27. }
  28. vec3 mx_bilerp(vec3 v0, vec3 v1, vec3 v2, vec3 v3, float s, float t)
  29. {
  30. float s1 = 1.0 - s;
  31. return (1.0 - t) * (v0*s1 + v1*s) + t * (v2*s1 + v3*s);
  32. }
  33. float mx_trilerp(float v0, float v1, float v2, float v3, float v4, float v5, float v6, float v7, float s, float t, float r)
  34. {
  35. float s1 = 1.0 - s;
  36. float t1 = 1.0 - t;
  37. float r1 = 1.0 - r;
  38. return (r1*(t1*(v0*s1 + v1*s) + t*(v2*s1 + v3*s)) +
  39. r*(t1*(v4*s1 + v5*s) + t*(v6*s1 + v7*s)));
  40. }
  41. vec3 mx_trilerp(vec3 v0, vec3 v1, vec3 v2, vec3 v3, vec3 v4, vec3 v5, vec3 v6, vec3 v7, float s, float t, float r)
  42. {
  43. float s1 = 1.0 - s;
  44. float t1 = 1.0 - t;
  45. float r1 = 1.0 - r;
  46. return (r1*(t1*(v0*s1 + v1*s) + t*(v2*s1 + v3*s)) +
  47. r*(t1*(v4*s1 + v5*s) + t*(v6*s1 + v7*s)));
  48. }
  49. // 2 and 3 dimensional gradient functions - perform a dot product against a
  50. // randomly chosen vector. Note that the gradient vector is not normalized, but
  51. // this only affects the overal "scale" of the result, so we simply account for
  52. // the scale by multiplying in the corresponding "perlin" function.
  53. float mx_gradient_float(uint hash, float x, float y)
  54. {
  55. // 8 possible directions (+-1,+-2) and (+-2,+-1)
  56. uint h = hash & 7u;
  57. float u = mx_select(h<4u, x, y);
  58. float v = 2.0 * mx_select(h<4u, y, x);
  59. // compute the dot product with (x,y).
  60. return mx_negate_if(u, bool(h&1u)) + mx_negate_if(v, bool(h&2u));
  61. }
  62. float mx_gradient_float(uint hash, float x, float y, float z)
  63. {
  64. // use vectors pointing to the edges of the cube
  65. uint h = hash & 15u;
  66. float u = mx_select(h<8u, x, y);
  67. float v = mx_select(h<4u, y, mx_select((h==12u)||(h==14u), x, z));
  68. return mx_negate_if(u, bool(h&1u)) + mx_negate_if(v, bool(h&2u));
  69. }
  70. vec3 mx_gradient_vec3(uvec3 hash, float x, float y)
  71. {
  72. return vec3(mx_gradient_float(hash.x, x, y), mx_gradient_float(hash.y, x, y), mx_gradient_float(hash.z, x, y));
  73. }
  74. vec3 mx_gradient_vec3(uvec3 hash, float x, float y, float z)
  75. {
  76. return vec3(mx_gradient_float(hash.x, x, y, z), mx_gradient_float(hash.y, x, y, z), mx_gradient_float(hash.z, x, y, z));
  77. }
  78. // Scaling factors to normalize the result of gradients above.
  79. // These factors were experimentally calculated to be:
  80. // 2D: 0.6616
  81. // 3D: 0.9820
  82. float mx_gradient_scale2d(float v) { return 0.6616 * v; }
  83. float mx_gradient_scale3d(float v) { return 0.9820 * v; }
  84. vec3 mx_gradient_scale2d(vec3 v) { return 0.6616 * v; }
  85. vec3 mx_gradient_scale3d(vec3 v) { return 0.9820 * v; }
  86. /// Bitwise circular rotation left by k bits (for 32 bit unsigned integers)
  87. uint mx_rotl32(uint x, int k)
  88. {
  89. return (x<<k) | (x>>(32-k));
  90. }
  91. void mx_bjmix(inout uint a, inout uint b, inout uint c)
  92. {
  93. a -= c; a ^= mx_rotl32(c, 4); c += b;
  94. b -= a; b ^= mx_rotl32(a, 6); a += c;
  95. c -= b; c ^= mx_rotl32(b, 8); b += a;
  96. a -= c; a ^= mx_rotl32(c,16); c += b;
  97. b -= a; b ^= mx_rotl32(a,19); a += c;
  98. c -= b; c ^= mx_rotl32(b, 4); b += a;
  99. }
  100. // Mix up and combine the bits of a, b, and c (doesn't change them, but
  101. // returns a hash of those three original values).
  102. uint mx_bjfinal(uint a, uint b, uint c)
  103. {
  104. c ^= b; c -= mx_rotl32(b,14);
  105. a ^= c; a -= mx_rotl32(c,11);
  106. b ^= a; b -= mx_rotl32(a,25);
  107. c ^= b; c -= mx_rotl32(b,16);
  108. a ^= c; a -= mx_rotl32(c,4);
  109. b ^= a; b -= mx_rotl32(a,14);
  110. c ^= b; c -= mx_rotl32(b,24);
  111. return c;
  112. }
  113. // Convert a 32 bit integer into a floating point number in [0,1]
  114. float mx_bits_to_01(uint bits)
  115. {
  116. return float(bits) / float(uint(0xffffffff));
  117. }
  118. float mx_fade(float t)
  119. {
  120. return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
  121. }
  122. uint mx_hash_int(int x)
  123. {
  124. uint len = 1u;
  125. uint seed = uint(0xdeadbeef) + (len << 2u) + 13u;
  126. return mx_bjfinal(seed+uint(x), seed, seed);
  127. }
  128. uint mx_hash_int(int x, int y)
  129. {
  130. uint len = 2u;
  131. uint a, b, c;
  132. a = b = c = uint(0xdeadbeef) + (len << 2u) + 13u;
  133. a += uint(x);
  134. b += uint(y);
  135. return mx_bjfinal(a, b, c);
  136. }
  137. uint mx_hash_int(int x, int y, int z)
  138. {
  139. uint len = 3u;
  140. uint a, b, c;
  141. a = b = c = uint(0xdeadbeef) + (len << 2u) + 13u;
  142. a += uint(x);
  143. b += uint(y);
  144. c += uint(z);
  145. return mx_bjfinal(a, b, c);
  146. }
  147. uint mx_hash_int(int x, int y, int z, int xx)
  148. {
  149. uint len = 4u;
  150. uint a, b, c;
  151. a = b = c = uint(0xdeadbeef) + (len << 2u) + 13u;
  152. a += uint(x);
  153. b += uint(y);
  154. c += uint(z);
  155. mx_bjmix(a, b, c);
  156. a += uint(xx);
  157. return mx_bjfinal(a, b, c);
  158. }
  159. uint mx_hash_int(int x, int y, int z, int xx, int yy)
  160. {
  161. uint len = 5u;
  162. uint a, b, c;
  163. a = b = c = uint(0xdeadbeef) + (len << 2u) + 13u;
  164. a += uint(x);
  165. b += uint(y);
  166. c += uint(z);
  167. mx_bjmix(a, b, c);
  168. a += uint(xx);
  169. b += uint(yy);
  170. return mx_bjfinal(a, b, c);
  171. }
  172. uvec3 mx_hash_vec3(int x, int y)
  173. {
  174. uint h = mx_hash_int(x, y);
  175. // we only need the low-order bits to be random, so split out
  176. // the 32 bit result into 3 parts for each channel
  177. uvec3 result;
  178. result.x = (h ) & 0xFFu;
  179. result.y = (h >> 8 ) & 0xFFu;
  180. result.z = (h >> 16) & 0xFFu;
  181. return result;
  182. }
  183. uvec3 mx_hash_vec3(int x, int y, int z)
  184. {
  185. uint h = mx_hash_int(x, y, z);
  186. // we only need the low-order bits to be random, so split out
  187. // the 32 bit result into 3 parts for each channel
  188. uvec3 result;
  189. result.x = (h ) & 0xFFu;
  190. result.y = (h >> 8 ) & 0xFFu;
  191. result.z = (h >> 16) & 0xFFu;
  192. return result;
  193. }
  194. float mx_perlin_noise_float(vec2 p)
  195. {
  196. int X, Y;
  197. float fx = mx_floorfrac(p.x, X);
  198. float fy = mx_floorfrac(p.y, Y);
  199. float u = mx_fade(fx);
  200. float v = mx_fade(fy);
  201. float result = mx_bilerp(
  202. mx_gradient_float(mx_hash_int(X , Y ), fx , fy ),
  203. mx_gradient_float(mx_hash_int(X+1, Y ), fx-1.0, fy ),
  204. mx_gradient_float(mx_hash_int(X , Y+1), fx , fy-1.0),
  205. mx_gradient_float(mx_hash_int(X+1, Y+1), fx-1.0, fy-1.0),
  206. u, v);
  207. return mx_gradient_scale2d(result);
  208. }
  209. float mx_perlin_noise_float(vec3 p)
  210. {
  211. int X, Y, Z;
  212. float fx = mx_floorfrac(p.x, X);
  213. float fy = mx_floorfrac(p.y, Y);
  214. float fz = mx_floorfrac(p.z, Z);
  215. float u = mx_fade(fx);
  216. float v = mx_fade(fy);
  217. float w = mx_fade(fz);
  218. float result = mx_trilerp(
  219. mx_gradient_float(mx_hash_int(X , Y , Z ), fx , fy , fz ),
  220. mx_gradient_float(mx_hash_int(X+1, Y , Z ), fx-1.0, fy , fz ),
  221. mx_gradient_float(mx_hash_int(X , Y+1, Z ), fx , fy-1.0, fz ),
  222. mx_gradient_float(mx_hash_int(X+1, Y+1, Z ), fx-1.0, fy-1.0, fz ),
  223. mx_gradient_float(mx_hash_int(X , Y , Z+1), fx , fy , fz-1.0),
  224. mx_gradient_float(mx_hash_int(X+1, Y , Z+1), fx-1.0, fy , fz-1.0),
  225. mx_gradient_float(mx_hash_int(X , Y+1, Z+1), fx , fy-1.0, fz-1.0),
  226. mx_gradient_float(mx_hash_int(X+1, Y+1, Z+1), fx-1.0, fy-1.0, fz-1.0),
  227. u, v, w);
  228. return mx_gradient_scale3d(result);
  229. }
  230. vec3 mx_perlin_noise_vec3(vec2 p)
  231. {
  232. int X, Y;
  233. float fx = mx_floorfrac(p.x, X);
  234. float fy = mx_floorfrac(p.y, Y);
  235. float u = mx_fade(fx);
  236. float v = mx_fade(fy);
  237. vec3 result = mx_bilerp(
  238. mx_gradient_vec3(mx_hash_vec3(X , Y ), fx , fy ),
  239. mx_gradient_vec3(mx_hash_vec3(X+1, Y ), fx-1.0, fy ),
  240. mx_gradient_vec3(mx_hash_vec3(X , Y+1), fx , fy-1.0),
  241. mx_gradient_vec3(mx_hash_vec3(X+1, Y+1), fx-1.0, fy-1.0),
  242. u, v);
  243. return mx_gradient_scale2d(result);
  244. }
  245. vec3 mx_perlin_noise_vec3(vec3 p)
  246. {
  247. int X, Y, Z;
  248. float fx = mx_floorfrac(p.x, X);
  249. float fy = mx_floorfrac(p.y, Y);
  250. float fz = mx_floorfrac(p.z, Z);
  251. float u = mx_fade(fx);
  252. float v = mx_fade(fy);
  253. float w = mx_fade(fz);
  254. vec3 result = mx_trilerp(
  255. mx_gradient_vec3(mx_hash_vec3(X , Y , Z ), fx , fy , fz ),
  256. mx_gradient_vec3(mx_hash_vec3(X+1, Y , Z ), fx-1.0, fy , fz ),
  257. mx_gradient_vec3(mx_hash_vec3(X , Y+1, Z ), fx , fy-1.0, fz ),
  258. mx_gradient_vec3(mx_hash_vec3(X+1, Y+1, Z ), fx-1.0, fy-1.0, fz ),
  259. mx_gradient_vec3(mx_hash_vec3(X , Y , Z+1), fx , fy , fz-1.0),
  260. mx_gradient_vec3(mx_hash_vec3(X+1, Y , Z+1), fx-1.0, fy , fz-1.0),
  261. mx_gradient_vec3(mx_hash_vec3(X , Y+1, Z+1), fx , fy-1.0, fz-1.0),
  262. mx_gradient_vec3(mx_hash_vec3(X+1, Y+1, Z+1), fx-1.0, fy-1.0, fz-1.0),
  263. u, v, w);
  264. return mx_gradient_scale3d(result);
  265. }
  266. float mx_cell_noise_float(float p)
  267. {
  268. int ix = mx_floor(p);
  269. return mx_bits_to_01(mx_hash_int(ix));
  270. }
  271. float mx_cell_noise_float(vec2 p)
  272. {
  273. int ix = mx_floor(p.x);
  274. int iy = mx_floor(p.y);
  275. return mx_bits_to_01(mx_hash_int(ix, iy));
  276. }
  277. float mx_cell_noise_float(vec3 p)
  278. {
  279. int ix = mx_floor(p.x);
  280. int iy = mx_floor(p.y);
  281. int iz = mx_floor(p.z);
  282. return mx_bits_to_01(mx_hash_int(ix, iy, iz));
  283. }
  284. float mx_cell_noise_float(vec4 p)
  285. {
  286. int ix = mx_floor(p.x);
  287. int iy = mx_floor(p.y);
  288. int iz = mx_floor(p.z);
  289. int iw = mx_floor(p.w);
  290. return mx_bits_to_01(mx_hash_int(ix, iy, iz, iw));
  291. }
  292. vec3 mx_cell_noise_vec3(float p)
  293. {
  294. int ix = mx_floor(p);
  295. return vec3(
  296. mx_bits_to_01(mx_hash_int(ix, 0)),
  297. mx_bits_to_01(mx_hash_int(ix, 1)),
  298. mx_bits_to_01(mx_hash_int(ix, 2))
  299. );
  300. }
  301. vec3 mx_cell_noise_vec3(vec2 p)
  302. {
  303. int ix = mx_floor(p.x);
  304. int iy = mx_floor(p.y);
  305. return vec3(
  306. mx_bits_to_01(mx_hash_int(ix, iy, 0)),
  307. mx_bits_to_01(mx_hash_int(ix, iy, 1)),
  308. mx_bits_to_01(mx_hash_int(ix, iy, 2))
  309. );
  310. }
  311. vec3 mx_cell_noise_vec3(vec3 p)
  312. {
  313. int ix = mx_floor(p.x);
  314. int iy = mx_floor(p.y);
  315. int iz = mx_floor(p.z);
  316. return vec3(
  317. mx_bits_to_01(mx_hash_int(ix, iy, iz, 0)),
  318. mx_bits_to_01(mx_hash_int(ix, iy, iz, 1)),
  319. mx_bits_to_01(mx_hash_int(ix, iy, iz, 2))
  320. );
  321. }
  322. vec3 mx_cell_noise_vec3(vec4 p)
  323. {
  324. int ix = mx_floor(p.x);
  325. int iy = mx_floor(p.y);
  326. int iz = mx_floor(p.z);
  327. int iw = mx_floor(p.w);
  328. return vec3(
  329. mx_bits_to_01(mx_hash_int(ix, iy, iz, iw, 0)),
  330. mx_bits_to_01(mx_hash_int(ix, iy, iz, iw, 1)),
  331. mx_bits_to_01(mx_hash_int(ix, iy, iz, iw, 2))
  332. );
  333. }
  334. float mx_fractal_noise_float(vec3 p, int octaves, float lacunarity, float diminish)
  335. {
  336. float result = 0.0;
  337. float amplitude = 1.0;
  338. for (int i = 0; i < octaves; ++i)
  339. {
  340. result += amplitude * mx_perlin_noise_float(p);
  341. amplitude *= diminish;
  342. p *= lacunarity;
  343. }
  344. return result;
  345. }
  346. vec3 mx_fractal_noise_vec3(vec3 p, int octaves, float lacunarity, float diminish)
  347. {
  348. vec3 result = vec3(0.0);
  349. float amplitude = 1.0;
  350. for (int i = 0; i < octaves; ++i)
  351. {
  352. result += amplitude * mx_perlin_noise_vec3(p);
  353. amplitude *= diminish;
  354. p *= lacunarity;
  355. }
  356. return result;
  357. }
  358. vec2 mx_fractal_noise_vec2(vec3 p, int octaves, float lacunarity, float diminish)
  359. {
  360. return vec2(mx_fractal_noise_float(p, octaves, lacunarity, diminish),
  361. mx_fractal_noise_float(p+vec3(19, 193, 17), octaves, lacunarity, diminish));
  362. }
  363. vec4 mx_fractal_noise_vec4(vec3 p, int octaves, float lacunarity, float diminish)
  364. {
  365. vec3 c = mx_fractal_noise_vec3(p, octaves, lacunarity, diminish);
  366. float f = mx_fractal_noise_float(p+vec3(19, 193, 17), octaves, lacunarity, diminish);
  367. return vec4(c, f);
  368. }
  369. float mx_worley_distance(vec2 p, int x, int y, int xoff, int yoff, float jitter, int metric)
  370. {
  371. vec3 tmp = mx_cell_noise_vec3(vec2(x+xoff, y+yoff));
  372. vec2 off = vec2(tmp.x, tmp.y);
  373. off -= 0.5f;
  374. off *= jitter;
  375. off += 0.5f;
  376. vec2 cellpos = vec2(float(x), float(y)) + off;
  377. vec2 diff = cellpos - p;
  378. if (metric == 2)
  379. return abs(diff.x) + abs(diff.y); // Manhattan distance
  380. if (metric == 3)
  381. return max(abs(diff.x), abs(diff.y)); // Chebyshev distance
  382. // Either Euclidian or Distance^2
  383. return dot(diff, diff);
  384. }
  385. float mx_worley_distance(vec3 p, int x, int y, int z, int xoff, int yoff, int zoff, float jitter, int metric)
  386. {
  387. vec3 off = mx_cell_noise_vec3(vec3(x+xoff, y+yoff, z+zoff));
  388. off -= 0.5f;
  389. off *= jitter;
  390. off += 0.5f;
  391. vec3 cellpos = vec3(float(x), float(y), float(z)) + off;
  392. vec3 diff = cellpos - p;
  393. if (metric == 2)
  394. return abs(diff.x) + abs(diff.y) + abs(diff.z); // Manhattan distance
  395. if (metric == 3)
  396. return max(max(abs(diff.x), abs(diff.y)), abs(diff.z)); // Chebyshev distance
  397. // Either Euclidian or Distance^2
  398. return dot(diff, diff);
  399. }
  400. float mx_worley_noise_float(vec2 p, float jitter, int metric)
  401. {
  402. int X, Y;
  403. vec2 localpos = vec2(mx_floorfrac(p.x, X), mx_floorfrac(p.y, Y));
  404. float sqdist = 1e6f; // Some big number for jitter > 1 (not all GPUs may be IEEE)
  405. for (int x = -1; x <= 1; ++x)
  406. {
  407. for (int y = -1; y <= 1; ++y)
  408. {
  409. float dist = mx_worley_distance(localpos, x, y, X, Y, jitter, metric);
  410. sqdist = min(sqdist, dist);
  411. }
  412. }
  413. if (metric == 0)
  414. sqdist = sqrt(sqdist);
  415. return sqdist;
  416. }
  417. vec2 mx_worley_noise_vec2(vec2 p, float jitter, int metric)
  418. {
  419. int X, Y;
  420. vec2 localpos = vec2(mx_floorfrac(p.x, X), mx_floorfrac(p.y, Y));
  421. vec2 sqdist = vec2(1e6f, 1e6f);
  422. for (int x = -1; x <= 1; ++x)
  423. {
  424. for (int y = -1; y <= 1; ++y)
  425. {
  426. float dist = mx_worley_distance(localpos, x, y, X, Y, jitter, metric);
  427. if (dist < sqdist.x)
  428. {
  429. sqdist.y = sqdist.x;
  430. sqdist.x = dist;
  431. }
  432. else if (dist < sqdist.y)
  433. {
  434. sqdist.y = dist;
  435. }
  436. }
  437. }
  438. if (metric == 0)
  439. sqdist = sqrt(sqdist);
  440. return sqdist;
  441. }
  442. vec3 mx_worley_noise_vec3(vec2 p, float jitter, int metric)
  443. {
  444. int X, Y;
  445. vec2 localpos = vec2(mx_floorfrac(p.x, X), mx_floorfrac(p.y, Y));
  446. vec3 sqdist = vec3(1e6f, 1e6f, 1e6f);
  447. for (int x = -1; x <= 1; ++x)
  448. {
  449. for (int y = -1; y <= 1; ++y)
  450. {
  451. float dist = mx_worley_distance(localpos, x, y, X, Y, jitter, metric);
  452. if (dist < sqdist.x)
  453. {
  454. sqdist.z = sqdist.y;
  455. sqdist.y = sqdist.x;
  456. sqdist.x = dist;
  457. }
  458. else if (dist < sqdist.y)
  459. {
  460. sqdist.z = sqdist.y;
  461. sqdist.y = dist;
  462. }
  463. else if (dist < sqdist.z)
  464. {
  465. sqdist.z = dist;
  466. }
  467. }
  468. }
  469. if (metric == 0)
  470. sqdist = sqrt(sqdist);
  471. return sqdist;
  472. }
  473. float mx_worley_noise_float(vec3 p, float jitter, int metric)
  474. {
  475. int X, Y, Z;
  476. vec3 localpos = vec3(mx_floorfrac(p.x, X), mx_floorfrac(p.y, Y), mx_floorfrac(p.z, Z));
  477. float sqdist = 1e6f;
  478. for (int x = -1; x <= 1; ++x)
  479. {
  480. for (int y = -1; y <= 1; ++y)
  481. {
  482. for (int z = -1; z <= 1; ++z)
  483. {
  484. float dist = mx_worley_distance(localpos, x, y, z, X, Y, Z, jitter, metric);
  485. sqdist = min(sqdist, dist);
  486. }
  487. }
  488. }
  489. if (metric == 0)
  490. sqdist = sqrt(sqdist);
  491. return sqdist;
  492. }
  493. vec2 mx_worley_noise_vec2(vec3 p, float jitter, int metric)
  494. {
  495. int X, Y, Z;
  496. vec3 localpos = vec3(mx_floorfrac(p.x, X), mx_floorfrac(p.y, Y), mx_floorfrac(p.z, Z));
  497. vec2 sqdist = vec2(1e6f, 1e6f);
  498. for (int x = -1; x <= 1; ++x)
  499. {
  500. for (int y = -1; y <= 1; ++y)
  501. {
  502. for (int z = -1; z <= 1; ++z)
  503. {
  504. float dist = mx_worley_distance(localpos, x, y, z, X, Y, Z, jitter, metric);
  505. if (dist < sqdist.x)
  506. {
  507. sqdist.y = sqdist.x;
  508. sqdist.x = dist;
  509. }
  510. else if (dist < sqdist.y)
  511. {
  512. sqdist.y = dist;
  513. }
  514. }
  515. }
  516. }
  517. if (metric == 0)
  518. sqdist = sqrt(sqdist);
  519. return sqdist;
  520. }
  521. vec3 mx_worley_noise_vec3(vec3 p, float jitter, int metric)
  522. {
  523. int X, Y, Z;
  524. vec3 localpos = vec3(mx_floorfrac(p.x, X), mx_floorfrac(p.y, Y), mx_floorfrac(p.z, Z));
  525. vec3 sqdist = vec3(1e6f, 1e6f, 1e6f);
  526. for (int x = -1; x <= 1; ++x)
  527. {
  528. for (int y = -1; y <= 1; ++y)
  529. {
  530. for (int z = -1; z <= 1; ++z)
  531. {
  532. float dist = mx_worley_distance(localpos, x, y, z, X, Y, Z, jitter, metric);
  533. if (dist < sqdist.x)
  534. {
  535. sqdist.z = sqdist.y;
  536. sqdist.y = sqdist.x;
  537. sqdist.x = dist;
  538. }
  539. else if (dist < sqdist.y)
  540. {
  541. sqdist.z = sqdist.y;
  542. sqdist.y = dist;
  543. }
  544. else if (dist < sqdist.z)
  545. {
  546. sqdist.z = dist;
  547. }
  548. }
  549. }
  550. }
  551. if (metric == 0)
  552. sqdist = sqrt(sqdist);
  553. return sqdist;
  554. }` );
  555. const includes = [ mx_noise ];
  556. export const mx_perlin_noise_float = glslFn( 'float mx_perlin_noise_float( any p )', includes );
  557. export const mx_perlin_noise_vec2 = glslFn( 'vec2 mx_perlin_noise_vec2( any p )', includes );
  558. export const mx_perlin_noise_vec3 = glslFn( 'vec3 mx_perlin_noise_vec3( any p )', includes );
  559. export const mx_cell_noise_float = glslFn( 'float mx_cell_noise_float( vec3 p )', includes );
  560. export const mx_worley_noise_float = glslFn( 'float mx_worley_noise_float( any p, float jitter, int metric )', includes );
  561. export const mx_worley_noise_vec2 = glslFn( 'float mx_worley_noise_vec2( any p, float jitter, int metric )', includes );
  562. export const mx_worley_noise_vec3 = glslFn( 'float mx_worley_noise_vec3( any p, float jitter, int metric )', includes );
  563. export const mx_fractal_noise_float = glslFn( 'float mx_fractal_noise_float( vec3 p, int octaves, float lacunarity, float diminish )', includes );
  564. export const mx_fractal_noise_vec2 = glslFn( 'float mx_fractal_noise_vec2( vec3 p, int octaves, float lacunarity, float diminish )', includes );
  565. export const mx_fractal_noise_vec3 = glslFn( 'float mx_fractal_noise_vec3( vec3 p, int octaves, float lacunarity, float diminish )', includes );
  566. export const mx_fractal_noise_vec4 = glslFn( 'float mx_fractal_noise_vec4( vec3 p, int octaves, float lacunarity, float diminish )', includes );