UNPKG

@thi.ng/shader-ast-stdlib

Version:

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

26 lines (25 loc) 976 B
import { F } 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, FLOAT05, FLOAT1, FLOAT2 } from "@thi.ng/shader-ast/ast/lit"; import { add, div, mul, neq, sub } from "@thi.ng/shader-ast/ast/ops"; import { mix } from "@thi.ng/shader-ast/builtin/math"; import { clamp01 } from "./clamp.js"; const fitNorm1 = defn(F, "fitNorm1", [F, F, F], (x, a, b) => [ ret(ternary(neq(a, b), div(sub(x, a), sub(b, a)), FLOAT0)) ]); const fitNorm = (x, a, b) => div(sub(x, a), sub(b, a)); const fit = (x, a, b, c, d) => mix(c, d, fitNorm(x, a, b)); const fitClamped = (x, a, b, c, d) => mix(c, d, clamp01(div(sub(x, a), sub(b, a)))); const fit01 = (a, b, c) => mix(b, c, a); const fit1101 = (x) => add(mul(x, FLOAT05), FLOAT05); const fit0111 = (x) => sub(mul(x, FLOAT2), FLOAT1); export { fit, fit01, fit0111, fit1101, fitClamped, fitNorm, fitNorm1 };