summaryrefslogtreecommitdiff
path: root/assets/final/shaders/render/scene_query_linear.wgsl
blob: b61a7e48b50b18ff6f23cdeda3a11d92ed5334d9 (plain)
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
#include "math/sdf_shapes"
#include "math/sdf_utils"

fn get_dist(p: vec3<f32>, obj_params: vec4<f32>) -> f32 {
    let obj_type = obj_params.x;
    if (obj_type == 1.0) { return length(p) - 1.0; } // Unit Sphere
    if (obj_type == 2.0) { return sdBox(p, vec3<f32>(1.0)); } // Unit Box
    if (obj_type == 3.0) { return sdTorus(p, vec2<f32>(1.0, 0.4)); } // Unit Torus
    if (obj_type == 4.0) { return sdPlane(p, vec3<f32>(0.0, 1.0, 0.0), 0.0); }
    if (obj_type == 5.0) { return sdBox(p, obj_params.yzw); } // MESH AABB
    return 100.0;
}

fn map_scene(p: vec3<f32>, skip_idx: u32) -> f32 {
    var d = 1000.0;
    let num_objects = arrayLength(&object_data.objects);
    for (var i = 0u; i < num_objects; i++) {
        if (i == skip_idx) { continue; }
        let obj = object_data.objects[i];
        let q = (obj.inv_model * vec4<f32>(p, 1.0)).xyz;
        let s = min(length(obj.model[0].xyz), min(length(obj.model[1].xyz), length(obj.model[2].xyz)));
        // IMPORTANT: Plane (type 4.0) and Mesh (type 5.0) should not be scaled by 's'.
        // The 's' factor is meant for unit primitives (sphere, box, torus) that are
        // scaled by the model matrix. Meshes already have correct local-space extents.
        if (obj.params.x != 4.0 && obj.params.x != 5.0) { // Not plane, not mesh
            d = min(d, get_dist(q, obj.params) * s);
        } else {
            d = min(d, get_dist(q, obj.params));
        }
    }
    return d;
}