-
-
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 2d worley noise & permutations
- Loading branch information
1 parent
283d008
commit a645c71
Showing
3 changed files
with
138 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,25 @@ | ||
import { | ||
add, | ||
defn, | ||
float, | ||
FLOAT1, | ||
mod, | ||
mul, | ||
ret | ||
} from "@thi.ng/shader-ast"; | ||
|
||
export const permute = defn("float", "permute", [["float"]], (v) => [ | ||
ret(mod(mul(v, add(mul(v, float(34)), FLOAT1)), float(289))) | ||
]); | ||
|
||
export const permute2 = defn("vec2", "permute2", [["vec2"]], (v) => [ | ||
ret(mod(mul(v, add(mul(v, float(34)), FLOAT1)), float(289))) | ||
]); | ||
|
||
export const permute3 = defn("vec3", "permute3", [["vec3"]], (v) => [ | ||
ret(mod(mul(v, add(mul(v, float(34)), FLOAT1)), float(289))) | ||
]); | ||
|
||
export const permute4 = defn("vec4", "permute4", [["vec4"]], (v) => [ | ||
ret(mod(mul(v, add(mul(v, float(34)), FLOAT1)), float(289))) | ||
]); |
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,110 @@ | ||
import { | ||
$, | ||
$x, | ||
$y, | ||
$z, | ||
abs, | ||
add, | ||
assign, | ||
defn, | ||
float, | ||
floor, | ||
fract, | ||
Func2, | ||
lt, | ||
max, | ||
min, | ||
mod, | ||
mul, | ||
ret, | ||
sqrt, | ||
sub, | ||
sym, | ||
ternary, | ||
Vec2Sym, | ||
vec3, | ||
Vec3Sym | ||
} from "@thi.ng/shader-ast"; | ||
import { permute3 } from "./permute"; | ||
|
||
export const worleyDist = defn( | ||
"vec3", | ||
"worleyDist", | ||
[["vec3"], ["vec3"]], | ||
(a, b) => [ret(add(mul(a, a), mul(b, b)))] | ||
); | ||
|
||
export const worleyDistManhatten = defn( | ||
"vec3", | ||
"worleyDistManhatten", | ||
[["vec3"], ["vec3"]], | ||
(a, b) => [ret(add(abs(a), abs(b)))] | ||
); | ||
|
||
/** | ||
* Higher order function. Computes 2D Worley noise using provided | ||
* distance function. The returned function takes 2 args: position and | ||
* jitter amount, the latter in [0..1] interval. Returns noise | ||
* components as vec2, with the x component containing the distance from | ||
* closest simplex center and y the noise value. The vector components | ||
* can be used individually or combined (e.g. `noise.y - noise.x`)... | ||
* | ||
* Based on implementation by Stefan Gustavson: | ||
* http://webstaff.itn.liu.se/~stegu/GLSL-cellular/GLSL-cellular-notes.pdf | ||
* | ||
* @param distFn | ||
*/ | ||
export const worley2 = (distFn: Func2<"vec3", "vec3", "vec3">) => | ||
defn("vec2", "worley2", [["vec2"], ["float"]], (P, jitter) => { | ||
const K = float(1 / 7); | ||
const Ko = float(3 / 7); | ||
const oI = sym(vec3(-1, 0, 1)); | ||
const oF = sym(vec3(-0.5, 0.5, 1.5)); | ||
let pI: Vec2Sym; | ||
let pF: Vec2Sym; | ||
let px: Vec3Sym; | ||
let p: Vec3Sym; | ||
let d1: Vec3Sym; | ||
let d2: Vec3Sym; | ||
let d3: Vec3Sym; | ||
let d1a: Vec3Sym; | ||
const $dist = (x: number) => | ||
distFn( | ||
add(add($x(pF), x), mul(sub(fract(mul(p, K)), Ko), jitter)), | ||
add( | ||
sub($y(pF), oF), | ||
mul( | ||
sub(mul(mod(floor(mul(p, K)), float(7)), K), Ko), | ||
jitter | ||
) | ||
) | ||
); | ||
return [ | ||
oI, | ||
oF, | ||
(pI = sym(mod(floor(P), float(289)))), | ||
(pF = sym(fract(P))), | ||
(px = sym(permute3(add(oI, $x(pI))))), | ||
(p = sym(permute3(add(add(oI, $y(pI)), $x(px))))), | ||
(d1 = sym($dist(0.5))), | ||
assign(p, permute3(add(oI, add($y(pI), $y(px))))), | ||
(d2 = sym($dist(-0.5))), | ||
assign(p, permute3(add(oI, add($y(pI), $z(px))))), | ||
(d3 = sym($dist(-1.5))), | ||
(d1a = sym(min(d1, d2))), | ||
assign(d2, min(max(d1, d2), d3)), | ||
assign(d1, min(d1a, d2)), | ||
assign(d2, max(d1a, d2)), | ||
assign( | ||
$(d1, "xy"), | ||
ternary(lt($x(d1), $y(d1)), $(d1, "xy"), $(d1, "yx")) | ||
), | ||
assign( | ||
$(d1, "xz"), | ||
ternary(lt($x(d1), $z(d1)), $(d1, "xz"), $(d1, "zx")) | ||
), | ||
assign($(d1, "yz"), min($(d1, "yz"), $(d2, "yz"))), | ||
assign($y(d1), min(min($y(d1), $z(d1)), $x(d2))), | ||
ret(sqrt($(d1, "xy"))) | ||
]; | ||
}); |