aboutsummaryrefslogtreecommitdiff
path: root/src/shaders/shader.slang
blob: 5598e1e94078acd82cdfd63fd2986cee6a301c81 (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
struct VertexInput {
    float3 pos;
    float3 normal;
};

struct UniformBuffer {
    float4x4 model, view, proj;
    float4x4 camera_rot;
    float3 camera_loc;
};

ConstantBuffer<UniformBuffer> ubo;

struct VertexOutput {
    float4 pos : SV_Position;
    float3 world_loc;
    float3 normal;
};

[shader("vertex")]
VertexOutput vert_main(VertexInput vi) {
    VertexOutput vo;
    vo.pos = mul(ubo.proj, mul(ubo.view, mul(ubo.model, float4(vi.pos, 1.))));
    vo.world_loc = vi.pos;
    vo.normal = vi.normal;
    return vo;
}

Sampler2D texture;

[shader("fragment")]
float4 frag_main(VertexOutput vo) : SV_Target {
    float3 u = vo.world_loc - ubo.camera_loc;
    float u_len_sq = dot(u, u);
    u = normalize(u);
    float attenuation = dot(mul(ubo.camera_rot, float4(0., 0., -1., 0.)).xyz, u);
    if (attenuation < .7) attenuation = 0.f;
    else if (attenuation > .8) attenuation = 1.;
    else attenuation = (attenuation - .7) / .1;
    float light = -dot(vo.normal, u) / u_len_sq;
    if (light < 0.) light = 0.;
    float final_light = .05 + light * attenuation * .8 * .95;
    if (final_light > 1.) final_light = 1.;
    return float4(final_light, final_light, final_light, 1.);
}