UNPKG

@thi.ng/shader-ast-stdlib

Version:

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

23 lines (22 loc) 675 B
import { F, V2, V3 } from "@thi.ng/shader-ast/api/types"; import { defn, ret } from "@thi.ng/shader-ast/ast/function"; import { div, mul, sub } from "@thi.ng/shader-ast/ast/ops"; import { sym } from "@thi.ng/shader-ast/ast/sym"; import { dot, length } from "@thi.ng/shader-ast/builtin/math"; import { clamp01 } from "../math/clamp.js"; const $ = (n, type) => defn(F, `sdLine${n}`, [type, type, type], (p, a, b) => { let pa, ba; return [ pa = sym(sub(p, a)), ba = sym(sub(b, a)), ret( length(sub(pa, mul(ba, clamp01(div(dot(pa, ba), dot(ba, ba)))))) ) ]; }); const sdfLine2 = $(2, V2); const sdfLine3 = $(3, V3); export { sdfLine2, sdfLine3 };