Skip to content

Commit

Permalink
feat(shader-ast-stdlib): add more hash fns, update voronoise2
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 6, 2019
1 parent 2fea507 commit 65b2a15
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 5 deletions.
231 changes: 228 additions & 3 deletions packages/shader-ast-stdlib/src/noise/hash.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
import {
$,
$x,
$y,
$z,
add,
assign,
defn,
dot,
fract,
mat2,
mul,
ret,
sin,
sym,
vec2,
vec3
vec3,
vec4
} from "@thi.ng/shader-ast";
import { FloatSym, Vec3Sym, Vec4Sym } from "@thi.ng/shader-ast/api";

/**
* IQ's hash PRNG producing 2D results.
* iq's hash PRNG producing 2D results.
*
* @param p
*/
export const hash2 = defn("vec2", "hash2", [["vec2"]], (p) => [
ret(fract(mul(sin(mul(p, mat2(127.1, 311.7, 269.5, 183.3))), 43758.5453)))
]);

/**
* IQ's hash PRNG producing 3D results.
* iq's hash PRNG producing 3D results.
*
* @param p
*/
export const hash3 = defn("vec3", "hash3", [["vec2"]], (p) => [
ret(
Expand All @@ -36,3 +49,215 @@ export const hash3 = defn("vec3", "hash3", [["vec2"]], (p) => [
)
)
]);

const H = vec3(0.1031, 0.103, 0.0973);
const H4 = vec4(0.1031, 0.103, 0.0973, 0.1099);

/**
* 1D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash11 = defn("float", "hash11", [["float"]], (p) => {
let x: FloatSym;
return [
(x = sym(fract(mul(p, 0.1031)))),
assign(x, mul(x, add(x, 19.19))),
assign(x, mul(x, add(x, x))),
ret(fract(x))
];
});

/**
* 2D -> 1D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash12 = defn("float", "hash12", [["vec2"]], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul($(p, "xyx"), 0.1031)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
ret(fract(mul(add($x(x), $y(x)), $z(x))))
];
});

/**
* 3D -> 1D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash13 = defn("float", "hash13", [["vec3"]], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul($(p, "xyx"), 0.1031)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
ret(fract(mul(add($x(x), $y(x)), $z(x))))
];
});

/**
* 1D -> 2D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash21 = defn("vec2", "hash21", [["float"]], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul(vec3(p), H)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
ret(fract(mul(add($(x, "xx"), $(x, "yz")), $(x, "zy"))))
];
});

/**
* 2D -> 2D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash22 = defn("vec2", "hash22", [["vec2"]], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul($(p, "xyx"), H)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
ret(fract(mul(add($(x, "xx"), $(x, "yz")), $(x, "zy"))))
];
});

/**
* 3D -> 2D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash23 = defn("vec2", "hash23", [["vec3"]], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul(p, H)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
ret(fract(mul(add($(x, "xx"), $(x, "yz")), $(x, "zy"))))
];
});

/**
* 1D -> 3D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash31 = defn("vec3", "hash31", [["float"]], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul(p, H)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
ret(fract(mul(add($(x, "xxy"), $(x, "yzz")), $(x, "zyx"))))
];
});

/**
* 2D -> 3D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash32 = defn("vec3", "hash32", [["vec2"]], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul($(p, "xyx"), H)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
ret(fract(mul(add($(x, "xxy"), $(x, "yzz")), $(x, "zyx"))))
];
});

/**
* 3D -> 3D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash33 = defn("vec3", "hash33", [["vec3"]], (p) => {
let x: Vec3Sym;
return [
(x = sym(fract(mul(p, H)))),
assign(x, add(x, dot(x, add($(x, "yzx"), 19.19)))),
ret(fract(mul(add($(x, "xxy"), $(x, "yzz")), $(x, "zyx"))))
];
});

/**
* 1D -> 4D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash41 = defn("vec4", "hash41", [["float"]], (p) => {
let x: Vec4Sym;
return [
(x = sym(fract(mul(p, H4)))),
assign(x, add(x, dot(x, add($(x, "wzxy"), 19.19)))),
ret(fract(mul(add($(x, "xxyz"), $(x, "yzzw")), $(x, "zywx"))))
];
});

/**
* 2D -> 4D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash42 = defn("vec4", "hash42", [["vec2"]], (p) => {
let x: Vec4Sym;
return [
(x = sym(fract(mul($(p, "xyxy"), H4)))),
assign(x, add(x, dot(x, add($(x, "wzxy"), 19.19)))),
ret(fract(mul(add($(x, "xxyz"), $(x, "yzzw")), $(x, "zywx"))))
];
});

/**
* 3D -> 4D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash43 = defn("vec4", "hash43", [["vec3"]], (p) => {
let x: Vec4Sym;
return [
(x = sym(fract(mul($(p, "xyzx"), H4)))),
assign(x, add(x, dot(x, add($(x, "wzxy"), 19.19)))),
ret(fract(mul(add($(x, "xxyz"), $(x, "yzzw")), $(x, "zywx"))))
];
});

/**
* 4D -> 4D hash.
*
* Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
*
* @param p
*/
export const hash44 = defn("vec4", "hash44", [["vec4"]], (p) => {
let x: Vec4Sym;
return [
(x = sym(fract(mul(p, H4)))),
assign(x, add(x, dot(x, add($(x, "wzxy"), 19.19)))),
ret(fract(mul(add($(x, "xxyz"), $(x, "yzzw")), $(x, "zywx"))))
];
});
7 changes: 5 additions & 2 deletions packages/shader-ast-stdlib/src/noise/voronoi2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
vec3,
Vec3Sym
} from "@thi.ng/shader-ast";
import { hash3 } from "./hash";
import { hash32 } from "./hash";

/**
* IQ's parametric 2D voronoise. Depending on `u` and `v`, this function
Expand All @@ -42,6 +42,9 @@ import { hash3 } from "./hash";
*
* http://www.iquilezles.org/www/articles/voronoise/voronoise.htm
*
* Note: This implementation uses the improved `hash32` by Dave Hoskins
* instead of iq's original `hash3`.
*
* @param p
* @param u
* @param v
Expand Down Expand Up @@ -71,7 +74,7 @@ export const voronoise2 = defn(
forLoop(sym(int(-2)), (i) => lte(i, int(2)), inc, (i) => [
forLoop(sym(int(-2)), (j) => lte(j, int(2)), inc, (j) => [
(g = sym(vec2(float(i), float(j)))),
(o = sym(mul(hash3(add(p, g)), coeff))),
(o = sym(mul(hash32(add(p, g)), coeff))),
(r = sym(add(sub(g, f), $xy(o)))),
(w = sym(
pow(
Expand Down

0 comments on commit 65b2a15

Please sign in to comment.