@thi.ng/shader-ast-stdlib
Version:
Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast
36 lines (35 loc) • 1.12 kB
JavaScript
import { F, S2D, V2, V3 } from "@thi.ng/shader-ast/api/types";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { vec2, vec3 } from "@thi.ng/shader-ast/ast/lit";
import { add, 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 { normalize } from "@thi.ng/shader-ast/builtin/math";
import { texture } from "@thi.ng/shader-ast/builtin/texture";
import { unpackFloat } from "../math/pack-float.js";
const __defNormal2 = (decode) => defn(V3, null, [S2D, V2, V2, F, F], (tex, uv, step, z, eps) => {
let sx;
let sy;
const sample1 = (tex2, uv2, unit) => sub(
decode(texture(tex2, sub(uv2, unit))),
decode(texture(tex2, add(uv2, unit)))
);
return [
sx = sym(vec2($x(step), 0)),
sy = sym(vec2(0, $y(step))),
ret(
normalize(
add(
vec3(sample1(tex, uv, sx), sample1(tex, uv, sy), z),
eps
)
)
)
];
});
const normal2 = __defNormal2($x);
const packedNormal2 = __defNormal2(unpackFloat);
export {
normal2,
packedNormal2
};