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+ float3 Color;
12+ };
13+
14+ struct VSOutput
15+ {
16+ float4 Pos : SV_POSITION;
17+ float3 Normal;
18+ float3 Color;
19+ float3 ViewVec;
20+ float3 LightVec;
21+ };
22+
23+ struct UBO
24+ {
25+ float4x4 projection;
26+ float4x4 model;
27+ float4 lightPos;
28+ };
29+ ConstantBuffer < UBO> ubo;
30+
31+ [shader(" vertex" )]
32+ VSOutput vertexMain(VSInput input)
33+ {
34+ VSOutput output;
35+ output .Normal = input .Normal ;
36+ output .Color = input .Color ;
37+ output .Pos = mul(ubo .projection , mul(ubo .model , float4(input .Pos .xyz , 1 . 0 )));
38+
39+ float4 pos = mul(ubo .model , float4(input .Pos , 1 . 0 ));
40+ // Output the vertex position using debug printf
41+ printf(" Position = % v4f" , pos);
42+
43+ output .Normal = mul((float4x3 )ubo .model , input .Normal ).xyz ;
44+ float3 lPos = mul((float4x3 )ubo .model , ubo .lightPos .xyz ).xyz ;
45+ output .LightVec = lPos - pos .xyz ;
46+ output .ViewVec = - pos .xyz ;
47+ return output;
48+ }
49+
50+ [shader(" fragment" )]
51+ float4 fragmentMain(VSOutput input)
52+ {
53+ // Desaturate color
54+ float3 color = float3(lerp(input .Color , dot(float3(0 . 2126 , 0 . 7152 , 0 . 0722 ), input .Color ).xxx , 0 . 65 ));
55+
56+ // High ambient colors because mesh materials are pretty dark
57+ float3 ambient = color * float3(1 . 0 , 1 . 0 , 1 . 0 );
58+ float3 N = normalize(input .Normal );
59+ float3 L = normalize(input .LightVec );
60+ float3 V = normalize(input .ViewVec );
61+ float3 R = reflect(- L, N);
62+ float3 diffuse = max(dot(N, L), 0 . 0 ) * color;
63+ float3 specular = pow(max(dot(R, V), 0 . 0 ), 16 . 0 ) * float3(0 . 75 , 0 . 75 , 0 . 75 );
64+
65+ float intensity = dot(N, L);
66+ float shade = 1 . 0 ;
67+ shade = intensity < 0 . 5 ? 0 . 75 : shade;
68+ shade = intensity < 0 . 35 ? 0 . 6 : shade;
69+ shade = intensity < 0 . 25 ? 0 . 5 : shade;
70+ shade = intensity < 0 . 1 ? 0 . 25 : shade;
71+
72+ return float4(input .Color * 3 . 0 * shade, 1 );
73+ }
0 commit comments