-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathvoronoi2.ts
101 lines (100 loc) · 2.1 KB
/
voronoi2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// SPDX-License-Identifier: Apache-2.0
import type { FloatSym, Vec2Sym, Vec3Sym } from "@thi.ng/shader-ast";
import { F, V2 } from "@thi.ng/shader-ast/api/types";
import { forLoop } from "@thi.ng/shader-ast/ast/controlflow";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import {
FLOAT0,
SQRT2,
float,
int,
vec2,
vec3,
} from "@thi.ng/shader-ast/ast/lit";
import {
add,
addSelf,
div,
inc,
lte,
mul,
sub,
} from "@thi.ng/shader-ast/ast/ops";
import { $xy, $z } from "@thi.ng/shader-ast/ast/swizzle";
import { sym } from "@thi.ng/shader-ast/ast/sym";
import {
dot,
floor,
fract,
pow,
smoothstep,
sqrt,
} from "@thi.ng/shader-ast/builtin/math";
import { hash32 } from "./hash.js";
/**
* IQ's parametric 2D voronoise. Depending on `u` and `v`, this function
* produces 4 different noise types
*
* - cell noise `(0,0)`
* - voronoi `(1,0)`
* - perlin noise `(0,1)`
* - voronoise `(1,1)`
*
* http://www.iquilezles.org/www/articles/voronoise/voronoise.htm
*
* Note: This implementation uses the improved {@link hash32} by Dave Hoskins
* instead of iq's original {@link hash3}.
*
* @param p -
* @param u -
* @param v -
*/
export const voronoise2 = defn(F, "voronoise2", [V2, F, F], (x, u, v) => {
let p: Vec2Sym;
let f: Vec2Sym;
let coeff: Vec3Sym;
let k: FloatSym;
let va: FloatSym;
let wt: FloatSym;
let g: Vec2Sym;
let o: Vec3Sym;
let r: Vec2Sym;
let w: FloatSym;
return [
(p = sym(floor(x))),
(f = sym(fract(x))),
(coeff = sym(vec3(u, u, 1))),
(k = sym(add(1, mul(63, pow(sub(1, v), float(4)))))),
(va = sym(FLOAT0)),
(wt = sym(FLOAT0)),
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(hash32(add(p, g)), coeff))),
(r = sym(add(sub(g, f), $xy(o)))),
(w = sym(
pow(
sub(
1,
smoothstep(FLOAT0, SQRT2, sqrt(dot(r, r)))
),
k
)
)),
addSelf(va, mul(w, $z(o))),
addSelf(wt, w),
]
),
]
),
ret(div(va, wt)),
];
});