@thi.ng/shader-ast-stdlib 0.16.9 → 0.16.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +1 -1
  3. package/api.js +0 -1
  4. package/color/aces-film.js +12 -10
  5. package/color/levels.js +80 -66
  6. package/color/linear-srgb.js +11 -21
  7. package/color/luminance.js +4 -6
  8. package/color/porter-duff.js +51 -55
  9. package/color/rgbe.js +13 -15
  10. package/fog/exp.js +5 -9
  11. package/fog/exp2.js +6 -12
  12. package/fog/linear.js +5 -11
  13. package/isec/point.js +19 -13
  14. package/light/lambert.js +8 -29
  15. package/light/trilight.js +17 -15
  16. package/math/additive.js +27 -32
  17. package/math/cartesian.js +21 -24
  18. package/math/clamp.js +7 -19
  19. package/math/cross2.js +7 -19
  20. package/math/dist-chebyshev.js +8 -3
  21. package/math/dist-manhattan.js +8 -3
  22. package/math/easing.js +325 -88
  23. package/math/fit.js +17 -65
  24. package/math/interval.js +23 -24
  25. package/math/magsq.js +8 -3
  26. package/math/maxcomp.js +8 -18
  27. package/math/mincomp.js +8 -18
  28. package/math/mix-cubic.js +32 -10
  29. package/math/mix-quadratic.js +27 -8
  30. package/math/orthogonal.js +15 -21
  31. package/math/osc.js +28 -39
  32. package/math/polar.js +12 -21
  33. package/math/sincos.js +6 -12
  34. package/math/smoother-step.js +26 -24
  35. package/matrix/convert.js +19 -8
  36. package/matrix/lookat.js +20 -18
  37. package/matrix/mvp.js +4 -10
  38. package/matrix/normal.js +4 -8
  39. package/matrix/rotation.js +131 -47
  40. package/noise/curl3.js +30 -18
  41. package/noise/hash.js +126 -197
  42. package/noise/permute.js +11 -5
  43. package/noise/simplex2.js +87 -41
  44. package/noise/simplex3.js +168 -75
  45. package/noise/voronoi2.js +67 -50
  46. package/noise/worley2.js +70 -58
  47. package/package.json +8 -5
  48. package/raymarch/ao.js +25 -24
  49. package/raymarch/direction.js +16 -9
  50. package/raymarch/normal.js +10 -16
  51. package/raymarch/point-at.js +4 -11
  52. package/raymarch/scene.js +35 -44
  53. package/screen/uv.js +23 -30
  54. package/sdf/annular.js +4 -7
  55. package/sdf/arc.js +20 -19
  56. package/sdf/bezier.js +93 -57
  57. package/sdf/box-rounded.js +14 -18
  58. package/sdf/box.js +16 -24
  59. package/sdf/cross.js +23 -44
  60. package/sdf/cylinder.js +9 -14
  61. package/sdf/hex.js +22 -19
  62. package/sdf/isec.js +4 -8
  63. package/sdf/line.js +14 -29
  64. package/sdf/mirror.js +17 -13
  65. package/sdf/plane.js +8 -18
  66. package/sdf/polygon.js +51 -29
  67. package/sdf/polyhedra.js +78 -43
  68. package/sdf/polyline.js +30 -7
  69. package/sdf/repeat-polar.js +13 -17
  70. package/sdf/repeat.js +7 -15
  71. package/sdf/round.js +4 -10
  72. package/sdf/smooth-isec.js +14 -17
  73. package/sdf/smooth-sub.js +14 -17
  74. package/sdf/smooth-union.js +14 -17
  75. package/sdf/sphere.js +8 -16
  76. package/sdf/sub.js +4 -8
  77. package/sdf/torus.js +5 -10
  78. package/sdf/tri.js +30 -20
  79. package/sdf/union.js +7 -18
  80. package/tex/blur.js +53 -70
  81. package/tex/index-coord.js +6 -16
  82. package/tex/index-uv.js +30 -20
  83. package/tex/read-index.js +10 -32
  84. package/viz/function.js +70 -68
@@ -6,23 +6,20 @@ import { sym } from "@thi.ng/shader-ast/ast/sym";
6
6
  import { mix } from "@thi.ng/shader-ast/builtin/math";
