Skip to content

Commit

Permalink
feat(shader-ast-stdlib): add additive() HOF
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 3, 2019
1 parent 0849f8b commit 43b2223
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/shader-ast-stdlib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from "./fog/linear";
export * from "./lighting/lambert";
export * from "./lighting/trilight";

export * from "./math/additive";
export * from "./math/clamp";
export * from "./math/cross2";
export * from "./math/dist-chebyshev";
Expand Down
68 changes: 68 additions & 0 deletions packages/shader-ast-stdlib/src/math/additive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
add,
assign,
defn,
float,
FLOAT0,
FLOAT05,
FloatSym,
forLoop,
Func1,
inc,
lt,
mul,
Prim,
ret,
sym
} from "@thi.ng/shader-ast";

/**
* Higher order function. Takes an AST type ID, a single-arg scalar
* function `fn`, number of octaves (default: 4) and an optional
* function name. Returns a new function which computes the summed value
* of `fn` over the given number octaves and accepts 3 args:
*
* - position (float)
* - octave shift (float)
* - octave decay (usually 0.5)
*
* For each octave `i` [0..oct), the function is (in principle)
* evaluated as:
*
* n += decay / exp2(i) * fn(pos * exp2(i) + i * shift)
*
* @param fn
* @param oct
* @param name
*/
export const additive = <T extends Prim>(
type: T,
fn: Func1<T, "float">,
oct = 4,
name = "additive"
) =>
defn("float", name, [[type], [type], ["float"]], (pos, shift, decay) => {
let n: FloatSym;
let amp: FloatSym;
return [
(n = sym(FLOAT0)),
(amp = sym(FLOAT05)),
forLoop(
sym("float", float(0)),
(i) => lt(i, float(oct)),
inc,
(i) => [
assign(
n,
add(
n,
mul(amp, fn(<any>add(<any>pos, mul(i, <any>shift))))
)
),
assign(amp, mul(amp, decay)),
assign(pos, <any>mul(<any>pos, 2))
]
),
ret(n)
];
});

0 comments on commit 43b2223

Please sign in to comment.