Skip to content

Commit e0bff55

Browse files
committed
Add slang shaders for shader objects sample
1 parent 0a6c03b commit e0bff55

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
Sampler2D samplerColor;
32+
33+
[shader("vertex")]
34+
VSOutput vertexMain(VSInput input)
35+
{
36+
VSOutput output;
37+
output.Color = input.Color;
38+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
39+
40+
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
41+
output.Normal = mul((float4x3)ubo.model, input.Normal).xyz;
42+
float3 lPos = mul((float4x3)ubo.model, ubo.lightPos.xyz).xyz;
43+
output.LightVec = lPos.xyz - pos.xyz;
44+
output.ViewVec = -pos.xyz;
45+
46+
return output;
47+
}
48+
49+
[shader("fragment")]
50+
float4 fragmentMain(VSOutput input)
51+
{
52+
// Desaturate color
53+
float3 color = float3(lerp(input.Color, float3(dot(float3(0.2126, 0.7152, 0.0722), input.Color)), 0.65));
54+
55+
// High ambient colors because mesh materials are pretty dark
56+
float3 ambient = color;
57+
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), 32.0) * float3(0.35);
64+
return float4(ambient + diffuse * 1.75 + specular, 1.0);
65+
}

0 commit comments

Comments
 (0)