@thi.ng/shader-ast-stdlib 0.13.13 → 0.13.15

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 (62) hide show
  1. package/CHANGELOG.md +14 -1
  2. package/README.md +1 -1
  3. package/color/aces-film.js +2 -1
  4. package/color/levels.js +5 -4
  5. package/color/porter-duff.js +2 -1
  6. package/color/rgbe.js +3 -2
  7. package/fog/exp.js +4 -1
  8. package/fog/exp2.js +2 -1
  9. package/fog/linear.js +4 -1
  10. package/light/trilight.js +2 -1
  11. package/math/additive.js +4 -2
  12. package/math/cartesian.js +2 -1
  13. package/math/clamp.js +4 -3
  14. package/math/cross2.js +2 -1
  15. package/math/fit.js +4 -1
  16. package/math/magsq.js +2 -1
  17. package/math/mix-cubic.js +6 -5
  18. package/math/mix-quadratic.js +6 -5
  19. package/math/orthogonal.js +2 -1
  20. package/math/polar.js +3 -2
  21. package/matrix/convert.js +4 -3
  22. package/matrix/lookat.js +2 -1
  23. package/matrix/rotation.js +10 -9
  24. package/noise/curl3.js +2 -1
  25. package/noise/hash.js +16 -15
  26. package/noise/permute.js +6 -5
  27. package/noise/simplex2.js +3 -2
  28. package/noise/simplex3.d.ts +1 -0
  29. package/noise/simplex3.js +9 -3
  30. package/noise/voronoi2.js +3 -2
  31. package/noise/worley2.d.ts +12 -7
  32. package/noise/worley2.js +18 -9
  33. package/package.json +8 -8
  34. package/raymarch/ao.d.ts +2 -1
  35. package/raymarch/ao.js +5 -2
  36. package/raymarch/direction.js +2 -1
  37. package/raymarch/normal.js +3 -1
  38. package/raymarch/scene.js +6 -4
  39. package/screen/uv.js +5 -4
  40. package/sdf/arc.js +2 -1
  41. package/sdf/bezier.js +3 -2
  42. package/sdf/box-rounded.js +2 -1
  43. package/sdf/box.js +3 -2
  44. package/sdf/cross.js +4 -3
  45. package/sdf/cylinder.js +3 -2
  46. package/sdf/hex.js +2 -1
  47. package/sdf/line.js +4 -3
  48. package/sdf/mirror.js +2 -1
  49. package/sdf/plane.js +7 -2
  50. package/sdf/polygon.js +4 -3
  51. package/sdf/polyhedra.js +3 -2
  52. package/sdf/repeat-polar.js +2 -1
  53. package/sdf/repeat.js +3 -2
  54. package/sdf/smooth-isec.js +2 -1
  55. package/sdf/smooth-sub.js +2 -1
  56. package/sdf/smooth-union.js +2 -1
  57. package/sdf/sphere.js +7 -2
  58. package/sdf/torus.js +2 -1
  59. package/sdf/tri.js +2 -1
  60. package/sdf/union.js +2 -1
  61. package/tex/blur.js +4 -3
  62. package/tex/index-uv.js +3 -2
package/noise/simplex2.js CHANGED
@@ -1,7 +1,8 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
1
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
2
3
  import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
3
4
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
4
- import { float, FLOAT05, FLOAT1, vec2, vec3, VEC3_0, vec4, } from "@thi.ng/shader-ast/ast/lit";
5
+ import { FLOAT05, FLOAT1, VEC3_0, float, vec2, vec3, vec4, } from "@thi.ng/shader-ast/ast/lit";
5
6
  import { add, gt, mul, sub } from "@thi.ng/shader-ast/ast/ops";
6
7
  import { $, $x, $xy, $y } from "@thi.ng/shader-ast/ast/swizzle";
7
8
  import { sym } from "@thi.ng/shader-ast/ast/sym";
@@ -14,7 +15,7 @@ import { permute3 } from "./permute.js";
14
15
  *
15
16
  * https://github.com/ashima/webgl-noise
16
17
  */
