Add slang shader for omni-directional shadow mapping sample
This commit is contained in:
parent
153aa3b932
commit
afd37811ec
3 changed files with 205 additions and 0 deletions
80
shaders/slang/shadowmappingomni/cubemapdisplay.slang
Normal file
80
shaders/slang/shadowmappingomni/cubemapdisplay.slang
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
/* Copyright (c) 2025, Sascha Willems
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct VSOutput
|
||||||
|
{
|
||||||
|
float4 Pos : SV_POSITION;
|
||||||
|
float2 UV;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct UBO
|
||||||
|
{
|
||||||
|
float4x4 projection;
|
||||||
|
float4x4 view;
|
||||||
|
float4x4 model;
|
||||||
|
};
|
||||||
|
ConstantBuffer<UBO> ubo;
|
||||||
|
|
||||||
|
SamplerCube shadowCubeMapSampler;
|
||||||
|
|
||||||
|
[shader("vertex")]
|
||||||
|
VSOutput vertexMain(uint VertexIndex: SV_VertexID)
|
||||||
|
{
|
||||||
|
VSOutput output;
|
||||||
|
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
|
||||||
|
output.Pos = float4(output.UV.xy * 2.0f - 1.0f, 0.0f, 1.0f);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
[shader("fragment")]
|
||||||
|
float4 fragmentMain(VSOutput input)
|
||||||
|
{
|
||||||
|
float4 outFragColor = float4(0, 0, 0, 0);
|
||||||
|
outFragColor.rgb = float3(0.05, 0.05, 0.05);
|
||||||
|
|
||||||
|
float3 samplePos = float3(0, 0, 0);
|
||||||
|
|
||||||
|
// Crude statement to visualize different cube map faces based on UV coordinates
|
||||||
|
int x = int(floor(input.UV.x / 0.25f));
|
||||||
|
int y = int(floor(input.UV.y / (1.0 / 3.0)));
|
||||||
|
if (y == 1) {
|
||||||
|
float2 uv = float2(input.UV.x * 4.0f, (input.UV.y - 1.0/3.0) * 3.0);
|
||||||
|
uv = 2.0 * float2(uv.x - float(x) * 1.0, uv.y) - 1.0;
|
||||||
|
switch (x) {
|
||||||
|
case 0: // NEGATIVE_X
|
||||||
|
samplePos = float3(-1.0f, uv.y, uv.x);
|
||||||
|
break;
|
||||||
|
case 1: // POSITIVE_Z
|
||||||
|
samplePos = float3(uv.x, uv.y, 1.0f);
|
||||||
|
break;
|
||||||
|
case 2: // POSITIVE_X
|
||||||
|
samplePos = float3(1.0, uv.y, -uv.x);
|
||||||
|
break;
|
||||||
|
case 3: // NEGATIVE_Z
|
||||||
|
samplePos = float3(-uv.x, uv.y, -1.0f);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (x == 1) {
|
||||||
|
float2 uv = float2((input.UV.x - 0.25) * 4.0, (input.UV.y - float(y) / 3.0) * 3.0);
|
||||||
|
uv = 2.0 * uv - 1.0;
|
||||||
|
switch (y) {
|
||||||
|
case 0: // NEGATIVE_Y
|
||||||
|
samplePos = float3(uv.x, -1.0f, uv.y);
|
||||||
|
break;
|
||||||
|
case 2: // POSITIVE_Y
|
||||||
|
samplePos = float3(uv.x, 1.0f, -uv.y);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((samplePos.x != 0.0f) && (samplePos.y != 0.0f)) {
|
||||||
|
float dist = length(shadowCubeMapSampler.Sample(samplePos).xyz) * 0.005;
|
||||||
|
outFragColor = float4(dist.xxx, 1.0);
|
||||||
|
}
|
||||||
|
return outFragColor;
|
||||||
|
}
|
||||||
45
shaders/slang/shadowmappingomni/offscreen.slang
Normal file
45
shaders/slang/shadowmappingomni/offscreen.slang
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
/* Copyright (c) 2025, Sascha Willems
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct VSInput
|
||||||
|
{
|
||||||
|
float3 Pos;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VSOutput
|
||||||
|
{
|
||||||
|
float4 Pos : SV_POSITION;
|
||||||
|
float4 WorldPos;
|
||||||
|
float3 LightPos;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct UBO
|
||||||
|
{
|
||||||
|
float4x4 projection;
|
||||||
|
float4x4 view;
|
||||||
|
float4x4 model;
|
||||||
|
float4 lightPos;
|
||||||
|
};
|
||||||
|
ConstantBuffer<UBO> ubo;
|
||||||
|
|
||||||
|
[shader("vertex")]
|
||||||
|
VSOutput vertexMain(VSInput input, uniform float4x4 view)
|
||||||
|
{
|
||||||
|
VSOutput output;
|
||||||
|
output.Pos = mul(ubo.projection, mul(view, mul(ubo.model, float4(input.Pos, 1.0))));
|
||||||
|
|
||||||
|
output.WorldPos = float4(input.Pos, 1.0);
|
||||||
|
output.LightPos = ubo.lightPos.xyz;
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
[shader("fragment")]
|
||||||
|
float fragmentMain(VSOutput input)
|
||||||
|
{
|
||||||
|
// Store distance to light as 32 bit float value
|
||||||
|
float3 lightVec = input.WorldPos.xyz - input.LightPos;
|
||||||
|
return length(lightVec);
|
||||||
|
}
|
||||||
80
shaders/slang/shadowmappingomni/scene.slang
Normal file
80
shaders/slang/shadowmappingomni/scene.slang
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
/* Copyright (c) 2025, Sascha Willems
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct VSInput
|
||||||
|
{
|
||||||
|
float3 Pos;
|
||||||
|
float3 Color;
|
||||||
|
float3 Normal;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VSOutput
|
||||||
|
{
|
||||||
|
float4 Pos : SV_POSITION;
|
||||||
|
float3 Normal;
|
||||||
|
float3 Color;
|
||||||
|
float3 EyePos;
|
||||||
|
float3 LightVec;
|
||||||
|
float3 WorldPos;
|
||||||
|
float3 LightPos;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct UBO
|
||||||
|
{
|
||||||
|
float4x4 projection;
|
||||||
|
float4x4 view;
|
||||||
|
float4x4 model;
|
||||||
|
float4 lightPos;
|
||||||
|
};
|
||||||
|
ConstantBuffer<UBO> ubo;
|
||||||
|
|
||||||
|
SamplerCube shadowCubeMapSampler;
|
||||||
|
|
||||||
|
#define EPSILON 0.15
|
||||||
|
#define SHADOW_OPACITY 0.5
|
||||||
|
|
||||||
|
[shader("vertex")]
|
||||||
|
VSOutput vertexMain(VSInput input)
|
||||||
|
{
|
||||||
|
VSOutput output;
|
||||||
|
output.Color = input.Color;
|
||||||
|
output.Normal = input.Normal;
|
||||||
|
|
||||||
|
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos.xyz, 1.0))));
|
||||||
|
output.EyePos = mul(ubo.model, float4(input.Pos, 1.0f)).xyz;
|
||||||
|
output.LightVec = normalize(ubo.lightPos.xyz - input.Pos.xyz);
|
||||||
|
output.WorldPos = input.Pos;
|
||||||
|
|
||||||
|
output.LightPos = ubo.lightPos.xyz;
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
[shader("fragment")]
|
||||||
|
float4 fragmentMain(VSOutput input)
|
||||||
|
{
|
||||||
|
// Lighting
|
||||||
|
float3 N = normalize(input.Normal);
|
||||||
|
float3 L = normalize(float3(1.0, 1.0, 1.0));
|
||||||
|
|
||||||
|
float3 Eye = normalize(-input.EyePos);
|
||||||
|
float3 Reflected = normalize(reflect(-input.LightVec, input.Normal));
|
||||||
|
|
||||||
|
float4 IAmbient = float4(float3(0.05, 0.05, 0.05), 1.0);
|
||||||
|
float4 IDiffuse = float4(1.0, 1.0, 1.0, 1.0) * max(dot(input.Normal, input.LightVec), 0.0);
|
||||||
|
|
||||||
|
float4 outFragColor = float4(IAmbient + IDiffuse * float4(input.Color, 1.0));
|
||||||
|
|
||||||
|
// Shadow
|
||||||
|
float3 lightVec = input.WorldPos - input.LightPos;
|
||||||
|
float sampledDist = shadowCubeMapSampler.Sample(lightVec).r;
|
||||||
|
float dist = length(lightVec);
|
||||||
|
|
||||||
|
// Check if fragment is in shadow
|
||||||
|
float shadow = (dist <= sampledDist + EPSILON) ? 1.0 : SHADOW_OPACITY;
|
||||||
|
|
||||||
|
outFragColor.rgb *= shadow;
|
||||||
|
return outFragColor;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue