UNPKG

@thi.ng/shader-ast-stdlib

Version:

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

26 lines (25 loc) 830 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 { add, div, mul, neg, 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 sdfSmoothSubtract = defn( F, "sdOpSmoothSubtract", [F, F, F], (a, b, k) => { let h; return [ h = sym(clamp01(fit1101(div(add(b, a), k)))), ret(add(mix(b, neg(a), h), mul(mul(k, h), sub(FLOAT1, h)))) ]; } ); const sdfSmoothSubtractAll = (k, ...terms) => terms.reduce((acc, x) => sdfSmoothSubtract(acc, x, k)); export { sdfSmoothSubtract, sdfSmoothSubtractAll };