@thi.ng/shader-ast-stdlib 0.16.9 → 0.16.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +1 -1
  3. package/api.js +0 -1
  4. package/color/aces-film.js +12 -10
  5. package/color/levels.js +80 -66
  6. package/color/linear-srgb.js +11 -21
  7. package/color/luminance.js +4 -6
  8. package/color/porter-duff.js +51 -55
  9. package/color/rgbe.js +13 -15
  10. package/fog/exp.js +5 -9
  11. package/fog/exp2.js +6 -12
  12. package/fog/linear.js +5 -11
  13. package/isec/point.js +19 -13
  14. package/light/lambert.js +8 -29
  15. package/light/trilight.js +17 -15
  16. package/math/additive.js +27 -32
  17. package/math/cartesian.js +21 -24
  18. package/math/clamp.js +7 -19
  19. package/math/cross2.js +7 -19
  20. package/math/dist-chebyshev.js +8 -3
  21. package/math/dist-manhattan.js +8 -3
  22. package/math/easing.js +325 -88
  23. package/math/fit.js +17 -65
  24. package/math/interval.js +23 -24
  25. package/math/magsq.js +8 -3
  26. package/math/maxcomp.js +8 -18
  27. package/math/mincomp.js +8 -18
  28. package/math/mix-cubic.js +32 -10
  29. package/math/mix-quadratic.js +27 -8
  30. package/math/orthogonal.js +15 -21
  31. package/math/osc.js +28 -39
  32. package/math/polar.js +12 -21
  33. package/math/sincos.js +6 -12
  34. package/math/smoother-step.js +26 -24
  35. package/matrix/convert.js +19 -8
  36. package/matrix/lookat.js +20 -18
  37. package/matrix/mvp.js +4 -10
  38. package/matrix/normal.js +4 -8
  39. package/matrix/rotation.js +131 -47
  40. package/noise/curl3.js +30 -18
  41. package/noise/hash.js +126 -197
  42. package/noise/permute.js +11 -5
  43. package/noise/simplex2.js +87 -41
  44. package/noise/simplex3.js +168 -75
  45. package/noise/voronoi2.js +67 -50
  46. package/noise/worley2.js +70 -58
  47. package/package.json +9 -6
  48. package/raymarch/ao.js +25 -24
  49. package/raymarch/direction.js +16 -9
  50. package/raymarch/normal.js +10 -16
  51. package/raymarch/point-at.js +4 -11
  52. package/raymarch/scene.js +35 -44
  53. package/screen/uv.js +23 -30
  54. package/sdf/annular.js +4 -7
  55. package/sdf/arc.js +20 -19
  56. package/sdf/bezier.js +93 -57
  57. package/sdf/box-rounded.js +14 -18
  58. package/sdf/box.js +16 -24
  59. package/sdf/cross.js +23 -44
  60. package/sdf/cylinder.js +9 -14
  61. package/sdf/hex.js +22 -19
  62. package/sdf/isec.js +4 -8
  63. package/sdf/line.js +14 -29
  64. package/sdf/mirror.js +17 -13
  65. package/sdf/plane.js +8 -18
  66. package/sdf/polygon.js +51 -29
  67. package/sdf/polyhedra.js +78 -43
  68. package/sdf/polyline.js +30 -7
  69. package/sdf/repeat-polar.js +13 -17
  70. package/sdf/repeat.js +7 -15
  71. package/sdf/round.js +4 -10
  72. package/sdf/smooth-isec.js +14 -17
  73. package/sdf/smooth-sub.js +14 -17
  74. package/sdf/smooth-union.js +14 -17
  75. package/sdf/sphere.js +8 -16
  76. package/sdf/sub.js +4 -8
  77. package/sdf/torus.js +5 -10
  78. package/sdf/tri.js +30 -20
  79. package/sdf/union.js +7 -18
  80. package/tex/blur.js +53 -70
  81. package/tex/index-coord.js +6 -16
  82. package/tex/index-uv.js +30 -20
  83. package/tex/read-index.js +10 -32
  84. package/viz/function.js +70 -68
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/shader-ast-stdlib",
3
- "version": "0.16.9",
3
+ "version": "0.16.11",
4
4
  "description": "Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -27,7 +27,9 @@
