UNPKG

@thi.ng/shader-ast-stdlib

Version:

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

34 lines (33 loc) 836 B
import { float, FLOAT0, FLOAT05, FLOAT1, FLOAT2, TAU } from "@thi.ng/shader-ast/ast/lit"; import { madd, mul, sub } from "@thi.ng/shader-ast/ast/ops"; import { abs, fract, sin, step } from "@thi.ng/shader-ast/builtin/math"; const __defOsc = (fn) => (phase, freq, amp = FLOAT1, dc = FLOAT0) => madd(fn(mul(float(phase), float(freq))), amp, dc); const sinOsc = __defOsc((phase) => sin(mul(phase, TAU))); const sawOsc = __defOsc( (phase) => sub(FLOAT1, mul(fract(phase), FLOAT2)) ); const triOsc = __defOsc( (phase) => sub(abs(madd(fract(phase), 4, float(-2))), FLOAT1) ); const rectOsc = (phase, freq, amp = FLOAT1, dc = FLOAT0, duty = FLOAT05) => madd( madd( step(float(duty), fract(mul(float(phase), float(freq)))), FLOAT2, float(-1) ), amp, dc ); export { rectOsc, sawOsc, sinOsc, triOsc };