Shadows do at least something

This commit is contained in:
Dynamitos
2025-05-09 14:59:58 +02:00
parent ed66635191
commit 174ff23164
11 changed files with 63 additions and 52 deletions
+11 -2
View File
@@ -17,15 +17,24 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
LightingParameter lightingParams = params.getLightingParameter();
MaterialParameter materialParams = params.getMaterialParameter();
var brdf = Material.prepare(materialParams);
brdf.setNormal(normalize(params.normal_WS));
uint2 tileIndex = uint2(floor(params.position_CS.xy / BLOCK_SIZE));
uint startOffset = pLightCullingData.lightGrid[tileIndex].x;
uint lightCount = pLightCullingData.lightGrid[tileIndex].y;
float3 result = float3(0, 0, 0);
for(int i = 0; i < pLightEnv.numDirectionalLights; ++i)
{
result += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
float4 lightSpacePos = mul(pLightEnv.directionalLights[i].lightSpaceMatrix, float4(params.position_WS, 1));
float3 projCoords = lightSpacePos.xyz / lightSpacePos.w;
projCoords = projCoords * 0.5 + 0.5;
float closestDepth = pLightEnv.shadowMap.Sample(pLightEnv.shadowSampler, projCoords.xy).r;
float currentDepth = projCoords.z;
float bias = max(0.05 * (1.0 - dot(brdf.getNormal(), pLightEnv.directionalLights[i].direction.xyz)), 0.005);
float shadow = currentDepth > closestDepth - bias ? 1.0 : 0.0;
result += shadow * pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
}
for (uint i = 0; i < lightCount; ++i)
for (uint i = 0; i < pLightEnv.numPo; ++i)
{
uint lightIndex = pLightCullingData.lightIndexList[startOffset + i];
result += pLightEnv.pointLights[lightIndex].illuminate(lightingParams, brdf);
+1 -1
View File
@@ -19,7 +19,7 @@ struct ViewParameter
uint frameIndex;
float time;
};
layout(set = 0)
layout(set=0)
ParameterBlock<ViewParameter> pViewParams;
float4 worldToModel(float4x4 inverseTransform, float4 world)
+3 -15
View File
@@ -11,25 +11,11 @@ struct DirectionalLight : ILightEnv
float4 color;
float4 direction;
float4x4 lightSpaceMatrix;
Texture2D shadowMap;
SamplerState shadowSampler;
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
{
float3 dir_WS = -normalize(direction.xyz);
float3 color = brdf.evaluate(params.viewDir_WS, dir_WS, color.xyz * color.w);
float4 lightSpacePos = mul(lightSpaceMatrix, float4(params.position_WS, 1));
float3 projCoords = lightSpacePos.xyz / lightSpacePos.w;
projCoords = projCoords * 0.5 + 0.5;
float closestDepth = shadowMap.Sample(shadowSampler, projCoords.xy).r;
float currentDepth = projCoords.z;
float bias = max(0.05 * (1.0 - dot(brdf.getNormal(), direction.xyz)), 0.005);
float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
if (projCoords.z > 1.0)
shadow = 0;
return shadow * color;
return brdf.evaluate(params.viewDir_WS, dir_WS, color.xyz);
}
};
@@ -76,6 +62,8 @@ struct PointLight : ILightEnv
struct LightEnv
{
StructuredBuffer<DirectionalLight> directionalLights;
Texture2D shadowMap; // todo: not an array
SamplerState shadowSampler;
uint numDirectionalLights;
StructuredBuffer<PointLight> pointLights;
uint numPointLights;
+1 -2
View File
@@ -136,8 +136,7 @@ struct VertexAttributes
{
float4 modelPos = float4(position_MS, 1);
float4 worldPos = mul(transformMatrix, modelPos);
float4 viewPos = mul(pViewParams.viewMatrix, worldPos);
float4 clipPos = mul(pViewParams.projectionMatrix, viewPos);
float4 clipPos = mul(pViewParams.viewProjectionMatrix, worldPos);
FragmentParameter result;
result.position_CS = clipPos;
#ifndef POS_ONLY