@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/viz/function.js CHANGED
@@ -2,76 +2,78 @@ import { F, V2 } from "@thi.ng/shader-ast/api/types";
2
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
3
3
  import { forLoop, ifThen } from "@thi.ng/shader-ast/ast/controlflow";
4
4
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
5
- import { FLOAT0, FLOAT2, VEC2_0, VEC2_1, float, vec2, } from "@thi.ng/shader-ast/ast/lit";
6
- import { add, div, gt, inc, lt, lte, or, sub, } from "@thi.ng/shader-ast/ast/ops";
5
+ import {
6
+ FLOAT0,
7
+ FLOAT2,
8
+ VEC2_0,
9
+ VEC2_1,
10
+ float,
11
+ vec2
12
+ } from "@thi.ng/shader-ast/ast/lit";
13
+ import {
14
+ add,
15
+ div,
16
+ gt,
17
+ inc,
18
+ lt,
19
+ lte,
20
+ or,
21
+ sub
22
+ } from "@thi.ng/shader-ast/ast/ops";
7
23
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
8
24
  import { sym } from "@thi.ng/shader-ast/ast/sym";
9
25
  import { fit } from "../math/fit.js";
10
- /**
11
- * Higher order function. Takes a unary 1D function `fn` to evaluate and plot
12
- * within a given interval. Also takes a point mapping function `map` to
13
- * transform fragment coordinates into the function domain. Returns a new
14
- * specialized function, which then accepts 2x vec2 args: a fragment coordinate
15
- * and overall viewport dimensions. It then transforms the point into the
16
- * function domain and evaluates/samples the function `fn` in the sub-pixel
17
- * region around the input position. Returns a coverage ratio of how many
18
- * samples where on the curve.
19
- *
20
- * @remarks
21
- * The function domain, sampling region & quality can be configured via options.
22
- * See {@link FnSampleOpts} for details.
23
- *
24
- * See {@link functionDomainMapper} for a configurable point mapping function.
25
- */
26
- export const functionSampler = (fn, map, opts) => {
27
- const { min, max, radius, step, area } = {
28
- min: 0,
29
- max: 1,
30
- radius: 1,
31
- step: 4,
32
- area: false,
33
- ...opts,
34
- };
35
- return defn(F, undefined, [V2, V2], (frag, res) => {
36
- let count, total;
37
- let q;
38
- const invStep = float((2 * radius) / step);
39
- return [
40
- (q = sym(map(frag, res))),
41
- // bail out early if outside [min..max] interval
42
- ifThen(or(lt($x(q), float(min)), gt($x(q), float(max))), [
43
- ret(FLOAT0),
44
- ]),
45
- (count = sym(FLOAT0)),
46
- (total = sym(FLOAT0)),
47
- forLoop(sym(float(-radius)), (x) => lte(x, float(radius)), (x) => assign(x, add(x, invStep)), (x) => [
48
- forLoop(sym(float(-radius)), (y) => lte(y, float(radius)), (y) => assign(y, add(y, invStep)), (y) => [
49
- inc(total),
50
- assign(q, map(add(frag, vec2(x, y)), res)),
51
- ifThen(gt(fn($x(q)), $y(q)), [inc(count)]),
52
- ]),
53
- ]),
54
- ...(!area
55
- ? [
56
- ifThen(gt(count, div(total, FLOAT2)), [
57
- assign(count, sub(total, count)),
58
- ]),
59
- ]
60
- : []),
61
- ret(div(count, total)),
62
- ];
63
- });
26
+ const functionSampler = (fn, map, opts) => {
27
+ const { min, max, radius, step, area } = {
28
+ min: 0,
29
+ max: 1,
30
+ radius: 1,
31
+ step: 4,
32
+ area: false,
33
+ ...opts
34
+ };
35
+ return defn(F, void 0, [V2, V2], (frag, res) => {
36
+ let count, total;
37
+ let q;
38
+ const invStep = float(2 * radius / step);
39
+ return [
40
+ q = sym(map(frag, res)),
41
+ // bail out early if outside [min..max] interval
42
+ ifThen(or(lt($x(q), float(min)), gt($x(q), float(max))), [
43
+ ret(FLOAT0)
44
+ ]),
45
+ count = sym(FLOAT0),
46
+ total = sym(FLOAT0),
47
+ forLoop(
48
+ sym(float(-radius)),
49
+ (x) => lte(x, float(radius)),
50
+ (x) => assign(x, add(x, invStep)),
51
+ (x) => [
52
+ forLoop(
53
+ sym(float(-radius)),
54
+ (y) => lte(y, float(radius)),
55
+ (y) => assign(y, add(y, invStep)),
56
+ (y) => [
57
+ inc(total),
58
+ assign(q, map(add(frag, vec2(x, y)), res)),
59
+ ifThen(gt(fn($x(q)), $y(q)), [inc(count)])
60
+ ]
61
+ )
62
+ ]
63
+ ),
64
+ ...!area ? [
65
+ ifThen(gt(count, div(total, FLOAT2)), [
66
+ assign(count, sub(total, count))
67
+ ])
68
+ ] : [],
69
+ ret(div(count, total))
70
+ ];
71
+ });
64
72
  };
65
- /**
66
- * Higher order helper function for {@link functionSampler} to map fragment
67
- * coordinates within a screen rect (defined by `pos` & `size` in UV space) into
68
- * the domain of the sampled function interval.
69
- *
70
- * @param amin
71
- * @param amax
72
- * @param bmin
73
- * @param bmax
74
- */
75
- export const functionDomainMapper = (amin, amax, bmin = VEC2_0, bmax = VEC2_1) => defn(V2, undefined, [V2, V2], (p, res) => [
76
- ret(fit(div(p, res), amin, amax, bmin, bmax)),
73
+ const functionDomainMapper = (amin, amax, bmin = VEC2_0, bmax = VEC2_1) => defn(V2, void 0, [V2, V2], (p, res) => [
74
+ ret(fit(div(p, res), amin, amax, bmin, bmax))
77
75
  ]);
76
+ export {
77
+ functionDomainMapper,
78
+ functionSampler
79
+ };