-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch06_01.wgsl
106 lines (81 loc) · 2.49 KB
/
ch06_01.wgsl
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
102
103
104
105
106
struct VertexOutput {
@builtin(position) pos: vec4<f32>,
};
struct GlobalsUniform {
@location(0) resolution: vec2<f32>,
@location(1) time: f32,
@location(2) channel: u32,
};
@group(0) @binding(0)
var<uniform> globals: GlobalsUniform;
let K = vec3<u32>(0x456789abu, 0x6789ab45u, 0x89ab4567u);
let U = vec3<u32>(1u, 2u, 3u);
let UINT_MAX = 0xffffffffu;
let PI = 3.1415926;
//********** Hash functions *********************************************//
fn uhash22(n_in: vec2<u32>) -> vec2<u32> {
var n = n_in;
n ^= (n.yx << U.xy);
n ^= (n.yx >> U.xy);
n *= K.xy;
n ^= (n.yx << U.xy);
return n * K.xy;
}
fn uhash33(n_in: vec3<u32>) -> vec3<u32> {
var n = n_in;
n ^= (n.yzx << U);
n ^= (n.yzx >> U);
n *= K;
n ^= (n.yzx << U);
return n * K;
}
fn hash22(p: vec2<f32>) -> vec2<f32> {
let n = bitcast<u32>(p);
return vec2<f32>(uhash22(n)) / vec2(f32(UINT_MAX));
}
fn hash33(p: vec3<f32>) -> vec3<f32> {
let n = bitcast<u32>(p);
return vec3<f32>(uhash33(n)) / vec3(f32(UINT_MAX));
}
fn hash21(p: vec2<f32>) -> f32 {
let n = bitcast<u32>(p);
return f32(uhash22(n).x) / f32(UINT_MAX);
}
fn hash31(p: vec3<f32>) -> f32 {
let n = bitcast<u32>(p);
return f32(uhash33(n).x) / f32(UINT_MAX);
}
fn mod2(x: f32, y: f32) -> f32 {
return x - y * floor(x / y);
}
//********** Cellular Noise *********************************************//
fn fdist(p: vec2<f32>) -> f32 {
let n = floor(p + 0.5); // nearest lattice point
var dist = sqrt(2.0);
for (var j = 0.0; j <= 2.0; j += 1.0) {
var grid: vec2<f32>; // near lattice point
grid.y = n.y + sign(j % 2.0 - 0.5) * ceil(j * 0.5);
if (abs(grid.y - p.y) - 0.5 > dist) {
continue;
}
for (var i = -1.0; i <= 1.0; i += 1.0) {
grid.x = n.x + i;
let jitter = hash22(grid) - 0.5;
dist = min(dist, length(grid + jitter - p));
}
}
return dist;
}
//********** Utilities *********************************************//
fn hsv2rgb(c: vec3<f32>) -> vec3<f32> {
// `saturate(e)` is a shortcut of `clamp(e, 0.0, 1.0)`.
let rgb = saturate(abs((c.x * 6.0 + vec3(0.0, 4.0, 2.0)) % 6.0 - 3.0) - 1.0);
return c.z * mix(vec3(1.0), rgb, c.y);
}
//********** Main *********************************************//
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
var pos = in.pos.xy / globals.resolution.xy;
pos = 10.0 * pos;
return vec4(vec3(fdist(pos)), 1.0);
}