27
27
  ],
28
28
  "license": "Apache-2.0",
29
29
  "scripts": {
30
- "build": "yarn clean && tsc --declaration",
30
+ "build": "yarn build:esbuild && yarn build:decl",
31
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
32
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
31
33
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc color fog light math matrix noise raymarch screen sdf tex",
32
34
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
33
35
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
@@ -36,11 +38,12 @@
36
38
  "test": "bun test"
37
39
  },
38
40
  "dependencies": {
39
- "@thi.ng/api": "^8.9.11",
40
- "@thi.ng/shader-ast": "^0.12.84"
41
+ "@thi.ng/api": "^8.9.13",
42
+ "@thi.ng/shader-ast": "^0.12.86"
41
43
  },
42
44
  "devDependencies": {
43
45
  "@microsoft/api-extractor": "^7.38.3",
46
+ "esbuild": "^0.19.8",
44
47
  "rimraf": "^5.0.5",
45
48
  "tools": "^0.0.1",
46
49
  "typedoc": "^0.25.4",
@@ -77,7 +80,7 @@
77
80
  "access": "public"
78
81
  },
79
82
  "engines": {
80
- "node": ">=12.7"
83
+ "node": ">=18"
81
84
  },
82
85
  "files": [
83
86
  "./*.js",
@@ -352,5 +355,5 @@
352
355
  ],
353
356
  "year": 2019
354
357
  },
355
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
358
+ "gitHead": "25a42a81fac8603a1e440a7aa8bc343276211ff4\n"
356
359
  }
package/raymarch/ao.js CHANGED
@@ -8,28 +8,29 @@ import { add, inc, lte, mul, sub } from "@thi.ng/shader-ast/ast/ops";
8
8
  import { $x } from "@thi.ng/shader-ast/ast/swizzle";
9
9
  import { sym } from "@thi.ng/shader-ast/ast/sym";
10
10
  import { clamp01 } from "../math/clamp.js";
