Add slang shaders for shader objects sample

This commit is contained in:
Sascha Willems 2025-05-03 11:43:55 +02:00
parent 0a6c03b58c
commit e0bff55eab

View file

@ -0,0 +1,65 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: MIT
*
*/
struct VSInput
{
float3 Pos;
float3 Normal;
float3 Color;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
float3 Normal;
float3 Color;
float3 ViewVec;
float3 LightVec;
};
struct UBO
{
float4x4 projection;
float4x4 model;
float4 lightPos;
};
ConstantBuffer<UBO> ubo;
Sampler2D samplerColor;
[shader("vertex")]
VSOutput vertexMain(VSInput input)
{
VSOutput output;
output.Color = input.Color;
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
output.Normal = mul((float4x3)ubo.model, input.Normal).xyz;
float3 lPos = mul((float4x3)ubo.model, ubo.lightPos.xyz).xyz;
output.LightVec = lPos.xyz - pos.xyz;
output.ViewVec = -pos.xyz;
return output;
}
[shader("fragment")]
float4 fragmentMain(VSOutput input)
{
// Desaturate color
float3 color = float3(lerp(input.Color, float3(dot(float3(0.2126, 0.7152, 0.0722), input.Color)), 0.65));
// High ambient colors because mesh materials are pretty dark
float3 ambient = color;
float3 N = normalize(input.Normal);
float3 L = normalize(input.LightVec);
float3 V = normalize(input.ViewVec);
float3 R = reflect(-L, N);
float3 diffuse = max(dot(N, L), 0.0) * color;
float3 specular = pow(max(dot(R, V), 0.0), 32.0) * float3(0.35);
return float4(ambient + diffuse * 1.75 + specular, 1.0);
}