Skip to content

Commit afbc3a5

Browse files
committed
Add slang shader for vertex attributes sample
1 parent 52c97db commit afbc3a5

2 files changed

Lines changed: 86 additions & 4 deletions

File tree

shaders/slang/gltfscenerendering/scene.slang

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ struct UBO
3333
};
3434
ConstantBuffer<UBO> ubo;
3535

36-
struct PushConsts {
37-
float4x4 model;
38-
};
39-
4036
[[vk::binding(0, 1)]] Sampler2D samplerColorMap;
4137
[[vk::binding(1, 1)]] Sampler2D samplerNormalMap;
4238

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
/* Copyright (c) 2025, Sascha Willems
8+
*
9+
* SPDX-License-Identifier: MIT
10+
*
11+
*/
12+
13+
struct VSInput
14+
{
15+
float3 Pos;
16+
float3 Normal;
17+
float2 UV;
18+
float4 Tangent;
19+
};
20+
21+
struct VSOutput
22+
{
23+
float4 Pos : SV_POSITION;
24+
float3 Normal;
25+
float2 UV;
26+
float3 ViewVec;
27+
float3 LightVec;
28+
float4 Tangent;
29+
};
30+
31+
struct UBO
32+
{
33+
float4x4 projection;
34+
float4x4 view;
35+
float4 lightPos;
36+
float4 viewPos;
37+
};
38+
ConstantBuffer<UBO> ubo;
39+
40+
[[vk::binding(0, 1)]] Sampler2D samplerColorMap;
41+
[[vk::binding(1, 1)]] Sampler2D samplerNormalMap;
42+
43+
[shader("vertex")]
44+
VSOutput vertexMain(VSInput input, uniform float4x4 modelMat)
45+
{
46+
VSOutput output;
47+
output.Normal = input.Normal;
48+
output.UV = input.UV;
49+
output.Tangent = input.Tangent;
50+
51+
float4x4 modelView = mul(ubo.view, modelMat);
52+
53+
output.Pos = mul(ubo.projection, mul(modelView, float4(input.Pos.xyz, 1.0)));
54+
55+
output.Normal = mul((float3x3)modelMat, input.Normal);
56+
float4 pos = mul(modelMat, float4(input.Pos, 1.0));
57+
output.LightVec = ubo.lightPos.xyz - pos.xyz;
58+
output.ViewVec = ubo.viewPos.xyz - pos.xyz;
59+
return output;
60+
}
61+
62+
[shader("fragment")]
63+
float4 fragmentMain(VSOutput input, uniform float4x4 modelMat, uniform uint alphaMask, uniform float alphaMaskCutoff)
64+
{
65+
float4 color = samplerColorMap.Sample(input.UV);
66+
67+
if (alphaMask == 1) {
68+
if (color.a < alphaMaskCutoff) {
69+
discard;
70+
}
71+
}
72+
73+
float3 N = normalize(input.Normal);
74+
float3 T = normalize(input.Tangent.xyz);
75+
float3 B = cross(input.Normal, input.Tangent.xyz) * input.Tangent.w;
76+
float3x3 TBN = float3x3(T, B, N);
77+
N = mul(normalize(samplerNormalMap.Sample(input.UV).xyz * 2.0 - float3(1.0, 1.0, 1.0)), TBN);
78+
79+
const float ambient = 0.1;
80+
float3 L = normalize(input.LightVec);
81+
float3 V = normalize(input.ViewVec);
82+
float3 R = reflect(-L, N);
83+
float3 diffuse = max(dot(N, L), ambient).rrr;
84+
float3 specular = pow(max(dot(R, V), 0.0), 32.0);
85+
return float4(diffuse * color.rgb + specular, color.a);
86+
}

0 commit comments

Comments
 (0)