@thi.ng/shader-ast-stdlib 0.11.6 → 0.12.0

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**: 2022-04-07T14:17:30Z
3
+ - **Last updated**: 2022-05-07T11:33:35Z
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,17 @@ 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.12.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-stdlib@0.12.0) (2022-05-07)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add more 2D SDF prims ([2672e75](https://github.com/thi-ng/umbrella/commit/2672e75))
17
+ - add 2D SDF arc/bezier fns ([07bd445](https://github.com/thi-ng/umbrella/commit/07bd445))
18
+
19
+ #### ♻️ Refactoring
20
+
21
+ - update cross2() as non-inline fn ([59d631a](https://github.com/thi-ng/umbrella/commit/59d631a))
22
+
12
23
  ## [0.11.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-stdlib@0.11.0) (2021-11-17)
13
24
 
14
25
  #### 🚀 Features
package/README.md CHANGED
@@ -104,7 +104,7 @@ node --experimental-repl-await
104
104
  > const shaderAstStdlib = await import("@thi.ng/shader-ast-stdlib");
105
105
  ```
106
106
 
107
- Package sizes (gzipped, pre-treeshake): ESM: 11.05 KB
107
+ Package sizes (gzipped, pre-treeshake): ESM: 12.82 KB
108
108
 
109
109
  ## Dependencies
110
110
 
@@ -443,6 +443,7 @@ for reference.
443
443
 
444
444
  #### Primitives
445
445
 
446
+ - `sdfArc2`
446
447
  - `sdfBox2`
447
448
  - `sdfBox3`
448
449
  - `sdfCircle`
@@ -451,6 +452,7 @@ for reference.
451
452
  - `sdfLine3`
452
453
  - `sdfPlane2`
453
454
  - `sdfPlane3`
455
+ - `sdfQuadratic2`
454
456
  - `sdfSphere`
455
457
  - `sdfTorus`
456
458
  - `sdfTriangle2`
package/index.d.ts CHANGED
@@ -44,13 +44,19 @@ export * from "./raymarch/point-at.js";
44
44
  export * from "./raymarch/scene.js";
45
45
  export * from "./screen/uv.js";
46
46
  export * from "./sdf/annular.js";
47
+ export * from "./sdf/arc.js";
48
+ export * from "./sdf/bezier.js";
47
49
  export * from "./sdf/box.js";
50
+ export * from "./sdf/box-rounded.js";
51
+ export * from "./sdf/cross.js";
48
52
  export * from "./sdf/cylinder.js";
53
+ export * from "./sdf/hex.js";
49
54
  export * from "./sdf/isec.js";
50
55
  export * from "./sdf/line.js";
51
56
  export * from "./sdf/mirror.js";
52
57
  export * from "./sdf/plane.js";
53
58
  export * from "./sdf/polyhedra.js";
59
+ export * from "./sdf/polygon.js";
54
60
  export * from "./sdf/repeat.js";
55
61
  export * from "./sdf/repeat-polar.js";
56
62
  export * from "./sdf/round.js";
package/index.js CHANGED
@@ -44,13 +44,19 @@ export * from "./raymarch/point-at.js";
44
44
  export * from "./raymarch/scene.js";
45
45
  export * from "./screen/uv.js";
46
46
  export * from "./sdf/annular.js";
47
+ export * from "./sdf/arc.js";
48
+ export * from "./sdf/bezier.js";
47
49
  export * from "./sdf/box.js";
50
+ export * from "./sdf/box-rounded.js";
51
+ export * from "./sdf/cross.js";
48
52
  export * from "./sdf/cylinder.js";
53
+ export * from "./sdf/hex.js";
49
54
  export * from "./sdf/isec.js";
50
55
  export * from "./sdf/line.js";
51
56
  export * from "./sdf/mirror.js";
52
57
  export * from "./sdf/plane.js";
53
58
  export * from "./sdf/polyhedra.js";
59
+ export * from "./sdf/polygon.js";
54
60
  export * from "./sdf/repeat.js";
55
61
  export * from "./sdf/repeat-polar.js";
56
62
  export * from "./sdf/round.js";
package/math/cross2.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- import type { FloatTerm, Vec2Term } from "@thi.ng/shader-ast";
1
+ import type { FloatTerm } from "@thi.ng/shader-ast";
2
2
  /**
3
- * Inline function. Computes 2D "cross product" of given vectors. See
3
+ * Computes 2D "cross product" of given vectors. Also see
4
4
  * {@link crossC2}.
5
5
  *
6
6
  * @param a -
7
7
  * @param b -
8
8
  */
9
- export declare const cross2: (a: Vec2Term, b: Vec2Term) => import("@thi.ng/shader-ast").Op2<"float">;
9
+ export declare const cross2: import("@thi.ng/shader-ast").TaggedFn2<"vec2", "vec2", "float">;
10
10
  /**
11
11
  * Inline function. Computes 2D cross product of given individual
12
12
  * components: ax * by - ay * bx
package/math/cross2.js CHANGED
@@ -1,13 +1,16 @@
1
+ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
1
2
  import { mul, sub } from "@thi.ng/shader-ast/ast/ops";
2
3
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
3
4
  /**
4
- * Inline function. Computes 2D "cross product" of given vectors. See
5
+ * Computes 2D "cross product" of given vectors. Also see
5
6
  * {@link crossC2}.
6
7
  *
7
8
  * @param a -
8
9
  * @param b -
9
10
  */
10
- export const cross2 = (a, b) => crossC2($x(a), $y(a), $x(b), $y(b));
11
+ export const cross2 = defn("float", "cross2", ["vec2", "vec2"], (a, b) => [
12
+ ret(sub(mul($x(a), $y(b)), mul($y(a), $x(b)))),
13
+ ]);
11
14
  /**
12
15
  * Inline function. Computes 2D cross product of given individual
13
16
  * components: ax * by - ay * bx
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/shader-ast-stdlib",
3
- "version": "0.11.6",
3
+ "version": "0.12.0",
4
4
  "description": "Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -34,16 +34,16 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.3.5",
38
- "@thi.ng/shader-ast": "^0.12.6"
37
+ "@thi.ng/api": "^8.3.6",
38
+ "@thi.ng/shader-ast": "^0.12.7"
39
39
  },
40
40
  "devDependencies": {
41
- "@microsoft/api-extractor": "^7.19.4",
42
- "@thi.ng/testament": "^0.2.5",
41
+ "@microsoft/api-extractor": "^7.23.1",
42
+ "@thi.ng/testament": "^0.2.6",
43
43
  "rimraf": "^3.0.2",
44
44
  "tools": "^0.0.1",
45
- "typedoc": "^0.22.13",
46
- "typescript": "^4.6.2"
45
+ "typedoc": "^0.22.15",
46
+ "typescript": "^4.6.4"
47
47
  },
48
48
  "keywords": [
49
49
  "ast",
@@ -236,12 +236,27 @@
236
236
  "./sdf/annular": {
237
237
  "default": "./sdf/annular.js"
238
238
  },
239
+ "./sdf/arc": {
240
+ "default": "./sdf/arc.js"
241
+ },
242
+ "./sdf/bezier": {
243
+ "default": "./sdf/bezier.js"
244
+ },
245
+ "./sdf/box-rounded": {
246
+ "default": "./sdf/box-rounded.js"
247
+ },
239
248
  "./sdf/box": {
240
249
  "default": "./sdf/box.js"
241
250
  },
251
+ "./sdf/cross": {
252
+ "default": "./sdf/cross.js"
253
+ },
242
254
  "./sdf/cylinder": {
243
255
  "default": "./sdf/cylinder.js"
244
256
  },
257
+ "./sdf/hex": {
258
+ "default": "./sdf/hex.js"
259
+ },
245
260
  "./sdf/isec": {
246
261
  "default": "./sdf/isec.js"
247
262
  },
@@ -254,6 +269,9 @@
254
269
  "./sdf/plane": {
255
270
  "default": "./sdf/plane.js"
256
271
  },
272
+ "./sdf/polygon": {
273
+ "default": "./sdf/polygon.js"
274
+ },
257
275
  "./sdf/polyhedra": {
258
276
  "default": "./sdf/polyhedra.js"
259
277
  },
@@ -312,5 +330,5 @@
312
330
  ],
313
331
  "year": 2019
314
332
  },
315
- "gitHead": "5ee1feb590dd935593b1dd4e7f38a3ed3ba64765\n"
333
+ "gitHead": "cf084be5fd5932226054d2dd32bad35481379f5d\n"
316
334
  }
package/sdf/arc.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Returns signed distance from `p` to 2D circular arc with `aperture` (in
3
+ * [0..π] interval), radius `ra` and `thickness`.
4
+ *
5
+ * @remarks
6
+ * Slightly modified version (easier to use aperture control) of original GLSL
7
+ * impl by Inigo Quilez:
8
+ *
9
+ * - https://www.shadertoy.com/view/wl23RK
10
+ * - https://iquilezles.org/articles/distfunctions2d/
11
+ */
12
+ export declare const sdfArc2: import("@thi.ng/shader-ast").TaggedFn4<"vec2", "float", "float", "float", "float">;
13
+ //# sourceMappingURL=arc.d.ts.map
package/sdf/arc.js ADDED
@@ -0,0 +1,27 @@
1
+ import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
2
+ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
+ import { vec2 } from "@thi.ng/shader-ast/ast/lit";
4
+ import { gt, madd, mul, neg, sub } 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 { abs, cos, length, sin } from "@thi.ng/shader-ast/builtin/math";
8
+ /**
9
+ * Returns signed distance from `p` to 2D circular arc with `aperture` (in
10
+ * [0..π] interval), radius `ra` and `thickness`.
11
+ *
12
+ * @remarks
13
+ * Slightly modified version (easier to use aperture control) of original GLSL
14
+ * impl by Inigo Quilez:
15
+ *
16
+ * - https://www.shadertoy.com/view/wl23RK
17
+ * - https://iquilezles.org/articles/distfunctions2d/
18
+ */
19
+ export const sdfArc2 = defn("float", "sdfArc", ["vec2", "float", "float", "float"], (p, apert, ra, rb) => {
20
+ let q;
21
+ let sc;
22
+ return [
23
+ (q = sym(vec2(abs($x(p)), $y(p)))),
24
+ (sc = sym(vec2(sin(apert), cos(apert)))),
25
+ ret(sub(ternary(gt(mul($y(sc), $x(q)), mul($x(sc), $y(q))), length(madd(sc, neg(ra), q)), abs(sub(length(q), ra))), rb)),
26
+ ];
27
+ });
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Returns signed distance from `p` to 2D quadratic bezier (given by points A,
3
+ * B, C).
4
+ *
5
+ * @remarks
6
+ * Ported from original GLSL impl by Inigo Quilez:
7
+ *
8
+ * - https://www.shadertoy.com/view/MlKcDD
9
+ * - https://iquilezles.org/articles/distfunctions2d/
10
+ */
11
+ export declare const sdfQuadratic2: import("@thi.ng/shader-ast").TaggedFn4<"vec2", "vec2", "vec2", "vec2", "float">;
12
+ //# sourceMappingURL=bezier.d.ts.map
package/sdf/bezier.js ADDED
@@ -0,0 +1,72 @@
1
+ import { assign } from "@thi.ng/shader-ast/ast/assign";
2
+ import { ifThen } from "@thi.ng/shader-ast/ast/controlflow";
3
+ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
4
+ import { float, FLOAT0, FLOAT1, FLOAT2, vec2, } from "@thi.ng/shader-ast/ast/lit";
5
+ import { add, div, gte, lt, madd, mul, neg, sub, } from "@thi.ng/shader-ast/ast/ops";
6
+ import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
7
+ import { sym } from "@thi.ng/shader-ast/ast/sym";
8
+ import { abs, acos, cos, dot, powf, sign, sin, sqrt, } from "@thi.ng/shader-ast/builtin/math";
9
+ import { clamp01 } from "../math/clamp.js";
10
+ import { cross2 } from "../math/cross2.js";
11
+ /**
12
+ * Returns signed distance from `p` to 2D quadratic bezier (given by points A,
13
+ * B, C).
14
+ *
15
+ * @remarks
16
+ * Ported from original GLSL impl by Inigo Quilez:
17
+ *
18
+ * - https://www.shadertoy.com/view/MlKcDD
19
+ * - https://iquilezles.org/articles/distfunctions2d/
20
+ */
21
+ export const sdfQuadratic2 = defn("float", "sdfQuadratic2", ["vec2", "vec2", "vec2", "vec2"], (pos, A, B, C) => {
22
+ let a, b, c, d;
23
+ let kk, kx, ky, kz;
24
+ let p, q, p3, q2, h;
25
+ let m, n, t, v, z;
26
+ let res, sgn;
27
+ let x, y, uv;
28
+ return [
29
+ (a = sym(sub(B, A))),
30
+ (b = sym(add(madd(B, -2, A), C))),
31
+ (c = sym(mul(a, 2))),
32
+ (d = sym(sub(A, pos))),
33
+ (kk = sym(div(FLOAT1, dot(b, b)))),
34
+ (kx = sym(mul(kk, dot(a, b)))),
35
+ (ky = sym(mul(kk, div(madd(FLOAT2, dot(a, a), dot(d, b)), 3)))),
36
+ (kz = sym(mul(kk, dot(d, a)))),
37
+ (res = sym(FLOAT0)),
38
+ (sgn = sym(FLOAT0)),
39
+ (p = sym(sub(ky, mul(kx, kx)))),
40
+ (q = sym(madd(sub(mul(mul(FLOAT2, kx), kx), mul(3, ky)), kx, kz))),
41
+ (p3 = sym(mul(mul(p, p), p))),
42
+ (q2 = sym(mul(q, q))),
43
+ (h = sym(madd(p3, 4, q2))),
44
+ ifThen(gte(h, FLOAT0), [
45
+ assign(h, sqrt(h)),
46
+ (x = sym(div(sub(vec2(h, neg(h)), q), FLOAT2))),
47
+ (uv = sym(mul(sign(x), powf(abs(x), float(1 / 3))))),
48
+ (t = sym(clamp01(sub(add($x(uv), $y(uv)), kx)))),
49
+ assign(x, madd(madd(b, t, c), t, d)),
50
+ assign(res, dot(x, x)),
51
+ assign(sgn, cross2(madd(b, mul(t, FLOAT2), c), x)),
52
+ ], [
53
+ (z = sym(sqrt(neg(p)))),
54
+ (v = sym(div(acos(div(q, mul(mul(p, z), FLOAT2))), float(3)))),
55
+ (m = sym(cos(v))),
56
+ (n = sym(mul(sin(v), 1.732050808))),
57
+ (uv = sym(clamp01(sub(mul(vec2(add(m, m), sub(neg(n), m)), z), kx)))),
58
+ (x = sym(madd(madd(b, $x(uv), c), $x(uv), d))),
59
+ (y = sym(madd(madd(b, $y(uv), c), $y(uv), d))),
60
+ assign(m, dot(x, x)),
61
+ assign(n, dot(y, y)),
62
+ ifThen(lt(m, n), [
63
+ assign(res, m),
64
+ assign(sgn, cross2(madd(b, mul($x(uv), 2), c), x)),
65
+ ], [
66
+ assign(res, n),
67
+ assign(sgn, cross2(madd(b, mul($y(uv), 2), c), y)),
68
+ ]),
69
+ ]),
70
+ ret(mul(sign(sgn), sqrt(res))),
71
+ ];
72
+ });
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Returns signed distance from `p` to centered 2D rect of `size` and corner
3
+ * radii defined by given vec4 `r`.
4
+ *
5
+ * @remarks
6
+ * Ported from original GLSL impl by Inigo Quilez:
7
+ * - https://iquilezles.org/articles/distfunctions2d/
8
+ *
9
+ * @param p - vec2
10
+ * @param size - vec2
11
+ * @param r - vec4
12
+ */
13
+ export declare const sdfBoxRounded: import("@thi.ng/shader-ast").TaggedFn3<"vec2", "vec2", "vec4", "float">;
14
+ //# sourceMappingURL=box-rounded.d.ts.map
@@ -0,0 +1,29 @@
1
+ import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
2
+ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
+ import { FLOAT0, VEC2_0 } from "@thi.ng/shader-ast/ast/lit";
4
+ import { add, gt, sub } from "@thi.ng/shader-ast/ast/ops";
5
+ import { $, $x, $xy, $y } from "@thi.ng/shader-ast/ast/swizzle";
6
+ import { sym } from "@thi.ng/shader-ast/ast/sym";
7
+ import { abs, length, max, min } from "@thi.ng/shader-ast/builtin/math";
8
+ import { maxComp2 } from "../math/maxcomp.js";
9
+ /**
10
+ * Returns signed distance from `p` to centered 2D rect of `size` and corner
11
+ * radii defined by given vec4 `r`.
12
+ *
13
+ * @remarks
14
+ * Ported from original GLSL impl by Inigo Quilez:
15
+ * - https://iquilezles.org/articles/distfunctions2d/
16
+ *
17
+ * @param p - vec2
18
+ * @param size - vec2
19
+ * @param r - vec4
20
+ */
21
+ export const sdfBoxRounded = defn("float", "sdfBoxRounded", ["vec2", "vec2", "vec4"], (p, size, r) => {
22
+ let q, t, d;
23
+ return [
24
+ (t = sym(ternary(gt($x(p), FLOAT0), $xy(r), $(r, "zw")))),
25
+ (d = sym(ternary(gt($y(p), FLOAT0), $x(t), $y(t)))),
26
+ (q = sym(add(sub(abs(p), size), d))),
27
+ ret(sub(add(min(maxComp2(q), FLOAT0), length(max(q, VEC2_0))), d)),
28
+ ];
29
+ });
package/sdf/cross.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Returns signed distance from `p` to 2D cross/plus of given `size` and corner
3
+ * radius `r`.
4
+ *
5
+ * @remarks
6
+ * - `size` consist of overall width/size (in x) and thickness (in y component)
7
+ * - corner radius can also be negative
8
+ *
9
+ * Ported from original GLSL impl by Inigo Quilez:
10
+ * - https://iquilezles.org/articles/distfunctions2d/
11
+ *
12
+ * @param p -
13
+ * @param size -
14
+ * @param r -
15
+ */
16
+ export declare const sdfCross2: import("@thi.ng/shader-ast").TaggedFn3<"vec2", "vec2", "float", "float">;
17
+ /**
18
+ * Returns signed distance from `p` to 2D "X-sign" of given `size` and stroke
19
+ * weight `r`.
20
+ *
21
+ * @remarks
22
+ * Ported from original GLSL impl by Inigo Quilez:
23
+ * - https://iquilezles.org/articles/distfunctions2d/
24
+ *
25
+ * @param p -
26
+ * @param size -
27
+ * @param r -
28
+ */
29
+ export declare const sdfRoundedX2: import("@thi.ng/shader-ast").TaggedFn3<"vec2", "float", "float", "float">;
30
+ //# sourceMappingURL=cross.d.ts.map
package/sdf/cross.js ADDED
@@ -0,0 +1,55 @@
1
+ import { assign } from "@thi.ng/shader-ast/ast/assign";
2
+ import { ifThen, ternary } from "@thi.ng/shader-ast/ast/controlflow";
3
+ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
4
+ import { FLOAT0, FLOAT05, vec2, VEC2_0 } from "@thi.ng/shader-ast/ast/lit";
5
+ import { add, gt, madd, mul, neg, sub } from "@thi.ng/shader-ast/ast/ops";
6
+ import { $, $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
7
+ import { sym } from "@thi.ng/shader-ast/ast/sym";
8
+ import { abs, length, max, min, sign } from "@thi.ng/shader-ast/builtin/math";
9
+ import { maxComp2 } from "../math/maxcomp.js";
10
+ /**
11
+ * Returns signed distance from `p` to 2D cross/plus of given `size` and corner
12
+ * radius `r`.
13
+ *
14
+ * @remarks
15
+ * - `size` consist of overall width/size (in x) and thickness (in y component)
16
+ * - corner radius can also be negative
17
+ *
18
+ * Ported from original GLSL impl by Inigo Quilez:
19
+ * - https://iquilezles.org/articles/distfunctions2d/
20
+ *
21
+ * @param p -
22
+ * @param size -
23
+ * @param r -
24
+ */
25
+ export const sdfCross2 = defn("float", "sdfCross2", ["vec2", "vec2", "float"], (p, size, r) => {
26
+ let a, q, w;
27
+ let k;
28
+ return [
29
+ (a = sym(abs(p))),
30
+ ifThen(gt($y(a), $x(a)), [assign(a, $(a, "yx"))]),
31
+ (q = sym(sub(a, size))),
32
+ (k = sym(maxComp2(q))),
33
+ (w = sym(ternary(gt(k, FLOAT0), q, vec2(sub($y(size), $x(a)), neg(k))))),
34
+ ret(madd(sign(k), length(max(w, VEC2_0)), r)),
35
+ ];
36
+ });
37
+ /**
38
+ * Returns signed distance from `p` to 2D "X-sign" of given `size` and stroke
39
+ * weight `r`.
40
+ *
41
+ * @remarks
42
+ * Ported from original GLSL impl by Inigo Quilez:
43
+ * - https://iquilezles.org/articles/distfunctions2d/
44
+ *
45
+ * @param p -
46
+ * @param size -
47
+ * @param r -
48
+ */
49
+ export const sdfRoundedX2 = defn("float", "sdfRoundedX", ["vec2", "float", "float"], (p, size, r) => {
50
+ let q;
51
+ return [
52
+ (q = sym(abs(p))),
53
+ ret(sub(length(sub(q, mul(min(add($x(q), $y(q)), size), FLOAT05))), r)),
54
+ ];
55
+ });
package/sdf/hex.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Returns signed distance from `p` to 2D hexagon of given radius `r`.
3
+ *
4
+ * @remarks
5
+ * Ported from original GLSL impl by Inigo Quilez:
6
+ * - https://iquilezles.org/articles/distfunctions2d/
7
+ */
8
+ export declare const sdfHexagon2: import("@thi.ng/shader-ast").TaggedFn2<"vec2", "float", "float">;
9
+ //# sourceMappingURL=hex.d.ts.map
package/sdf/hex.js ADDED
@@ -0,0 +1,26 @@
1
+ import { assign } from "@thi.ng/shader-ast/ast/assign";
2
+ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
+ import { FLOAT0, vec2 } from "@thi.ng/shader-ast/ast/lit";
4
+ import { mul, sub } 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 { abs, clamp, dot, length, min, sign, } from "@thi.ng/shader-ast/builtin/math";
8
+ /**
9
+ * Returns signed distance from `p` to 2D hexagon of given radius `r`.
10
+ *
11
+ * @remarks
12
+ * Ported from original GLSL impl by Inigo Quilez:
13
+ * - https://iquilezles.org/articles/distfunctions2d/
14
+ */
15
+ export const sdfHexagon2 = defn("float", "sdfHexagon2", ["vec2", "float"], (p, r) => {
16
+ const TAN30 = 0.5773502691896257;
17
+ let k, q;
18
+ return [
19
+ // sin/cos @ 60deg
20
+ (k = sym(vec2(-0.8660254037844386, 0.5))),
21
+ (q = sym(abs(p))),
22
+ assign(q, sub(q, mul(k, mul(2, min(dot(k, q), FLOAT0))))),
23
+ assign(q, sub(q, vec2(clamp($x(q), mul(r, -TAN30), mul(r, TAN30)), r))),
24
+ ret(mul(length(q), sign($y(q)))),
25
+ ];
26
+ });
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Higher-order function. Returns specialized function to compute signed
3
+ * distance for 2D polygons of `N` vertices.
4
+ *
5
+ * @remarks
6
+ * Based on original GLSL impl by Inigo Quilez:
7
+ * - https://iquilezles.org/articles/distfunctions2d/
8
+ *
9
+ * @param N
10
+ */
11
+ export declare const sdfPolygon2: (N: number) => import("@thi.ng/shader-ast").TaggedFn2<"vec2", "vec2[]", "float">;
12
+ //# sourceMappingURL=polygon.d.ts.map
package/sdf/polygon.js ADDED
@@ -0,0 +1,46 @@
1
+ import { assign } from "@thi.ng/shader-ast/ast/assign";
2
+ import { forLoop, ifThen } from "@thi.ng/shader-ast/ast/controlflow";
3
+ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
4
+ import { index } from "@thi.ng/shader-ast/ast/indexed";
5
+ import { bvec3, FLOAT1, int, INT0 } from "@thi.ng/shader-ast/ast/lit";
6
+ import { div, gt, gte, inc, lt, mul, neg, not, 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 { all, _any } from "@thi.ng/shader-ast/builtin/bvec";
10
+ import { dot, min, sqrt } from "@thi.ng/shader-ast/builtin/math";
11
+ import { clamp01 } from "../math/clamp.js";
12
+ /**
13
+ * Higher-order function. Returns specialized function to compute signed
14
+ * distance for 2D polygons of `N` vertices.
15
+ *
16
+ * @remarks
17
+ * Based on original GLSL impl by Inigo Quilez:
18
+ * - https://iquilezles.org/articles/distfunctions2d/
19
+ *
20
+ * @param N
21
+ */
22
+ export const sdfPolygon2 = (N) => defn("float", `sdfPolygon2_${N}`, ["vec2", ["vec2[]", "pts", { num: N }]], (p, pts) => {
23
+ let d, s;
24
+ let b, e, w, t;
25
+ let pi, pj;
26
+ let c;
27
+ let j;
28
+ return [
29
+ (t = sym(sub(p, index(pts, 0)))),
30
+ (d = sym(dot(t, t))),
31
+ (s = sym(FLOAT1)),
32
+ (j = sym(int(N - 1))),
33
+ forLoop(sym(INT0), (i) => lt(i, int(N)), inc, (i) => [
34
+ (pi = sym(index(pts, i))),
35
+ (pj = sym(index(pts, j))),
36
+ (e = sym(sub(pj, pi))),
37
+ (w = sym(sub(p, pi))),
38
+ (b = sym(sub(w, mul(e, clamp01(div(dot(w, e), dot(e, e))))))),
39
+ assign(d, min(d, dot(b, b))),
40
+ (c = sym(bvec3(gte($y(p), $y(pi)), lt($y(p), $y(pj)), gt(mul($x(e), $y(w)), mul($y(e), $x(w)))))),
41
+ ifThen(or(all(c), not(_any(c))), [assign(s, neg(s))]),
42
+ assign(j, i),
43
+ ]),
44
+ ret(mul(s, sqrt(d))),
45
+ ];
46
+ });
package/sdf/union.d.ts CHANGED
@@ -9,7 +9,8 @@ import type { FloatTerm } from "@thi.ng/shader-ast";
9
9
  export declare const sdfUnion: (a: FloatTerm, ...terms: FloatTerm[]) => FloatTerm;
10
10
  /**
11
11
  * SDF shape union for vec2 terms, i.e. the common form where the X coord
12
- * defines distance and Y an object or material ID.
12
+ * defines distance and Y an object or material ID. Returns `a` or `b`,
13
+ * depending whose x-coord (dist) is smaller.
13
14
  *
14
15
  * @param a -
15
16
  * @param b -
package/sdf/union.js CHANGED
@@ -13,7 +13,8 @@ import { min } from "@thi.ng/shader-ast/builtin/math";
13
13
  export const sdfUnion = (a, ...terms) => terms.reduce((a, b) => min(a, b), a);
14
14
  /**
15
15
  * SDF shape union for vec2 terms, i.e. the common form where the X coord
16
- * defines distance and Y an object or material ID.
16
+ * defines distance and Y an object or material ID. Returns `a` or `b`,
17
+ * depending whose x-coord (dist) is smaller.
17
18
  *
18
19
  * @param a -
19
20
  * @param b -