7
7
  import { clamp01 } from "../math/clamp.js";
8
8
  import { fit1101 } from "../math/fit.js";
9
- /**
10
- * @param d1 - float
11
- * @param d2 - float
12
- * @param k - float
13
- */
14
- export const sdfSmoothIntersect = defn(F, "sdOpSmoothIntersect", [F, F, F], (a, b, k) => {
9
+ const sdfSmoothIntersect = defn(
10
+ F,
11
+ "sdOpSmoothIntersect",
12
+ [F, F, F],
13
+ (a, b, k) => {
15
14
  let h;
16
15
  return [
17
- (h = sym(clamp01(fit1101(div(sub(b, a), k))))),
18
- ret(add(mix(b, a, h), mul(mul(k, h), sub(FLOAT1, h)))),
16
+ h = sym(clamp01(fit1101(div(sub(b, a), k)))),
17
+ ret(add(mix(b, a, h), mul(mul(k, h), sub(FLOAT1, h))))
19
18
  ];
20
- });
21
- /**
22
- * Variadic compiletime macro for {@link sdfSmoothIntersect}. Takes smooth factor
23
- * `k`, followed by any number (at least 1 required) of SDF terms.
24
- *
25
- * @param k -
26
- * @param terms -
27
- */
28
- export const sdfSmoothIntersectAll = (k, ...terms) => terms.reduce((acc, x) => sdfSmoothIntersect(acc, x, k));
19
+ }
20
+ );
21
+ const sdfSmoothIntersectAll = (k, ...terms) => terms.reduce((acc, x) => sdfSmoothIntersect(acc, x, k));
22
+ export {
23
+ sdfSmoothIntersect,
24
+ sdfSmoothIntersectAll
25
+ };
package/sdf/smooth-sub.js CHANGED
@@ -6,23 +6,20 @@ import { sym } from "@thi.ng/shader-ast/ast/sym";
6
6
  import { mix } from "@thi.ng/shader-ast/builtin/math";
7
7
  import { clamp01 } from "../math/clamp.js";
8
8
  import { fit1101 } from "../math/fit.js";
9
- /**
10
- * @param d1 - float
11
- * @param d2 - float
12
- * @param k - float
13
- */
14
- export const sdfSmoothSubtract = defn(F, "sdOpSmoothSubtract", [F, F, F], (a, b, k) => {
9
+ const sdfSmoothSubtract = defn(
10
+ F,
11
+ "sdOpSmoothSubtract",
12
+ [F, F, F],
13
+ (a, b, k) => {
15
14
  let h;
16
15
  return [
17
- (h = sym(clamp01(fit1101(div(add(b, a), k))))),
18
- ret(add(mix(b, neg(a), h), mul(mul(k, h), sub(FLOAT1, h)))),
16
+ h = sym(clamp01(fit1101(div(add(b, a), k)))),
17
+ ret(add(mix(b, neg(a), h), mul(mul(k, h), sub(FLOAT1, h))))
19
18
  ];
20
- });
21
- /**
22
- * Variadic compiletime macro for {@link sdfSmoothSubtract}. Takes smooth factor
23
- * `k`, followed by any number (at least 1 required) of SDF terms.
24
- *
25
- * @param k -
26
- * @param terms -
27
- */
28
- export const sdfSmoothSubtractAll = (k, ...terms) => terms.reduce((acc, x) => sdfSmoothSubtract(acc, x, k));
19
+ }
20
+ );
21
+ const sdfSmoothSubtractAll = (k, ...terms) => terms.reduce((acc, x) => sdfSmoothSubtract(acc, x, k));
22
+ export {
23
+ sdfSmoothSubtract,
24
+ sdfSmoothSubtractAll
25
+ };
@@ -6,23 +6,20 @@ import { sym } from "@thi.ng/shader-ast/ast/sym";
6
6
  import { mix } from "@thi.ng/shader-ast/builtin/math";
7
7
  import { clamp01 } from "../math/clamp.js";
8
8
  import { fit1101 } from "../math/fit.js";
