UNPKG

@thi.ng/shader-ast-stdlib

Version:

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

20 lines (19 loc) 800 B
import { F, V3 } from "@thi.ng/shader-ast/api/types"; import { defn, ret } from "@thi.ng/shader-ast/ast/function"; import { gensym } from "@thi.ng/shader-ast/ast/idgen"; import { vec2, vec3 } from "@thi.ng/shader-ast/ast/lit"; import { add, 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 { normalize } from "@thi.ng/shader-ast/builtin/math"; const raymarchNormal = (scene, name = gensym("raymarchNormal_")) => defn(V3, name, [V3, F], (p, smooth) => { let dn; const comp = (id) => sub($x(scene(add(p, $(dn, id)))), $x(scene(sub(p, $(dn, id))))); return [ dn = sym(vec2(smooth, 0)), ret(normalize(vec3(comp("xyy"), comp("yxy"), comp("yyx")))) ]; }); export { raymarchNormal };