Skip to content

Commit

Permalink
feat(shader-ast-stdlib): variadic sdf isec/sub/union
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 9, 2021
1 parent 9d52838 commit fbff935
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
10 changes: 6 additions & 4 deletions packages/shader-ast-stdlib/src/sdf/isec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { FloatTerm, max } from "@thi.ng/shader-ast";

/**
* Inline function. SDF shape intersection (a & b).
* Inline function. Variadic SDF shape intersection (a & b) for any number of
* terms (at least 1 required).
*
* @param a - float
* @param b - float
* @param a -
* @param terms -
*/
export const sdfIntersect = (a: FloatTerm, b: FloatTerm) => max(b, a);
export const sdfIntersect = (a: FloatTerm, ...terms: FloatTerm[]) =>
terms.reduce((a, b) => max(a, b), a);
10 changes: 6 additions & 4 deletions packages/shader-ast-stdlib/src/sdf/sub.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { FloatTerm, max, neg } from "@thi.ng/shader-ast";

/**
* Inline function. SDF shape subtraction (a - b).
* Inline function. Variadic SDF shape subtraction (a - b) for any number of
* terms (at least 1 required).
*
* @param a - float
* @param b - float
* @param a -
* @param terms -
*/
export const sdfSubtract = (a: FloatTerm, b: FloatTerm) => max(neg(b), a);
export const sdfSubtract = (a: FloatTerm, ...terms: FloatTerm[]) =>
terms.reduce((a, b) => max(a, neg(b)), a);
10 changes: 6 additions & 4 deletions packages/shader-ast-stdlib/src/sdf/union.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { $x, defn, FloatTerm, lt, min, ret, ternary } from "@thi.ng/shader-ast";

/**
* Inline function. SDF shape union (a || b).
* Inline function. Variadic SDF shape union (a || b) for any number of terms
* (at least 1 required).
*
* @param a - float
* @param b - float
* @param a -
* @param terms -
*/
export const sdfUnion = (a: FloatTerm, b: FloatTerm) => min(a, b);
export const sdfUnion = (a: FloatTerm, ...terms: FloatTerm[]) =>
terms.reduce((a, b) => min(a, b), a);

/**
* SDF shape union for vec2 terms, i.e. the common form where the X coord
Expand Down

0 comments on commit fbff935

Please sign in to comment.