2023-10-09 17:20:30 +02:00
|
|
|
import Common;
|
2023-11-05 10:36:01 +01:00
|
|
|
import LightEnv;
|
|
|
|
|
import MaterialParameter;
|
2024-07-10 21:07:10 +02:00
|
|
|
import MATERIAL_FILE_NAME;
|
2023-11-05 10:36:01 +01:00
|
|
|
|
|
|
|
|
struct LightCullingData
|
|
|
|
|
{
|
2024-02-21 10:30:04 +01:00
|
|
|
RWStructuredBuffer<uint> lightIndexList;
|
2023-11-05 10:36:01 +01:00
|
|
|
|
2024-02-21 10:30:04 +01:00
|
|
|
RWTexture2D<uint2> lightGrid;
|
2023-11-05 10:36:01 +01:00
|
|
|
};
|
2023-11-08 23:27:21 +01:00
|
|
|
ParameterBlock<LightCullingData> pLightCullingData;
|
2023-10-09 17:20:30 +02:00
|
|
|
|
|
|
|
|
[shader("pixel")]
|
2024-05-30 21:24:21 +02:00
|
|
|
float4 fragmentMain(in FragmentParameter params) : SV_Target
|
2023-10-09 17:20:30 +02:00
|
|
|
{
|
2023-11-13 09:07:23 +01:00
|
|
|
LightingParameter lightingParams = params.getLightingParameter();
|
2023-12-15 11:57:13 +01:00
|
|
|
MaterialParameter materialParams = params.getMaterialParameter();
|
2025-05-04 21:30:01 +02:00
|
|
|
var brdf = Material.prepare(materialParams);
|
2025-05-09 14:59:58 +02:00
|
|
|
brdf.setNormal(normalize(params.normal_WS));
|
2024-02-20 22:38:35 +01:00
|
|
|
uint2 tileIndex = uint2(floor(params.position_CS.xy / BLOCK_SIZE));
|
2024-02-21 10:30:04 +01:00
|
|
|
uint startOffset = pLightCullingData.lightGrid[tileIndex].x;
|
|
|
|
|
uint lightCount = pLightCullingData.lightGrid[tileIndex].y;
|
2023-11-05 10:36:01 +01:00
|
|
|
float3 result = float3(0, 0, 0);
|
2023-11-08 23:27:21 +01:00
|
|
|
for(int i = 0; i < pLightEnv.numDirectionalLights; ++i)
|
2023-11-05 10:36:01 +01:00
|
|
|
{
|
2025-05-09 14:59:58 +02:00
|
|
|
float4 lightSpacePos = mul(pLightEnv.directionalLights[i].lightSpaceMatrix, float4(params.position_WS, 1));
|
|
|
|
|
float3 projCoords = lightSpacePos.xyz / lightSpacePos.w;
|
2025-05-09 23:05:47 +02:00
|
|
|
projCoords.xy = projCoords.xy * 0.5 + 0.5;
|
|
|
|
|
projCoords.z /= 2;
|
2025-05-09 14:59:58 +02:00
|
|
|
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);
|
2025-05-09 23:05:47 +02:00
|
|
|
float shadow = currentDepth + bias < closestDepth ? 1.0 : 0.0;
|
2025-05-09 14:59:58 +02:00
|
|
|
|
2025-05-09 23:05:47 +02:00
|
|
|
result += float3(currentDepth, 0, 0);//shadow * pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
|
2023-11-05 10:36:01 +01:00
|
|
|
}
|
2025-05-09 23:05:47 +02:00
|
|
|
for (uint i = 0; i < lightCount; ++i)
|
2023-11-05 10:36:01 +01:00
|
|
|
{
|
2025-05-04 21:30:01 +02:00
|
|
|
uint lightIndex = pLightCullingData.lightIndexList[startOffset + i];
|
|
|
|
|
result += pLightEnv.pointLights[lightIndex].illuminate(lightingParams, brdf);
|
2023-11-05 10:36:01 +01:00
|
|
|
}
|
2025-04-06 09:57:47 +02:00
|
|
|
result += brdf.evaluateAmbient(lightingParams.viewDir_WS);
|
2024-08-07 21:52:45 +02:00
|
|
|
return float4(result, brdf.getAlpha());
|
2024-12-27 17:06:43 +01:00
|
|
|
}
|