summaryrefslogtreecommitdiffstats
path: root/assets/shaders/tacmap/orbit.wgsl
blob: 72763453692a209c39e7f1db96b27d6568bff1dd (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
struct VertexInput {
    @builtin(vertex_index) index: u32,
    @location(0) origin: u32,
    @location(1) position: vec3<f32>
};

struct VertexOutput {
    @builtin(position) clip_position: vec4<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;

@group(1) @binding(0)
var<storage> origins: array<array<f32,3>>;

const POINT_NORMALS = array<f32,2>(
    -0.5, 0.5
);

@vertex
fn vs_main(
    model: VertexInput,
) -> VertexOutput {
    var out: VertexOutput;

    let index = model.index % 2u;
    let origin = vec3<f32>(
        origins[model.origin][0],
        origins[model.origin][1],
        origins[model.origin][2]);

    let offset_pos = model.position;
    let origin_pos = origin - camera.abs_pos - camera.rel_pos;

    let view_proj = camera.proj * camera.view;

    let origin_view_pos = camera.view * vec4<f32>(origin_pos * camera.scale, 1.0);
    let offset_view = camera.view * vec4<f32>(offset_pos * camera.scale, 0.0);
    let point_view_pos = origin_view_pos + offset_view;

    let orbit_normal = normalize(model.position);
    var normal = orbit_normal;

    let offset_ndc = offset_view.xy / origin_view_pos.z;

    let discard_radius = 0.05;

    if length(offset_ndc) > discard_radius {
        normal *= POINT_NORMALS[index];
    }

    let point_vp_pos = camera.proj * point_view_pos;

    //Scale the world around the camera scale and translate about the camera's
    //absolute (/target) position
    let segment_vp_pos = point_vp_pos + (view_proj * vec4<f32>(normal * 0.005, 0.0));
    out.clip_position = segment_vp_pos;

    return out;
}

@fragment
fn fs_main(in: VertexOutput
) -> @location(0) vec4<f32> {
    return vec4<f32>(0.0, 0.5, 0.0, 1.0);
}