summaryrefslogtreecommitdiff
path: root/assets/final/shaders/lighting.wgsl
blob: 277909bc857f4f37fb3e217a9c492883f62928a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fn get_normal_basic(p: vec3<f32>, obj_type: f32) -> vec3<f32> {
    if (obj_type == 1.0) { return normalize(p); }
    let e = vec2<f32>(0.001, 0.0);
    return normalize(vec3<f32>(
        get_dist(p + e.xyy, obj_type) - get_dist(p - e.xyy, obj_type),
        get_dist(p + e.yxy, obj_type) - get_dist(p - e.yxy, obj_type),
        get_dist(p + e.yyx, obj_type) - get_dist(p - e.yyx, obj_type)
    ));
}

fn calc_shadow(ro: vec3<f32>, rd: vec3<f32>, tmin: f32, tmax: f32, skip_idx: u32) -> f32 {
    var res = 1.0;
    var t = tmin;
    if (t < 0.05) { t = 0.05; } 
    for (var i = 0; i < 32; i = i + 1) {
        let h = map_scene(ro + rd * t, skip_idx);
        if (h < 0.001) { return 0.0; }
        res = min(res, 16.0 * h / t); 
        t = t + clamp(h, 0.02, 0.4);
        if (t > tmax) { break; }
    }
    return clamp(res, 0.0, 1.0);
}