From 0849f8b07fccb5be0cbf8b30b5206de3c6237b34 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Wed, 3 Jul 2019 14:18:35 +0100 Subject: [PATCH] feat(shader-ast-stdlib): add snoise2, distance fns --- packages/shader-ast-stdlib/src/index.ts | 7 +- .../src/math/dist-chebyshev.ts | 25 +++++ .../src/math/dist-manhattan.ts | 25 +++++ packages/shader-ast-stdlib/src/math/magsq.ts | 13 +++ .../shader-ast-stdlib/src/noise/simplex2.ts | 106 ++++++++++++++++++ .../src/noise/{voronoise.ts => voronoi2.ts} | 0 6 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 packages/shader-ast-stdlib/src/math/dist-chebyshev.ts create mode 100644 packages/shader-ast-stdlib/src/math/dist-manhattan.ts create mode 100644 packages/shader-ast-stdlib/src/math/magsq.ts create mode 100644 packages/shader-ast-stdlib/src/noise/simplex2.ts rename packages/shader-ast-stdlib/src/noise/{voronoise.ts => voronoi2.ts} (100%) diff --git a/packages/shader-ast-stdlib/src/index.ts b/packages/shader-ast-stdlib/src/index.ts index 8aec8dde64..90b964dbcb 100644 --- a/packages/shader-ast-stdlib/src/index.ts +++ b/packages/shader-ast-stdlib/src/index.ts @@ -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"; @@ -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"; diff --git a/packages/shader-ast-stdlib/src/math/dist-chebyshev.ts b/packages/shader-ast-stdlib/src/math/dist-chebyshev.ts new file mode 100644 index 0000000000..8c5e506998 --- /dev/null +++ b/packages/shader-ast-stdlib/src/math/dist-chebyshev.ts @@ -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)))); diff --git a/packages/shader-ast-stdlib/src/math/dist-manhattan.ts b/packages/shader-ast-stdlib/src/math/dist-manhattan.ts new file mode 100644 index 0000000000..617d01c2dc --- /dev/null +++ b/packages/shader-ast-stdlib/src/math/dist-manhattan.ts @@ -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)))); diff --git a/packages/shader-ast-stdlib/src/math/magsq.ts b/packages/shader-ast-stdlib/src/math/magsq.ts new file mode 100644 index 0000000000..a99dffc7f8 --- /dev/null +++ b/packages/shader-ast-stdlib/src/math/magsq.ts @@ -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)) +]); diff --git a/packages/shader-ast-stdlib/src/noise/simplex2.ts b/packages/shader-ast-stdlib/src/noise/simplex2.ts new file mode 100644 index 0000000000..a6c1bc66de --- /dev/null +++ b/packages/shader-ast-stdlib/src/noise/simplex2.ts @@ -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))) + ]; +}); diff --git a/packages/shader-ast-stdlib/src/noise/voronoise.ts b/packages/shader-ast-stdlib/src/noise/voronoi2.ts similarity index 100% rename from packages/shader-ast-stdlib/src/noise/voronoise.ts rename to packages/shader-ast-stdlib/src/noise/voronoi2.ts