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

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 +8 -5
  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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-09T19:12:04Z
3
+ - **Last updated**: 2023-12-11T10:07:09Z
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.
package/README.md CHANGED
@@ -105,7 +105,7 @@ For Node.js REPL:
105
105
  const shaderAstStdlib = await import("@thi.ng/shader-ast-stdlib");
106
106
  ```
107
107
 
108
- Package sizes (brotli'd, pre-treeshake): ESM: 13.42 KB
108
+ Package sizes (brotli'd, pre-treeshake): ESM: 13.40 KB
109
109
 
110
110
  ## Dependencies
111
111
 
package/api.js CHANGED
@@ -1 +0,0 @@
1
- export {};
@@ -2,14 +2,16 @@ import { V3 } from "@thi.ng/shader-ast/api/types";
2
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
3
  import { add, div, mul } from "@thi.ng/shader-ast/ast/ops";
4
4
  import { clamp01 } from "../math/clamp.js";
5
- /**
6
- * Filmic HDR -> LDR mapping based on ACES.
7
- *
8
- * Reference:
9
- * https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/
10
- *
11
- * @param col -
12
- */
13
- export const ACESFilm = defn(V3, "ACESFilm", [V3], (col) => [
14
- ret(clamp01(div(mul(col, add(mul(col, 2.51), 0.03)), add(mul(col, add(mul(col, 2.43), 0.59)), 0.14)))),
5
+ const ACESFilm = defn(V3, "ACESFilm", [V3], (col) => [
6
+ ret(
7
+ clamp01(
8
+ div(
9
+ mul(col, add(mul(col, 2.51), 0.03)),
10
+ add(mul(col, add(mul(col, 2.43), 0.59)), 0.14)
11
+ )
12
+ )
13
+ )
15
14
  ]);
15
+ export {
16
+ ACESFilm
17
+ };
package/color/levels.js CHANGED
@@ -2,74 +2,88 @@ import { F, M3, V2, V3 } from "@thi.ng/shader-ast/api/types";
2
2
  import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
3
3
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
4
4
  import { indexMat } from "@thi.ng/shader-ast/ast/indexed";
5
- import { FLOAT0, FLOAT05, FLOAT1, VEC3_0, VEC3_1, float, vec3, } from "@thi.ng/shader-ast/ast/lit";
5
+ import {
6
+ FLOAT0,
7
+ FLOAT05,
8
+ FLOAT1,
9
+ VEC3_0,
10
+ VEC3_1,
11
+ float,
12
+ vec3
13
+ } from "@thi.ng/shader-ast/ast/lit";
6
14
  import { lt, madd, mul, reciprocal, sub } from "@thi.ng/shader-ast/ast/ops";
7
15
  import { $x, $y, $z } from "@thi.ng/shader-ast/ast/swizzle";
8
16
  import { max, min, pow } from "@thi.ng/shader-ast/builtin/math";
9
17
  import { fit01, fitClamped } from "../math/fit.js";
10
- /**
11
- * Takes normalized `mid` level and converts it into an exponent for gamma
12
- * correction using {@link levelAdjustGamma}.
13
- *
14
- * @remarks
15
- * Reference: https://stackoverflow.com/a/48859502/294515
16
- */
17
- export const midLevelGamma = defn(F, "midLevelGamma", [F], (mid) => [
18
- ret(reciprocal(ternary(lt(mid, FLOAT05), min(float(9.99), madd(float(9), sub(1, mul(mid, 2)), 1)), max(float(0.01), sub(1, sub(mul(mid, 2), 1)))))),
18
+ const midLevelGamma = defn(F, "midLevelGamma", [F], (mid) => [
19
+ ret(
20
+ reciprocal(
21
+ ternary(
22
+ lt(mid, FLOAT05),
23
+ min(float(9.99), madd(float(9), sub(1, mul(mid, 2)), 1)),
24
+ max(float(0.01), sub(1, sub(mul(mid, 2), 1)))
25
+ )
26
+ )
27
+ )
19
28
  ]);
20
- /**
21
- * Inline function. Similar to {@link midLevelGamma}, but for RGB values (with
22
- * potentially varying settings per channel). To be used with
23
- * {@link levelAdjustGammaRGB}.
24
- *
25
- * @param mid -
26
- */
27
- export const midLevelGammaRGB = (mid) => vec3(midLevelGamma($x(mid)), midLevelGamma($y(mid)), midLevelGamma($z(mid)));
28
- /**
29
- * Applies level remapping from `input` to `output` (each given as
30
- * `vec2(min,max)`) with `gamma` correction. Results will be clamped to [0..1]
31
- * range.
32
- */
33
- export const levelAdjustGamma = defn(F, "levelAdjustGamma", [F, F, V2, V2], (x, gamma, input, output) => [
34
- ret(fit01(pow(fitClamped(x, $x(input), $y(input), FLOAT0, FLOAT1), gamma), $x(output), $y(output))),
35
- ]);
36
- /**
37
- * Similar to {@link levelAdjustGamma}, but for RGB values (with potentially
38
- * varying settings per channel).
39
- *
40
- * @remarks
41
- * Note: Only the first to columns of the `mat3` args will be used (for WebGL1
42
- * compatibility):
43
- *
44
- * - 1st column = RGB min values
45
- * - 2nd column = RGB max values
46
- */
47
- export const levelAdjustGammaRGB = defn(V3, "levelAdjustGammaRGB", [V3, V3, M3, M3], (x, gamma, input, output) => [
48
- ret(fit01(pow(fitClamped(x, indexMat(input, 0), indexMat(input, 1), VEC3_0, VEC3_1), gamma), indexMat(output, 0), indexMat(output, 1))),
49
- ]);
50
- /**
51
- * Inline function. Similar to {@link levelAdjustGamma}, but first computes
52
- * gamma from given `mid` level.
53
- *
54
- * @remarks
55
- * Also see {@link midLevelGamma}.
56
- *
57
- * @param x - float
58
- * @param mid - float
59
- * @param input - vec2(min,max)
60
- * @param output - vec2(min,max)
61
- */
62
- export const levelAdjustMid = (x, mid, input, output) => levelAdjustGamma(x, midLevelGamma(mid), input, output);
63
- /**
64
- * Inline function. Similar to {@link levelAdjustMid}, but for RGB values (with
65
- * potentially varying settings per channel).
66
- *
67
- * @remarks
68
- * Also see {@link levelAdjustGammaRGB}, {@link midLevelGammaRGB}.
69
- *
70
- * @param x -
71
- * @param mid -
72
- * @param input -
73
- * @param output -
74
- */
75
- export const levelAdjustMidRGB = (x, mid, input, output) => levelAdjustGammaRGB(x, midLevelGammaRGB(mid), input, output);
29
+ const midLevelGammaRGB = (mid) => vec3(
30
+ midLevelGamma($x(mid)),
31
+ midLevelGamma($y(mid)),
32
+ midLevelGamma($z(mid))
33
+ );
34
+ const levelAdjustGamma = defn(
35
+ F,
36
+ "levelAdjustGamma",
37
+ [F, F, V2, V2],
38
+ (x, gamma, input, output) => [
39
+ ret(
40
+ fit01(
41
+ pow(
42
+ fitClamped(
43
+ x,
44
+ $x(input),
45
+ $y(input),
46
+ FLOAT0,
47
+ FLOAT1
48
+ ),
49
+ gamma
50
+ ),
51
+ $x(output),
52
+ $y(output)
53
+ )
54
+ )
55
+ ]
56
+ );
57
+ const levelAdjustGammaRGB = defn(
58
+ V3,
59
+ "levelAdjustGammaRGB",
60
+ [V3, V3, M3, M3],
61
+ (x, gamma, input, output) => [
62
+ ret(
63
+ fit01(
64
+ pow(
65
+ fitClamped(
66
+ x,
67
+ indexMat(input, 0),
68
+ indexMat(input, 1),
69
+ VEC3_0,
70
+ VEC3_1
71
+ ),
72
+ gamma
73
+ ),
74
+ indexMat(output, 0),
75
+ indexMat(output, 1)
76
+ )
77
+ )
78
+ ]
79
+ );
80
+ const levelAdjustMid = (x, mid, input, output) => levelAdjustGamma(x, midLevelGamma(mid), input, output);
81
+ const levelAdjustMidRGB = (x, mid, input, output) => levelAdjustGammaRGB(x, midLevelGammaRGB(mid), input, output);
82
+ export {
83
+ levelAdjustGamma,
84
+ levelAdjustGammaRGB,
85
+ levelAdjustMid,
86
+ levelAdjustMidRGB,
87
+ midLevelGamma,
88
+ midLevelGammaRGB
89
+ };
@@ -3,24 +3,14 @@ import { pow } from "@thi.ng/shader-ast/builtin/math";
3
3
  const GAMMA = float(2.2);
4
4
  const INV_GAMMA = float(1 / 2.2);
5
5
  const $ = (t, x) => ({
6
- float: x,
7
- vec2: vec2(x),
8
- vec3: vec3(x),
9
- vec4: vec4(x, x, x, 1),
10
- }[t.type]);
11
- /**
12
- * Inline function. Converts sRGB color term (1D-4D) to linear space via
13
- * `pow(rgb, GAMMA)`, where gamma is hardcoded to 2.2. For vec4 args,
14
- * the `w` component (alpha) remains unchanged.
15
- *
16
- * @param x -
17
- */
18
- export const toLinear = (x) => pow(x, $(x, GAMMA));
19
- /**
20
- * Inline function. Converts linear color term (1D-4D) to sRGB via
21
- * `pow(rgb, 1.0 / GAMMA)`, where gamma is hardcoded to 2.2. For vec4
22
- * args, the `w` component (alpha) remains unchanged.
23
- *
24
- * @param x -
25
- */
26
- export const toSRGB = (x) => pow(x, $(x, INV_GAMMA));
6
+ float: x,
7
+ vec2: vec2(x),
8
+ vec3: vec3(x),
9
+ vec4: vec4(x, x, x, 1)
10
+ })[t.type];
11
+ const toLinear = (x) => pow(x, $(x, GAMMA));
12
+ const toSRGB = (x) => pow(x, $(x, INV_GAMMA));
13
+ export {
14
+ toLinear,
15
+ toSRGB
16
+ };
@@ -1,8 +1,6 @@
1
1
  import { vec3 } from "@thi.ng/shader-ast/ast/lit";
2
2
  import { dot } from "@thi.ng/shader-ast/builtin/math";
3
- /**
4
- * Inline function. Computes luminance of given RGB color
5
- *
6
- * @param rgb -
7
- */
8
- export const luminanceRGB = (rgb) => dot(rgb, vec3(0.299, 0.587, 0.114));
3
+ const luminanceRGB = (rgb) => dot(rgb, vec3(0.299, 0.587, 0.114));
4
+ export {
5
+ luminanceRGB
6
+ };
@@ -4,60 +4,56 @@ import { FLOAT0, FLOAT1, vec4 } from "@thi.ng/shader-ast/ast/lit";
4
4
  import { add, div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { $w, $xyz } from "@thi.ng/shader-ast/ast/swizzle";
6
6
  import { clamp01 } from "../math/clamp.js";
7
- /** @internal */
8
7
  const __coeff = (col, f) => f === FLOAT0 ? vec4() : f === FLOAT1 ? col : mul(col, f);
9
- /**
10
- * Higher-order Porter-Duff alpha compositing operator. See
11
- * [thi.ng/porter-duff](https://thi.ng/porter-duff) for reference. Returns an
12
- * optimized AST function which accepts 2 RGBA colors (vec4) and returns blended
13
- * & clamped result (also a vec4).
14
- *
15
- * @remark
16
- * All built-in PD operators are defined via this HOF. The two given coefficient
17
- * functions are used to extract blending coefficients for src/dest colors and
18
- * are called with the alpha components of both colors.
19
- *
20
- * Optimization only happens for cases where either `fa` and/or `fb` are
21
- * {@link ZERO} or {@link ONE}.
22
- *
23
- * *IMPORTANT*: Both colors MUST be use pre-multiplied alpha for correct
24
- * results. If needed, use {@link premultiplyAlpha} and
25
- * {@link postmultiplyAlpha}.
26
- *
27
- * @param name - function name
28
- * @param fa - src coeff fn
29
- * @param fb - dest coeff fn
30
- */
31
- export const porterDuff = (name, fa, fb) => defn(V4, name, [V4, V4], (a, b) => {
32
- const sa = fa($w(a), $w(b));
33
- const sb = fb($w(a), $w(b));
34
- const src = __coeff(a, sa);
35
- const dest = __coeff(b, sb);
36
- const srcZero = sa === FLOAT0;
37
- const destZero = sb === FLOAT0;
38
- return [
39
- ret(srcZero && destZero
40
- ? vec4()
41
- : clamp01(srcZero ? dest : destZero ? src : add(src, dest))),
42
- ];
8
+ const porterDuff = (name, fa, fb) => defn(V4, name, [V4, V4], (a, b) => {
9
+ const sa = fa($w(a), $w(b));
10
+ const sb = fb($w(a), $w(b));
11
+ const src = __coeff(a, sa);
12
+ const dest = __coeff(b, sb);
13
+ const srcZero = sa === FLOAT0;
14
+ const destZero = sb === FLOAT0;
15
+ return [
16
+ ret(
17
+ srcZero && destZero ? vec4() : clamp01(srcZero ? dest : destZero ? src : add(src, dest))
18
+ )
19
+ ];
43
20
  });
44
- export const premultiplyAlpha = (col) => vec4(mul($xyz(col), $w(col)), $w(col));
45
- export const postmultiplyAlpha = (col) => vec4(div($xyz(col), $w(col)), $w(col));
46
- // coefficient functions
47
- export const ZERO = () => FLOAT0;
48
- export const ONE = () => FLOAT1;
49
- export const A = (a) => a;
50
- export const B = (_, b) => b;
51
- export const ONE_MINUS_A = (a) => sub(FLOAT1, a);
52
- export const ONE_MINUS_B = (_, b) => sub(FLOAT1, b);
53
- // standard Porter-Duff operators
54
- export const blendSrcOver = porterDuff("blendSrcOver", ONE, ONE_MINUS_A);
55
- export const blendDestOver = porterDuff("blendDestOver", ONE_MINUS_B, ONE);
56
- export const blendSrcIn = porterDuff("blendSrcIn", B, ZERO);
57
- export const blendDestIn = porterDuff("blendDestIn", ZERO, A);
58
- export const blendSrcOut = porterDuff("blendSrcOut", ONE_MINUS_B, ZERO);
59
- export const blendDestOut = porterDuff("blendDestOut", ZERO, ONE_MINUS_A);
60
- export const blendSrcAtop = porterDuff("blendSrcAtop", B, ONE_MINUS_A);
61
- export const blendDestAtop = porterDuff("blendDestAtop", ONE_MINUS_B, A);
62
- export const blendXor = porterDuff("blendXor", ONE_MINUS_B, ONE_MINUS_A);
63
- export const blendPlus = porterDuff("blendPlus", ONE, ONE);
21
+ const premultiplyAlpha = (col) => vec4(mul($xyz(col), $w(col)), $w(col));
22
+ const postmultiplyAlpha = (col) => vec4(div($xyz(col), $w(col)), $w(col));
23
+ const ZERO = () => FLOAT0;
24
+ const ONE = () => FLOAT1;
25
+ const A = (a) => a;
26
+ const B = (_, b) => b;
27
+ const ONE_MINUS_A = (a) => sub(FLOAT1, a);
28
+ const ONE_MINUS_B = (_, b) => sub(FLOAT1, b);
29
+ const blendSrcOver = porterDuff("blendSrcOver", ONE, ONE_MINUS_A);
30
+ const blendDestOver = porterDuff("blendDestOver", ONE_MINUS_B, ONE);
31
+ const blendSrcIn = porterDuff("blendSrcIn", B, ZERO);
32
+ const blendDestIn = porterDuff("blendDestIn", ZERO, A);
33
+ const blendSrcOut = porterDuff("blendSrcOut", ONE_MINUS_B, ZERO);
34
+ const blendDestOut = porterDuff("blendDestOut", ZERO, ONE_MINUS_A);
35
+ const blendSrcAtop = porterDuff("blendSrcAtop", B, ONE_MINUS_A);
36
+ const blendDestAtop = porterDuff("blendDestAtop", ONE_MINUS_B, A);
37
+ const blendXor = porterDuff("blendXor", ONE_MINUS_B, ONE_MINUS_A);
38
+ const blendPlus = porterDuff("blendPlus", ONE, ONE);
39
+ export {
40
+ A,
41
+ B,
42
+ ONE,
43
+ ONE_MINUS_A,
44
+ ONE_MINUS_B,
45
+ ZERO,
46
+ blendDestAtop,
47
+ blendDestIn,
48
+ blendDestOut,
49
+ blendDestOver,
50
+ blendPlus,
51
+ blendSrcAtop,
52
+ blendSrcIn,
53
+ blendSrcOut,
54
+ blendSrcOver,
55
+ blendXor,
56
+ porterDuff,
57
+ postmultiplyAlpha,
58
+ premultiplyAlpha
59
+ };
package/color/rgbe.js CHANGED
@@ -5,19 +5,17 @@ import { INT0, VEC3_0, float, vec3 } from "@thi.ng/shader-ast/ast/lit";
5
5
  import { gt, mul, sub } from "@thi.ng/shader-ast/ast/ops";
6
6
  import { $w, $xyz } from "@thi.ng/shader-ast/ast/swizzle";
7
7
  import { exp2 } from "@thi.ng/shader-ast/builtin/math";
8
- /**
9
- * RGBE (Radiance HDR) to linear float RGB conversion. The input vec is supposed
10
- * to contain unsigned byte values, with the last component being the exponent.
11
- *
12
- * @remarks
13
- * Reference: https://en.wikipedia.org/wiki/RGBE_image_format
14
- *
15
- * Code based on:
16
- * https://github.com/box/hdrCompressor/blob/master/src/rgbe/rgbe.c
17
- *
18
- */
19
- export const decodeRGBE = defn(V3, "decodeRGBE", ["ivec4"], (col) => {
20
- return [
21
- ret(ternary(gt($w(col), INT0), mul(vec3($xyz(col)), exp2(float(sub($w(col), 136)))), VEC3_0)),
22
- ];
8
+ const decodeRGBE = defn(V3, "decodeRGBE", ["ivec4"], (col) => {
9
+ return [
10
+ ret(
11
+ ternary(
12
+ gt($w(col), INT0),
13
+ mul(vec3($xyz(col)), exp2(float(sub($w(col), 136)))),
14
+ VEC3_0
15
+ )
16
+ )
17
+ ];
23
18
  });
19
+ export {
20
+ decodeRGBE
21
+ };
package/fog/exp.js CHANGED
@@ -4,13 +4,9 @@ import { FLOAT1 } from "@thi.ng/shader-ast/ast/lit";
4
4
  import { mul, neg, sub } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { exp } from "@thi.ng/shader-ast/builtin/math";
6
6
  import { clamp01 } from "../math/clamp.js";
7
- /**
8
- * Computes exponential fog factor [0..1], based on given fully
9
- * saturated fog distance and density.
10
- *
11
- * @param dist - float
12
- * @param density - float
13
- */
14
- export const fogExp = defn(F, "fogExp", [F, F], (dist, density) => [
15
- ret(sub(FLOAT1, clamp01(exp(mul(neg(density), dist))))),
7
+ const fogExp = defn(F, "fogExp", [F, F], (dist, density) => [
8
+ ret(sub(FLOAT1, clamp01(exp(mul(neg(density), dist)))))
16
9
  ]);
10
+ export {
11
+ fogExp
12
+ };
package/fog/exp2.js CHANGED
@@ -5,16 +5,10 @@ import { FLOAT1 } from "@thi.ng/shader-ast/ast/lit";
5
5
  import { mul, sub } from "@thi.ng/shader-ast/ast/ops";
6
6
  import { exp2 } from "@thi.ng/shader-ast/builtin/math";
7
7
  import { clamp01 } from "../math/clamp.js";
8
- /**
9
- * Similar to {@link fogExp}. Computes exponential fog factor [0..1], based on
10
- * given fully saturated fog distance and density. Uses
11
- * [`exp2()`](https://docs.thi.ng/umbrella/shader-ast/functions/exp2.html)
12
- * internally.
13
- *
14
- * @param dist - float
15
- * @param density - float
16
- */
17
- export const fogExp2 = defn(F, "fogExp2", [F, F], (dist, density) => [
18
- assign(density, mul(density, dist)),
19
- ret(sub(FLOAT1, clamp01(exp2(mul(mul(density, density), -Math.LOG2E))))),
8
+ const fogExp2 = defn(F, "fogExp2", [F, F], (dist, density) => [
9
+ assign(density, mul(density, dist)),
10
+ ret(sub(FLOAT1, clamp01(exp2(mul(mul(density, density), -Math.LOG2E)))))
20
11
  ]);
12
+ export {
13
+ fogExp2
14
+ };
package/fog/linear.js CHANGED
@@ -2,15 +2,9 @@ import { F } from "@thi.ng/shader-ast/api/types";
2
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
3
  import { clamp01 } from "../math/clamp.js";
4
4
  import { fitNorm1 } from "../math/fit.js";
5
- /**
6
- * Computes linear fog factor [0..1], based on given fog min/max
7
- * distances.
8
- *
9
- * @param dist - float
10
- * @param start - float
11
- * @param end - float
12
- *
13
- */
14
- export const fogLinear = defn(F, "fogLinear", [F, F, F], (dist, start, end) => [
15
- ret(clamp01(fitNorm1(dist, start, end))),
5
+ const fogLinear = defn(F, "fogLinear", [F, F, F], (dist, start, end) => [
6
+ ret(clamp01(fitNorm1(dist, start, end)))
16
7
  ]);
8
+ export {
9
+ fogLinear
10
+ };
package/isec/point.js CHANGED
@@ -5,19 +5,25 @@ import { add } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
6
6
  import { sym } from "@thi.ng/shader-ast/ast/sym";
7
7
  import { distance, min, step } from "@thi.ng/shader-ast/builtin/math";
8
- /**
9
- * Predicate function to check if given point `p` is inside the circle defined
10
- * by `pos` and `radius`.
11
- */
12
- export const isPointInCircle = defn("bool", null, [V2, V2, F], (p, pos, radius) => [ret(bool(step(distance(p, pos), radius)))]);
13
- /**
14
- * Predicate function to check if given point `p` is inside the axis-aligned
15
- * rect defined by `pos` and `size`. Branchless.
16
- */
17
- export const isPointInRect = defn("bool", null, [V2, V2, V2], (p, pos, size) => {
8
+ const isPointInCircle = defn(
9
+ "bool",
10
+ null,
11
+ [V2, V2, F],
12
+ (p, pos, radius) => [ret(bool(step(distance(p, pos), radius)))]
13
+ );
14
+ const isPointInRect = defn(
15
+ "bool",
16
+ null,
17
+ [V2, V2, V2],
18
+ (p, pos, size) => {
18
19
  let q;
19
20
  return [
20
- (q = sym(min(step(pos, p), step(p, add(pos, size))))),
21
- ret(bool(min($x(q), $y(q)))),
21
+ q = sym(min(step(pos, p), step(p, add(pos, size)))),
22
+ ret(bool(min($x(q), $y(q))))
22
23
  ];
23
- });
24
+ }
25
+ );
26
+ export {
27
+ isPointInCircle,
28
+ isPointInRect
29
+ };
package/light/lambert.js CHANGED
@@ -2,32 +2,11 @@ import { FLOAT0 } from "@thi.ng/shader-ast/ast/lit";
2
2
  import { madd, mul } from "@thi.ng/shader-ast/ast/ops";
3
3
  import { dot, max } from "@thi.ng/shader-ast/builtin/math";
4
4
  import { fit1101 } from "../math/fit.js";
5
- /**
6
- * Inline function. Computes Lambert term, i.e. `max(dot(n, l), 0)`. Both
7
- * vectors must be pre-normalized.
8
- *
9
- * @param surfNormal - vec3
10
- * @param lightDir - vec3
11
- */
12
- export const lambert = (n, ldir) => max(dot(n, ldir), FLOAT0);
13
- /**
14
- * Inline function. Computes Half-Lambertian term. Both vectors must be
15
- * pre-normalized.
16
- *
17
- * https://developer.valvesoftware.com/wiki/Half_Lambert
18
- *
19
- * @param n -
20
- * @param ldir -
21
- */
22
- export const halfLambert = (n, ldir) => fit1101(dot(n, ldir));
23
- /**
24
- * Inline function. Computes:
25
- *
26
- * col = lambert * lightCol * diffuseCol + ambientCol
27
- *
28
- * @param lambertian - float
29
- * @param diffuseCol - vec3
30
- * @param lightCol - vec3
31
- * @param ambientCol - vec3
32
- */
33
- export const diffuseLighting = (lambertian, diffuse, light, ambient) => madd(mul(light, lambertian), diffuse, ambient);
5
+ const lambert = (n, ldir) => max(dot(n, ldir), FLOAT0);
6
+ const halfLambert = (n, ldir) => fit1101(dot(n, ldir));
7
+ const diffuseLighting = (lambertian, diffuse, light, ambient) => madd(mul(light, lambertian), diffuse, ambient);
8
+ export {
9
+ diffuseLighting,
10
+ halfLambert,
11
+ lambert
12
+ };
package/light/trilight.js CHANGED
@@ -4,21 +4,23 @@ import { FLOAT0, FLOAT1 } from "@thi.ng/shader-ast/ast/lit";
4
4
  import { add, mul, neg, sub } from "@thi.ng/shader-ast/ast/ops";
5
5
  import { sym } from "@thi.ng/shader-ast/ast/sym";
6
6
  import { abs, dot, max } from "@thi.ng/shader-ast/builtin/math";
7
- /**
8
- * Tom Forsyth's Trilight lighting model.
9
- *
10
- * {@link tomforsyth1000.github.io/papers/trilight/trilight.html}
11
- *
12
- * @param surfNormal - vec3
13
- * @param lightDir - vec3
14
- * @param col1 - vec3
15
- * @param col2 - vec3
16
- * @param col3 - vec3
17
- */
18
- export const trilight = defn(V3, "trilight", [V3, V3, V3, V3, V3], (n, l, c1, c2, c3) => {
7
+ const trilight = defn(
8
+ V3,
9
+ "trilight",
10
+ [V3, V3, V3, V3, V3],
11
+ (n, l, c1, c2, c3) => {
19
12
  let d;
20
13
  return [
21
- (d = sym(dot(n, l))),
22
- ret(add(add(mul(c1, max(d, FLOAT0)), mul(c2, sub(FLOAT1, abs(d)))), mul(c3, max(dot(neg(n), l), FLOAT0)))),
14
+ d = sym(dot(n, l)),
15
+ ret(
16
+ add(
17
+ add(mul(c1, max(d, FLOAT0)), mul(c2, sub(FLOAT1, abs(d)))),
18
+ mul(c3, max(dot(neg(n), l), FLOAT0))
19
+ )
20
+ )
23
21
  ];
24
- });
22
+ }
23
+ );
24
+ export {
25
+ trilight
26
+ };