@thi.ng/shader-ast-stdlib
Version:
Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast
16 lines (15 loc) • 428 B
JavaScript
import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
import { float } from "@thi.ng/shader-ast/ast/lit";
import { lt } from "@thi.ng/shader-ast/ast/ops";
const branches = (x, a, ...terms) => {
const n = terms.length + 1;
if (n < 2) return a;
const delta = 1 / n;
terms = [a, ...terms];
return terms.reduceRight(
(acc, t, i) => ternary(lt(x, float((i + 1) * delta)), t, acc)
);
};
export {
branches
};