2020-06-02 11:46:18 +02:00
|
|
|
import Common;
|
2023-11-08 23:27:21 +01:00
|
|
|
import BRDF;
|
|
|
|
|
import MaterialParameter;
|
2020-06-02 11:46:18 +02:00
|
|
|
|
|
|
|
|
interface ILightEnv
|
|
|
|
|
{
|
2023-11-13 09:07:23 +01:00
|
|
|
float3 illuminate<B:IBRDF>(LightingParameter input, B brdf);
|
2020-06-02 11:46:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct DirectionalLight : ILightEnv
|
|
|
|
|
{
|
2022-03-19 22:45:30 +01:00
|
|
|
float4 color;
|
|
|
|
|
float4 direction;
|
2020-06-02 11:46:18 +02:00
|
|
|
|
2023-11-13 09:07:23 +01:00
|
|
|
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
|
2022-03-19 22:45:30 +01:00
|
|
|
{
|
2023-12-03 23:12:20 +01:00
|
|
|
float3 lightDir_TS = mul(params.tbn, direction.xyz);
|
|
|
|
|
return brdf.evaluate(params.tbn, params.viewDir_TS, -normalize(lightDir_TS), color.xyz);
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct PointLight : ILightEnv
|
|
|
|
|
{
|
2023-02-27 13:52:57 +01:00
|
|
|
float4 position_WS;
|
2020-10-03 11:00:10 +02:00
|
|
|
float4 colorRange;
|
2024-02-02 11:39:40 +01:00
|
|
|
float4 position_CS;
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2023-11-13 09:07:23 +01:00
|
|
|
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
|
2022-03-19 22:45:30 +01:00
|
|
|
{
|
2023-11-30 11:51:53 +01:00
|
|
|
float3 lightDir_WS = position_WS.xyz - params.position_WS;
|
2023-12-03 23:12:20 +01:00
|
|
|
float3 lightDir_TS = mul(params.tbn, lightDir_WS);
|
|
|
|
|
float d = length(lightDir_TS);
|
2020-10-03 11:00:10 +02:00
|
|
|
float illuminance = max(1 - d / colorRange.w, 0);
|
2023-12-11 14:45:37 +01:00
|
|
|
return illuminance * brdf.evaluate(params.tbn, params.viewDir_TS, normalize(lightDir_TS), colorRange.xyz);
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
2024-02-02 11:39:40 +01:00
|
|
|
void updatePosition()
|
2022-03-19 22:45:30 +01:00
|
|
|
{
|
2024-02-02 11:39:40 +01:00
|
|
|
position_CS = mul(pViewParams.projectionMatrix, mul(pViewParams.viewMatrix, position_WS));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool insidePlane(Plane plane)
|
|
|
|
|
{
|
|
|
|
|
float3 edge_CS = position_CS + plane.n * colorRange.w;
|
|
|
|
|
return plane.pointInside(edge_CS);
|
2022-03-19 22:45:30 +01:00
|
|
|
}
|
2020-06-02 11:46:18 +02:00
|
|
|
|
2024-02-02 09:42:47 +01:00
|
|
|
bool insideFrustum(Frustum frustum, float nearClipVS, float maxDepthVS)
|
2022-03-19 22:45:30 +01:00
|
|
|
{
|
2024-02-02 09:42:47 +01:00
|
|
|
float3 center_CS = clipPos.xyz / clipPos.w;
|
|
|
|
|
if(insidePlane(frustum.basePlane, center_CS))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
uint result = 0;
|
2024-02-01 22:54:20 +01:00
|
|
|
for(int i = 0; i < 4; ++i)
|
|
|
|
|
{
|
2024-02-02 09:42:47 +01:00
|
|
|
if(insidePlane(frustum.sides[i], center_CS))
|
|
|
|
|
{
|
|
|
|
|
result++;
|
|
|
|
|
}
|
2024-02-01 22:54:20 +01:00
|
|
|
}
|
2024-02-02 09:42:47 +01:00
|
|
|
return result > 0;
|
2022-03-19 22:45:30 +01:00
|
|
|
}
|
2020-06-02 11:46:18 +02:00
|
|
|
};
|
|
|
|
|
|
2023-10-07 19:29:53 +02:00
|
|
|
struct LightEnv
|
|
|
|
|
{
|
|
|
|
|
StructuredBuffer<DirectionalLight> directionalLights;
|
|
|
|
|
uint numDirectionalLights;
|
2023-10-26 18:37:29 +02:00
|
|
|
StructuredBuffer<PointLight> pointLights;
|
2023-10-07 19:29:53 +02:00
|
|
|
uint numPointLights;
|
|
|
|
|
};
|
2023-11-10 22:26:47 +01:00
|
|
|
layout(set=3)
|
2023-11-08 23:27:21 +01:00
|
|
|
ParameterBlock<LightEnv> pLightEnv;
|