-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathworley2x2x2.glsl
59 lines (51 loc) · 1.93 KB
/
worley2x2x2.glsl
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
// Permutation polynomial: (34x^2 + x) mod 289
vec4 permute(vec4 x) {
return mod((34.0 * x + 1.0) * x, 289.0);
}
vec3 permute(vec3 x) {
return mod((34.0 * x + 1.0) * x, 289.0);
}
vec4 dist(vec4 x, vec4 y, vec4 z, bool manhattanDistance) {
return manhattanDistance ? abs(x) + abs(y) + abs(z) : (x * x + y * y + z * z);
}
vec2 worley(vec3 P, float jitter, bool manhattanDistance) {
float K = 0.142857142857; // 1/7
float Ko = 0.428571428571; // 1/2-K/2
float K2 = 0.020408163265306; // 1/(7*7)
float Kz = 0.166666666667; // 1/6
float Kzo = 0.416666666667; // 1/2-1/6*2
vec3 Pi = mod(floor(P), 289.0);
vec3 Pf = fract(P);
vec4 Pfx = Pf.x + vec4(0.0, -1.0, 0.0, -1.0);
vec4 Pfy = Pf.y + vec4(0.0, 0.0, -1.0, -1.0);
vec4 p = permute(Pi.x + vec4(0.0, 1.0, 0.0, 1.0));
p = permute(p + Pi.y + vec4(0.0, 0.0, 1.0, 1.0));
vec4 p1 = permute(p + Pi.z); // z+0
vec4 p2 = permute(p + Pi.z + vec4(1.0)); // z+1
vec4 ox1 = fract(p1*K) - Ko;
vec4 oy1 = mod(floor(p1*K), 7.0)*K - Ko;
vec4 oz1 = floor(p1*K2)*Kz - Kzo; // p1 < 289 guaranteed
vec4 ox2 = fract(p2*K) - Ko;
vec4 oy2 = mod(floor(p2*K), 7.0)*K - Ko;
vec4 oz2 = floor(p2*K2)*Kz - Kzo;
vec4 dx1 = Pfx + jitter*ox1;
vec4 dy1 = Pfy + jitter*oy1;
vec4 dz1 = Pf.z + jitter*oz1;
vec4 dx2 = Pfx + jitter*ox2;
vec4 dy2 = Pfy + jitter*oy2;
vec4 dz2 = Pf.z - 1.0 + jitter*oz2;
vec4 d1 = dist(dx1, dy1, dz1, manhattanDistance);
vec4 d2 = dist(dx2, dy2, dz2, manhattanDistance);
// Do it right and sort out both F1 and F2
vec4 d = min(d1,d2); // F1 is now in d
d2 = max(d1,d2); // Make sure we keep all candidates for F2
d.xy = (d.x < d.y) ? d.xy : d.yx; // Swap smallest to d.x
d.xz = (d.x < d.z) ? d.xz : d.zx;
d.xw = (d.x < d.w) ? d.xw : d.wx; // F1 is now in d.x
d.yzw = min(d.yzw, d2.yzw); // F2 now not in d2.yzw
d.y = min(d.y, d.z); // nor in d.z
d.y = min(d.y, d.w); // nor in d.w
d.y = min(d.y, d2.x); // F2 is now in d.y
return sqrt(d.xy); // F1 and F2
}
#pragma glslify: export(worley)