@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.
- package/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/api.js +0 -1
- package/color/aces-film.js +12 -10
- package/color/levels.js +80 -66
- package/color/linear-srgb.js +11 -21
- package/color/luminance.js +4 -6
- package/color/porter-duff.js +51 -55
- package/color/rgbe.js +13 -15
- package/fog/exp.js +5 -9
- package/fog/exp2.js +6 -12
- package/fog/linear.js +5 -11
- package/isec/point.js +19 -13
- package/light/lambert.js +8 -29
- package/light/trilight.js +17 -15
- package/math/additive.js +27 -32
- package/math/cartesian.js +21 -24
- package/math/clamp.js +7 -19
- package/math/cross2.js +7 -19
- package/math/dist-chebyshev.js +8 -3
- package/math/dist-manhattan.js +8 -3
- package/math/easing.js +325 -88
- package/math/fit.js +17 -65
- package/math/interval.js +23 -24
- package/math/magsq.js +8 -3
- package/math/maxcomp.js +8 -18
- package/math/mincomp.js +8 -18
- package/math/mix-cubic.js +32 -10
- package/math/mix-quadratic.js +27 -8
- package/math/orthogonal.js +15 -21
- package/math/osc.js +28 -39
- package/math/polar.js +12 -21
- package/math/sincos.js +6 -12
- package/math/smoother-step.js +26 -24
- package/matrix/convert.js +19 -8
- package/matrix/lookat.js +20 -18
- package/matrix/mvp.js +4 -10
- package/matrix/normal.js +4 -8
- package/matrix/rotation.js +131 -47
- package/noise/curl3.js +30 -18
- package/noise/hash.js +126 -197
- package/noise/permute.js +11 -5
- package/noise/simplex2.js +87 -41
- package/noise/simplex3.js +168 -75
- package/noise/voronoi2.js +67 -50
- package/noise/worley2.js +70 -58
- package/package.json +8 -5
- package/raymarch/ao.js +25 -24
- package/raymarch/direction.js +16 -9
- package/raymarch/normal.js +10 -16
- package/raymarch/point-at.js +4 -11
- package/raymarch/scene.js +35 -44
- package/screen/uv.js +23 -30
- package/sdf/annular.js +4 -7
- package/sdf/arc.js +20 -19
- package/sdf/bezier.js +93 -57
- package/sdf/box-rounded.js +14 -18
- package/sdf/box.js +16 -24
- package/sdf/cross.js +23 -44
- package/sdf/cylinder.js +9 -14
- package/sdf/hex.js +22 -19
- package/sdf/isec.js +4 -8
- package/sdf/line.js +14 -29
- package/sdf/mirror.js +17 -13
- package/sdf/plane.js +8 -18
- package/sdf/polygon.js +51 -29
- package/sdf/polyhedra.js +78 -43
- package/sdf/polyline.js +30 -7
- package/sdf/repeat-polar.js +13 -17
- package/sdf/repeat.js +7 -15
- package/sdf/round.js +4 -10
- package/sdf/smooth-isec.js +14 -17
- package/sdf/smooth-sub.js +14 -17
- package/sdf/smooth-union.js +14 -17
- package/sdf/sphere.js +8 -16
- package/sdf/sub.js +4 -8
- package/sdf/torus.js +5 -10
- package/sdf/tri.js +30 -20
- package/sdf/union.js +7 -18
- package/tex/blur.js +53 -70
- package/tex/index-coord.js +6 -16
- package/tex/index-uv.js +30 -20
- package/tex/read-index.js +10 -32
- 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.
|
|
3
|
+
"version": "0.16.10",
|
|
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
|
|
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.
|
|
40
|
-
"@thi.ng/shader-ast": "^0.12.
|
|
41
|
+
"@thi.ng/api": "^8.9.12",
|
|
42
|
+
"@thi.ng/shader-ast": "^0.12.85"
|
|
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",
|
|
@@ -352,5 +355,5 @@
|
|
|
352
355
|
],
|
|
353
356
|
"year": 2019
|
|
354
357
|
},
|
|
355
|
-
"gitHead": "
|
|
358
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
+
};
|
package/raymarch/direction.js
CHANGED
|
@@ -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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
17
|
-
|
|
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
|
+
};
|
package/raymarch/normal.js
CHANGED
|
@@ -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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
+
};
|
package/raymarch/point-at.js
CHANGED
|
@@ -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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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 {
|
|
6
|
-
|
|
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 {
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
+
};
|
package/sdf/box-rounded.js
CHANGED
|
@@ -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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
+
};
|