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
|
// Common functions for Signed Distance Field (SDF) raymarching.
//
// Required user-defined functions:
// - df(vec3f) -> f32
// Distance field for single-pass rendering (rayMarch, normal, shadow)
//
// For two-pass rendering with object IDs, see raymarching_id.wgsl
//
// Provided constants:
// TOLERANCE, MAX_RAY_LENGTH, MAX_RAY_MARCHES, NORM_OFF
const TOLERANCE: f32 = 0.0005;
const MAX_RAY_LENGTH: f32 = 20.0;
const MAX_RAY_MARCHES: i32 = 80;
const NORM_OFF: f32 = 0.005;
// Computes the surface normal of the distance field at a point `pos`.
fn normal(pos: vec3f) -> vec3f {
let eps = vec2f(NORM_OFF, 0.0);
var nor: vec3f;
nor.x = df(pos + eps.xyy) - df(pos - eps.xyy);
nor.y = df(pos + eps.yxy) - df(pos - eps.yxy);
nor.z = df(pos + eps.yyx) - df(pos - eps.yyx);
return normalize(nor);
}
// Performs the raymarching operation.
// Returns the distance along the ray to the surface, or MAX_RAY_LENGTH if no surface is hit.
fn rayMarch(ro: vec3f, rd: vec3f, tmin: f32) -> f32 {
var t = tmin;
for (var i = 0; i < MAX_RAY_MARCHES; i++) {
if (t > MAX_RAY_LENGTH) {
t = MAX_RAY_LENGTH;
break;
}
let d = df(ro + rd * t);
if (d < TOLERANCE) {
break;
}
t += d;
}
return t;
}
// Computes a soft shadow for a given point.
fn shadow(lp: vec3f, ld: vec3f, mint: f32, maxt: f32) -> f32 {
let ds = 1.0 - 0.4;
var t = mint;
var nd = 1e6;
let soff = 0.05;
let smul = 1.5;
let MAX_SHD_MARCHES = 20;
for (var i = 0; i < MAX_SHD_MARCHES; i++) {
let p = lp + ld * t;
let d = df(p);
if (d < TOLERANCE || t >= maxt) {
let sd = 1.0 - exp(-smul * max(t / maxt - soff, 0.0));
return select(mix(sd, 1.0, smoothstep(0.0, 0.025, nd)), sd, t >= maxt);
}
nd = min(nd, d);
t += ds * d;
}
let sd = 1.0 - exp(-smul * max(t / maxt - soff, 0.0));
return sd;
}
|