Skip to content

Commit

Permalink
feat(shader-ast-stdlib): add SDF domain ops
Browse files Browse the repository at this point in the history
- add sdfMirror2()
- add sdfRepeatPolar()
  • Loading branch information
postspectacular committed Aug 12, 2021
1 parent f14e8cb commit c41b288
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/shader-ast-stdlib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ export * from "./sdf/box";
export * from "./sdf/cylinder";
export * from "./sdf/isec";
export * from "./sdf/line";
export * from "./sdf/mirror";
export * from "./sdf/plane";
export * from "./sdf/polyhedra";
export * from "./sdf/repeat";
export * from "./sdf/repeat-polar";
export * from "./sdf/round";
export * from "./sdf/smooth-isec";
export * from "./sdf/smooth-sub";
Expand Down
43 changes: 43 additions & 0 deletions packages/shader-ast-stdlib/src/sdf/mirror.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
add,
defn,
div,
FLOAT05,
floor,
mod,
mul,
ret,
sub,
sym,
Vec2Sym,
VEC2_1,
VEC2_2,
} from "@thi.ng/shader-ast";

/**
* @remarks
* Based on HG_SDF (Mercury Demogroup). Does not compute cell index.
*
* @param p - point
* @param size - mirror box size
*/
export const sdfMirror2 = defn(
"vec2",
"sdfMirror2",
["vec2", "vec2"],
(p, size) => {
let halfSize: Vec2Sym;
return [
(halfSize = sym(mul(size, FLOAT05))),
ret(
mul(
sub(mod(add(p, halfSize), size), halfSize),
sub(
mul(mod(floor(div(add(p, halfSize), size)), VEC2_2), 2),
VEC2_1
)
)
),
];
}
);
42 changes: 42 additions & 0 deletions packages/shader-ast-stdlib/src/sdf/repeat-polar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
$x,
$y,
add,
atan,
defn,
div,
FLOAT05,
FloatSym,
length,
mod,
mul,
ret,
sub,
sym,
TAU,
} from "@thi.ng/shader-ast";
import { cossin } from "../math/sincos";

/**
* @remarks
* Based on HG_SDF (Mercury Demogroup). Does not compute cell index.
*
* @param p - point
* @param n - number of polar repetitions
*/
export const sdfRepeatPolar2 = defn(
"vec2",
"sdfRepeatPolar2",
["vec2", "float"],
(p, n) => {
let angle: FloatSym;
let angle2: FloatSym;
let a: FloatSym;
return [
(angle = sym(div(TAU, n))),
(angle2 = sym(mul(angle, FLOAT05))),
(a = sym(sub(mod(add(angle2, atan($y(p), $x(p))), angle), angle2))),
ret(mul(cossin(a), length(p))),
];
}
);

0 comments on commit c41b288

Please sign in to comment.