summaryrefslogtreecommitdiff
path: root/assets/final/shaders/math/sdf_utils.wgsl
blob: 502ba5b2960dcda99014c871a7b1749477c15fcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)
    ));
}

// 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);
}