1+ /* Copyright (c) 2025, Sascha Willems
2+ *
3+ * SPDX-License-Identifier: MIT
4+ *
5+ */
6+
7+ struct VSInput
8+ {
9+ float3 Pos;
10+ float3 Normal;
11+ float2 UV;
12+ };
13+
14+ struct VSOutput
15+ {
16+ float4 Pos : SV_POSITION;
17+ float2 UV;
18+ float3 Normal;
19+ float3 ViewVec;
20+ float3 LightVec;
21+ };
22+
23+ struct UBO
24+ {
25+ float4x4 projection;
26+ float4x4 model;
27+ float4 viewPos;
28+ };
29+ ConstantBuffer < UBO> ubo;
30+
31+ [[vk::binding(0 , 1 )]] Sampler2D samplerColor;
32+
33+ [shader(" vertex" )]
34+ VSOutput vertexMain(VSInput input)
35+ {
36+ VSOutput output;
37+ output .UV = input .UV ;
38+
39+ float3 worldPos = mul(ubo .model , float4(input .Pos , 1 . 0 )).xyz ;
40+
41+ output .Pos = mul(ubo .projection , mul(ubo .model , float4(input .Pos .xyz , 1 . 0 )));
42+
43+ float4 pos = mul(ubo .model , float4(input .Pos , 1 . 0 ));
44+ output .Normal = mul((float3x3 )ubo .model , input .Normal );
45+ float3 lightPos = float3(0 . 0 , 0 . 0 , 0 . 0 );
46+ float3 lPos = mul((float3x3 )ubo .model , lightPos .xyz );
47+ output .LightVec = lPos - pos .xyz ;
48+ output .ViewVec = ubo .viewPos .xyz - pos .xyz ;
49+ return output;
50+ }
51+
52+ [shader(" fragment" )]
53+ float4 fragmentMain(VSOutput input)
54+ {
55+ float4 color = samplerColor .Sample (input .UV );
56+
57+ float3 N = normalize(input .Normal );
58+ float3 L = normalize(input .LightVec );
59+ float3 V = normalize(input .ViewVec );
60+ float3 R = reflect(- L, N);
61+ float3 diffuse = max(dot(N, L), 0 . 0 ) * float3(1 . 0 , 1 . 0 , 1 . 0 );
62+ float specular = pow(max(dot(R, V), 0 . 0 ), 16 . 0 ) * color .a ;
63+
64+ return float4(diffuse * color .rgb + specular, 1 . 0 );
65+ }
0 commit comments