9
- /**
10
- * @param d1 - float
11
- * @param d2 - float
12
- * @param k - float
13
- */
14
- export const sdfSmoothUnion = defn(F, "sdOpSmoothUnion", [F, F, F], (a, b, k) => {
9
+ const sdfSmoothUnion = defn(
10
+ F,
11
+ "sdOpSmoothUnion",
12
+ [F, F, F],
13
+ (a, b, k) => {
15
14
  let h;
16
15
  return [
17
- (h = sym(clamp01(fit1101(div(sub(b, a), k))))),
18
- ret(sub(mix(b, a, h), mul(mul(k, h), sub(FLOAT1, h)))),
16
+ h = sym(clamp01(fit1101(div(sub(b, a), k)))),
17
+ ret(sub(mix(b, a, h), mul(mul(k, h), sub(FLOAT1, h))))
19
18
  ];
20
- });
21
- /**
22
- * Variadic compiletime macro for {@link sdfSmoothUnion}. Takes smooth factor
23
- * `k`, followed by any number (at least 1 required) of SDF terms.
24
- *
25
- * @param k -
26
- * @param terms -
27
- */
28
- export const sdfSmoothUnionAll = (k, ...terms) => terms.reduce((acc, x) => sdfSmoothUnion(acc, x, k));
19
+ }
20
+ );
21
+ const sdfSmoothUnionAll = (k, ...terms) => terms.reduce((acc, x) => sdfSmoothUnion(acc, x, k));
22
+ export {
23
+ sdfSmoothUnion,
24
+ sdfSmoothUnionAll
25
+ };
package/sdf/sphere.js CHANGED
@@ -2,21 +2,13 @@ import { F, V2, V3 } from "@thi.ng/shader-ast/api/types";
2
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
3
  import { sub } from "@thi.ng/shader-ast/ast/ops";
4
4
  import { length } from "@thi.ng/shader-ast/builtin/math";