11
- /**
12
- * Higher order function returning an function to compute the Ambient
13
- * Occlusion term / shadow factor for given SDF scene function. The
14
- * returned function takes 2 arguments: surface pos and normal. It
15
- * returns a float in [0..1] interval (zero = fully occluded).
16
- *
17
- * @param scene -
18
- * @param numSamples -
19
- * @param name -
20
- */
21
- export const raymarchAO = (scene, numSamples = 5, name = gensym("raymarchAO_")) => defn(F, name, [V3, V3], (p, n) => {
22
- let r;
23
- let w;
24
- let d0;
25
- return [
26
- (r = sym(FLOAT0)),
27
- (w = sym(FLOAT1)),
28
- forLoop(sym(float(1)), (i) => lte(i, float(numSamples)), inc, (i) => [
29
- (d0 = sym(mul(i, 1 / numSamples))),
30
- assign(r, add(r, mul(w, sub(d0, $x(scene(add(p, mul(n, d0)))))))),
31
- assign(w, mul(w, FLOAT05)),
32
- ]),
33
- ret(sub(FLOAT1, clamp01(r))),
34
- ];
11
+ const raymarchAO = (scene, numSamples = 5, name = gensym("raymarchAO_")) => defn(F, name, [V3, V3], (p, n) => {
12
+ let r;
13
+ let w;
14
+ let d0;
15
+ return [
16
+ r = sym(FLOAT0),
17
+ w = sym(FLOAT1),
18
+ forLoop(
19
+ sym(float(1)),
20
+ (i) => lte(i, float(numSamples)),
21
+ inc,
22
+ (i) => [
23
+ d0 = sym(mul(i, 1 / numSamples)),
24
+ assign(
25
+ r,
26
+ add(r, mul(w, sub(d0, $x(scene(add(p, mul(n, d0)))))))
27
+ ),
28
+ assign(w, mul(w, FLOAT05))
29
+ ]
30
+ ),
31
+ ret(sub(FLOAT1, clamp01(r)))
32
+ ];
35
33
  });
34
+ export {
35
+ raymarchAO
36
+ };
@@ -5,15 +5,22 @@ import { div, neg, sub } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { $y } from "@thi.ng/shader-ast/ast/swizzle";
6
6
  import { sym } from "@thi.ng/shader-ast/ast/sym";
7
7
  import { normalize, radians, tan } from "@thi.ng/shader-ast/builtin/math";
8
- /**
9
- * @param fragCoord - vec2
10
- * @param res - vec2
11
- * @param fov - float vertical FOV (in degrees)
12
- */
13
- export const raymarchDir = defn(V3, "raymarchDir", [V2, V2, F], (frag, res, fov) => {
8
+ const raymarchDir = defn(
9
+ V3,
10
+ "raymarchDir",
11
+ [V2, V2, F],
12
+ (frag, res, fov) => {
14
13
  let uv;
15
14
  return [
16
- (uv = sym(sub(frag, div(res, FLOAT2)))),
17
- ret(normalize(vec3(uv, neg(div($y(res), tan(div(radians(fov), FLOAT2))))))),
15
+ uv = sym(sub(frag, div(res, FLOAT2))),
16
+ ret(
17
+ normalize(
18
+ vec3(uv, neg(div($y(res), tan(div(radians(fov), FLOAT2)))))
19
+ )
20
+ )
18
21
  ];
19
- });
22
+ }
23
+ );
24
+ export {
25
+ raymarchDir
26
+ };
@@ -6,20 +6,14 @@ import { add, sub } from "@thi.ng/shader-ast/ast/ops";
6
6
  import { $, $x } from "@thi.ng/shader-ast/ast/swizzle";
7
7
  import { sym } from "@thi.ng/shader-ast/ast/sym";
8
8
  import { normalize } from "@thi.ng/shader-ast/builtin/math";
9
- /**
10
- * Higher order function producing a function to compute the raymarched
11
- * scene normal for a given scene function and intersection position.
12
- * Like {@link raymarchScene}, this function takes an existing scene
13
- * function as argument.
14
- *
15
- * @param scene -
16
- * @param name -
17
- */
18
- export const raymarchNormal = (scene, name = gensym("raymarchNormal_")) => defn(V3, name, [V3, F], (p, smooth) => {
19
- let dn;
20
- const comp = (id) => sub($x(scene(add(p, $(dn, id)))), $x(scene(sub(p, $(dn, id)))));
21
- return [
22
- (dn = sym(vec2(smooth, 0))),
23
- ret(normalize(vec3(comp("xyy"), comp("yxy"), comp("yyx")))),
24
- ];
9
+ const raymarchNormal = (scene, name = gensym("raymarchNormal_")) => defn(V3, name, [V3, F], (p, smooth) => {
10
+ let dn;
11
+ const comp = (id) => sub($x(scene(add(p, $(dn, id)))), $x(scene(sub(p, $(dn, id)))));
12
+ return [
13
+ dn = sym(vec2(smooth, 0)),
14
+ ret(normalize(vec3(comp("xyy"), comp("yxy"), comp("yyx"))))
15
+ ];
25
16
  });
17
+ export {
18
+ raymarchNormal
19
+ };
@@ -1,13 +1,6 @@
1
1
  import { add, mul } from "@thi.ng/shader-ast/ast/ops";
2
2
  import { normalize } from "@thi.ng/shader-ast/builtin/math";
3
- /**
4
- * Inline function. Returns point on ray (`p`, `dir`) at distance `t`.
5
- * Unless `norm` is true (default false), `dir` must be already
6
- * normalized.
7
- *
8
- * @param p -
9
- * @param dir -
10
- * @param t -
11
- * @param normalize -
12
- */
13
- export const rayPointAt = (p, dir, t, norm = false) => add(p, mul(norm ? normalize(dir) : dir, t));
3
+ const rayPointAt = (p, dir, t, norm = false) => add(p, mul(norm ? normalize(dir) : dir, t));
4
+ export {
5
+ rayPointAt
6
+ };
package/raymarch/scene.js CHANGED
@@ -8,48 +8,39 @@ import { gt, inc, lt, madd } from "@thi.ng/shader-ast/ast/ops";
8
8
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
9
9
  import { sym } from "@thi.ng/shader-ast/ast/sym";
10
10
  import { rayPointAt } from "./point-at.js";
11
- /**
12
- * Higher order function producing a function to perform a raymarch
13
- * using the provided `scene` function and options to configure the
14
- * raymarch process itself.
15
- *
16
- * Returns an AST function which takes 2 args: ray origin, normalized
17
- * ray direction and returning a 2D vector of:
18
- *
19
- * - x = the signed distance to the `scene` defined SDF surface
20
- * - y = user data provided by `scene` (e.g. material ID)
21
- *
22
- * The `scene` function itself takes a 3D point as input and returns a
23
- * vec2 with the same component meaning as above.
24
- *
25
- * @param scene -
26
- * @param _opts -
27
- */
28
- export const raymarchScene = (scene, _opts) => {
29
- const opts = {
30
- name: gensym("raymarchScene_"),
31
- near: 0.1,
32
- far: 10,
33
- steps: 100,
34
- eps: 0.01,
35
- bias: 0.7,
36
- ..._opts,
37
- };
38
- return defn(V2, opts.name, [V3, V3], (pos, dir) => {
39
- let total;
40
- let res;
41
- return [
42
- (total = sym(float(opts.near))),
43
- (res = sym(V2)),
44
- forLoop(sym(INT0), (i) => lt(i, int(opts.steps)), inc, () => [
45
- assign(res, scene(rayPointAt(pos, dir, total))),
46
- ifThen(lt($x(res), float(opts.eps)), [
47
- ret(vec2(total, $y(res))),
48
- ]),
49
- assign(total, madd($x(res), float(opts.bias), total)),
50
- ifThen(gt(total, float(opts.far)), [brk]),
51
- ]),
52
- ret(vec2(opts.far, 0)),
53
- ];
54
- });
11
+ const raymarchScene = (scene, _opts) => {
12
+ const opts = {
13
+ name: gensym("raymarchScene_"),
14
+ near: 0.1,
15
+ far: 10,
16
+ steps: 100,
17
+ eps: 0.01,
18
+ bias: 0.7,
19
+ ..._opts
20
+ };
21
+ return defn(V2, opts.name, [V3, V3], (pos, dir) => {
22
+ let total;
23
+ let res;
24
+ return [
25
+ total = sym(float(opts.near)),
26
+ res = sym(V2),
27
+ forLoop(
28
+ sym(INT0),
29
+ (i) => lt(i, int(opts.steps)),
30
+ inc,
31
+ () => [
32
+ assign(res, scene(rayPointAt(pos, dir, total))),
33
+ ifThen(lt($x(res), float(opts.eps)), [
34
+ ret(vec2(total, $y(res)))
35
+ ]),
36
+ assign(total, madd($x(res), float(opts.bias), total)),
37
+ ifThen(gt(total, float(opts.far)), [brk])
38
+ ]
39
+ ),
40
+ ret(vec2(opts.far, 0))
41
+ ];
42
+ });
43
+ };
44
+ export {
45
+ raymarchScene
55
46
  };
package/screen/uv.js CHANGED
@@ -4,34 +4,27 @@ import { VEC2_1, bvec4, vec2 } from "@thi.ng/shader-ast/ast/lit";
4
4
  import { add, div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { $xy, $y } from "@thi.ng/shader-ast/ast/swizzle";
6
6
  import { _any, greaterThan, lessThan } from "@thi.ng/shader-ast/builtin/bvec";
7
- /**
8
- * Computes UV coord in [0..1] interval from given `fragCoord` and screen `res`.
9
- *
10
- * @param fragCoord -
11
- * @param res -
12
- */
13
- export const fragUV = (fragCoord, res) => div($xy(fragCoord), res);
14
- /**
15
- * Takes `pos`, a screen coord (e.g. gl_FragCoord) and viewport resolution
16
- * `res`, returns aspect corrected uv, with uv.y in [-1..1] interval and uv.x
17
- * scaled by the aspect ratio `resx / resy`.
18
- *
19
- * @param fragCoord - vec2
20
- * @param resolution - vec2
21
- */
22
- export const aspectCorrectedUV = defn(V2, "aspectCorrectedUV2", [V2, V2], (fragCoord, resolution) => [
23
- ret(div(sub(mul(2, fragCoord), resolution), $y(resolution))),
24
- ]);
25
- /**
26
- * Returns true if at least one coordinate of the given point is within the
27
- * `width` internal border region of UV rect ([0,0] .. [1,1]).
28
- *
29
- * ```c
30
- * borderMask(vec2(0.91, 0.5), 0.1) // true
31
- * borderMask(vec2(0.2, 0.01), 0.1) // true
32
- * borderMask(vec2(0.2, 0.2), 0.1) // false
33
- * ```
34
- */
35
- export const borderMask = defn("bool", "borderMask", [V2, F], (uv, width) => [
36
- ret(_any(bvec4(lessThan(uv, vec2(width)), greaterThan(add(uv, width), VEC2_1)))),
7
+ const fragUV = (fragCoord, res) => div($xy(fragCoord), res);
8
+ const aspectCorrectedUV = defn(
9
+ V2,
10
+ "aspectCorrectedUV2",
11
+ [V2, V2],
12
+ (fragCoord, resolution) => [
13
+ ret(div(sub(mul(2, fragCoord), resolution), $y(resolution)))
14
+ ]
15
+ );
16
+ const borderMask = defn("bool", "borderMask", [V2, F], (uv, width) => [
17
+ ret(
18
+ _any(
19
+ bvec4(
20
+ lessThan(uv, vec2(width)),
21
+ greaterThan(add(uv, width), VEC2_1)
22
+ )
23
+ )
24
+ )
37
25
  ]);
26
+ export {
27
+ aspectCorrectedUV,
28
+ borderMask,
29
+ fragUV
30
+ };
package/sdf/annular.js CHANGED
@@ -1,9 +1,6 @@
1
1
  import { sub } from "@thi.ng/shader-ast/ast/ops";
2
2
  import { abs } from "@thi.ng/shader-ast/builtin/math";
3
- /**
4
- * Inline function. Bi-directional offset to create ring like shapes.
5
- *
6
- * @param d -
7
- * @param r -
8
- */
9
- export const sdfAnnular = (d, r) => sub(abs(d), r);
3
+ const sdfAnnular = (d, r) => sub(abs(d), r);
4
+ export {
5
+ sdfAnnular
6
+ };
package/sdf/arc.js CHANGED
@@ -6,23 +6,24 @@ import { gt, madd, mul, neg, sub } from "@thi.ng/shader-ast/ast/ops";
6
6
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
7
7
  import { sym } from "@thi.ng/shader-ast/ast/sym";
8
8
  import { abs, cos, length, sin } from "@thi.ng/shader-ast/builtin/math";
9
- /**
10
- * Returns signed distance from `p` to 2D circular arc with `aperture` (in
11
- * [0..π] interval), radius `ra` and `thickness`.
12
- *
13
- * @remarks
14
- * Slightly modified version (easier to use aperture control) of original GLSL
15
- * impl by Inigo Quilez:
16
- *
17
- * - https://www.shadertoy.com/view/wl23RK
18
- * - https://iquilezles.org/articles/distfunctions2d/
19
- */
20
- export const sdfArc2 = defn(F, "sdfArc", [V2, F, F, F], (p, apert, ra, rb) => {
21
- let q;
22
- let sc;
23
- return [
24
- (q = sym(vec2(abs($x(p)), $y(p)))),
25
- (sc = sym(vec2(sin(apert), cos(apert)))),
26
- 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)),
27
- ];
9
+ const sdfArc2 = defn(F, "sdfArc", [V2, F, F, F], (p, apert, ra, rb) => {
10
+ let q;
11
+ let sc;
12
+ return [
13
+ q = sym(vec2(abs($x(p)), $y(p))),
14
+ sc = sym(vec2(sin(apert), cos(apert))),
15
+ ret(
16
+ sub(
17
+ ternary(
18
+ gt(mul($y(sc), $x(q)), mul($x(sc), $y(q))),
19
+ length(madd(sc, neg(ra), q)),
20
+ abs(sub(length(q), ra))
21
+ ),
22
+ rb
23
+ )
24
+ )
25
+ ];
28
26
  });
27
+ export {
28
+ sdfArc2
29
+ };
package/sdf/bezier.js CHANGED
@@ -2,24 +2,42 @@ import { F, V2 } from "@thi.ng/shader-ast/api/types";
2
2
  import { assign } from "@thi.ng/shader-ast/ast/assign";
3
3
  import { ifThen } from "@thi.ng/shader-ast/ast/controlflow";
4
4
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
5
- import { FLOAT0, FLOAT1, FLOAT2, float, vec2, } from "@thi.ng/shader-ast/ast/lit";
6
- import { add, div, gte, lt, madd, mul, neg, sub, } from "@thi.ng/shader-ast/ast/ops";
5
+ import {
6
+ FLOAT0,
7
+ FLOAT1,
8
+ FLOAT2,
9
+ float,
10
+ vec2
11
+ } from "@thi.ng/shader-ast/ast/lit";
12
+ import {
13
+ add,
14
+ div,
15
+ gte,
16
+ lt,
17
+ madd,
18
+ mul,
19
+ neg,
20
+ sub
21
+ } from "@thi.ng/shader-ast/ast/ops";
7
22
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
8
23
  import { sym } from "@thi.ng/shader-ast/ast/sym";
9
- import { abs, acos, cos, dot, powf, sign, sin, sqrt, } from "@thi.ng/shader-ast/builtin/math";
24
+ import {
25
+ abs,
26
+ acos,
27
+ cos,
28
+ dot,
29
+ powf,
30
+ sign,
31
+ sin,
32
+ sqrt
33
+ } from "@thi.ng/shader-ast/builtin/math";
10
34
  import { clamp01 } from "../math/clamp.js";
11
35
  import { cross2 } from "../math/cross2.js";
12
- /**
13
- * Returns signed distance from `p` to 2D quadratic bezier (given by points A,
14
- * B, C).
15
- *
16
- * @remarks
17
- * Ported from original GLSL impl by Inigo Quilez:
18
- *
19
- * - https://www.shadertoy.com/view/MlKcDD
20
- * - https://iquilezles.org/articles/distfunctions2d/
21
- */
22
- export const sdfQuadratic2 = defn(F, "sdfQuadratic2", [V2, V2, V2, V2], (pos, A, B, C) => {
36
+ const sdfQuadratic2 = defn(
37
+ F,
38
+ "sdfQuadratic2",
39
+ [V2, V2, V2, V2],
40
+ (pos, A, B, C) => {
23
41
  let a, b, c, d;
24
42
  let kk, kx, ky, kz;
25
43
  let p, q, p3, q2, h;
@@ -27,47 +45,65 @@ export const sdfQuadratic2 = defn(F, "sdfQuadratic2", [V2, V2, V2, V2], (pos, A,
27
45
  let res, sgn;
28
46
  let x, y, uv;
29
47
  return [
30
- (a = sym(sub(B, A))),
31
- (b = sym(add(madd(B, -2, A), C))),
32
- (c = sym(mul(a, 2))),
33
- (d = sym(sub(A, pos))),
34
- (kk = sym(div(FLOAT1, dot(b, b)))),
35
- (kx = sym(mul(kk, dot(a, b)))),
36
- (ky = sym(mul(kk, div(madd(FLOAT2, dot(a, a), dot(d, b)), 3)))),
37
- (kz = sym(mul(kk, dot(d, a)))),
38
- (res = sym(FLOAT0)),
39
- (sgn = sym(FLOAT0)),
40
- (p = sym(sub(ky, mul(kx, kx)))),
41
- (q = sym(madd(sub(mul(mul(FLOAT2, kx), kx), mul(3, ky)), kx, kz))),
42
- (p3 = sym(mul(mul(p, p), p))),
43
- (q2 = sym(mul(q, q))),
44
- (h = sym(madd(p3, 4, q2))),
45
- ifThen(gte(h, FLOAT0), [
46
- assign(h, sqrt(h)),
47
- (x = sym(div(sub(vec2(h, neg(h)), q), FLOAT2))),
48
- (uv = sym(mul(sign(x), powf(abs(x), float(1 / 3))))),
49
- (t = sym(clamp01(sub(add($x(uv), $y(uv)), kx)))),
50
- assign(x, madd(madd(b, t, c), t, d)),
51
- assign(res, dot(x, x)),
52
- assign(sgn, cross2(madd(b, mul(t, FLOAT2), c), x)),
53
- ], [
54
- (z = sym(sqrt(neg(p)))),
55
- (v = sym(div(acos(div(q, mul(mul(p, z), FLOAT2))), float(3)))),
56
- (m = sym(cos(v))),
57
- (n = sym(mul(sin(v), 1.732050808))),
58
- (uv = sym(clamp01(sub(mul(vec2(add(m, m), sub(neg(n), m)), z), kx)))),
59
- (x = sym(madd(madd(b, $x(uv), c), $x(uv), d))),
60
- (y = sym(madd(madd(b, $y(uv), c), $y(uv), d))),
61
- assign(m, dot(x, x)),
62
- assign(n, dot(y, y)),
63
- ifThen(lt(m, n), [
64
- assign(res, m),
65
- assign(sgn, cross2(madd(b, mul($x(uv), 2), c), x)),
66
- ], [
67
- assign(res, n),
68
- assign(sgn, cross2(madd(b, mul($y(uv), 2), c), y)),
69
- ]),
70
- ]),
71
- ret(mul(sign(sgn), sqrt(res))),
48
+ a = sym(sub(B, A)),
49
+ b = sym(add(madd(B, -2, A), C)),
50
+ c = sym(mul(a, 2)),
51
+ d = sym(sub(A, pos)),
52
+ kk = sym(div(FLOAT1, dot(b, b))),
53
+ kx = sym(mul(kk, dot(a, b))),
54
+ ky = sym(mul(kk, div(madd(FLOAT2, dot(a, a), dot(d, b)), 3))),
55
+ kz = sym(mul(kk, dot(d, a))),
56
+ res = sym(FLOAT0),
57
+ sgn = sym(FLOAT0),
58
+ p = sym(sub(ky, mul(kx, kx))),
59
+ q = sym(madd(sub(mul(mul(FLOAT2, kx), kx), mul(3, ky)), kx, kz)),
60
+ p3 = sym(mul(mul(p, p), p)),
61
+ q2 = sym(mul(q, q)),
62
+ h = sym(madd(p3, 4, q2)),
63
+ ifThen(
64
+ gte(h, FLOAT0),
65
+ [
66
+ assign(h, sqrt(h)),
67
+ x = sym(div(sub(vec2(h, neg(h)), q), FLOAT2)),
68
+ uv = sym(mul(sign(x), powf(abs(x), float(1 / 3)))),
69
+ t = sym(clamp01(sub(add($x(uv), $y(uv)), kx))),
70
+ assign(x, madd(madd(b, t, c), t, d)),
71
+ assign(res, dot(x, x)),
72
+ assign(sgn, cross2(madd(b, mul(t, FLOAT2), c), x))
73
+ ],
74
+ [
75
+ z = sym(sqrt(neg(p))),
76
+ v = sym(
77
+ div(acos(div(q, mul(mul(p, z), FLOAT2))), float(3))
78
+ ),
79
+ m = sym(cos(v)),
80
+ n = sym(mul(sin(v), 1.732050808)),
81
+ uv = sym(
82
+ clamp01(
83
+ sub(mul(vec2(add(m, m), sub(neg(n), m)), z), kx)
84
+ )
85
+ ),
86
+ x = sym(madd(madd(b, $x(uv), c), $x(uv), d)),
87
+ y = sym(madd(madd(b, $y(uv), c), $y(uv), d)),
88
+ assign(m, dot(x, x)),
89
+ assign(n, dot(y, y)),
90
+ ifThen(
91
+ lt(m, n),
92
+ [
93
+ assign(res, m),
94
+ assign(sgn, cross2(madd(b, mul($x(uv), 2), c), x))
95
+ ],
96
+ [
97
+ assign(res, n),
98
+ assign(sgn, cross2(madd(b, mul($y(uv), 2), c), y))
99
+ ]
100
+ )
101
+ ]
102
+ ),
103
+ ret(mul(sign(sgn), sqrt(res)))
72
104
  ];
73
- });
105
+ }
106
+ );
107
+ export {
108
+ sdfQuadratic2
109
+ };
@@ -7,24 +7,20 @@ import { $, $x, $xy, $y } from "@thi.ng/shader-ast/ast/swizzle";
7
7
  import { sym } from "@thi.ng/shader-ast/ast/sym";
8
8
  import { abs, length, max, min } from "@thi.ng/shader-ast/builtin/math";
9
9
  import { maxComp2 } from "../math/maxcomp.js";
10
- /**
11
- * Returns signed distance from `p` to centered 2D rect of `size` and corner
12
- * radii defined by given vec4 `r`.
13
- *
14
- * @remarks
15
- * Ported from original GLSL impl by Inigo Quilez:
16
- * - https://iquilezles.org/articles/distfunctions2d/
17
- *
18
- * @param p - vec2
19
- * @param size - vec2
20
- * @param r - vec4
21
- */
22
- export const sdfBoxRounded = defn(F, "sdfBoxRounded", [V2, V2, V4], (p, size, r) => {
10
+ const sdfBoxRounded = defn(
11
+ F,
12
+ "sdfBoxRounded",
13
+ [V2, V2, V4],
14
+ (p, size, r) => {
23
15
  let q, t, d;
24
16
  return [
25
- (t = sym(ternary(gt($x(p), FLOAT0), $xy(r), $(r, "zw")))),
26
- (d = sym(ternary(gt($y(p), FLOAT0), $x(t), $y(t)))),
27
- (q = sym(add(sub(abs(p), size), d))),
28
- ret(sub(add(min(maxComp2(q), FLOAT0), length(max(q, VEC2_0))), d)),
17
+ t = sym(ternary(gt($x(p), FLOAT0), $xy(r), $(r, "zw"))),
18
+ d = sym(ternary(gt($y(p), FLOAT0), $x(t), $y(t))),
19
+ q = sym(add(sub(abs(p), size), d)),
20
+ ret(sub(add(min(maxComp2(q), FLOAT0), length(max(q, VEC2_0))), d))
29
21
  ];
30
- });
22
+ }
23
+ );
24
+ export {
25
+ sdfBoxRounded
26
+ };
package/sdf/box.js CHANGED
@@ -5,29 +5,21 @@ import { add, sub } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { sym } from "@thi.ng/shader-ast/ast/sym";
6
6
  import { abs, length, max, min } from "@thi.ng/shader-ast/builtin/math";
7
7
  import { maxComp2, maxComp3 } from "../math/maxcomp.js";
8
- /**
9
- * Returns signed distance from `p` to centered AABB of `size`.
10
- *
11
- * @param p - vec2
12
- * @param size - vec2
13
- */
14
- export const sdfBox2 = defn(F, "sdfBox2", [V2, V2], (p, size) => {
15
- let d;
16
- return [
17
- (d = sym(sub(abs(p), size))),
18
- ret(add(min(maxComp2(d), FLOAT0), length(max(d, VEC2_0)))),
19
- ];
8
+ const sdfBox2 = defn(F, "sdfBox2", [V2, V2], (p, size) => {
9
+ let d;
10
+ return [
11
+ d = sym(sub(abs(p), size)),
12
+ ret(add(min(maxComp2(d), FLOAT0), length(max(d, VEC2_0))))
13
+ ];
20
14
  });
21
- /**
22
- * Returns signed distance from `p` to centered AABB of `size`.
23
- *
24
- * @param p - vec3
25
- * @param size - vec3
26
- */
27
- export const sdfBox3 = defn(F, "sdfBox3", [V3, V3], (p, size) => {
28
- let d;
29
- return [
30
- (d = sym(sub(abs(p), size))),
31
- ret(add(min(maxComp3(d), FLOAT0), length(max(d, VEC3_0)))),
32
- ];
15
+ const sdfBox3 = defn(F, "sdfBox3", [V3, V3], (p, size) => {
16
+ let d;
17
+ return [
18
+ d = sym(sub(abs(p), size)),
19
+ ret(add(min(maxComp3(d), FLOAT0), length(max(d, VEC3_0))))
20
+ ];
33
21
  });
22
+ export {
23
+ sdfBox2,
24
+ sdfBox3
25
+ };