@thi.ng/shader-ast-stdlib 0.16.0 → 0.16.1

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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-10-27T16:56:24Z
3
+ - **Last updated**: 2023-10-27T20:44:01Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,12 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ### [0.16.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-stdlib@0.16.1) (2023-10-27)
13
+
14
+ #### 🩹 Bug fixes
15
+
16
+ - fix pkg exports ([20d5d2d](https://github.com/thi-ng/umbrella/commit/20d5d2d))
17
+
12
18
  ## [0.16.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-stdlib@0.16.0) (2023-10-27)
13
19
 
14
20
  #### 🚀 Features
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Predicate function to check if given point `p` is inside the circle defined
3
+ * by `pos` and `radius`.
4
+ */
5
+ export declare const isPointInCircle: import("@thi.ng/shader-ast").TaggedFn3<"vec2", "vec2", "float", "bool">;
6
+ /**
7
+ * Predicate function to check if given point `p` is inside the axis-aligned
8
+ * rect defined by `pos` and `size`. Branchless.
9
+ */
10
+ export declare const isPointInRect: import("@thi.ng/shader-ast").TaggedFn3<"vec2", "vec2", "vec2", "bool">;
11
+ //# sourceMappingURL=point.d.ts.map
package/isec/point.js ADDED
@@ -0,0 +1,23 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
2
+ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
+ import { bool } from "@thi.ng/shader-ast/ast/lit";
4
+ import { add } from "@thi.ng/shader-ast/ast/ops";
5
+ import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
6
+ import { sym } from "@thi.ng/shader-ast/ast/sym";
7
+ import { distance, min, step } from "@thi.ng/shader-ast/builtin/math";
8
+ /**
9
+ * Predicate function to check if given point `p` is inside the circle defined
10
+ * by `pos` and `radius`.
11
+ */
12
+ export const isPointInCircle = defn("bool", null, [V2, V2, F], (p, pos, radius) => [ret(bool(step(distance(p, pos), radius)))]);
13
+ /**
14
+ * Predicate function to check if given point `p` is inside the axis-aligned
15
+ * rect defined by `pos` and `size`. Branchless.
16
+ */
17
+ export const isPointInRect = defn("bool", null, [V2, V2, V2], (p, pos, size) => {
18
+ let q;
19
+ return [
20
+ (q = sym(min(step(pos, p), step(p, add(pos, size))))),
21
+ ret(bool(min($x(q), $y(q)))),
22
+ ];
23
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/shader-ast-stdlib",
3
- "version": "0.16.0",
3
+ "version": "0.16.1",
4
4
  "description": "Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -86,6 +86,7 @@
86
86
  "./*.d.ts",
87
87
  "color",
88
88
  "fog",
89
+ "isec",
89
90
  "light",
90
91
  "math",
91
92
  "matrix",
@@ -93,7 +94,8 @@
93
94
  "raymarch",
94
95
  "screen",
95
96
  "sdf",
96
- "tex"
97
+ "tex",
98
+ "viz"
97
99
  ],
98
100
  "exports": {
99
101
  ".": {
@@ -352,5 +354,5 @@
352
354
  ],
353
355
  "year": 2019
354
356
  },
355
- "gitHead": "502e8fa937677ff7bc4fbd0906d8c8b4b0b471e5\n"
357
+ "gitHead": "f540c4a501157b764229035035c2a858bace2a61\n"
356
358
  }
@@ -0,0 +1,68 @@
1
+ import type { Func1, Func2, Vec2Term } from "@thi.ng/shader-ast";
2
+ export interface FnSampleOpts {
3
+ /**
4
+ * Pixel radius per axis. The function will be sampled at ± N around the
5
+ * given base fragment position.
6
+ *
7
+ * @defaultValue 1
8
+ */
9
+ radius: number;
10
+ /**
11
+ * Number of sub-fragment steps per axis. The function will be sampled at ±
12
+ * {@link FnSampleOpts.radius} around the given base fragment position.
13
+ *
14
+ * @defaultValue 4
15
+ */
16
+ steps: number;
17
+ /**
18
+ * If true, the {@link functionSampler} will return 1.0 for the entire area
19
+ * below the curve (i.e. turns a line plot into an area plot). By default
20
+ * only the fragments touching the curve will produce non-zero results.
21
+ *
22
+ * @defaultValue false
23
+ */
24
+ area: boolean;
25
+ /**
26
+ * Min X position (in function domain). The function will only be evaluated
27
+ * within the [min..max] domain.
28
+ *
29
+ * @defaultValue 0
30
+ */
31
+ min: number;
32
+ /**
33
+ * Max X position (in function domain). The function will only be evaluated
34
+ * within the [min..max] domain.
35
+ *
36
+ * @defaultValue 1
37
+ */
38
+ max: number;
39
+ }
40
+ /**
41
+ * Higher order function. Takes a unary 1D function `fn` to evaluate and plot
42
+ * within a given interval. Also takes a point mapping function `map` to
43
+ * transform fragment coordinates into the function domain. Returns a new
44
+ * specialized function, which then accepts 2x vec2 args: a fragment coordinate
45
+ * and overall viewport dimensions. It then transforms the point into the
46
+ * function domain and evaluates/samples the function `fn` in the sub-pixel
47
+ * region around the input position. Returns a coverage ratio of how many
48
+ * samples where on the curve.
49
+ *
50
+ * @remarks
51
+ * The function domain, sampling region & quality can be configured via options.
52
+ * See {@link FnSampleOpts} for details.
53
+ *
54
+ * See {@link functionDomainMapper} for a configurable point mapping function.
55
+ */
56
+ export declare const functionSampler: (fn: Func1<"float", "float">, map: Func2<"vec2", "vec2", "vec2">, opts?: Partial<FnSampleOpts>) => import("@thi.ng/shader-ast").TaggedFn2<"vec2", "vec2", "float">;
57
+ /**
58
+ * Higher order helper function for {@link functionSampler} to map fragment
59
+ * coordinates within a screen rect (defined by `pos` & `size` in UV space) into
60
+ * the domain of the sampled function interval.
61
+ *
62
+ * @param amin
63
+ * @param amax
64
+ * @param bmin
65
+ * @param bmax
66
+ */
67
+ export declare const functionDomainMapper: (amin: Vec2Term, amax: Vec2Term, bmin?: Vec2Term, bmax?: Vec2Term) => import("@thi.ng/shader-ast").TaggedFn2<"vec2", "vec2", "vec2">;
68
+ //# sourceMappingURL=function.d.ts.map
@@ -0,0 +1,77 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
2
+ import { assign } from "@thi.ng/shader-ast/ast/assign";
3
+ import { forLoop, ifThen } from "@thi.ng/shader-ast/ast/controlflow";
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";
7
+ import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
8
+ import { sym } from "@thi.ng/shader-ast/ast/sym";
9
+ 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
+ });
64
+ };
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)),
77
+ ]);