UNPKG

@thi.ng/shader-ast-stdlib

Version:

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

70 lines (69 loc) 1.79 kB
import { F, V2 } from "@thi.ng/shader-ast/api/types"; import { assign } from "@thi.ng/shader-ast/ast/assign"; import { forLoop, ifThen } from "@thi.ng/shader-ast/ast/controlflow"; import { defn, ret } from "@thi.ng/shader-ast/ast/function"; import { index } from "@thi.ng/shader-ast/ast/indexed"; import { FLOAT1, INT0, bvec3, int } from "@thi.ng/shader-ast/ast/lit"; import { div, gt, gte, inc, lt, mul, neg, not, or, sub } from "@thi.ng/shader-ast/ast/ops"; import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle"; import { sym } from "@thi.ng/shader-ast/ast/sym"; import { _any, all } from "@thi.ng/shader-ast/builtin/bvec"; import { dot, minSelf, sqrt } from "@thi.ng/shader-ast/builtin/math"; import { clamp01 } from "../math/clamp.js"; const sdfPolygon2 = (N) => defn( F, `sdfPolygon2_${N}`, [V2, ["vec2[]", "pts", { num: N }]], (p, pts) => { let d, s; let b, e, w, t; let pi, pj; let c; let j; return [ t = sym(sub(p, index(pts, 0))), d = sym(dot(t, t)), s = sym(FLOAT1), j = sym(int(N - 1)), forLoop( sym(INT0), (i) => lt(i, int(N)), inc, (i) => [ pi = sym(index(pts, i)), pj = sym(index(pts, j)), e = sym(sub(pj, pi)), w = sym(sub(p, pi)), b = sym( sub(w, mul(e, clamp01(div(dot(w, e), dot(e, e))))) ), minSelf(d, dot(b, b)), c = sym( bvec3( gte($y(p), $y(pi)), lt($y(p), $y(pj)), gt(mul($x(e), $y(w)), mul($y(e), $x(w))) ) ), ifThen(or(all(c), not(_any(c))), [assign(s, neg(s))]), assign(j, i) ] ), ret(mul(s, sqrt(d))) ]; } ); export { sdfPolygon2 };