summaryrefslogtreecommitdiffstats
path: root/assets/shaders/tacmap/body.wgsl
blob: 9e6419e89ed15acf0ce684181722b6d53a01b3cd (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
struct InstanceInput {
    @location(0) position: vec3<f32>,
    @location(1) origin: vec3<f32>,
    @location(2) radius: f32
};

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) local_position: vec3<f32>,
};

struct CameraUniform {
    view: mat4x4<f32>,
    proj: mat4x4<f32>,
    abs_pos: vec3<f32>,
    rel_pos: vec3<f32>,
    scale: f32,
};

@group(0) @binding(0)
var<uniform> camera: CameraUniform;

const QUAD_VERTICES = array<vec3<f32>,6>(
    vec3<f32>(-1.0, -1.0, 0.0),
    vec3<f32>(-1.0, 1.0 , 0.0),
    vec3<f32>(1.0,  1.0 , 0.0),
    vec3<f32>(1.0,  1.0 , 0.0),
    vec3<f32>(1.0,  -1.0, 0.0),
    vec3<f32>(-1.0, -1.0, 0.0),
);

@vertex
fn vs_main(
    @builtin(vertex_index) index: u32,
    instance: InstanceInput
) -> VertexOutput {
    var out: VertexOutput;

    let model = QUAD_VERTICES[index];
    var view = camera.view;

    out.local_position = model;

    let min_size = 0.025;
    let real_radius = max(min_size / 2.0, instance.radius * camera.scale);

    //Scale the world around the camera scale and translate about the camera's
    //absolute (/target) position
    let relative_pos = instance.position;
    let origin_pos = instance.origin - camera.abs_pos - camera.rel_pos;

    let view_proj = camera.proj * view;

    let origin_vp_pos = view_proj * vec4<f32>(origin_pos * camera.scale, 1.0);
    let offset_vp = view_proj * vec4<f32>(relative_pos * camera.scale, 0.0);
    let center_vp_pos = origin_vp_pos + offset_vp;

    let offset_ndc = offset_vp.xy / origin_vp_pos.w;

    if (length(offset_ndc) < 2.0 * min_size){
        if all(offset_vp != vec4<f32>(0.0)) {
            out.clip_position = vec4<f32>(0.0, 0.0, 0.0, 1.0);
            return out;
        }
    }

    let model_proj = camera.proj * vec4<f32>(model.xy, 0.0, 0.0);
    let vertex_vp_pos = center_vp_pos + vec4<f32>(model_proj.xy * instance.radius * camera.scale, 0.0, 0.0);

    let vertex_dist = length(vertex_vp_pos - center_vp_pos) / center_vp_pos.w;
    if vertex_dist < min_size {
        out.clip_position = center_vp_pos / center_vp_pos.w;
        out.clip_position += model_proj * (min_size / 2.0);
    }else{
        out.clip_position = vertex_vp_pos;
    }

    return out;
}

@fragment
fn fs_main(in: VertexOutput
) -> @location(0) vec4<f32> {
    let point_dist = length(in.local_position);
    var alpha = 1.0;
    if point_dist > 1.0 {
        alpha = 0.0;
    }
    let color = vec3<f32>(1.0);
    return vec4<f32>(color, alpha);
}