aboutsummaryrefslogtreecommitdiff
path: root/assets/shaders/grid.slang
blob: 13aa34913451b4558ec4c1adc3c0b24153a91a38 (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
33
34
35
36
37
38
39
// reference grid on the XZ plane. uniforms live in a ConstantBuffer (UBO) so both
// backends drive it through the same RHI path (bind a uniform buffer at binding 0).
struct Grid
{
    float4x4 u_view_projection;
    float4x4 u_transform;
    float    u_grid_size;
    float3   u_grid_color;
    float    u_grid_alpha;
    float3   u_camera_pos;
};
ConstantBuffer<Grid> gu;

struct VSInput  { float3 position : POSITION; };
struct VSOutput { float4 position : SV_Position; };

[shader("vertex")]
VSOutput vertexMain(VSInput input)
{
    VSOutput output;
    float3 scaledPosition = input.position * (gu.u_grid_size / 50.0);
    output.position = mul(gu.u_view_projection, mul(gu.u_transform, float4(scaledPosition, 1.0)));
    return output;
}

[shader("fragment")]
float4 fragmentMain(VSOutput input) : SV_Target
{
    float dist       = length(gu.u_camera_pos);
    float fadeFactor = 1.0 - clamp(dist / 100.0, 0.0, 0.8);

    float3 color = gu.u_grid_color;
    float  alpha = gu.u_grid_alpha * fadeFactor;

    float gridFade = 1.0 - clamp(dist / 50.0, 0.0, 0.9);
    alpha *= gridFade;

    return float4(color, alpha);
}