Add shadows

This commit is contained in:
2025-01-17 18:16:29 +01:00
parent f413bf886b
commit 1ee5dd4ad0
13 changed files with 3898 additions and 3801 deletions

View File

@@ -1,2 +1,42 @@
# Blender 4.3.2 MTL File: 'None'
# www.blender.org
newmtl Material.001
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800119 0.001607 0.000000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Material.002
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800149 0.000000 0.387990
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Material.003
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.003822 0.800111 0.000000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
newmtl Material.004
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2

File diff suppressed because it is too large Load Diff

View File

@@ -77,26 +77,37 @@ float ShadowCalculation(float4 shadowCoord, float3 normal) {
// Normalize the depth by dividing by the w component
float3 projCoord = shadowCoord.xyz / shadowCoord.w;
float shadow = 0.0;
uint width, height;
gShadowMap.GetDimensions(width, height);
float2 texelSize = float2(1.f / width, 1.f / height);
// Convert from [-1, 1] to [0, 1]
float2 UVCoords;
UVCoords.x = 0.5f * projCoord.x + 0.5f;
UVCoords.y = -0.5f * projCoord.y + 0.5f;
// float z = 0.5 * projCoord.z + 0.5;
float z = projCoord.z;
// Sample the shadow map
float shadowDepth = gShadowMap.Sample(gShadowSampler2, UVCoords.xy).r;
// float bias = 0.0025f;
float bias = max(0.05 * (1.0 - dot(normal, -gLightDirection)), 0.005);
float bias = max(0.05 * (1.0 - dot(normal, gLightDirection)), 0.005);
for(int x = -1; x <= 1; ++x)
{
for(int y = -1; y <= 1; ++y)
{
float pcfDepth = gShadowMap.Sample(gShadowSampler2, UVCoords.xy + float2(x, y) * texelSize).r;
shadow += z - bias > pcfDepth ? 1.0 : 0.0;
}
}
// return gShadowMap.Sample(gShadowSampler2, UVCoords.xy).r;
// Check if the current fragment is in shadow
if (shadowDepth + bias < z)
return 0.5f;
else
return 1.0f;
shadow /= 9.0f;
// float shadowDepth = gShadowMap.Sample(gShadowSampler2, UVCoords.xy).r;
if(projCoord.z > 1.0)
shadow = 0.0;
return shadow;
}
// Pixel shader
float4 PS(VS_OUTPUT input) : SV_TARGET {
@@ -104,7 +115,7 @@ float4 PS(VS_OUTPUT input) : SV_TARGET {
float3 diffuseColor = gDiffuseMap.Sample(gSampleState, input.TexCoord).rgb;
float3 finalColor = float3(1.f, 1.f, 1.f) * shadowFactor;
float3 finalColor = diffuseColor * (1 - shadowFactor);
finalColor += gAmbient;
return float4(finalColor, 1.0f);

Binary file not shown.

After

Width:  |  Height:  |  Size: 133 KiB