-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdistance.wgsl
97 lines (76 loc) · 2.52 KB
/
distance.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
var<private> mouse_pos = vec2<f32>(0.);
var<private> mouse_wheel: f32 = 0.;
fn rotate(p: vec2<f32>, r: f32) -> vec2<f32> {
let c: f32 = cos(r);
let s: f32 = sin(r);
return mat2x2f(c, s, -s, c) * p;
}
// shaded distance
struct SD {
d : f32,
albedo: vec3f,
emissive: bool
};
fn sd_min(a: SD, b: SD) -> SD {
if (a.d <= b.d) {
return a;
} else {
return b;
}
}
fn sd_max(a: SD, b: SD) -> SD {
if (a.d >= b.d) {
return a;
}
else {
return b;
}
}
fn sd_neg(a: SD) -> SD {
return SD(-a.d, a.albedo, a.emissive);
}
fn shade(d: f32, albedo: vec3<f32>) -> SD{
return SD(d, albedo, false);
}
fn emitter(d : f32, light: vec3<f32>) -> SD {
return SD(d, light, true);
}
fn box(_p: vec2<f32>, r: vec2<f32>) -> f32 {
let p = abs(_p) - r;
return max(p.x, p.y);
}
fn sphere(p: vec2<f32>, r: f32) -> f32 {
return length(p) - r;
}
fn slitlight(p: vec2<f32>) -> SD {
const color: vec3<f32> = vec3(0.01, 0.5, 0.95);
var angle : f32 = max(0.0, cos(atan2(p.y, p.x)*6.0));
if (angle < 0.5) {
angle = 0.0;
}
let d2: SD = emitter(sphere(p, 0.1), color * angle * 20.0);
return d2;
}
fn map(p: vec2<f32>) -> SD {
const color1 = vec3<f32>(0.8);
const color3 = vec3<f32>(0.95, 0.5, 0.01);
const color4 = vec3<f32>(0.01, 1.0, 0.8);
//let c = vec4<f32>(textureSampleLevel(signed_distance, signed_distance_sampler, p, 0.)).r;
//let distance: vec2<f32> = textureLoad(signed_distance, p, 0).x;
//let d: SD = SD(distance.x, color1, false);
let d: SD = shade(sphere(p, 0.5), color1);
let d2: SD = slitlight(p - vec2(-0.8,-0.1));
let d3: SD = shade(box(p - vec2(-0.5,-0.2), vec2(0.52,0.05)),color1);
let d4: SD = shade(box(rotate(p, radians(90.0+30.5)) - vec2(0.0, 0.0), vec2(0.05,0.8)), color3);
let d5: SD = shade(box(p - vec2(0.0, 0.0), vec2(0.05,0.4)), color3);
let d7: SD = shade(box(p - vec2(0.20, 0.1), vec2(0.05,0.2)), color4);
let d6: SD = emitter(box(rotate(p - vec2(0.25, -0.2), radians(30.5)), vec2(0.01,0.04)), 20.0*vec3(1.0,0.9,0.8));
//let mouse_pos: vec2<f32> = vec2<f32>(0.0, 0.0);
let d8: SD = shade(sphere(p - mouse_pos, (-mouse_wheel*0.005+0.05)), vec3(1.0));
return sd_min(d8,sd_min(d6,sd_min(d3,sd_min(d2, sd_max(d,sd_neg(sd_min(d7,sd_min(d4,d5))))))));
}
fn normal_map(p: vec2<f32>, eps: f32) -> vec2<f32> {
return normalize(vec2<f32>(
(map(p + vec2(eps, 0.0)).d - map(p - vec2(eps, 0.0)).d),
(map(p + vec2(0.0, eps)).d - map(p - vec2(0.0, eps)).d)));
}