@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
package/math/fit.js CHANGED
@@ -5,69 +5,21 @@ import { FLOAT0, FLOAT05, FLOAT1, FLOAT2 } from "@thi.ng/shader-ast/ast/lit";
5
5
  import { add, div, mul, neq, sub } from "@thi.ng/shader-ast/ast/ops";
6
6
  import { mix } from "@thi.ng/shader-ast/builtin/math";
7
7
  import { clamp01 } from "./clamp.js";
8
- /**
9
- * Returns normalized value of `x` WRT to interval [a,b]. Returns 0, if
10
- * `a` equals `b`.
11
- *
12
- * @param x -
13
- * @param a -
14
- * @param b -
15
- */
16
- export const fitNorm1 = defn(F, "fitNorm1", [F, F, F], (x, a, b) => [
17
- ret(ternary(neq(a, b), div(sub(x, a), sub(b, a)), FLOAT0)),
8
+ const fitNorm1 = defn(F, "fitNorm1", [F, F, F], (x, a, b) => [
9
+ ret(ternary(neq(a, b), div(sub(x, a), sub(b, a)), FLOAT0))
18
10
  ]);
19
- /**
20
- * Similar to {@link fitNorm1} but also for vector types and without checking if
21
- * `a == b`. Scales value `x` from closed interval [a,b] to closed [0,1]
22
- * interval. No clamping performed.
23
- *
24
- * @param x
25
- * @param a
26
- * @param b
27
- * @returns
28
- */
29
- export const fitNorm = (x, a, b) => div(sub(x, a), sub(b, a));
30
- /**
31
- * Fits value `x` from closed interval [a,b] to closed interval [c,d]. No
32
- * clamping performed.
33
- *
34
- * @param x -
35
- * @param a -
36
- * @param b -
37
- * @param c -
38
- * @param d -
39
- */
40
- export const fit = (x, a, b, c, d) => mix(c, d, fitNorm(x, a, b));
41
- /**
42
- * Same as {@link fit}, but first clamps `x` to closed [a,b] interval.
43
- *
44
- * @param x -
45
- * @param a -
46
- * @param b -
47
- * @param c -
48
- * @param d -
49
- */
50
- export const fitClamped = (x, a, b, c, d) => mix(c, d, clamp01(div(sub(x, a), sub(b, a))));
51
- /**
52
- * Inline function. Fits value `a` in [0..1] interval to new interval
53
- * [b..c]. No clamping performed. Same as `mix(b, c, a)`
54
- *
55
- * @param a -
56
- * @param b -
57
- * @param c -
58
- */
59
- export const fit01 = (a, b, c) => mix(b, c, a);
60
- /**
61
- * Inline function. Fits value `x` in [-1..+1] interval to [0..1]
62
- * interval. No clamping performed.
63
- *
64
- * @param x -
65
- */
66
- export const fit1101 = (x) => add(mul(x, FLOAT05), FLOAT05);
67
- /**
68
- * Inline function. Fits value `x` in [0..1] interval to [-1..+1]
69
- * interval. No clamping performed.
70
- *
71
- * @param x -
72
- */
73
- export const fit0111 = (x) => sub(mul(x, FLOAT2), FLOAT1);
11
+ const fitNorm = (x, a, b) => div(sub(x, a), sub(b, a));
12
+ const fit = (x, a, b, c, d) => mix(c, d, fitNorm(x, a, b));
13
+ const fitClamped = (x, a, b, c, d) => mix(c, d, clamp01(div(sub(x, a), sub(b, a))));
14
+ const fit01 = (a, b, c) => mix(b, c, a);
15
+ const fit1101 = (x) => add(mul(x, FLOAT05), FLOAT05);
16
+ const fit0111 = (x) => sub(mul(x, FLOAT2), FLOAT1);
17
+ export {
18
+ fit,
19
+ fit01,
20
+ fit0111,
21
+ fit1101,
22
+ fitClamped,
23
+ fitNorm,
24
+ fitNorm1
25
+ };
package/math/interval.js CHANGED
@@ -4,30 +4,29 @@ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
4
4
  import { FLOAT0, FLOAT1, FLOAT2 } from "@thi.ng/shader-ast/ast/lit";
5
5
  import { div, lt, mul, sub } from "@thi.ng/shader-ast/ast/ops";
6
6
  import { sym } from "@thi.ng/shader-ast/ast/sym";
7
- import { abs, ceil, floor, mix, mod, step, } from "@thi.ng/shader-ast/builtin/math";
8
- /**
9
- * GLSL 100 trunc() polyfill.
10
- */
11
- export const trunc = defn(F, null, [F], (x) => [
12
- ret(ternary(lt(x, FLOAT0), ceil(x), floor(x))),
7
+ import {
8
+ abs,
9
+ ceil,
10
+ floor,
11
+ mix,
12
+ mod,
13
+ step
14
+ } from "@thi.ng/shader-ast/builtin/math";
15
+ const trunc = defn(F, null, [F], (x) => [
16
+ ret(ternary(lt(x, FLOAT0), ceil(x), floor(x)))
13
17
  ]);
14
- /**
15
- * Returns `x - y * trunc(x / y)`, i.e. essentially the same as JS `%` operator.
16
- * Result will always have the sign of `x`.
17
- */
18
- export const modulo = defn(F, null, [F, F], (x, y) => [
19
- ret(sub(x, mul(y, trunc(div(x, y))))),
18
+ const modulo = defn(F, null, [F, F], (x, y) => [
19
+ ret(sub(x, mul(y, trunc(div(x, y)))))
20
20
  ]);
21
- /**
22
- * Same as thi.ng/math foldback01(). Folds `x` into the closed [0..1] interval,
23
- * using infinite internal reflection on either side of the interval.
24
- *
25
- * @param x
26
- */
27
- export const foldback01 = defn(F, null, [F], (x) => {
28
- let y;
29
- return [
30
- (y = sym(mod(abs(x), FLOAT2))),
31
- ret(mix(y, sub(FLOAT2, y), step(FLOAT1, y))),
32
- ];
21
+ const foldback01 = defn(F, null, [F], (x) => {
22
+ let y;
23
+ return [
24
+ y = sym(mod(abs(x), FLOAT2)),
25
+ ret(mix(y, sub(FLOAT2, y), step(FLOAT1, y)))
26
+ ];
33
27
  });
28
+ export {
29
+ foldback01,
30
+ modulo,
31
+ trunc
32
+ };
package/math/magsq.js CHANGED
@@ -2,6 +2,11 @@ import { F } from "@thi.ng/shader-ast/api/types";
2
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
3
  import { dot } from "@thi.ng/shader-ast/builtin/math";
4
4
  const $ = (n) => defn(F, `magSq${n}`, [`vec${n}`], (v) => [ret(dot(v, v))]);
5
- export const magSq2 = $(2);
6
- export const magSq3 = $(3);
7
- export const magSq4 = $(4);
5
+ const magSq2 = $(2);
6
+ const magSq3 = $(3);
7
+ const magSq4 = $(4);
8
+ export {
9
+ magSq2,
10
+ magSq3,
11
+ magSq4
12
+ };
package/math/maxcomp.js CHANGED
@@ -1,20 +1,10 @@
1
1
  import { $w, $x, $y, $z } from "@thi.ng/shader-ast/ast/swizzle";
2
2
  import { max } from "@thi.ng/shader-ast/builtin/math";
3
- /**
4
- * Inline function. Returns max(v.x, v.y)
5
- *
6
- * @param v -
7
- */
8
- export const maxComp2 = (v) => max($x(v), $y(v));
9
- /**
10
- * Inline function. Returns max(v.x, v.y, v.z)
11
- *
12
- * @param v -
13
- */
14
- export const maxComp3 = (v) => max(maxComp2(v), $z(v));
15
- /**
16
- * Inline function. Returns max(v.x, v.y, v.z, v.w)
17
- *
18
- * @param v -
19
- */
20
- export const maxComp4 = (v) => max(maxComp3(v), $w(v));
3
+ const maxComp2 = (v) => max($x(v), $y(v));
4
+ const maxComp3 = (v) => max(maxComp2(v), $z(v));
5
+ const maxComp4 = (v) => max(maxComp3(v), $w(v));
6
+ export {
7
+ maxComp2,
8
+ maxComp3,
9
+ maxComp4
10
+ };
package/math/mincomp.js CHANGED
@@ -1,20 +1,10 @@
1
1
  import { $w, $x, $y, $z } from "@thi.ng/shader-ast/ast/swizzle";
2
2
  import { min } from "@thi.ng/shader-ast/builtin/math";
3
- /**
4
- * Inline function. Returns min(v.x, v.y)
5
- *
6
- * @param v -
7
- */
8
- export const minComp2 = (v) => min($x(v), $y(v));
9
- /**
10
- * Inline function. Returns min(v.x, v.y, v.z)
11
- *
12
- * @param v -
13
- */
14
- export const minComp3 = (v) => min(minComp2(v), $z(v));
15
- /**
16
- * Inline function. Returns min(v.x, v.y, v.z, v.w)
17
- *
18
- * @param v -
19
- */
20
- export const minComp4 = (v) => min(minComp3(v), $w(v));
3
+ const minComp2 = (v) => min($x(v), $y(v));
4
+ const minComp3 = (v) => min(minComp2(v), $z(v));
5
+ const minComp4 = (v) => min(minComp3(v), $w(v));
6
+ export {
7
+ minComp2,
8
+ minComp3,
9
+ minComp4
10
+ };
package/math/mix-cubic.js CHANGED
@@ -2,18 +2,40 @@ import { F, V2, V3, V4 } from "@thi.ng/shader-ast/api/types";
2
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
3
  import { add, mul, sub } from "@thi.ng/shader-ast/ast/ops";
4
4
  import { sym } from "@thi.ng/shader-ast/ast/sym";
5
- const $ = (n, type) => defn(type, `mixCubic${n > 1 ? n : ""}`, [type, type, type, type, F], (a, b, c, d, t) => {
5
+ const $ = (n, type) => defn(
6
+ type,
7
+ `mixCubic${n > 1 ? n : ""}`,
8
+ [type, type, type, type, F],
9
+ (a, b, c, d, t) => {
6
10
  let s;
7
11
  let s2;
8
12
  let t2;
9
13
  return [
10
- (t2 = sym(mul(t, t))),
11
- (s = sym(sub(1, t))),
12
- (s2 = sym(mul(s, s))),
13
- ret(add(add(add(mul(a, mul(s, s2)), mul(b, mul(3, mul(s2, t)))), mul(c, mul(3, mul(t2, s)))), mul(d, mul(t, t2)))),
14
+ t2 = sym(mul(t, t)),
15
+ s = sym(sub(1, t)),
16
+ s2 = sym(mul(s, s)),
17
+ ret(
18
+ add(
19
+ add(
20
+ add(
21
+ mul(a, mul(s, s2)),
22
+ mul(b, mul(3, mul(s2, t)))
23
+ ),
24
+ mul(c, mul(3, mul(t2, s)))
25
+ ),
26
+ mul(d, mul(t, t2))
27
+ )
28
+ )
14
29
  ];
15
- });
16
- export const mixCubic = $(1, F);
17
- export const mixCubic2 = $(2, V2);
18
- export const mixCubic3 = $(3, V3);
19
- export const mixCubic4 = $(4, V4);
30
+ }
31
+ );
32
+ const mixCubic = $(1, F);
33
+ const mixCubic2 = $(2, V2);
34
+ const mixCubic3 = $(3, V3);
35
+ const mixCubic4 = $(4, V4);
36
+ export {
37
+ mixCubic,
38
+ mixCubic2,
39
+ mixCubic3,
40
+ mixCubic4
41
+ };
@@ -2,14 +2,33 @@ import { F, V2, V3, V4 } from "@thi.ng/shader-ast/api/types";
2
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
3
  import { add, mul, sub } from "@thi.ng/shader-ast/ast/ops";
4
4
  import { sym } from "@thi.ng/shader-ast/ast/sym";
5
- const $ = (n, type) => defn(type, `mixQuadratic${n > 1 ? n : ""}`, [type, type, type, F], (a, b, c, t) => {
5
+ const $ = (n, type) => defn(
6
+ type,
7
+ `mixQuadratic${n > 1 ? n : ""}`,
8
+ [type, type, type, F],
9
+ (a, b, c, t) => {
6
10
  let s;
7
11
  return [
8
- (s = sym(sub(1, t))),
9
- ret(add(add(mul(a, mul(s, s)), mul(b, mul(2, mul(s, t)))), mul(c, mul(t, t)))),
12
+ s = sym(sub(1, t)),
13
+ ret(
14
+ add(
15
+ add(
16
+ mul(a, mul(s, s)),
17
+ mul(b, mul(2, mul(s, t)))
18
+ ),
19
+ mul(c, mul(t, t))
20
+ )
21
+ )
10
22
  ];
11
- });
12
- export const mixQuadratic = $(1, F);
13
- export const mixQuadratic2 = $(2, V2);
14
- export const mixQuadratic3 = $(3, V3);
15
- export const mixQuadratic4 = $(4, V4);
23
+ }
24
+ );
25
+ const mixQuadratic = $(1, F);
26
+ const mixQuadratic2 = $(2, V2);
27
+ const mixQuadratic3 = $(3, V3);
28
+ const mixQuadratic4 = $(4, V4);
29
+ export {
30
+ mixQuadratic,
31
+ mixQuadratic2,
32
+ mixQuadratic3,
33
+ mixQuadratic4
34
+ };
@@ -5,25 +5,19 @@ import { vec2, vec3 } from "@thi.ng/shader-ast/ast/lit";
5
5
  import { gt, neg } from "@thi.ng/shader-ast/ast/ops";
6
6
  import { $x, $y, $z } from "@thi.ng/shader-ast/ast/swizzle";
7
7
  import { abs } from "@thi.ng/shader-ast/builtin/math";
8
- /**
9
- * Inline function. Returns counter-clockwise perpendicular vector (assuming
10
- * Y-up). [-y, x]
11
- *
12
- * @param v -
13
- */
14
- export const perpendicularCCW = (v) => vec2(neg($y(v)), $x(v));
15
- /**
16
- * Inline function. Returns clockwise perpendicular vector (assuming Y-up).
17
- * [y,-x]
18
- *
19
- * @param v -
20
- */
21
- export const perpendicularCW = (v) => vec2($y(v), neg($x(v)));
22
- /**
23
- * Returns an orthogonal vector to `v`.
24
- *
25
- * http://lolengine.net/blog/2013/09/21/picking-orthogonal-vector-combing-coconuts
26
- */
27
- export const orthogonal3 = defn(V3, "orthogonal3", [V3], (v) => [
28
- ret(ternary(gt(abs($x(v)), abs($z(v))), vec3(neg($y(v)), $x(v), 0), vec3(0, neg($z(v)), $y(v)))),
8
+ const perpendicularCCW = (v) => vec2(neg($y(v)), $x(v));
9
+ const perpendicularCW = (v) => vec2($y(v), neg($x(v)));
10
+ const orthogonal3 = defn(V3, "orthogonal3", [V3], (v) => [
11
+ ret(
12
+ ternary(
13
+ gt(abs($x(v)), abs($z(v))),
14
+ vec3(neg($y(v)), $x(v), 0),
15
+ vec3(0, neg($z(v)), $y(v))
16
+ )
17
+ )
29
18
  ]);
19
+ export {
20
+ orthogonal3,
21
+ perpendicularCCW,
22
+ perpendicularCW
23
+ };
package/math/osc.js CHANGED
@@ -1,42 +1,31 @@
1
- import { float, FLOAT0, FLOAT05, FLOAT1, FLOAT2, TAU, } from "@thi.ng/shader-ast/ast/lit";
1
+ import {
2
+ float,
3
+ FLOAT0,
4
+ FLOAT05,
5
+ FLOAT1,
6
+ FLOAT2,
7
+ TAU
8
+ } from "@thi.ng/shader-ast/ast/lit";
2
9
  import { madd, mul, sub } from "@thi.ng/shader-ast/ast/ops";
3
10
  import { abs, fract, sin, step } from "@thi.ng/shader-ast/builtin/math";
4
11
  const defOsc = (fn) => (phase, freq, amp = FLOAT1, dc = FLOAT0) => madd(fn(mul(float(phase), float(freq))), amp, dc);
5
- /**
6
- * Sine oscillator (ported from thi.ng/dsp)
7
- *
8
- * @param phase - normalized phase
9
- * @param freq -
10
- * @param amp -
11
- * @param dc -
12
- */
13
- export const sinOsc = defOsc((phase) => sin(mul(phase, TAU)));
14
- /**
15
- * Sawtooth oscillator (ported from thi.ng/dsp)
16
- *
17
- * @param phase - normalized phase
18
- * @param freq -
19
- * @param amp -
20
- * @param dc -
21
- */
22
- export const sawOsc = defOsc((phase) => sub(FLOAT1, mul(fract(phase), FLOAT2)));
23
- /**
24
- * Triangle oscillator (ported from thi.ng/dsp)
25
- *
26
- * @param phase - normalized phase
27
- * @param freq -
28
- * @param amp -
29
- * @param dc -
30
- */
31
- export const triOsc = defOsc((phase) => sub(abs(madd(fract(phase), 4, float(-2))), FLOAT1));
32
- /**
33
- * Rect oscillator w/ customizable duty cycle (default: 0.5) (ported from
34
- * thi.ng/dsp)
35
- *
36
- * @param phase - normalized phase
37
- * @param freq -
38
- * @param amp -
39
- * @param dc -
40
- * @param duty -
41
- */
42
- export const rectOsc = (phase, freq, amp = FLOAT1, dc = FLOAT0, duty = FLOAT05) => madd(madd(step(float(duty), fract(mul(float(phase), float(freq)))), FLOAT2, float(-1)), amp, dc);
12
+ const sinOsc = defOsc((phase) => sin(mul(phase, TAU)));
13
+ const sawOsc = defOsc((phase) => sub(FLOAT1, mul(fract(phase), FLOAT2)));
14
+ const triOsc = defOsc(
15
+ (phase) => sub(abs(madd(fract(phase), 4, float(-2))), FLOAT1)
16
+ );
17
+ const rectOsc = (phase, freq, amp = FLOAT1, dc = FLOAT0, duty = FLOAT05) => madd(
18
+ madd(
19
+ step(float(duty), fract(mul(float(phase), float(freq)))),
20
+ FLOAT2,
21
+ float(-1)
22
+ ),
23
+ amp,
24
+ dc
25
+ );
26
+ export {
27
+ rectOsc,
28
+ sawOsc,
29
+ sinOsc,
30
+ triOsc
31
+ };
package/math/polar.js CHANGED
@@ -5,26 +5,17 @@ import { div } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { $x, $y, $z } from "@thi.ng/shader-ast/ast/swizzle";
6
6
  import { sym } from "@thi.ng/shader-ast/ast/sym";
7
7
  import { asin, atan, length } from "@thi.ng/shader-ast/builtin/math";
8
- /**
9
- * Converts 2D cartesian vector `v` to polar coordinates, i.e. `[r,θ]`
10
- * (angle in radians). See {@link cartesian2} for reverse operation.
11
- *
12
- * @param v -
13
- */
14
- export const polar2 = defn(V2, "polar2", [V2], (v) => [
15
- ret(vec2(length(v), atan(div($y(v), $x(v))))),
8
+ const polar2 = defn(V2, "polar2", [V2], (v) => [
9
+ ret(vec2(length(v), atan(div($y(v), $x(v)))))
16
10
  ]);
17
- /**
18
- * Converts 3D cartesian vector `v` to spherical coordinates, i.e.
19
- * `[r,θ,ϕ]` (angles in radians). See {@link cartesian3} for reverse
20
- * operation.
21
- *
22
- * @param v -
23
- */
24
- export const polar3 = defn(V3, "polar3", [V3], (v) => {
25
- let r;
26
- return [
27
- (r = sym(length(v))),
28
- ret(vec3(r, asin(div($z(v), r)), atan(div($y(v), $x(v))))),
29
- ];
11
+ const polar3 = defn(V3, "polar3", [V3], (v) => {
12
+ let r;
13
+ return [
14
+ r = sym(length(v)),
15
+ ret(vec3(r, asin(div($z(v), r)), atan(div($y(v), $x(v)))))
16
+ ];
30
17
  });
18
+ export {
19
+ polar2,
20
+ polar3
21
+ };
package/math/sincos.js CHANGED
@@ -1,14 +1,8 @@
1
1
  import { vec2 } from "@thi.ng/shader-ast/ast/lit";
2
2
  import { cos, sin } from "@thi.ng/shader-ast/builtin/math";
3
- /**
4
- * Inline function. Returns vec2(sin(x), cos(x)).
5
- *
6
- * @param x -
7
- */
8
- export const sincos = (x) => vec2(sin(x), cos(x));
9
- /**
10
- * Inline function. Returns vec2(cos(x), sin(x)).
11
- *
12
- * @param x -
13
- */
14
- export const cossin = (x) => vec2(cos(x), sin(x));
3
+ const sincos = (x) => vec2(sin(x), cos(x));
4
+ const cossin = (x) => vec2(cos(x), sin(x));
5
+ export {
6
+ cossin,
7
+ sincos
8
+ };
@@ -3,28 +3,30 @@ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
3
  import { add, div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
4
4
  import { clamp01 } from "./clamp.js";
5
5
  const $ = (n, type) => defn(type, `smootherStep01${n > 1 ? "_" + n : ""}`, [type], (x) => [
6
- // @ts-ignore
7
- ret(mul(x, mul(x, mul(x, add(mul(x, sub(mul(x, 6), 15)), 10))))),
6
+ // @ts-ignore
7
+ ret(mul(x, mul(x, mul(x, add(mul(x, sub(mul(x, 6), 15)), 10)))))
8
8
  ]);
9
- /**
10
- * Specialized version of {@link smootherStep}, assuming edges are 0/1
11
- * respectively and `x` is in [0..1]. No clamping performed.
12
- *
13
- * @param x
14
- */
15
- export const smootherStep01 = $(1, F);
16
- export const smootherStep01_2 = $(2, V2);
17
- export const smootherStep01_3 = $(3, V3);
18
- export const smootherStep01_4 = $(4, V4);
19
- const $$ = (n, type, fn) => defn(type, `smootherStep${n > 1 ? n : ""}`, [type, type, type], (e0, e1, x) => [ret(fn(clamp01(div(sub(x, e0), sub(e1, e0)))))]);
20
- /**
21
- * Similar to GLSL-native `smoothstep()` but using different, higher degree
22
- * polynomial.
23
- *
24
- * @remarks
25
- * Also see {@link smootherStep01}
26
- */
27
- export const smootherStep = $$(1, F, smootherStep01);
28
- export const smootherStep2 = $$(2, V2, smootherStep01_2);
29
- export const smootherStep3 = $$(3, V3, smootherStep01_3);
30
- export const smootherStep4 = $$(4, V4, smootherStep01_4);
9
+ const smootherStep01 = $(1, F);
10
+ const smootherStep01_2 = $(2, V2);
11
+ const smootherStep01_3 = $(3, V3);
12
+ const smootherStep01_4 = $(4, V4);
13
+ const $$ = (n, type, fn) => defn(
14
+ type,
15
+ `smootherStep${n > 1 ? n : ""}`,
16
+ [type, type, type],
17
+ (e0, e1, x) => [ret(fn(clamp01(div(sub(x, e0), sub(e1, e0)))))]
18
+ );
19
+ const smootherStep = $$(1, F, smootherStep01);
20
+ const smootherStep2 = $$(2, V2, smootherStep01_2);
21
+ const smootherStep3 = $$(3, V3, smootherStep01_3);
22
+ const smootherStep4 = $$(4, V4, smootherStep01_4);
23
+ export {
24
+ smootherStep,
25
+ smootherStep01,
26
+ smootherStep01_2,
27
+ smootherStep01_3,
28
+ smootherStep01_4,
29
+ smootherStep2,
30
+ smootherStep3,
31
+ smootherStep4
32
+ };
package/matrix/convert.js CHANGED
@@ -2,13 +2,24 @@ import { M2, M3, M4 } from "@thi.ng/shader-ast/api/types";
2
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
3
  import { indexMat } from "@thi.ng/shader-ast/ast/indexed";
4
4
  import { VEC3_0, mat3, mat4, vec3, vec4 } from "@thi.ng/shader-ast/ast/lit";
5
- export const m22ToM33 = defn(M3, "m22ToM33", [M2], (m) => {
6
- return [
7
- ret(mat3(vec3(indexMat(m, 0), 0), vec3(indexMat(m, 1), 0), VEC3_0)),
8
- ];
5
+ const m22ToM33 = defn(M3, "m22ToM33", [M2], (m) => {
6
+ return [
7
+ ret(mat3(vec3(indexMat(m, 0), 0), vec3(indexMat(m, 1), 0), VEC3_0))
8
+ ];
9
9
  });
10
- export const m33ToM44 = defn(M4, "m33ToM44", [M3], (m) => {
11
- return [
12
- ret(mat4(vec4(indexMat(m, 0), 0), vec4(indexMat(m, 1), 0), vec4(indexMat(m, 2), 0), vec4())),
13
- ];
10
+ const m33ToM44 = defn(M4, "m33ToM44", [M3], (m) => {
11
+ return [
12
+ ret(
13
+ mat4(
14
+ vec4(indexMat(m, 0), 0),
15
+ vec4(indexMat(m, 1), 0),
16
+ vec4(indexMat(m, 2), 0),
17
+ vec4()
18
+ )
19
+ )
20
+ ];
14
21
  });
22
+ export {
23
+ m22ToM33,
24
+ m33ToM44
25
+ };
package/matrix/lookat.js CHANGED
@@ -4,22 +4,24 @@ import { mat4, vec4 } from "@thi.ng/shader-ast/ast/lit";
4
4
  import { neg, sub } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { sym } from "@thi.ng/shader-ast/ast/sym";
6
6
  import { cross, dot, normalize } from "@thi.ng/shader-ast/builtin/math";
7
- /**
8
- * Creates a mat4 view matrix from given `eyePos`, `target` and `up`
9
- * vector.
10
- *
11
- * @param eye - vec3
12
- * @param target - vec3
13
- * @param up - vec3
14
- */
15
- export const lookat = defn(M4, "lookat", [V3, V3, V3], (eye, target, up) => {
16
- let x;
17
- let y;
18
- let z;
19
- return [
20
- (z = sym(normalize(sub(eye, target)))),
21
- (x = sym(normalize(cross(up, z)))),
22
- (y = sym(normalize(cross(z, x)))),
23
- ret(mat4(vec4(x, neg(dot(eye, x))), vec4(up, neg(dot(eye, y))), vec4(z, neg(dot(eye, z))), vec4(0, 0, 0, 1))),
24
- ];
7
+ const lookat = defn(M4, "lookat", [V3, V3, V3], (eye, target, up) => {
8
+ let x;
9
+ let y;
10
+ let z;
11
+ return [
12
+ z = sym(normalize(sub(eye, target))),
13
+ x = sym(normalize(cross(up, z))),
14
+ y = sym(normalize(cross(z, x))),
15
+ ret(
16
+ mat4(
17
+ vec4(x, neg(dot(eye, x))),
18
+ vec4(up, neg(dot(eye, y))),
19
+ vec4(z, neg(dot(eye, z))),
20
+ vec4(0, 0, 0, 1)
21
+ )
22
+ )
23
+ ];
25
24
  });
25
+ export {
26
+ lookat
27
+ };
package/matrix/mvp.js CHANGED
@@ -1,12 +1,6 @@
1
1
  import { FLOAT1, vec4 } from "@thi.ng/shader-ast/ast/lit";
2
2
  import { mul } from "@thi.ng/shader-ast/ast/ops";
3
- /**
4
- * Inline function. Multiplies `pos` with given model, view & projection
5
- * matrices (in order). `p` is extended to a vec4.
6
- *
7
- * @param p -
8
- * @param model -
9
- * @param view -
10
- * @param proj -
11
- */
12
- export const transformMVP = (p, model, view, proj) => mul(mul(proj, mul(view, model)), vec4(p, FLOAT1));
3
+ const transformMVP = (p, model, view, proj) => mul(mul(proj, mul(view, model)), vec4(p, FLOAT1));
4
+ export {
5
+ transformMVP
6
+ };
package/matrix/normal.js CHANGED
@@ -2,11 +2,7 @@ import { vec4 } from "@thi.ng/shader-ast/ast/lit";
2
2
  import { mul } from "@thi.ng/shader-ast/ast/ops";
3
3
  import { $xyz } from "@thi.ng/shader-ast/ast/swizzle";
4
4
  import { normalize } from "@thi.ng/shader-ast/builtin/math";
5
- /**
6
- * Inline function. Multiplies `normal` with given 4x4 normal matrix
7
- * (e.g. transpose inverse of view * model).
8
- *
9
- * @param n -
10
- * @param normalMat -
11
- */
12
- export const surfaceNormal = (n, normalMat) => normalize($xyz(mul(normalMat, vec4(n, 0))));
5
+ const surfaceNormal = (n, normalMat) => normalize($xyz(mul(normalMat, vec4(n, 0))));
6
+ export {
7
+ surfaceNormal
8
+ };