@thi.ng/shader-ast-stdlib 0.16.8 → 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 -6
  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
package/sdf/cross.js CHANGED
@@ -8,49 +8,28 @@ import { $, $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
8
8
  import { sym } from "@thi.ng/shader-ast/ast/sym";
9
9
  import { abs, length, max, min, sign } from "@thi.ng/shader-ast/builtin/math";
10
10
  import { maxComp2 } from "../math/maxcomp.js";
11
- /**
12
- * Returns signed distance from `p` to 2D cross/plus of given `size` and corner
13
- * radius `r`.
14
- *
15
- * @remarks
16
- * - `size` consist of overall width/size (in x) and thickness (in y component)
17
- * - corner radius can also be negative
18
- *
19
- * Ported from original GLSL impl by Inigo Quilez:
20
- * - https://iquilezles.org/articles/distfunctions2d/
21
- *
22
- * @param p -
23
- * @param size -
24
- * @param r -
25
- */
26
- export const sdfCross2 = defn(F, "sdfCross2", [V2, V2, F], (p, size, r) => {
27
- let a, q, w;
28
- let k;
29
- return [
30
- (a = sym(abs(p))),
31
- ifThen(gt($y(a), $x(a)), [assign(a, $(a, "yx"))]),
32
- (q = sym(sub(a, size))),
33
- (k = sym(maxComp2(q))),
34
- (w = sym(ternary(gt(k, FLOAT0), q, vec2(sub($y(size), $x(a)), neg(k))))),
35
- ret(madd(sign(k), length(max(w, VEC2_0)), r)),
36
- ];
11
+ const sdfCross2 = defn(F, "sdfCross2", [V2, V2, F], (p, size, r) => {
12
+ let a, q, w;
13
+ let k;
14
+ return [
15
+ a = sym(abs(p)),
16
+ ifThen(gt($y(a), $x(a)), [assign(a, $(a, "yx"))]),
17
+ q = sym(sub(a, size)),
18
+ k = sym(maxComp2(q)),
19
+ w = sym(
20
+ ternary(gt(k, FLOAT0), q, vec2(sub($y(size), $x(a)), neg(k)))
21
+ ),
22
+ ret(madd(sign(k), length(max(w, VEC2_0)), r))
23
+ ];
37
24
  });
38
- /**
39
- * Returns signed distance from `p` to 2D "X-sign" of given `size` and stroke
40
- * weight `r`.
41
- *
42
- * @remarks
43
- * Ported from original GLSL impl by Inigo Quilez:
44
- * - https://iquilezles.org/articles/distfunctions2d/
45
- *
46
- * @param p -
47
- * @param size -
48
- * @param r -
49
- */
50
- export const sdfRoundedX2 = defn(F, "sdfRoundedX", [V2, F, F], (p, size, r) => {
51
- let q;
52
- return [
53
- (q = sym(abs(p))),
54
- ret(sub(length(sub(q, mul(min(add($x(q), $y(q)), size), FLOAT05))), r)),
55
- ];
25
+ const sdfRoundedX2 = defn(F, "sdfRoundedX", [V2, F, F], (p, size, r) => {
26
+ let q;
27
+ return [
28
+ q = sym(abs(p)),
29
+ ret(sub(length(sub(q, mul(min(add($x(q), $y(q)), size), FLOAT05))), r))
30
+ ];
56
31
  });
32
+ export {
33
+ sdfCross2,
34
+ sdfRoundedX2
35
+ };
package/sdf/cylinder.js CHANGED
@@ -6,18 +6,13 @@ import { $, $y } from "@thi.ng/shader-ast/ast/swizzle";
6
6
  import { sym } from "@thi.ng/shader-ast/ast/sym";
7
7
  import { abs, length, max, min } from "@thi.ng/shader-ast/builtin/math";
8
8
  import { maxComp2 } from "../math/maxcomp.js";
9
- /**
10
- * Returns signed distance from `p` to cylinder centered around Y-axis
11
- * with height `h` and radius `r`.
12
- *
13
- * @param p - vec3
14
- * @param h - float
15
- * @param r - float
16
- */
17
- export const sdfCylinder = defn(F, "sdCylinder", [V3, F, F], (p, h, r) => {
18
- let d;
19
- return [
20
- (d = sym(sub(abs(vec2(length($(p, "xz")), $y(p))), vec2(h, r)))),
21
- ret(add(min(maxComp2(d), FLOAT0), length(max(d, VEC2_0)))),
22
- ];
9
+ const sdfCylinder = defn(F, "sdCylinder", [V3, F, F], (p, h, r) => {
10
+ let d;
11
+ return [
12
+ d = sym(sub(abs(vec2(length($(p, "xz")), $y(p))), vec2(h, r))),
13
+ ret(add(min(maxComp2(d), FLOAT0), length(max(d, VEC2_0))))
14
+ ];
23
15
  });
16
+ export {
17
+ sdfCylinder
18
+ };
package/sdf/hex.js CHANGED
@@ -5,23 +5,26 @@ import { FLOAT0, vec2 } from "@thi.ng/shader-ast/ast/lit";
5
5
  import { mul, sub } from "@thi.ng/shader-ast/ast/ops";
6
6
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
7
7
  import { sym } from "@thi.ng/shader-ast/ast/sym";
8
- import { abs, clamp, dot, length, min, sign, } from "@thi.ng/shader-ast/builtin/math";
9
- /**
10
- * Returns signed distance from `p` to 2D hexagon of given radius `r`.
11
- *
12
- * @remarks
13
- * Ported from original GLSL impl by Inigo Quilez:
14
- * - https://iquilezles.org/articles/distfunctions2d/
15
- */
16
- export const sdfHexagon2 = defn(F, "sdfHexagon2", [V2, F], (p, r) => {
17
- const TAN30 = 0.5773502691896257;
18
- let k, q;
19
- return [
20
- // sin/cos @ 60deg
21
- (k = sym(vec2(-0.8660254037844386, 0.5))),
22
- (q = sym(abs(p))),
23
- assign(q, sub(q, mul(k, mul(2, min(dot(k, q), FLOAT0))))),
24
- assign(q, sub(q, vec2(clamp($x(q), mul(r, -TAN30), mul(r, TAN30)), r))),
25
- ret(mul(length(q), sign($y(q)))),
26
- ];
8
+ import {
9
+ abs,
10
+ clamp,
11
+ dot,
12
+ length,
13
+ min,
14
+ sign
15
+ } from "@thi.ng/shader-ast/builtin/math";
16
+ const sdfHexagon2 = defn(F, "sdfHexagon2", [V2, F], (p, r) => {
17
+ const TAN30 = 0.5773502691896257;
18
+ let k, q;
19
+ return [
20
+ // sin/cos @ 60deg
21
+ k = sym(vec2(-0.8660254037844386, 0.5)),
22
+ q = sym(abs(p)),
23
+ assign(q, sub(q, mul(k, mul(2, min(dot(k, q), FLOAT0))))),
24
+ assign(q, sub(q, vec2(clamp($x(q), mul(r, -TAN30), mul(r, TAN30)), r))),
25
+ ret(mul(length(q), sign($y(q))))
26
+ ];
27
27
  });
28
+ export {
29
+ sdfHexagon2
30
+ };
package/sdf/isec.js CHANGED
@@ -1,9 +1,5 @@
1
1
  import { max } from "@thi.ng/shader-ast/builtin/math";
2
- /**
3
- * Inline function. Variadic SDF shape intersection (a & b) for any number of
4
- * terms (at least 1 required).
5
- *
6
- * @param a -
7
- * @param terms -
8
- */
9
- export const sdfIntersect = (a, ...terms) => terms.reduce((a, b) => max(a, b), a);
2
+ const sdfIntersect = (a, ...terms) => terms.reduce((a2, b) => max(a2, b), a);
3
+ export {
4
+ sdfIntersect
5
+ };
package/sdf/line.js CHANGED
@@ -4,34 +4,19 @@ import { div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
4
4
  import { sym } from "@thi.ng/shader-ast/ast/sym";
5
5
  import { dot, length } from "@thi.ng/shader-ast/builtin/math";
6
6
  import { clamp01 } from "../math/clamp.js";
7
- /**
8
- * Shared impl for sdLine2/3
9
- *
10
- * @param p -
11
- * @param a -
12
- * @param b -
13
- */
14
7
  const $ = (n, type) => defn(F, `sdLine${n}`, [type, type, type], (p, a, b) => {
15
- let pa, ba;
16
- return [
17
- (pa = sym(sub(p, a))),
18
- (ba = sym(sub(b, a))),
19
- ret(length(sub(pa, mul(ba, clamp01(div(dot(pa, ba), dot(ba, ba))))))),
20
- ];
8
+ let pa, ba;
9
+ return [
10
+ pa = sym(sub(p, a)),
11
+ ba = sym(sub(b, a)),
12
+ ret(
13
+ length(sub(pa, mul(ba, clamp01(div(dot(pa, ba), dot(ba, ba))))))
14
+ )
15
+ ];
21
16
  });
22
- /**
23
- * Returns signed distance from `p` to 2D line segment `a` -> `b`.
24
- *
25
- * @param p - vec2
26
- * @param a - vec2
27
- * @param b - vec2
28
- */
29
- export const sdfLine2 = $(2, V2);
30
- /**
31
- * Returns signed distance from `p` to 3D line segment `a` -> `b`.
32
- *
33
- * @param p - vec3
34
- * @param a - vec3
35
- * @param b - vec3
36
- */
37
- export const sdfLine3 = $(3, V3);
17
+ const sdfLine2 = $(2, V2);
18
+ const sdfLine3 = $(3, V3);
19
+ export {
20
+ sdfLine2,
21
+ sdfLine3
22
+ };
package/sdf/mirror.js CHANGED
@@ -4,17 +4,21 @@ import { FLOAT05, VEC2_1, VEC2_2 } from "@thi.ng/shader-ast/ast/lit";
4
4
  import { add, div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { sym } from "@thi.ng/shader-ast/ast/sym";
6
6
  import { floor, mod } from "@thi.ng/shader-ast/builtin/math";
7
- /**
8
- * @remarks
9
- * Based on HG_SDF (Mercury Demogroup). Does not compute cell index.
10
- *
11
- * @param p - point
12
- * @param size - mirror box size
13
- */
14
- export const sdfMirror2 = defn(V2, "sdfMirror2", [V2, V2], (p, size) => {
15
- let halfSize;
16
- return [
17
- (halfSize = sym(mul(size, FLOAT05))),
18
- ret(mul(sub(mod(add(p, halfSize), size), halfSize), sub(mul(mod(floor(div(add(p, halfSize), size)), VEC2_2), 2), VEC2_1))),
19
- ];
7
+ const sdfMirror2 = defn(V2, "sdfMirror2", [V2, V2], (p, size) => {
8
+ let halfSize;
9
+ return [
10
+ halfSize = sym(mul(size, FLOAT05)),
11
+ ret(
12
+ mul(
13
+ sub(mod(add(p, halfSize), size), halfSize),
14
+ sub(
15
+ mul(mod(floor(div(add(p, halfSize), size)), VEC2_2), 2),
16
+ VEC2_1
17
+ )
18
+ )
19
+ )
20
+ ];
20
21
  });
22
+ export {
23
+ sdfMirror2
24
+ };
package/sdf/plane.js CHANGED
@@ -2,23 +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 { add } from "@thi.ng/shader-ast/ast/ops";
4
4
  import { dot } from "@thi.ng/shader-ast/builtin/math";
5
- /**
6
- * Returns signed distance from `p` to plane defined by `normal` and `w`.
7
- *
8
- * @param p - vec2
9
- * @param normal - vec2
10
- * @param w - float
11
- */
12
- export const sdfPlane2 = defn(F, "sdPlane2", [V2, V2, F], (p, n, w) => [
13
- ret(add(dot(p, n), w)),
5
+ const sdfPlane2 = defn(F, "sdPlane2", [V2, V2, F], (p, n, w) => [
6
+ ret(add(dot(p, n), w))
14
7
  ]);
15
- /**
16
- * Returns signed distance from `p` to plane defined by `normal` and `w`.
17
- *
18
- * @param p - vec3
19
- * @param normal - vec3
20
- * @param w - float
21
- */
22
- export const sdfPlane3 = defn(F, "sdPlane3", [V3, V3, F], (p, n, w) => [
23
- ret(add(dot(p, n), w)),
8
+ const sdfPlane3 = defn(F, "sdPlane3", [V3, V3, F], (p, n, w) => [
9
+ ret(add(dot(p, n), w))
24
10
  ]);
11
+ export {
12
+ sdfPlane2,
13
+ sdfPlane3
14
+ };
package/sdf/polygon.js CHANGED
@@ -4,44 +4,66 @@ import { forLoop, ifThen } from "@thi.ng/shader-ast/ast/controlflow";
4
4
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
5
5
  import { index } from "@thi.ng/shader-ast/ast/indexed";
6
6
  import { FLOAT1, INT0, bvec3, int } from "@thi.ng/shader-ast/ast/lit";
7
- import { div, gt, gte, inc, lt, mul, neg, not, or, sub, } from "@thi.ng/shader-ast/ast/ops";
7
+ import {
8
+ div,
9
+ gt,
10
+ gte,
11
+ inc,
12
+ lt,
13
+ mul,
14
+ neg,
15
+ not,
16
+ or,
17
+ sub
18
+ } from "@thi.ng/shader-ast/ast/ops";
8
19
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
9
20
  import { sym } from "@thi.ng/shader-ast/ast/sym";
10
21
  import { _any, all } from "@thi.ng/shader-ast/builtin/bvec";
11
22
  import { dot, min, sqrt } from "@thi.ng/shader-ast/builtin/math";
12
23
  import { clamp01 } from "../math/clamp.js";
13
- /**
14
- * Higher-order function. Returns specialized function to compute signed
15
- * distance for 2D polygons of `N` vertices.
16
- *
17
- * @remarks
18
- * Based on original GLSL impl by Inigo Quilez:
19
- * - https://iquilezles.org/articles/distfunctions2d/
20
- *
21
- * @param N
22
- */
23
- export const sdfPolygon2 = (N) => defn(F, `sdfPolygon2_${N}`, [V2, ["vec2[]", "pts", { num: N }]], (p, pts) => {
24
+ const sdfPolygon2 = (N) => defn(
25
+ F,
26
+ `sdfPolygon2_${N}`,
27
+ [V2, ["vec2[]", "pts", { num: N }]],
28
+ (p, pts) => {
24
29
  let d, s;
25
30
  let b, e, w, t;
26
31
  let pi, pj;
27
32
  let c;
28
33
  let j;
29
34
  return [
30
- (t = sym(sub(p, index(pts, 0)))),
31
- (d = sym(dot(t, t))),
32
- (s = sym(FLOAT1)),
33
- (j = sym(int(N - 1))),
34
- forLoop(sym(INT0), (i) => lt(i, int(N)), inc, (i) => [
35
- (pi = sym(index(pts, i))),
36
- (pj = sym(index(pts, j))),
37
- (e = sym(sub(pj, pi))),
38
- (w = sym(sub(p, pi))),
39
- (b = sym(sub(w, mul(e, clamp01(div(dot(w, e), dot(e, e))))))),
40
- assign(d, min(d, dot(b, b))),
41
- (c = sym(bvec3(gte($y(p), $y(pi)), lt($y(p), $y(pj)), gt(mul($x(e), $y(w)), mul($y(e), $x(w)))))),
42
- ifThen(or(all(c), not(_any(c))), [assign(s, neg(s))]),
43
- assign(j, i),
44
- ]),
45
- ret(mul(s, sqrt(d))),
35
+ t = sym(sub(p, index(pts, 0))),
36
+ d = sym(dot(t, t)),
37
+ s = sym(FLOAT1),
38
+ j = sym(int(N - 1)),
39
+ forLoop(
40
+ sym(INT0),
41
+ (i) => lt(i, int(N)),
42
+ inc,
43
+ (i) => [
44
+ pi = sym(index(pts, i)),
45
+ pj = sym(index(pts, j)),
46
+ e = sym(sub(pj, pi)),
47
+ w = sym(sub(p, pi)),
48
+ b = sym(
49
+ sub(w, mul(e, clamp01(div(dot(w, e), dot(e, e)))))
50
+ ),
51
+ assign(d, min(d, dot(b, b))),
52
+ c = sym(
53
+ bvec3(
54
+ gte($y(p), $y(pi)),
55
+ lt($y(p), $y(pj)),
56
+ gt(mul($x(e), $y(w)), mul($y(e), $x(w)))
57
+ )
58
+ ),
59
+ ifThen(or(all(c), not(_any(c))), [assign(s, neg(s))]),
60
+ assign(j, i)
61
+ ]
62
+ ),
63
+ ret(mul(s, sqrt(d)))
46
64
  ];
47
- });
65
+ }
66
+ );
67
+ export {
68
+ sdfPolygon2
69
+ };
package/sdf/polyhedra.js CHANGED
@@ -3,53 +3,88 @@ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
3
  import { FLOAT0, PHI, vec3 } from "@thi.ng/shader-ast/ast/lit";
4
4
  import { add, reciprocal, sub } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { abs, dot, max, pow } from "@thi.ng/shader-ast/builtin/math";
6
- // could use @thi.ng/vectors, but avoiding dependency
7
6
  const normalize = ([x, y, z]) => {
8
- const m = 1 / Math.hypot(x, y, z);
9
- return [x * m, y * m, z * m];
7
+ const m = 1 / Math.hypot(x, y, z);
8
+ return [x * m, y * m, z * m];
10
9
  };
11
10
  const phi = PHI.val;
12
11
  const GDF = [
13
- [1, 0, 0],
14
- [0, 1, 0],
15
- [0, 0, 1],
16
- [1, 1, 1],
17
- [-1, 1, 1],
18
- [1, -1, 1],
19
- [1, 1, -1],
20
- [0, 1, phi + 1],
21
- [0, -1, phi + 1],
22
- [phi + 1, 0, 1],
23
- [-phi - 1, 0, 1],
24
- [1, phi + 1, 0],
25
- [-1, phi + 1, 0],
26
- [0, phi, 1],
27
- [0, -phi, 1],
28
- [1, 0, phi],
29
- [-1, 0, phi],
30
- [phi, 1, 0],
31
- [-phi, 1, 0],
12
+ [1, 0, 0],
13
+ [0, 1, 0],
14
+ [0, 0, 1],
15
+ [1, 1, 1],
16
+ [-1, 1, 1],
17
+ [1, -1, 1],
18
+ [1, 1, -1],
19
+ [0, 1, phi + 1],
20
+ [0, -1, phi + 1],
21
+ [phi + 1, 0, 1],
22
+ [-phi - 1, 0, 1],
23
+ [1, phi + 1, 0],
24
+ [-1, phi + 1, 0],
25
+ [0, phi, 1],
26
+ [0, -phi, 1],
27
+ [1, 0, phi],
28
+ [-1, 0, phi],
29
+ [phi, 1, 0],
30
+ [-phi, 1, 0]
32
31
  ].map((v) => vec3(...normalize(v)));
33
- /**
34
- * @remarks
35
- * Based on: "Generalized Distance Functions" (E. Akleman, J. Chen)
36
- * http://people.tamu.edu/~ergun/research/implicitmodeling/papers/sm99.pdf
37
- *
38
- * Also based on: HG_SDF (Mercury Demogroup)
39
- *
40
- * @param id -
41
- * @param vecs -
42
- */
43
32
  const defGDF = (id, vecs) => [
44
- defn(F, id, [V3, F], (p, r) => [
45
- ret(sub(vecs.reduce((acc, v) => max(acc, abs(dot(p, v))), FLOAT0), r)),
46
- ]),
47
- defn(F, id + "Smooth", [V3, F, F], (p, r, e) => [
48
- ret(sub(pow(vecs.reduce((acc, v) => add(acc, pow(abs(dot(p, v)), e)), FLOAT0), reciprocal(e)), r)),
49
- ]),
33
+ defn(F, id, [V3, F], (p, r) => [
34
+ ret(
35
+ sub(
36
+ vecs.reduce(
37
+ (acc, v) => max(acc, abs(dot(p, v))),
38
+ FLOAT0
39
+ ),
40
+ r
41
+ )
42
+ )
43
+ ]),
44
+ defn(F, id + "Smooth", [V3, F, F], (p, r, e) => [
45
+ ret(
46
+ sub(
47
+ pow(
48
+ vecs.reduce(
49
+ (acc, v) => add(acc, pow(abs(dot(p, v)), e)),
50
+ FLOAT0
51
+ ),
52
+ reciprocal(e)
53
+ ),
54
+ r
55
+ )
56
+ )
57
+ ])
50
58
  ];
51
- export const [sdfOctahedron, sdfOctahedronSmooth] = defGDF("sdfOctahedron", GDF.slice(3, 7));
52
- export const [sdfDodecahedron, sdfDodecahedronSmooth] = defGDF("sdfDodecahedron", GDF.slice(13));
53
- export const [sdfIcosahedron, sdfIcosahedronSmooth] = defGDF("sdfIcosahedron", GDF.slice(3, 13));
54
- export const [sdfTruncatedOctahedron, sdfTruncatedOctahedronSmooth] = defGDF("sdfTruncatedOctahedron", GDF.slice(0, 7));
55
- export const [sdfTruncatedIcosahedron, sdfTruncatedIcosahedronSmooth] = defGDF("sdfTruncatedIcosahedron", GDF.slice(3));
59
+ const [sdfOctahedron, sdfOctahedronSmooth] = defGDF(
60
+ "sdfOctahedron",
61
+ GDF.slice(3, 7)
62
+ );
63
+ const [sdfDodecahedron, sdfDodecahedronSmooth] = defGDF(
64
+ "sdfDodecahedron",
65
+ GDF.slice(13)
66
+ );
67
+ const [sdfIcosahedron, sdfIcosahedronSmooth] = defGDF(
68
+ "sdfIcosahedron",
69
+ GDF.slice(3, 13)
70
+ );
71
+ const [sdfTruncatedOctahedron, sdfTruncatedOctahedronSmooth] = defGDF(
72
+ "sdfTruncatedOctahedron",
73
+ GDF.slice(0, 7)
74
+ );
75
+ const [sdfTruncatedIcosahedron, sdfTruncatedIcosahedronSmooth] = defGDF(
76
+ "sdfTruncatedIcosahedron",
77
+ GDF.slice(3)
78
+ );
79
+ export {
80
+ sdfDodecahedron,
81
+ sdfDodecahedronSmooth,
82
+ sdfIcosahedron,
83
+ sdfIcosahedronSmooth,
84
+ sdfOctahedron,
85
+ sdfOctahedronSmooth,
86
+ sdfTruncatedIcosahedron,
87
+ sdfTruncatedIcosahedronSmooth,
88
+ sdfTruncatedOctahedron,
89
+ sdfTruncatedOctahedronSmooth
90
+ };
package/sdf/polyline.js CHANGED
@@ -8,13 +8,36 @@ import { add, inc, lt } from "@thi.ng/shader-ast/ast/ops";
8
8
  import { sym } from "@thi.ng/shader-ast/ast/sym";
9
9
  import { min } from "@thi.ng/shader-ast/builtin/math";
10
10
  import { sdfLine2 } from "./line.js";
11
- export const sdfPolyline2 = (N) => defn(F, `sdfPolyline2_${N}`, [V2, ["vec2[]", "pts", { num: N }]], (p, pts) => {
11
+ const sdfPolyline2 = (N) => defn(
12
+ F,
13
+ `sdfPolyline2_${N}`,
14
+ [V2, ["vec2[]", "pts", { num: N }]],
15
+ (p, pts) => {
12
16
  let minD;
13
17
  return [
14
- (minD = sym(float(1e6))),
15
- forLoop(sym(INT0), (i) => lt(i, int(N - 1)), inc, (i) => [
16
- assign(minD, min(minD, sdfLine2(p, index(pts, i), index(pts, add(i, 1))))),
17
- ]),
18
- ret(minD),
18
+ minD = sym(float(1e6)),
19
+ forLoop(
20
+ sym(INT0),
21
+ (i) => lt(i, int(N - 1)),
22
+ inc,
23
+ (i) => [
24
+ assign(
25
+ minD,
26
+ min(
27
+ minD,
28
+ sdfLine2(
29
+ p,
30
+ index(pts, i),
31
+ index(pts, add(i, 1))
32
+ )
33
+ )
34
+ )
35
+ ]
36
+ ),
37
+ ret(minD)
19
38
  ];
20
- });
39
+ }
40
+ );
41
+ export {
42
+ sdfPolyline2
43
+ };
@@ -6,21 +6,17 @@ import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
6
6
  import { sym } from "@thi.ng/shader-ast/ast/sym";
7
7
  import { atan, length, mod } from "@thi.ng/shader-ast/builtin/math";
8
8
  import { cossin } from "../math/sincos.js";
9
- /**
10
- * @remarks
11
- * Based on HG_SDF (Mercury Demogroup). Does not compute cell index.
12
- *
13
- * @param p - point
14
- * @param n - number of polar repetitions
15
- */
16
- export const sdfRepeatPolar2 = defn(V2, "sdfRepeatPolar2", [V2, F], (p, n) => {
17
- let angle;
18
- let angle2;
19
- let a;
20
- return [
21
- (angle = sym(div(TAU, n))),
22
- (angle2 = sym(mul(angle, FLOAT05))),
23
- (a = sym(sub(mod(add(angle2, atan($y(p), $x(p))), angle), angle2))),
24
- ret(mul(cossin(a), length(p))),
25
- ];
9
+ const sdfRepeatPolar2 = defn(V2, "sdfRepeatPolar2", [V2, F], (p, n) => {
10
+ let angle;
11
+ let angle2;
12
+ let a;
13
+ return [
14
+ angle = sym(div(TAU, n)),
15
+ angle2 = sym(mul(angle, FLOAT05)),
16
+ a = sym(sub(mod(add(angle2, atan($y(p), $x(p))), angle), angle2)),
17
+ ret(mul(cossin(a), length(p)))
18
+ ];
26
19
  });
20
+ export {
21
+ sdfRepeatPolar2
22
+ };
package/sdf/repeat.js CHANGED
@@ -4,19 +4,11 @@ import { FLOAT05 } from "@thi.ng/shader-ast/ast/lit";
4
4
  import { mul, sub } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { mod } from "@thi.ng/shader-ast/builtin/math";
6
6
  const $ = (n, type) => defn(type, `sdTxRepeat${n}`, [type, type], (p, c) => [
7
- ret(sub(mod(p, c), mul(c, FLOAT05))),
7
+ ret(sub(mod(p, c), mul(c, FLOAT05)))
8
8
  ]);
9
- /**
10
- * 2D domain repetition by wrapping position `p` into period `c`.
11
- *
12
- * @param p - vec2
13
- * @param c - vec2
14
- */
15
- export const sdfRepeat2 = $(2, V2);
16
- /**
17
- * 3D domain repetition by wrapping position `p` into period `c`.
18
- *
19
- * @param p - vec3
20
- * @param c - vec3
21
- */
22
- export const sdfRepeat3 = $(3, V3);
9
+ const sdfRepeat2 = $(2, V2);
10
+ const sdfRepeat3 = $(3, V3);
11
+ export {
12
+ sdfRepeat2,
13
+ sdfRepeat3
14
+ };
package/sdf/round.js CHANGED
@@ -1,11 +1,5 @@
1
1
  import { sub } from "@thi.ng/shader-ast/ast/ops";
2
- /**
3
- * Inline function. Essentially an isoline offset to create:
4
- *
5
- * - `r > 0`: rounded/thicker shapes
6
- * - `r < 0`: sharper/thinner shapes
7
- *
8
- * @param d -
9
- * @param r -
10
- */
11
- export const sdfRound = (d, r) => sub(d, r);
2
+ const sdfRound = (d, r) => sub(d, r);
3
+ export {
4
+ sdfRound
5
+ };