summaryrefslogtreecommitdiffstats
path: root/assets/shaders/tacmap/body.wgsl
blob: 217a0b26b55b58905dcdf0c553444b0dcd641171 (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
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>,
    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;

    //Billboard the circle
    let camera_right = vec3<f32>(view[0][0], view[1][0], view[2][0]);
    let camera_up = vec3<f32>(view[0][1], view[1][1], view[2][1]);
    let model_pos = camera_right * model.x +
                    camera_up * model.y;

    let min_size = 0.025;

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

    if all(relative_pos != origin_pos) {
        if length(relative_pos) < 0.01 {
            out.clip_position = vec4<f32>(0.0, 0.0, 0.0, 1.0);
            return out;
        }
    }

    let instance_pos = relative_pos + origin_pos;

    let view_proj = camera.proj * view;

    let center_view_pos = view_proj * vec4<f32>(instance_pos, 1.0);
    let vertex_view_pos = view_proj * vec4<f32>(instance_pos + (model_pos * instance.radius * camera.scale), 1.0);

    let vertex_dist = length(vertex_view_pos - center_view_pos) / center_view_pos.w;
    if vertex_dist < min_size {
        out.clip_position = center_view_pos / center_view_pos.w;
        out.clip_position += camera.proj * vec4<f32>(model.xy * (min_size / 2.0), 0.0, 0.0);
    }else{
        out.clip_position = vertex_view_pos;
    }

    out.local_position = model;

    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;
    }
    return vec4<f32>(1.0, 1.0, 1.0, alpha);
}