5
- /**
6
- * Returns signed distance from `p` to centered circle of radius `r`.
7
- *
8
- * @param p - vec2
9
- * @param r - float
10
- */
11
- export const sdfCircle = defn(F, "sdCircle", [V2, F], (p, r) => [
12
- ret(sub(length(p), r)),
5
+ const sdfCircle = defn(F, "sdCircle", [V2, F], (p, r) => [
6
+ ret(sub(length(p), r))
13
7
  ]);
14
- /**
15
- * Returns signed distance from `p` to centered sphere of radius `r`.
16
- *
17
- * @param p - vec3
18
- * @param r - float
19
- */
20
- export const sdfSphere = defn(F, "sdSphere", [V3, F], (p, r) => [
21
- ret(sub(length(p), r)),
8
+ const sdfSphere = defn(F, "sdSphere", [V3, F], (p, r) => [
9
+ ret(sub(length(p), r))
22
10
  ]);
11
+ export {
12
+ sdfCircle,
13
+ sdfSphere
14
+ };
package/sdf/sub.js CHANGED
@@ -1,10 +1,6 @@
1
1
  import { neg } from "@thi.ng/shader-ast/ast/ops";
2
2
  import { max } from "@thi.ng/shader-ast/builtin/math";
3
- /**
4
- * Inline function. Variadic SDF shape subtraction (a - b) for any number of
5
- * terms (at least 1 required).
6
- *
7
- * @param a -
8
- * @param terms -
9
- */
10
- export const sdfSubtract = (a, ...terms) => terms.reduce((a, b) => max(a, neg(b)), a);
3
+ const sdfSubtract = (a, ...terms) => terms.reduce((a2, b) => max(a2, neg(b)), a);
4
+ export {
5
+ sdfSubtract
6
+ };
package/sdf/torus.js CHANGED
@@ -4,14 +4,9 @@ import { vec2 } from "@thi.ng/shader-ast/ast/lit";
4
4
  import { sub } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { $, $y } from "@thi.ng/shader-ast/ast/swizzle";
6
6
  import { length } from "@thi.ng/shader-ast/builtin/math";
7
- /**
8
- * Returns signed distance from `p` to torus centered around Y-axis with
9
- * radii `r1`, `r2`.
10
- *
11
- * @param p - vec3
12
- * @param r1 - float
13
- * @param r2 - float
14
- */
15
- export const sdfTorus = defn(F, "sdTorus", [V3, F, F], (p, r1, r2) => [
16
- ret(sub(length(vec2(sub(length($(p, "xz")), r2), $y(p))), r1)),
7
+ const sdfTorus = defn(F, "sdTorus", [V3, F, F], (p, r1, r2) => [
8
+ ret(sub(length(vec2(sub(length($(p, "xz")), r2), $y(p))), r1))
17
9
  ]);
10
+ export {
11
+ sdfTorus
12
+ };
package/sdf/tri.js CHANGED
@@ -7,13 +7,11 @@ import { sym } from "@thi.ng/shader-ast/ast/sym";
7
7
  import { dot, min, sign, sqrt } from "@thi.ng/shader-ast/builtin/math";
8
8
  import { clamp01 } from "../math/clamp.js";
9
9
  import { cross2 } from "../math/cross2.js";
10
- /**
11
- * @param p - vec2
12
- * @param a - vec2
13
- * @param b - vec2
14
- * @param c - vec2
15
- */
16
- export const sdfTriangle2 = defn(F, "sdTriangle", [V2, V2, V2, V2], (p, a, b, c) => {
10
+ const sdfTriangle2 = defn(
11
+ F,
12
+ "sdTriangle",
13
+ [V2, V2, V2, V2],
14
+ (p, a, b, c) => {
17
15
  let e0, e1, e2;
18
16
  let v0, v1, v2;
19
17
  let pq0, pq1, pq2;
@@ -21,17 +19,29 @@ export const sdfTriangle2 = defn(F, "sdTriangle", [V2, V2, V2, V2], (p, a, b, c)
21
19
  let d;
22
20
  const $pq = (v, e) => sub(v, mul(e, clamp01(div(dot(v, e), dot(e, e)))));
23
21
  return [
24
- (e0 = sym(sub(b, a))),
25
- (e1 = sym(sub(c, b))),
26
- (e2 = sym(sub(a, c))),
27
- (v0 = sym(sub(p, a))),
28
- (v1 = sym(sub(p, b))),
29
- (v2 = sym(sub(p, c))),
30
- (pq0 = sym($pq(v0, e0))),
31
- (pq1 = sym($pq(v1, e1))),
32
- (pq2 = sym($pq(v2, e2))),
33
- (s = sym(sign(cross2(e0, e2)))),
34
- (d = sym(min(min(vec2(dot(pq0, pq0), mul(s, cross2(v0, e0))), vec2(dot(pq1, pq1), mul(s, cross2(v1, e1)))), vec2(dot(pq2, pq2), mul(s, cross2(v2, e2)))))),
35
- ret(mul(neg(sqrt($x(d))), sign($y(d)))),
22
+ e0 = sym(sub(b, a)),
23
+ e1 = sym(sub(c, b)),
24
+ e2 = sym(sub(a, c)),
25
+ v0 = sym(sub(p, a)),
26
+ v1 = sym(sub(p, b)),
27
+ v2 = sym(sub(p, c)),
28
+ pq0 = sym($pq(v0, e0)),
29
+ pq1 = sym($pq(v1, e1)),
30
+ pq2 = sym($pq(v2, e2)),
31
+ s = sym(sign(cross2(e0, e2))),
32
+ d = sym(
33
+ min(
34
+ min(
35
+ vec2(dot(pq0, pq0), mul(s, cross2(v0, e0))),
36
+ vec2(dot(pq1, pq1), mul(s, cross2(v1, e1)))
37
+ ),
38
+ vec2(dot(pq2, pq2), mul(s, cross2(v2, e2)))
39
+ )
40
+ ),
41
+ ret(mul(neg(sqrt($x(d))), sign($y(d))))
36
42
  ];
37
- });
43
+ }
44
+ );
45
+ export {
46
+ sdfTriangle2
47
+ };
package/sdf/union.js CHANGED
@@ -4,22 +4,11 @@ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
4
4
  import { lt } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { $x } from "@thi.ng/shader-ast/ast/swizzle";
6
6
  import { min } from "@thi.ng/shader-ast/builtin/math";
7
- /**
8
- * Inline function. Variadic SDF shape union (a || b) for any number of terms
9
- * (at least 1 required).
10
- *
11
- * @param a -
12
- * @param terms -
13
- */
14
- export const sdfUnion = (a, ...terms) => terms.reduce((a, b) => min(a, b), a);
15
- /**
16
- * SDF shape union for vec2 terms, i.e. the common form where the X coord
17
- * defines distance and Y an object or material ID. Returns `a` or `b`,
18
- * depending whose x-coord (dist) is smaller.
19
- *
20
- * @param a -
21
- * @param b -
22
- */
23
- export const sdfUnion2 = defn(V2, "sdfUnion2", [V2, V2], (a, b) => [
24
- ret(ternary(lt($x(a), $x(b)), a, b)),
7
+ const sdfUnion = (a, ...terms) => terms.reduce((a2, b) => min(a2, b), a);
8
+ const sdfUnion2 = defn(V2, "sdfUnion2", [V2, V2], (a, b) => [
9
+ ret(ternary(lt($x(a), $x(b)), a, b))
25
10
  ]);
11
+ export {
12
+ sdfUnion,
13
+ sdfUnion2
14
+ };
package/tex/blur.js CHANGED
@@ -5,52 +5,35 @@ import { vec2 } from "@thi.ng/shader-ast/ast/lit";
5
5
  import { add, div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
6
6
  import { sym } from "@thi.ng/shader-ast/ast/sym";
7
7
  import { texture } from "@thi.ng/shader-ast/builtin/texture";
8
- /**
9
- * Inline function. Computes single blur step for given +/- offset &
10
- * weight.
11
- *
12
- * @param col -
13
- * @param tex -
14
- * @param uv -
15
- * @param off -
16
- * @param k -
17
- */
18
- const singlePass = (col, tex, uv, off, k) => assign(col, add(col, mul(add(texture(tex, add(uv, off)), texture(tex, sub(uv, off))), k)));
19
- /**
20
- * 5x5 gaussian blur texture lookup, optimized, but based on:
21
- *
22
- * - http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/
23
- * - https://github.com/Jam3/glsl-fast-gaussian-blur
24
- *
25
- * @param tex - sampler2D
26
- * @param res - resolution
27
- * @param uv -
28
- * @param dir - blur pass direction (`vec2(1,0)` or `vec2(0,1)`)
29
- */
30
- export const blur5 = defn(V4, "blur5", ["sampler2D", V2, V2, V2], (tex, res, uv, dir) => {
8
+ const singlePass = (col, tex, uv, off, k) => assign(
9
+ col,
10
+ add(
11
+ col,
12
+ mul(add(texture(tex, add(uv, off)), texture(tex, sub(uv, off))), k)
13
+ )
14
+ );
15
+ const blur5 = defn(
16
+ V4,
17
+ "blur5",
18
+ ["sampler2D", V2, V2, V2],
19
+ (tex, res, uv, dir) => {
31
20
  let col;
32
21
  let off;
33
22
  const k1 = 0.29411764705882354;
34
23
  const k2 = 0.35294117647058826;
35
24
  return [
36
- (off = sym(div(mul(vec2(1 + 1 / 3), dir), res))),
37
- (col = sym(mul(texture(tex, uv), k1))),
38
- singlePass(col, tex, uv, off, k2),
39
- ret(col),
25
+ off = sym(div(mul(vec2(1 + 1 / 3), dir), res)),
26
+ col = sym(mul(texture(tex, uv), k1)),
27
+ singlePass(col, tex, uv, off, k2),
28
+ ret(col)
40
29
  ];
41
- });
42
- /**
43
- * 9x9 gaussian blur texture lookup, optimized, but based on:
44
- *
45
- * - http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/
46
- * - https://github.com/Jam3/glsl-fast-gaussian-blur
47
- *
48
- * @param tex - sampler2D
49
- * @param res - resolution
50
- * @param uv -
51
- * @param dir - blur pass direction (`vec2(1,0)` or `vec2(0,1)`)
52
- */
53
- export const blur9 = defn(V4, "blur9", ["sampler2D", V2, V2, V2], (tex, res, uv, dir) => {
30
+ }
31
+ );
32
+ const blur9 = defn(
33
+ V4,
34
+ "blur9",
35
+ ["sampler2D", V2, V2, V2],
36
+ (tex, res, uv, dir) => {
54
37
  let col;
55
38
  let off;
56
39
  let off2;
@@ -58,27 +41,21 @@ export const blur9 = defn(V4, "blur9", ["sampler2D", V2, V2, V2], (tex, res, uv,
58
41
  const k1 = 0.3162162162;
59
42
  const k2 = 0.0702702703;
60
43
  return [
61
- (delta = sym(div(dir, res))),
62
- (off = sym(mul(delta, 1.3846153846))),
63
- (off2 = sym(mul(delta, 3.2307692308))),
64
- (col = sym(mul(texture(tex, uv), 0.227027027))),
65
- singlePass(col, tex, uv, off, k1),
66
- singlePass(col, tex, uv, off2, k2),
67
- ret(col),
44
+ delta = sym(div(dir, res)),
45
+ off = sym(mul(delta, 1.3846153846)),
46
+ off2 = sym(mul(delta, 3.2307692308)),
47
+ col = sym(mul(texture(tex, uv), 0.227027027)),
48
+ singlePass(col, tex, uv, off, k1),
49
+ singlePass(col, tex, uv, off2, k2),
50
+ ret(col)
68
51
  ];
69
- });
70
- /**
71
- * 13x13 gaussian blur texture lookup, optimized, but based on:
72
- *
73
- * - http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/
74
- * - https://github.com/Jam3/glsl-fast-gaussian-blur
75
- *
76
- * @param tex - sampler2D
77
- * @param res - resolution
78
- * @param uv -
79
- * @param dir - blur pass direction (`vec2(1,0)` or `vec2(0,1)`)
80
- */
81
- export const blur13 = defn(V4, "blur13", ["sampler2D", V2, V2, V2], (tex, res, uv, dir) => {
52
+ }
53
+ );
54
+ const blur13 = defn(
55
+ V4,
56
+ "blur13",
57
+ ["sampler2D", V2, V2, V2],
58
+ (tex, res, uv, dir) => {
82
59
  let col;
83
60
  let off;
84
61
  let off2;
@@ -88,14 +65,20 @@ export const blur13 = defn(V4, "blur13", ["sampler2D", V2, V2, V2], (tex, res, u
88
65
  const k2 = 0.09447039785044732;
89
66
  const k3 = 0.010381362401148057;
90
67
  return [
91
- (delta = sym(div(dir, res))),
92
- (off = sym(mul(delta, 1.411764705882353))),
93
- (off2 = sym(mul(delta, 3.2941176470588234))),
94
- (off3 = sym(mul(delta, 5.176470588235294))),
95
- (col = sym(mul(texture(tex, uv), 0.1964825501511404))),
96
- singlePass(col, tex, uv, off, k1),
97
- singlePass(col, tex, uv, off2, k2),
98
- singlePass(col, tex, uv, off3, k3),
99
- ret(col),
68
+ delta = sym(div(dir, res)),
69
+ off = sym(mul(delta, 1.411764705882353)),
70
+ off2 = sym(mul(delta, 3.2941176470588234)),
71
+ off3 = sym(mul(delta, 5.176470588235294)),
72
+ col = sym(mul(texture(tex, uv), 0.1964825501511404)),
73
+ singlePass(col, tex, uv, off, k1),
74
+ singlePass(col, tex, uv, off2, k2),
75
+ singlePass(col, tex, uv, off3, k3),
76
+ ret(col)
100
77
  ];
101
- });
78
+ }
79
+ );
80
+ export {
81
+ blur13,
82
+ blur5,
83
+ blur9
84
+ };
@@ -1,19 +1,9 @@
1
1
  import { uvec2 } from "@thi.ng/shader-ast/ast/lit";
2
2
  import { div, madd, modi } from "@thi.ng/shader-ast/ast/ops";
3
3
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
4
- /**
5
- * Inline function. Similar to {@link indexToUV}, but returns uvec2 in pixel
6
- * coords. Not compatible with WebGL1.
7
- *
8
- * @param i -
9
- * @param width -
10
- */
11
- export const indexToCoord = (i, width) => uvec2(modi(i, width), div(i, width));
12
- /**
13
- * Inline function. Reverse op to {@link indexToCoord}. Not compatible with
14
- * WebGL1.
15
- *
16
- * @param coord -
17
- * @param width -
18
- */
19
- export const coordToIndex = (coord, width) => madd($y(coord), width, $x(coord));
4
+ const indexToCoord = (i, width) => uvec2(modi(i, width), div(i, width));
5
+ const coordToIndex = (coord, width) => madd($y(coord), width, $x(coord));
6
+ export {
7
+ coordToIndex,
8
+ indexToCoord
9
+ };
package/tex/index-uv.js CHANGED
@@ -3,23 +3,33 @@ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
3
  import { float, int, vec2 } from "@thi.ng/shader-ast/ast/lit";
4
4
  import { add, div, modi, mul } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
6
- /**
7
- * Converts linearized 2D index `i` into a vec2 UV coord, based on given
8
- * texture `size` (in pixels).
9
- *
10
- * @param i -
11
- * @param size -
12
- */
13
- export const indexToUV = defn(V2, "indexToUV", [[I, "i", { prec: "highp" }], ["ivec2"]], (i, size) => [
14
- ret(vec2(div(float(modi(i, $x(size))), float($x(size))), div(float(div(i, $x(size))), float($y(size))))),
15
- ]);
16
- /**
17
- * Inverse operation of {@link indexToUV}. Converts vec2 UV coord into
18
- * linearized 2D index, based on given texture `width` (in pixels).
19
- *
20
- * @param i -
21
- * @param width -
22
- */
23
- export const uvToIndex = defn(I, "uvToIndex", [V2, [I, "width", { prec: "highp" }]], (uv, width) => [
24
- ret(add(int(mul($x(uv), float(width))), int(mul($y(uv), float(mul(width, width)))))),
25
- ]);
6
+ const indexToUV = defn(
7
+ V2,
8
+ "indexToUV",
9
+ [[I, "i", { prec: "highp" }], ["ivec2"]],
10
+ (i, size) => [
11
+ ret(
12
+ vec2(
13
+ div(float(modi(i, $x(size))), float($x(size))),
14
+ div(float(div(i, $x(size))), float($y(size)))
15
+ )
16
+ )
17
+ ]
18
+ );
19
+ const uvToIndex = defn(
20
+ I,
21
+ "uvToIndex",
22
+ [V2, [I, "width", { prec: "highp" }]],
23
+ (uv, width) => [
24
+ ret(
25
+ add(
26
+ int(mul($x(uv), float(width))),
27
+ int(mul($y(uv), float(mul(width, width))))
28
+ )
29
+ )
30
+ ]
31
+ );
32
+ export {
33
+ indexToUV,
34
+ uvToIndex
35
+ };
package/tex/read-index.js CHANGED
@@ -1,35 +1,13 @@
1
1
  import { $x, $xy, $xyz } from "@thi.ng/shader-ast/ast/swizzle";
2
2
  import { texture } from "@thi.ng/shader-ast/builtin/texture";
3
3
  import { indexToUV } from "./index-uv.js";
4
- /**
5
- * Inline function. Returns x component at index `i` in `tex`.
6
- *
7
- * @param tex -
8
- * @param i -
9
- * @param size -
10
- */
11
- export const readIndex1 = (tex, i, size) => $x(readIndex4(tex, i, size));
12
- /**
13
- * Inline function. Returns vec2 (x,y components) at index `i` in `tex`.
14
- *
15
- * @param tex -
16
- * @param i -
17
- * @param size -
18
- */
19
- export const readIndex2 = (tex, i, size) => $xy(readIndex4(tex, i, size));
20
- /**
21
- * Inline function. Returns vec3 (x,y,z components) at index `i` in `tex`.
22
- *
23
- * @param tex -
24
- * @param i -
25
- * @param size -
26
- */
27
- export const readIndex3 = (tex, i, size) => $xyz(readIndex4(tex, i, size));
28
- /**
29
- * Inline function. Returns vec4 at index `i` in `tex`.
30
- *
31
- * @param tex -
32
- * @param i -
33
- * @param size -
34
- */
35
- export const readIndex4 = (tex, i, size) => texture(tex, indexToUV(i, size));
4
+ const readIndex1 = (tex, i, size) => $x(readIndex4(tex, i, size));
5
+ const readIndex2 = (tex, i, size) => $xy(readIndex4(tex, i, size));
6
+ const readIndex3 = (tex, i, size) => $xyz(readIndex4(tex, i, size));
7
+ const readIndex4 = (tex, i, size) => texture(tex, indexToUV(i, size));
8
+ export {
9
+ readIndex1,
10
+ readIndex2,
11
+ readIndex3,
12
+ readIndex4
13
+ };