UNPKG

@thi.ng/shader-ast-stdlib

Version:

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

41 lines (40 loc) 1.07 kB
import { F, V3 } from "@thi.ng/shader-ast/api/types"; import { forLoop } from "@thi.ng/shader-ast/ast/controlflow"; import { defn, ret } from "@thi.ng/shader-ast/ast/function"; import { gensym } from "@thi.ng/shader-ast/ast/idgen"; import { FLOAT0, FLOAT05, FLOAT1, float } from "@thi.ng/shader-ast/ast/lit"; import { add, addSelf, inc, lte, mul, mulSelf, sub } from "@thi.ng/shader-ast/ast/ops"; import { $x } from "@thi.ng/shader-ast/ast/swizzle"; import { sym } from "@thi.ng/shader-ast/ast/sym"; import { clamp01 } from "../math/clamp.js"; const raymarchAO = (scene, numSamples = 5, name = gensym("raymarchAO_")) => defn(F, name, [V3, V3], (p, n) => { let r; let w; let d0; return [ r = sym(FLOAT0), w = sym(FLOAT1), forLoop( sym(float(1)), (i) => lte(i, float(numSamples)), inc, (i) => [ d0 = sym(mul(i, 1 / numSamples)), addSelf(r, mul(w, sub(d0, $x(scene(add(p, mul(n, d0))))))), mulSelf(w, FLOAT05) ] ), ret(sub(FLOAT1, clamp01(r))) ]; }); export { raymarchAO };