UNPKG

@thi.ng/shader-ast-stdlib

Version:

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

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