summaryrefslogtreecommitdiff
path: root/assets/final/shaders/math/sdf_utils.wgsl
blob: c2e49cf52e063e77ce96bcf5f8d01ccdd11f0a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn get_normal_basic(p: vec3<f32>, obj_params: vec4<f32>) -> vec3<f32> {
    let obj_type = obj_params.x;
    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_params) - get_dist(p - e.xyy, obj_params),
        get_dist(p + e.yxy, obj_params) - get_dist(p - e.yxy, obj_params),
        get_dist(p + e.yyx, obj_params) - get_dist(p - e.yyx, obj_params)
    ));
}

// Distance to an Axis-Aligned Bounding Box
fn aabb_sdf(p: vec3<f32>, min_p: vec3<f32>, max_p: vec3<f32>) -> f32 {
    let center = (min_p + max_p) * 0.5;
    let extent = (max_p - min_p) * 0.5;
    let q = abs(p - center) - extent;
    return length(max(q, vec3<f32>(0.0))) + min(max(q.x, max(q.y, q.z)), 0.0);
}