@thi.ng/shader-ast-stdlib
Version:
Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast
27 lines (26 loc) • 931 B
JavaScript
import { F, V2, V4 } from "@thi.ng/shader-ast/api/types";
import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { FLOAT0, VEC2_0 } from "@thi.ng/shader-ast/ast/lit";
import { add, gt, sub } from "@thi.ng/shader-ast/ast/ops";
import { $, $x, $xy, $y } from "@thi.ng/shader-ast/ast/swizzle";
import { sym } from "@thi.ng/shader-ast/ast/sym";
import { abs, length, max, min } from "@thi.ng/shader-ast/builtin/math";
import { maxComp2 } from "../math/maxcomp.js";
const sdfBoxRounded = defn(
F,
"sdfBoxRounded",
[V2, V2, V4],
(p, size, r) => {
let q, t, d;
return [
t = sym(ternary(gt($x(p), FLOAT0), $xy(r), $(r, "zw"))),
d = sym(ternary(gt($y(p), FLOAT0), $x(t), $y(t))),
q = sym(add(sub(abs(p), size), d)),
ret(sub(add(min(maxComp2(q), FLOAT0), length(max(q, VEC2_0))), d))
];
}
);
export {
sdfBoxRounded
};