@thi.ng/shader-ast-stdlib
Version:
Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast
47 lines (46 loc) • 1.4 kB
JavaScript
import { V2, V3 } from "@thi.ng/shader-ast/api/types";
import { assign } from "@thi.ng/shader-ast/ast/assign";
import { brk, forLoop, ifThen } 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 { INT0, float, int, vec2 } from "@thi.ng/shader-ast/ast/lit";
import { gt, inc, lt, madd } 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 { rayPointAt } from "./point-at.js";
const raymarchScene = (scene, _opts) => {
const opts = {
name: gensym("raymarchScene_"),
near: 0.1,
far: 10,
steps: 100,
eps: 0.01,
bias: 0.7,
..._opts
};
return defn(V2, opts.name, [V3, V3], (pos, dir) => {
let total;
let res;
return [
total = sym(float(opts.near)),
res = sym(V2),
forLoop(
sym(INT0),
(i) => lt(i, int(opts.steps)),
inc,
() => [
assign(res, scene(rayPointAt(pos, dir, total))),
ifThen(lt($x(res), float(opts.eps)), [
ret(vec2(total, $y(res)))
]),
assign(total, madd($x(res), float(opts.bias), total)),
ifThen(gt(total, float(opts.far)), [brk])
]
),
ret(vec2(opts.far, 0))
];
});
};
export {
raymarchScene
};