17
- export const snoise2 = defn("float", "snoise2", ["vec2"], (v) => {
18
+ export const snoise2 = defn(F, "snoise2", [V2], (v) => {
18
19
  let C;
19
20
  let i;
20
21
  let i1;
@@ -1,3 +1,4 @@
1
1
  export declare const snoise3: import("@thi.ng/shader-ast").TaggedFn1<"vec3", "float">;
2
+ export declare const snoiseVec32: import("@thi.ng/shader-ast").TaggedFn1<"vec3", "vec2">;
2
3
  export declare const snoiseVec3: import("@thi.ng/shader-ast").TaggedFn1<"vec3", "vec3">;
3
4
  //# sourceMappingURL=simplex3.d.ts.map
package/noise/simplex3.js CHANGED
@@ -1,12 +1,13 @@
1
+ import { F, V2, V3 } from "@thi.ng/shader-ast/api/types";
1
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
2
3
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
- import { float, FLOAT0, FLOAT05, FLOAT1, FLOAT2, vec3, vec4, } from "@thi.ng/shader-ast/ast/lit";
4
+ import { FLOAT0, FLOAT05, FLOAT1, FLOAT2, float, vec2, vec3, vec4, } from "@thi.ng/shader-ast/ast/lit";
4
5
  import { add, mul, neg, sub } from "@thi.ng/shader-ast/ast/ops";
5
6
  import { $, $w, $x, $xy, $y, $z } from "@thi.ng/shader-ast/ast/swizzle";
6
7
  import { sym } from "@thi.ng/shader-ast/ast/sym";
7
8
  import { abs, dot, floor, max, min, mod, step, } from "@thi.ng/shader-ast/builtin/math";
8
9
  import { permute4 } from "./permute.js";
9
- export const snoise3 = defn("float", "snoise3", ["vec3"], (v) => {
10
+ export const snoise3 = defn(F, "snoise3", [V3], (v) => {
10
11
  let g;
11
12
  let j;
12
13
  let l;
@@ -72,7 +73,12 @@ export const snoise3 = defn("float", "snoise3", ["vec3"], (v) => {
72
73
  ret(mul(42, dot(mul(m, m), vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3))))),
73
74
  ];
74
75
  });
75
- export const snoiseVec3 = defn("vec3", "snoiseVec3", ["vec3"], (p) => {
76
+ export const snoiseVec32 = defn(V2, "snoiseVec3", [V3], (p) => {
77
+ return [
78
+ ret(vec2(snoise3(p), snoise3(vec3(sub($y(p), 19.1), add($z(p), 33.4), add($x(p), 47.2))))),
79
+ ];
80
+ });
81
+ export const snoiseVec3 = defn(V3, "snoiseVec3", [V3], (p) => {
76
82
  return [
77
83
  ret(vec3(snoise3(p), snoise3(vec3(sub($y(p), 19.1), add($z(p), 33.4), add($x(p), 47.2))), snoise3(vec3(add($z(p), 74.2), sub($x(p), 124.5), add($y(p), 99.4))))),
78
84
  ];
package/noise/voronoi2.js CHANGED
@@ -1,7 +1,8 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
1
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
2
3
  import { forLoop } from "@thi.ng/shader-ast/ast/controlflow";
3
4
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
4
- import { float, FLOAT0, int, SQRT2, vec2, vec3, } from "@thi.ng/shader-ast/ast/lit";
5
+ import { FLOAT0, SQRT2, float, int, vec2, vec3, } from "@thi.ng/shader-ast/ast/lit";
5
6
  import { add, div, inc, lte, madd, mul, sub } from "@thi.ng/shader-ast/ast/ops";
6
7
  import { $xy, $z } from "@thi.ng/shader-ast/ast/swizzle";
7
8
  import { sym } from "@thi.ng/shader-ast/ast/sym";
@@ -25,7 +26,7 @@ import { hash32 } from "./hash.js";
25
26
  * @param u -
26
27
  * @param v -
27
28
  */
28
- export const voronoise2 = defn("float", "voronoise2", ["vec2", "float", "float"], (x, u, v) => {
29
+ export const voronoise2 = defn(F, "voronoise2", [V2, F, F], (x, u, v) => {
29
30
  let p;
30
31
  let f;
31
32
  let coeff;
@@ -2,17 +2,22 @@ import type { Func2 } from "@thi.ng/shader-ast";
2
2
  export declare const worleyDist: import("@thi.ng/shader-ast").TaggedFn2<"vec3", "vec3", "vec3">;
3
3
  export declare const worleyDistManhattan: import("@thi.ng/shader-ast").TaggedFn2<"vec3", "vec3", "vec3">;
4
4
  /**
5
- * Higher order function. Computes 2D Worley noise using provided
6
- * distance function. The returned function takes 2 args: position and
7
- * jitter amount, the latter in [0..1] interval. Returns noise
8
- * components as vec2, with the x component containing the distance from
9
- * closest simplex center and y the noise value. The vector components
10
- * can be used individually or combined (e.g. `noise.y - noise.x`)...
5
+ * Higher order function. Computes 2D Worley noise using provided distance
6
+ * function. The returned function takes 2 args: position and jitter amount, the
7
+ * latter in [0..1] interval. Returns noise components as vec2, with the x
8
+ * component containing the distance from closest simplex center and y the noise
9
+ * value. The vector components can be used individually or combined (e.g.
10
+ * `noise.y - noise.x`)...
11
11
  *
12
+ * THe optional `name` arg can be used to customize the name of the generated
13
+ * function.
14
+ *
15
+ * @remarks
12
16
  * Based on implementation by Stefan Gustavson:
13
17
  * http://webstaff.itn.liu.se/~stegu/GLSL-cellular/GLSL-cellular-notes.pdf
14
18
  *
15
19
  * @param distFn -
20
+ * @param name -
16
21
  */
17
- export declare const worley2: (distFn: Func2<"vec3", "vec3", "vec3">) => import("@thi.ng/shader-ast").TaggedFn2<"vec2", "float", "vec2">;
22
+ export declare const worley2: (distFn: Func2<"vec3", "vec3", "vec3">, name?: string) => import("@thi.ng/shader-ast").TaggedFn2<"vec2", "float", "vec2">;
18
23
  //# sourceMappingURL=worley2.d.ts.map
package/noise/worley2.js CHANGED
@@ -1,28 +1,37 @@
1
+ import { F, V2, V3 } from "@thi.ng/shader-ast/api/types";
1
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
2
3
  import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
3
4
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
5
+ import { gensym } from "@thi.ng/shader-ast/ast/idgen";
4
6
  import { float, vec3 } from "@thi.ng/shader-ast/ast/lit";
5
7
  import { add, lt, mul, sub } from "@thi.ng/shader-ast/ast/ops";
6
8
  import { $, $x, $xy, $y, $z } from "@thi.ng/shader-ast/ast/swizzle";
7
9
  import { sym } from "@thi.ng/shader-ast/ast/sym";
8
10
  import { abs, floor, fract, max, min, mod, sqrt, } from "@thi.ng/shader-ast/builtin/math";
9
11
  import { permute3 } from "./permute.js";
10
- export const worleyDist = defn("vec3", "worleyDist", ["vec3", "vec3"], (a, b) => [ret(add(mul(a, a), mul(b, b)))]);
11
- export const worleyDistManhattan = defn("vec3", "worleyDistManhatten", ["vec3", "vec3"], (a, b) => [ret(add(abs(a), abs(b)))]);
12
+ export const worleyDist = defn(V3, "worleyDist", [V3, V3], (a, b) => [
13
+ ret(add(mul(a, a), mul(b, b))),
14
+ ]);
15
+ export const worleyDistManhattan = defn(V3, "worleyDistManhatten", [V3, V3], (a, b) => [ret(add(abs(a), abs(b)))]);
12
16
  /**
13
- * Higher order function. Computes 2D Worley noise using provided
14
- * distance function. The returned function takes 2 args: position and
15
- * jitter amount, the latter in [0..1] interval. Returns noise
16
- * components as vec2, with the x component containing the distance from
17
- * closest simplex center and y the noise value. The vector components
18
- * can be used individually or combined (e.g. `noise.y - noise.x`)...
17
+ * Higher order function. Computes 2D Worley noise using provided distance
18
+ * function. The returned function takes 2 args: position and jitter amount, the
19
+ * latter in [0..1] interval. Returns noise components as vec2, with the x
20
+ * component containing the distance from closest simplex center and y the noise
21
+ * value. The vector components can be used individually or combined (e.g.
22
+ * `noise.y - noise.x`)...
19
23
  *
24
+ * THe optional `name` arg can be used to customize the name of the generated
25
+ * function.
26
+ *
27
+ * @remarks
20
28
  * Based on implementation by Stefan Gustavson:
21
29
  * http://webstaff.itn.liu.se/~stegu/GLSL-cellular/GLSL-cellular-notes.pdf
22
30
  *
23
31
  * @param distFn -
32
+ * @param name -
24
33
  */
25
- export const worley2 = (distFn) => defn("vec2", "worley2", ["vec2", "float"], (P, jitter) => {
34
+ export const worley2 = (distFn, name = gensym("worley2_")) => defn(V2, name, [V2, F], (P, jitter) => {
26
35
  const K = float(1 / 7);
27
36
  const Ko = float(3 / 7);
28
37
  const oI = sym(vec3(-1, 0, 1));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/shader-ast-stdlib",
3
- "version": "0.13.13",
3
+ "version": "0.13.15",
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,15 +34,15 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.8.0",
38
- "@thi.ng/shader-ast": "^0.12.50"
37
+ "@thi.ng/api": "^8.8.1",
38
+ "@thi.ng/shader-ast": "^0.12.52"
39
39
  },
40
40
  "devDependencies": {
41
- "@microsoft/api-extractor": "^7.34.4",
42
- "@thi.ng/testament": "^0.3.15",
43
- "rimraf": "^4.4.1",
41
+ "@microsoft/api-extractor": "^7.34.8",
42
+ "@thi.ng/testament": "^0.3.16",
43
+ "rimraf": "^5.0.0",
44
44
  "tools": "^0.0.1",
45
- "typedoc": "^0.23.28",
45
+ "typedoc": "^0.24.6",
46
46
  "typescript": "^5.0.4"
47
47
  },
48
48
  "keywords": [
@@ -330,5 +330,5 @@
330
330
  ],
331
331
  "year": 2019
332
332
  },
333
- "gitHead": "ca22f02a137c0a4e3a38ef81e82e2bc7e3c43849\n"
333
+ "gitHead": "20ab11b687a13228f6a8cecdc5f05ba9105122ea\n"
334
334
  }
package/raymarch/ao.d.ts CHANGED
@@ -7,6 +7,7 @@ import type { RaymarchScene } from "../api.js";
7
7
  *
8
8
  * @param scene -
9
9
  * @param numSamples -
10
+ * @param name -
10
11
  */
11
- export declare const raymarchAO: (scene: RaymarchScene, numSamples?: number) => import("@thi.ng/shader-ast").TaggedFn2<"vec3", "vec3", "float">;
12
+ export declare const raymarchAO: (scene: RaymarchScene, numSamples?: number, name?: string) => import("@thi.ng/shader-ast").TaggedFn2<"vec3", "vec3", "float">;
12
13
  //# sourceMappingURL=ao.d.ts.map
package/raymarch/ao.js CHANGED
@@ -1,7 +1,9 @@
1
+ import { F, V3 } from "@thi.ng/shader-ast/api/types";
1
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
2
3
  import { forLoop } from "@thi.ng/shader-ast/ast/controlflow";
3
4
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
4
- import { float, FLOAT0, FLOAT05, FLOAT1 } from "@thi.ng/shader-ast/ast/lit";
5
+ import { gensym } from "@thi.ng/shader-ast/ast/idgen";
6
+ import { FLOAT0, FLOAT05, FLOAT1, float } from "@thi.ng/shader-ast/ast/lit";
5
7
  import { add, inc, lte, mul, sub } from "@thi.ng/shader-ast/ast/ops";
6
8
  import { $x } from "@thi.ng/shader-ast/ast/swizzle";
7
9
  import { sym } from "@thi.ng/shader-ast/ast/sym";
@@ -14,8 +16,9 @@ import { clamp01 } from "../math/clamp.js";
14
16
  *
15
17
  * @param scene -
16
18
  * @param numSamples -
19
+ * @param name -
17
20
  */
18
- export const raymarchAO = (scene, numSamples = 5) => defn("float", "raymarchAO", ["vec3", "vec3"], (p, n) => {
21
+ export const raymarchAO = (scene, numSamples = 5, name = gensym("raymarchAO_")) => defn(F, name, [V3, V3], (p, n) => {
19
22
  let r;
20
23
  let w;
21
24
  let d0;
@@ -1,3 +1,4 @@
1
+ import { F, V2, V3 } from "@thi.ng/shader-ast/api/types";
1
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
2
3
  import { FLOAT2, vec3 } from "@thi.ng/shader-ast/ast/lit";
3
4
  import { div, neg, sub } from "@thi.ng/shader-ast/ast/ops";
@@ -9,7 +10,7 @@ import { normalize, radians, tan } from "@thi.ng/shader-ast/builtin/math";
9
10
  * @param res - vec2
10
11
  * @param fov - float vertical FOV (in degrees)
11
12
  */
12
- export const raymarchDir = defn("vec3", "raymarchDir", ["vec2", "vec2", "float"], (frag, res, fov) => {
13
+ export const raymarchDir = defn(V3, "raymarchDir", [V2, V2, F], (frag, res, fov) => {
13
14
  let uv;
14
15
  return [
15
16
  (uv = sym(sub(frag, div(res, FLOAT2)))),
@@ -1,4 +1,6 @@
1
+ import { F, V3 } from "@thi.ng/shader-ast/api/types";
1
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
+ import { gensym } from "@thi.ng/shader-ast/ast/idgen";
2
4
  import { vec2, vec3 } from "@thi.ng/shader-ast/ast/lit";
3
5
  import { add, sub } from "@thi.ng/shader-ast/ast/ops";
4
6
  import { $, $x } from "@thi.ng/shader-ast/ast/swizzle";
@@ -13,7 +15,7 @@ import { normalize } from "@thi.ng/shader-ast/builtin/math";
13
15
  * @param scene -
14
16
  * @param name -
15
17
  */
16
- export const raymarchNormal = (scene, name = "raymarchNormal") => defn("vec3", name, ["vec3", "float"], (p, smooth) => {
18
+ export const raymarchNormal = (scene, name = gensym("raymarchNormal_")) => defn(V3, name, [V3, F], (p, smooth) => {
17
19
  let dn;
18
20
  const comp = (id) => sub($x(scene(add(p, $(dn, id)))), $x(scene(sub(p, $(dn, id)))));
19
21
  return [
package/raymarch/scene.js CHANGED
@@ -1,7 +1,9 @@
1
+ import { V2, V3 } from "@thi.ng/shader-ast/api/types";
1
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
2
3
  import { brk, forLoop, ifThen } from "@thi.ng/shader-ast/ast/controlflow";
3
4
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
4
- import { float, int, INT0, vec2 } from "@thi.ng/shader-ast/ast/lit";
5
+ import { gensym } from "@thi.ng/shader-ast/ast/idgen";
6
+ import { INT0, float, int, vec2 } from "@thi.ng/shader-ast/ast/lit";
5
7
  import { gt, inc, lt, madd } from "@thi.ng/shader-ast/ast/ops";
6
8
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
7
9
  import { sym } from "@thi.ng/shader-ast/ast/sym";
@@ -25,7 +27,7 @@ import { rayPointAt } from "./point-at.js";
25
27
  */
26
28
  export const raymarchScene = (scene, _opts) => {
27
29
  const opts = {
28
- name: "raymarchScene",
30
+ name: gensym("raymarchScene_"),
29
31
  near: 0.1,
30
32
  far: 10,
31
33
  steps: 100,
@@ -33,12 +35,12 @@ export const raymarchScene = (scene, _opts) => {
33
35
  bias: 0.7,
34
36
  ..._opts,
35
37
  };
36
- return defn("vec2", opts.name, ["vec3", "vec3"], (pos, dir) => {
38
+ return defn(V2, opts.name, [V3, V3], (pos, dir) => {
37
39
  let total;
38
40
  let res;
39
41
  return [
40
42
  (total = sym(float(opts.near))),
41
- (res = sym("vec2")),
43
+ (res = sym(V2)),
42
44
  forLoop(sym(INT0), (i) => lt(i, int(opts.steps)), inc, () => [
43
45
  assign(res, scene(rayPointAt(pos, dir, total))),
44
46
  ifThen(lt($x(res), float(opts.eps)), [
package/screen/uv.js CHANGED
@@ -1,10 +1,11 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
1
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
2
3
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
- import { bvec4, vec2, VEC2_1 } from "@thi.ng/shader-ast/ast/lit";
4
+ import { VEC2_1, bvec4, vec2 } from "@thi.ng/shader-ast/ast/lit";
4
5
  import { add, div, mul } from "@thi.ng/shader-ast/ast/ops";
5
6
  import { $x, $xy, $y } from "@thi.ng/shader-ast/ast/swizzle";
6
7
  import { sym } from "@thi.ng/shader-ast/ast/sym";
7
- import { greaterThan, lessThan, _any } from "@thi.ng/shader-ast/builtin/bvec";
8
+ import { _any, greaterThan, lessThan } from "@thi.ng/shader-ast/builtin/bvec";
8
9
  import { fit0111 } from "../math/fit.js";
9
10
  /**
10
11
  * Computes UV coord in [0..1] interval from given `fragCoord` and screen `res`.
@@ -21,7 +22,7 @@ export const fragUV = (fragCoord, res) => div($xy(fragCoord), res);
21
22
  * @param fragCoord - vec2
22
23
  * @param res - vec2
23
24
  */
24
- export const aspectCorrectedUV = defn("vec2", "aspectCorrectedUV", ["vec2", "vec2"], (pos, res) => {
25
+ export const aspectCorrectedUV = defn(V2, "aspectCorrectedUV", [V2, V2], (pos, res) => {
25
26
  let uv;
26
27
  return [
27
28
  (uv = sym(fit0111(div(pos, res)))),
@@ -39,6 +40,6 @@ export const aspectCorrectedUV = defn("vec2", "aspectCorrectedUV", ["vec2", "vec
39
40
  * borderMask(vec2(0.2, 0.2), 0.1) // false
40
41
  * ```
41
42
  */
42
- export const borderMask = defn("bool", "borderMask", ["vec2", "float"], (uv, width) => [
43
+ export const borderMask = defn("bool", "borderMask", [V2, F], (uv, width) => [
43
44
  ret(_any(bvec4(lessThan(uv, vec2(width)), greaterThan(add(uv, width), VEC2_1)))),
44
45
  ]);
package/sdf/arc.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
1
2
  import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
2
3
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
4
  import { vec2 } from "@thi.ng/shader-ast/ast/lit";
@@ -16,7 +17,7 @@ import { abs, cos, length, sin } from "@thi.ng/shader-ast/builtin/math";
16
17
  * - https://www.shadertoy.com/view/wl23RK
17
18
  * - https://iquilezles.org/articles/distfunctions2d/
18
19
  */
19
- export const sdfArc2 = defn("float", "sdfArc", ["vec2", "float", "float", "float"], (p, apert, ra, rb) => {
20
+ export const sdfArc2 = defn(F, "sdfArc", [V2, F, F, F], (p, apert, ra, rb) => {
20
21
  let q;
21
22
  let sc;
22
23
  return [
package/sdf/bezier.js CHANGED
@@ -1,7 +1,8 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
1
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
2
3
  import { ifThen } from "@thi.ng/shader-ast/ast/controlflow";
3
4
  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 { FLOAT0, FLOAT1, FLOAT2, float, vec2, } from "@thi.ng/shader-ast/ast/lit";
5
6
  import { add, div, gte, lt, madd, mul, neg, sub, } from "@thi.ng/shader-ast/ast/ops";
6
7
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
7
8
  import { sym } from "@thi.ng/shader-ast/ast/sym";
@@ -18,7 +19,7 @@ import { cross2 } from "../math/cross2.js";
18
19
  * - https://www.shadertoy.com/view/MlKcDD
19
20
  * - https://iquilezles.org/articles/distfunctions2d/
20
21
  */
21
- export const sdfQuadratic2 = defn("float", "sdfQuadratic2", ["vec2", "vec2", "vec2", "vec2"], (pos, A, B, C) => {
22
+ export const sdfQuadratic2 = defn(F, "sdfQuadratic2", [V2, V2, V2, V2], (pos, A, B, C) => {
22
23
  let a, b, c, d;
23
24
  let kk, kx, ky, kz;
24
25
  let p, q, p3, q2, h;
@@ -1,3 +1,4 @@
1
+ import { F, V2, V4 } from "@thi.ng/shader-ast/api/types";
1
2
  import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
2
3
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
4
  import { FLOAT0, VEC2_0 } from "@thi.ng/shader-ast/ast/lit";
@@ -18,7 +19,7 @@ import { maxComp2 } from "../math/maxcomp.js";
18
19
  * @param size - vec2
19
20
  * @param r - vec4
20
21
  */
21
- export const sdfBoxRounded = defn("float", "sdfBoxRounded", ["vec2", "vec2", "vec4"], (p, size, r) => {
22
+ export const sdfBoxRounded = defn(F, "sdfBoxRounded", [V2, V2, V4], (p, size, r) => {
22
23
  let q, t, d;
23
24
  return [
24
25
  (t = sym(ternary(gt($x(p), FLOAT0), $xy(r), $(r, "zw")))),
package/sdf/box.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { F, V2, V3 } from "@thi.ng/shader-ast/api/types";
1
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
2
3
  import { FLOAT0, VEC2_0, VEC3_0 } from "@thi.ng/shader-ast/ast/lit";
3
4
  import { add, sub } from "@thi.ng/shader-ast/ast/ops";
@@ -10,7 +11,7 @@ import { maxComp2, maxComp3 } from "../math/maxcomp.js";
10
11
  * @param p - vec2
11
12
  * @param size - vec2
12
13
  */
13
- export const sdfBox2 = defn("float", "sdfBox2", ["vec2", "vec2"], (p, size) => {
14
+ export const sdfBox2 = defn(F, "sdfBox2", [V2, V2], (p, size) => {
14
15
  let d;
15
16
  return [
16
17
  (d = sym(sub(abs(p), size))),
@@ -23,7 +24,7 @@ export const sdfBox2 = defn("float", "sdfBox2", ["vec2", "vec2"], (p, size) => {
23
24
  * @param p - vec3
24
25
  * @param size - vec3
25
26
  */
26
- export const sdfBox3 = defn("float", "sdfBox3", ["vec3", "vec3"], (p, size) => {
27
+ export const sdfBox3 = defn(F, "sdfBox3", [V3, V3], (p, size) => {
27
28
  let d;
28
29
  return [
29
30
  (d = sym(sub(abs(p), size))),
package/sdf/cross.js CHANGED
@@ -1,7 +1,8 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
1
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
2
3
  import { ifThen, ternary } from "@thi.ng/shader-ast/ast/controlflow";
3
4
  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 { FLOAT0, FLOAT05, VEC2_0, vec2 } from "@thi.ng/shader-ast/ast/lit";
5
6
  import { add, gt, madd, mul, neg, sub } from "@thi.ng/shader-ast/ast/ops";
6
7
  import { $, $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
7
8
  import { sym } from "@thi.ng/shader-ast/ast/sym";
@@ -22,7 +23,7 @@ import { maxComp2 } from "../math/maxcomp.js";
22
23
  * @param size -
23
24
  * @param r -
24
25
  */
25
- export const sdfCross2 = defn("float", "sdfCross2", ["vec2", "vec2", "float"], (p, size, r) => {
26
+ export const sdfCross2 = defn(F, "sdfCross2", [V2, V2, F], (p, size, r) => {
26
27
  let a, q, w;
27
28
  let k;
28
29
  return [
@@ -46,7 +47,7 @@ export const sdfCross2 = defn("float", "sdfCross2", ["vec2", "vec2", "float"], (
46
47
  * @param size -
47
48
  * @param r -
48
49
  */
49
- export const sdfRoundedX2 = defn("float", "sdfRoundedX", ["vec2", "float", "float"], (p, size, r) => {
50
+ export const sdfRoundedX2 = defn(F, "sdfRoundedX", [V2, F, F], (p, size, r) => {
50
51
  let q;
51
52
  return [
52
53
  (q = sym(abs(p))),
package/sdf/cylinder.js CHANGED
@@ -1,5 +1,6 @@
1
+ import { F, V3 } from "@thi.ng/shader-ast/api/types";
1
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
2
- import { FLOAT0, vec2, VEC2_0 } from "@thi.ng/shader-ast/ast/lit";
3
+ import { FLOAT0, VEC2_0, vec2 } from "@thi.ng/shader-ast/ast/lit";
3
4
  import { add, sub } from "@thi.ng/shader-ast/ast/ops";
4
5
  import { $, $y } from "@thi.ng/shader-ast/ast/swizzle";
5
6
  import { sym } from "@thi.ng/shader-ast/ast/sym";
@@ -13,7 +14,7 @@ import { maxComp2 } from "../math/maxcomp.js";
13
14
  * @param h - float
14
15
  * @param r - float
15
16
  */
16
- export const sdfCylinder = defn("float", "sdCylinder", ["vec3", "float", "float"], (p, h, r) => {
17
+ export const sdfCylinder = defn(F, "sdCylinder", [V3, F, F], (p, h, r) => {
17
18
  let d;
18
19
  return [
19
20
  (d = sym(sub(abs(vec2(length($(p, "xz")), $y(p))), vec2(h, r)))),
package/sdf/hex.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
1
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
2
3
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
4
  import { FLOAT0, vec2 } from "@thi.ng/shader-ast/ast/lit";
@@ -12,7 +13,7 @@ import { abs, clamp, dot, length, min, sign, } from "@thi.ng/shader-ast/builtin/
12
13
  * Ported from original GLSL impl by Inigo Quilez:
13
14
  * - https://iquilezles.org/articles/distfunctions2d/
14
15
  */
15
- export const sdfHexagon2 = defn("float", "sdfHexagon2", ["vec2", "float"], (p, r) => {
16
+ export const sdfHexagon2 = defn(F, "sdfHexagon2", [V2, F], (p, r) => {
16
17
  const TAN30 = 0.5773502691896257;
17
18
  let k, q;
18
19
  return [
package/sdf/line.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { F, V2, V3 } from "@thi.ng/shader-ast/api/types";
1
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
2
3
  import { div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
3
4
  import { sym } from "@thi.ng/shader-ast/ast/sym";
@@ -10,7 +11,7 @@ import { clamp01 } from "../math/clamp.js";
10
11
  * @param a -
11
12
  * @param b -
12
13
  */
13
- const $ = (n, type) => defn("float", `sdLine${n}`, [type, type, type], (p, a, b) => {
14
+ const $ = (n, type) => defn(F, `sdLine${n}`, [type, type, type], (p, a, b) => {
14
15
  let pa, ba;
15
16
  return [
16
17
  (pa = sym(sub(p, a))),
@@ -25,7 +26,7 @@ const $ = (n, type) => defn("float", `sdLine${n}`, [type, type, type], (p, a, b)
25
26
  * @param a - vec2
26
27
  * @param b - vec2
27
28
  */
28
- export const sdfLine2 = $(2, "vec2");
29
+ export const sdfLine2 = $(2, V2);
29
30
  /**
30
31
  * Returns signed distance from `p` to 3D line segment `a` -> `b`.
31
32
  *
@@ -33,4 +34,4 @@ export const sdfLine2 = $(2, "vec2");
33
34
  * @param a - vec3
34
35
  * @param b - vec3
35
36
  */
36
- export const sdfLine3 = $(3, "vec3");
37
+ export const sdfLine3 = $(3, V3);
package/sdf/mirror.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { V2 } from "@thi.ng/shader-ast/api/types";
1
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
2
3
  import { FLOAT05, VEC2_1, VEC2_2 } from "@thi.ng/shader-ast/ast/lit";
3
4
  import { add, div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
@@ -10,7 +11,7 @@ import { floor, mod } from "@thi.ng/shader-ast/builtin/math";
10
11
  * @param p - point
11
12
  * @param size - mirror box size
12
13
  */
13
- export const sdfMirror2 = defn("vec2", "sdfMirror2", ["vec2", "vec2"], (p, size) => {
14
+ export const sdfMirror2 = defn(V2, "sdfMirror2", [V2, V2], (p, size) => {
14
15
  let halfSize;
15
16
  return [
16
17
  (halfSize = sym(mul(size, FLOAT05))),
package/sdf/plane.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { F, V2, V3 } from "@thi.ng/shader-ast/api/types";
1
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
2
3
  import { add } from "@thi.ng/shader-ast/ast/ops";
3
4
  import { dot } from "@thi.ng/shader-ast/builtin/math";
@@ -8,7 +9,9 @@ import { dot } from "@thi.ng/shader-ast/builtin/math";
8
9
  * @param normal - vec2
9
10
  * @param w - float
10
11
  */
11
- export const sdfPlane2 = defn("float", "sdPlane2", ["vec2", "vec2", "float"], (p, n, w) => [ret(add(dot(p, n), w))]);
12
+ export const sdfPlane2 = defn(F, "sdPlane2", [V2, V2, F], (p, n, w) => [
13
+ ret(add(dot(p, n), w)),
14
+ ]);
12
15
  /**
13
16
  * Returns signed distance from `p` to plane defined by `normal` and `w`.
14
17
  *
@@ -16,4 +19,6 @@ export const sdfPlane2 = defn("float", "sdPlane2", ["vec2", "vec2", "float"], (p
16
19
  * @param normal - vec3
17
20
  * @param w - float
18
21
  */
19
- export const sdfPlane3 = defn("float", "sdPlane3", ["vec3", "vec3", "float"], (p, n, w) => [ret(add(dot(p, n), w))]);
22
+ export const sdfPlane3 = defn(F, "sdPlane3", [V3, V3, F], (p, n, w) => [
23
+ ret(add(dot(p, n), w)),
24
+ ]);
package/sdf/polygon.js CHANGED
@@ -1,12 +1,13 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
1
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
2
3
  import { forLoop, ifThen } from "@thi.ng/shader-ast/ast/controlflow";
3
4
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
4
5
  import { index } from "@thi.ng/shader-ast/ast/indexed";
5
- import { bvec3, FLOAT1, int, INT0 } from "@thi.ng/shader-ast/ast/lit";
6
+ import { FLOAT1, INT0, bvec3, int } from "@thi.ng/shader-ast/ast/lit";
6
7
  import { div, gt, gte, inc, lt, mul, neg, not, or, sub, } from "@thi.ng/shader-ast/ast/ops";
7
8
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
8
9
  import { sym } from "@thi.ng/shader-ast/ast/sym";
9
- import { all, _any } from "@thi.ng/shader-ast/builtin/bvec";
10
+ import { _any, all } from "@thi.ng/shader-ast/builtin/bvec";
10
11
  import { dot, min, sqrt } from "@thi.ng/shader-ast/builtin/math";
11
12
  import { clamp01 } from "../math/clamp.js";
12
13
  /**
@@ -19,7 +20,7 @@ import { clamp01 } from "../math/clamp.js";
19
20
  *
20
21
  * @param N
21
22
  */
22
- export const sdfPolygon2 = (N) => defn("float", `sdfPolygon2_${N}`, ["vec2", ["vec2[]", "pts", { num: N }]], (p, pts) => {
23
+ export const sdfPolygon2 = (N) => defn(F, `sdfPolygon2_${N}`, [V2, ["vec2[]", "pts", { num: N }]], (p, pts) => {
23
24
  let d, s;
24
25
  let b, e, w, t;
25
26
  let pi, pj;
package/sdf/polyhedra.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { F, V3 } from "@thi.ng/shader-ast/api/types";
1
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
2
3
  import { FLOAT0, PHI, vec3 } from "@thi.ng/shader-ast/ast/lit";
3
4
  import { add, reciprocal, sub } from "@thi.ng/shader-ast/ast/ops";
@@ -40,10 +41,10 @@ const GDF = [
40
41
  * @param vecs -
41
42
  */
42
43
  const defGDF = (id, vecs) => [
43
- defn("float", id, ["vec3", "float"], (p, r) => [
44
+ defn(F, id, [V3, F], (p, r) => [
44
45
  ret(sub(vecs.reduce((acc, v) => max(acc, abs(dot(p, v))), FLOAT0), r)),
45
46
  ]),
46
- defn("float", id + "Smooth", ["vec3", "float", "float"], (p, r, e) => [
47
+ defn(F, id + "Smooth", [V3, F, F], (p, r, e) => [
47
48
  ret(sub(pow(vecs.reduce((acc, v) => add(acc, pow(abs(dot(p, v)), e)), FLOAT0), reciprocal(e)), r)),
48
49
  ]),
49
50
  ];
@@ -1,3 +1,4 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
1
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
2
3
  import { FLOAT05, TAU } from "@thi.ng/shader-ast/ast/lit";
3
4
  import { add, div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
@@ -12,7 +13,7 @@ import { cossin } from "../math/sincos.js";
12
13
  * @param p - point
13
14
  * @param n - number of polar repetitions
14
15
  */
15
- export const sdfRepeatPolar2 = defn("vec2", "sdfRepeatPolar2", ["vec2", "float"], (p, n) => {
16
+ export const sdfRepeatPolar2 = defn(V2, "sdfRepeatPolar2", [V2, F], (p, n) => {
16
17
  let angle;
17
18
  let angle2;
18
19
  let a;
package/sdf/repeat.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { V2, V3 } from "@thi.ng/shader-ast/api/types";
1
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
2
3
  import { FLOAT05 } from "@thi.ng/shader-ast/ast/lit";
3
4
  import { mul, sub } from "@thi.ng/shader-ast/ast/ops";
@@ -11,11 +12,11 @@ const $ = (n, type) => defn(type, `sdTxRepeat${n}`, [type, type], (p, c) => [
11
12
  * @param p - vec2
12
13
  * @param c - vec2
13
14
  */
14
- export const sdfRepeat2 = $(2, "vec2");
15
+ export const sdfRepeat2 = $(2, V2);
15
16
  /**
16
17
  * 3D domain repetition by wrapping position `p` into period `c`.
17
18
  *
18
19
  * @param p - vec3
19
20
  * @param c - vec3
20
21
  */
21
- export const sdfRepeat3 = $(3, "vec3");
22
+ export const sdfRepeat3 = $(3, V3);
@@ -1,3 +1,4 @@
1
+ import { F } from "@thi.ng/shader-ast/api/types";
1
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
2
3
  import { FLOAT1 } from "@thi.ng/shader-ast/ast/lit";
3
4
  import { add, div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
@@ -10,7 +11,7 @@ import { fit1101 } from "../math/fit.js";
10
11
  * @param d2 - float
11
12
  * @param k - float
12
13
  */
13
- export const sdfSmoothIntersect = defn("float", "sdOpSmoothIntersect", ["float", "float", "float"], (a, b, k) => {
14
+ export const sdfSmoothIntersect = defn(F, "sdOpSmoothIntersect", [F, F, F], (a, b, k) => {
14
15
  let h;
15
16
  return [
16
17
  (h = sym(clamp01(fit1101(div(sub(b, a), k))))),