Skip to content

Commit

Permalink
feat(shader-ast-stdlib): add snoise2, distance fns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 3, 2019
1 parent 7f7f1f6 commit 0849f8b
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/shader-ast-stdlib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export * from "./lighting/trilight";

export * from "./math/clamp";
export * from "./math/cross2";
export * from "./math/dist-chebyshev";
export * from "./math/dist-manhattan";
export * from "./math/fit";
export * from "./math/magsq";
export * from "./math/maxcomp";
export * from "./math/mincomp";
export * from "./math/orthogonal";
Expand All @@ -18,8 +21,10 @@ export * from "./matrix/lookat";
export * from "./matrix/mvp";
export * from "./matrix/normal";

export * from "./noise/hash3";
export * from "./noise/permute";
export * from "./noise/voronoise";
export * from "./noise/simplex2";
export * from "./noise/voronoi2";
export * from "./noise/worley2";

export * from "./raymarch/ao";
Expand Down
25 changes: 25 additions & 0 deletions packages/shader-ast-stdlib/src/math/dist-chebyshev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
$w,
$x,
$y,
$z,
abs,
max,
sub,
Vec2Term,
Vec3Term,
Vec4Term
} from "@thi.ng/shader-ast";

export const distChebyshev2 = (
a: Vec2Term | Vec3Term | Vec4Term,
b: Vec2Term | Vec3Term | Vec4Term
) => max(abs(sub($x(a), $x(b))), abs(sub($y(a), $y(b))));

export const distChebyshev3 = (
a: Vec3Term | Vec4Term,
b: Vec3Term | Vec4Term
) => max(distChebyshev2(a, b), abs(sub($z(a), $z(b))));

export const distChebyshev4 = (a: Vec4Term, b: Vec4Term) =>
max(distChebyshev3(a, b), abs(sub($w(a), $w(b))));
25 changes: 25 additions & 0 deletions packages/shader-ast-stdlib/src/math/dist-manhattan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
$w,
$x,
$y,
$z,
abs,
add,
sub,
Vec2Term,
Vec3Term,
Vec4Term
} from "@thi.ng/shader-ast";

export const distManhattan2 = (
a: Vec2Term | Vec3Term | Vec4Term,
b: Vec2Term | Vec3Term | Vec4Term
) => add(abs(sub($x(a), $x(b))), abs(sub($y(a), $y(b))));

export const distManhattan3 = (
a: Vec3Term | Vec4Term,
b: Vec3Term | Vec4Term
) => add(distManhattan2(a, b), abs(sub($z(a), $z(b))));

export const distManhattan4 = (a: Vec4Term, b: Vec4Term) =>
add(distManhattan3(a, b), abs(sub($w(a), $w(b))));
13 changes: 13 additions & 0 deletions packages/shader-ast-stdlib/src/math/magsq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defn, dot, ret } from "@thi.ng/shader-ast";

export const magSq2 = defn("float", "magSq2", [["vec2"]], (v) => [
ret(dot(v, v))
]);

export const magSq3 = defn("float", "magSq3", [["vec3"]], (v) => [
ret(dot(v, v))
]);

export const magSq4 = defn("float", "magSq4", [["vec4"]], (v) => [
ret(dot(v, v))
]);
106 changes: 106 additions & 0 deletions packages/shader-ast-stdlib/src/noise/simplex2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {
$,
$x,
$y,
abs,
add,
assign,
defn,
dot,
float,
FLOAT05,
FLOAT1,
floor,
fract,
gt,
max,
mod,
mul,
ret,
sub,
sym,
ternary,
vec2,
Vec2Sym,
vec3,
Vec3Sym,
vec4,
Vec4Sym
} from "@thi.ng/shader-ast";
import { magSq2 } from "../math/magsq";
import { permute3 } from "./permute";

/**
* Array and textureless GLSL 2D simplex noise function. Ported from
* original GLSL implementation by Ian McEwan & Ashima Arts.
*
* https://github.com/ashima/webgl-noise
*/
export const snoise2 = defn("float", "snoise2", [["vec2"]], (v) => {
let C: Vec4Sym;
let i: Vec2Sym;
let i1: Vec2Sym;
let x0: Vec2Sym;
let x12: Vec4Sym;
let p: Vec3Sym;
let m: Vec3Sym;
let x: Vec3Sym;
let h: Vec3Sym;
let ox: Vec3Sym;
let a0: Vec3Sym;
let g: Vec3Sym;
return [
(C = sym(
vec4(
0.211324865405187,
0.366025403784439,
-0.577350269189626,
0.024390243902439
)
)),
(i = sym(floor(add(v, dot(v, $(C, "yy")))))),
(x0 = sym(add(sub(v, i), dot(i, $(C, "xx"))))),
(i1 = sym(ternary(gt($x(x0), $y(x0)), vec2(1, 0), vec2(0, 1)))),
(x12 = sym(sub(add($(x0, "xyxy"), $(C, "xxzz")), vec4(i1, 0, 0)))),
assign(i, mod(i, float(289))),
(p = sym(
permute3(
add(
permute3(add(vec3(0, $y(i1), 1), $y(i))),
add(vec3(0, $x(i1), 1), $x(i))
)
)
)),
(m = sym(
max(
sub(
FLOAT05,
vec3(magSq2(x0), magSq2($(x12, "xy")), magSq2($(x12, "zw")))
),
vec3()
)
)),
assign(m, mul(m, m)),
assign(m, mul(m, m)),
(x = sym(sub(mul(2, fract(mul(p, $(C, "www")))), FLOAT1))),
(h = sym(sub(abs(x), FLOAT05))),
(ox = sym(floor(add(x, FLOAT05)))),
(a0 = sym(sub(x, ox))),
assign(
m,
mul(
m,
sub(
1.79284291400159,
mul(0.85373472095314, add(mul(a0, a0), mul(h, h)))
)
)
),
(g = sym(vec3(add(mul($x(a0), $x(x0)), mul($x(h), $y(x0)))))),
assign(
$(g, "yz"),
add(mul($(a0, "yz"), $(x12, "xz")), mul($(h, "yz"), $(x12, "yw")))
),
ret(mul(130, dot(m, g)))
];
});

0 comments on commit 0849f8b

Please sign in to comment.