@thi.ng/shader-ast-stdlib 0.11.5 → 0.12.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**: 2022-03-11T12:13:49Z
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.5",
3
+ "version": "0.12.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",
@@ -34,16 +34,16 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.3.4",
38
- "@thi.ng/shader-ast": "^0.12.5"
37
+ "@thi.ng/api": "^8.3.6",
38
+ "@thi.ng/shader-ast": "^0.12.8"
39
39
  },
40
40
  "devDependencies": {
41
- "@microsoft/api-extractor": "^7.19.4",
42
- "@thi.ng/testament": "^0.2.4",
41
+ "@microsoft/api-extractor": "^7.23.1",
42
+ "@thi.ng/testament": "^0.2.7",
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",
@@ -93,214 +93,232 @@
93
93
  ],
94
94
  "exports": {
95
95
  ".": {
96
- "import": "./index.js"
96
+ "default": "./index.js"
97
97
  },
98
98
  "./api": {
99
- "import": "./api.js"
99
+ "default": "./api.js"
100
100
  },
101
101
  "./color/aces-film": {
102
- "import": "./color/aces-film.js"
102
+ "default": "./color/aces-film.js"
103
103
  },
104
104
  "./color/levels": {
105
- "import": "./color/levels.js"
105
+ "default": "./color/levels.js"
106
106
  },
107
107
  "./color/linear-srgb": {
108
- "import": "./color/linear-srgb.js"
108
+ "default": "./color/linear-srgb.js"
109
109
  },
110
110
  "./color/luminance": {
111
- "import": "./color/luminance.js"
111
+ "default": "./color/luminance.js"
112
112
  },
113
113
  "./color/porter-duff": {
114
- "import": "./color/porter-duff.js"
114
+ "default": "./color/porter-duff.js"
115
115
  },
116
116
  "./color/rgbe": {
117
- "import": "./color/rgbe.js"
117
+ "default": "./color/rgbe.js"
118
118
  },
119
119
  "./fog/exp": {
120
- "import": "./fog/exp.js"
120
+ "default": "./fog/exp.js"
121
121
  },
122
122
  "./fog/exp2": {
123
- "import": "./fog/exp2.js"
123
+ "default": "./fog/exp2.js"
124
124
  },
125
125
  "./fog/linear": {
126
- "import": "./fog/linear.js"
126
+ "default": "./fog/linear.js"
127
127
  },
128
128
  "./light/lambert": {
129
- "import": "./light/lambert.js"
129
+ "default": "./light/lambert.js"
130
130
  },
131
131
  "./light/trilight": {
132
- "import": "./light/trilight.js"
132
+ "default": "./light/trilight.js"
133
133
  },
134
134
  "./math/additive": {
135
- "import": "./math/additive.js"
135
+ "default": "./math/additive.js"
136
136
  },
137
137
  "./math/cartesian": {
138
- "import": "./math/cartesian.js"
138
+ "default": "./math/cartesian.js"
139
139
  },
140
140
  "./math/clamp": {
141
- "import": "./math/clamp.js"
141
+ "default": "./math/clamp.js"
142
142
  },
143
143
  "./math/cross2": {
144
- "import": "./math/cross2.js"
144
+ "default": "./math/cross2.js"
145
145
  },
146
146
  "./math/dist-chebyshev": {
147
- "import": "./math/dist-chebyshev.js"
147
+ "default": "./math/dist-chebyshev.js"
148
148
  },
149
149
  "./math/dist-manhattan": {
150
- "import": "./math/dist-manhattan.js"
150
+ "default": "./math/dist-manhattan.js"
151
151
  },
152
152
  "./math/fit": {
153
- "import": "./math/fit.js"
153
+ "default": "./math/fit.js"
154
154
  },
155
155
  "./math/magsq": {
156
- "import": "./math/magsq.js"
156
+ "default": "./math/magsq.js"
157
157
  },
158
158
  "./math/maxcomp": {
159
- "import": "./math/maxcomp.js"
159
+ "default": "./math/maxcomp.js"
160
160
  },
161
161
  "./math/mincomp": {
162
- "import": "./math/mincomp.js"
162
+ "default": "./math/mincomp.js"
163
163
  },
164
164
  "./math/mix-cubic": {
165
- "import": "./math/mix-cubic.js"
165
+ "default": "./math/mix-cubic.js"
166
166
  },
167
167
  "./math/mix-quadratic": {
168
- "import": "./math/mix-quadratic.js"
168
+ "default": "./math/mix-quadratic.js"
169
169
  },
170
170
  "./math/orthogonal": {
171
- "import": "./math/orthogonal.js"
171
+ "default": "./math/orthogonal.js"
172
172
  },
173
173
  "./math/osc": {
174
- "import": "./math/osc.js"
174
+ "default": "./math/osc.js"
175
175
  },
176
176
  "./math/polar": {
177
- "import": "./math/polar.js"
177
+ "default": "./math/polar.js"
178
178
  },
179
179
  "./math/sincos": {
180
- "import": "./math/sincos.js"
180
+ "default": "./math/sincos.js"
181
181
  },
182
182
  "./matrix/convert": {
183
- "import": "./matrix/convert.js"
183
+ "default": "./matrix/convert.js"
184
184
  },
185
185
  "./matrix/lookat": {
186
- "import": "./matrix/lookat.js"
186
+ "default": "./matrix/lookat.js"
187
187
  },
188
188
  "./matrix/mvp": {
189
- "import": "./matrix/mvp.js"
189
+ "default": "./matrix/mvp.js"
190
190
  },
191
191
  "./matrix/normal": {
192
- "import": "./matrix/normal.js"
192
+ "default": "./matrix/normal.js"
193
193
  },
194
194
  "./matrix/rotation": {
195
- "import": "./matrix/rotation.js"
195
+ "default": "./matrix/rotation.js"
196
196
  },
197
197
  "./noise/curl3": {
198
- "import": "./noise/curl3.js"
198
+ "default": "./noise/curl3.js"
199
199
  },
200
200
  "./noise/hash": {
201
- "import": "./noise/hash.js"
201
+ "default": "./noise/hash.js"
202
202
  },
203
203
  "./noise/permute": {
204
- "import": "./noise/permute.js"
204
+ "default": "./noise/permute.js"
205
205
  },
206
206
  "./noise/simplex2": {
207
- "import": "./noise/simplex2.js"
207
+ "default": "./noise/simplex2.js"
208
208
  },
209
209
  "./noise/simplex3": {
210
- "import": "./noise/simplex3.js"
210
+ "default": "./noise/simplex3.js"
211
211
  },
212
212
  "./noise/voronoi2": {
213
- "import": "./noise/voronoi2.js"
213
+ "default": "./noise/voronoi2.js"
214
214
  },
215
215
  "./noise/worley2": {
216
- "import": "./noise/worley2.js"
216
+ "default": "./noise/worley2.js"
217
217
  },
218
218
  "./raymarch/ao": {
219
- "import": "./raymarch/ao.js"
219
+ "default": "./raymarch/ao.js"
220
220
  },
221
221
  "./raymarch/direction": {
222
- "import": "./raymarch/direction.js"
222
+ "default": "./raymarch/direction.js"
223
223
  },
224
224
  "./raymarch/normal": {
225
- "import": "./raymarch/normal.js"
225
+ "default": "./raymarch/normal.js"
226
226
  },
227
227
  "./raymarch/point-at": {
228
- "import": "./raymarch/point-at.js"
228
+ "default": "./raymarch/point-at.js"
229
229
  },
230
230
  "./raymarch/scene": {
231
- "import": "./raymarch/scene.js"
231
+ "default": "./raymarch/scene.js"
232
232
  },
233
233
  "./screen/uv": {
234
- "import": "./screen/uv.js"
234
+ "default": "./screen/uv.js"
235
235
  },
236
236
  "./sdf/annular": {
237
- "import": "./sdf/annular.js"
237
+ "default": "./sdf/annular.js"
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"
238
247
  },
239
248
  "./sdf/box": {
240
- "import": "./sdf/box.js"
249
+ "default": "./sdf/box.js"
250
+ },
251
+ "./sdf/cross": {
252
+ "default": "./sdf/cross.js"
241
253
  },
242
254
  "./sdf/cylinder": {
243
- "import": "./sdf/cylinder.js"
255
+ "default": "./sdf/cylinder.js"
256
+ },
257
+ "./sdf/hex": {
258
+ "default": "./sdf/hex.js"
244
259
  },
245
260
  "./sdf/isec": {
246
- "import": "./sdf/isec.js"
261
+ "default": "./sdf/isec.js"
247
262
  },
248
263
  "./sdf/line": {
249
- "import": "./sdf/line.js"
264
+ "default": "./sdf/line.js"
250
265
  },
251
266
  "./sdf/mirror": {
252
- "import": "./sdf/mirror.js"
267
+ "default": "./sdf/mirror.js"
253
268
  },
254
269
  "./sdf/plane": {
255
- "import": "./sdf/plane.js"
270
+ "default": "./sdf/plane.js"
271
+ },
272
+ "./sdf/polygon": {
273
+ "default": "./sdf/polygon.js"
256
274
  },
257
275
  "./sdf/polyhedra": {
258
- "import": "./sdf/polyhedra.js"
276
+ "default": "./sdf/polyhedra.js"
259
277
  },
260
278
  "./sdf/repeat-polar": {
261
- "import": "./sdf/repeat-polar.js"
279
+ "default": "./sdf/repeat-polar.js"
262
280
  },
263
281
  "./sdf/repeat": {
264
- "import": "./sdf/repeat.js"
282
+ "default": "./sdf/repeat.js"
265
283
  },
266
284
  "./sdf/round": {
267
- "import": "./sdf/round.js"
285
+ "default": "./sdf/round.js"
268
286
  },
269
287
  "./sdf/smooth-isec": {
270
- "import": "./sdf/smooth-isec.js"
288
+ "default": "./sdf/smooth-isec.js"
271
289
  },
272
290
  "./sdf/smooth-sub": {
273
- "import": "./sdf/smooth-sub.js"
291
+ "default": "./sdf/smooth-sub.js"
274
292
  },
275
293
  "./sdf/smooth-union": {
276
- "import": "./sdf/smooth-union.js"
294
+ "default": "./sdf/smooth-union.js"
277
295
  },
278
296
  "./sdf/sphere": {
279
- "import": "./sdf/sphere.js"
297
+ "default": "./sdf/sphere.js"
280
298
  },
281
299
  "./sdf/sub": {
282
- "import": "./sdf/sub.js"
300
+ "default": "./sdf/sub.js"
283
301
  },
284
302
  "./sdf/torus": {
285
- "import": "./sdf/torus.js"
303
+ "default": "./sdf/torus.js"
286
304
  },
287
305
  "./sdf/tri": {
288
- "import": "./sdf/tri.js"
306
+ "default": "./sdf/tri.js"
289
307
  },
290
308
  "./sdf/union": {
291
- "import": "./sdf/union.js"
309
+ "default": "./sdf/union.js"
292
310
  },
293
311
  "./tex/blur": {
294
- "import": "./tex/blur.js"
312
+ "default": "./tex/blur.js"
295
313
  },
296
314
  "./tex/index-coord": {
297
- "import": "./tex/index-coord.js"
315
+ "default": "./tex/index-coord.js"
298
316
  },
299
317
  "./tex/index-uv": {
300
- "import": "./tex/index-uv.js"
318
+ "default": "./tex/index-uv.js"
301
319
  },
302
320
  "./tex/read-index": {
303
- "import": "./tex/read-index.js"
321
+ "default": "./tex/read-index.js"
304
322
  }
305
323
  },
306
324
  "thi.ng": {
@@ -312,5 +330,5 @@
312
330
  ],
313
331
  "year": 2019
314
332
  },
315
- "gitHead": "0fc692a3225c068aacafdc4cb6140cf603c67ad8\n"
333
+ "gitHead": "e23901b8582af71d8a29e0ce4929f15ac509f9e5\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 -