-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shader-ast-stdlib): add SDF domain ops
- add sdfMirror2() - add sdfRepeatPolar()
- Loading branch information
1 parent
f14e8cb
commit c41b288
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
) | ||
), | ||
]; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))), | ||
]; | ||
} | ||
); |