UNPKG

@thi.ng/shader-ast-stdlib

Version:

Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast

26 lines (25 loc) 797 B
import { F } from "@thi.ng/shader-ast/api/types"; import { defn, ret } from "@thi.ng/shader-ast/ast/function"; import { FLOAT1 } from "@thi.ng/shader-ast/ast/lit"; import { div, mul, sub } from "@thi.ng/shader-ast/ast/ops"; import { sym } from "@thi.ng/shader-ast/ast/sym"; import { mix } from "@thi.ng/shader-ast/builtin/math"; import { clamp01 } from "../math/clamp.js"; import { fit1101 } from "../math/fit.js"; const sdfSmoothUnion = defn( F, "sdOpSmoothUnion", [F, F, F], (a, b, k) => { let h; return [ h = sym(clamp01(fit1101(div(sub(b, a), k)))), ret(sub(mix(b, a, h), mul(mul(k, h), sub(FLOAT1, h)))) ]; } ); const sdfSmoothUnionAll = (k, ...terms) => terms.reduce((acc, x) => sdfSmoothUnion(acc, x, k)); export { sdfSmoothUnion, sdfSmoothUnionAll };