mx_noise.js 19 KB

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