Skip to content

Commit b2272c5

Browse files
committed
Add slang shaders for gears sample
1 parent 7b88f68 commit b2272c5

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

shaders/slang/gears/gears.slang

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 EyePos;
20+
float3 LightVec;
21+
};
22+
23+
struct UBO
24+
{
25+
float4x4 projection;
26+
float4x4 view;
27+
float4 lightpos;
28+
float4x4 model[3];
29+
};
30+
ConstantBuffer<UBO> ubo;
31+
32+
[shader("vertex")]
33+
VSOutput vertexMain(VSInput input, uint InstanceIndex: SV_StartInstanceLocation)
34+
{
35+
VSOutput output;
36+
output.Normal = normalize(mul((float3x3)transpose(ubo.model[InstanceIndex]), input.Normal).xyz);
37+
output.Color = input.Color;
38+
float4x4 modelView = mul(ubo.view, ubo.model[InstanceIndex]);
39+
float4 pos = mul(modelView, float4(input.Pos, 1.0));
40+
output.EyePos = mul(modelView, pos).xyz;
41+
float4 lightPos = mul(float4(ubo.lightpos.xyz, 1.0), modelView);
42+
output.LightVec = normalize(lightPos.xyz - output.EyePos);
43+
output.Pos = mul(ubo.projection, pos);
44+
return output;
45+
}
46+
47+
[shader("fragment")]
48+
float4 fragmentMain(VSOutput input)
49+
{
50+
float3 eye = normalize(-input.EyePos);
51+
float3 reflected = normalize(reflect(-input.LightVec, input.Normal));
52+
53+
float4 ambient = float4(0.2, 0.2, 0.2, 1.0);
54+
float4 diffuse = float4(0.5, 0.5, 0.5, 0.5) * max(dot(input.Normal, input.LightVec), 0.0);
55+
float4 specular = float4(0.5, 0.5, 0.5, 1.0) * pow(max(dot(reflected, eye), 0.0), 0.8) * 0.25;
56+
57+
return float4((ambient + diffuse) * float4(input.Color, 1.0) + specular);
58+
}

0 commit comments

Comments
 (0)