Files
Seele/res/shaders/lib/LightEnv.slang
T

68 lines
1.8 KiB
Plaintext
Raw Normal View History

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;
2023-11-13 09:07:23 +01:00
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
2022-03-19 22:45:30 +01:00
{
2024-02-20 22:38:35 +01:00
float3 lightDir_WS = params.position_WS - position_WS.xyz;
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);
2024-02-21 10:30:04 +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
2024-02-23 08:31:21 +01:00
bool insidePlane(Plane plane, float3 position_VS)
2024-02-02 11:39:40 +01:00
{
2024-02-23 08:31:21 +01:00
return dot(plane.n, position_VS) - plane.d < -colorRange.w;
2022-03-19 22:45:30 +01:00
}
2020-06-02 11:46:18 +02:00
2024-02-23 08:31:21 +01:00
bool insideFrustum(Frustum frustum, float3 position_VS, float minDepth, float maxDepth)
2022-03-19 22:45:30 +01:00
{
2024-02-20 21:07:17 +01:00
bool result = true;
2024-02-23 08:31:21 +01:00
if(position_VS.z - colorRange.w > minDepth || position_VS.z + colorRange.w < maxDepth)
2024-02-02 09:42:47 +01:00
{
2024-02-20 21:07:17 +01:00
result = false;
2024-02-02 09:42:47 +01:00
}
2024-02-20 21:07:17 +01:00
for(int i = 0; i < 4 && result; ++i)
2024-02-01 22:54:20 +01:00
{
2024-02-23 08:31:21 +01:00
if(insidePlane(frustum.sides[i], position_VS))
2024-02-02 09:42:47 +01:00
{
2024-02-20 21:07:17 +01:00
result = false;
2024-02-02 09:42:47 +01:00
}
2024-02-01 22:54:20 +01:00
}
2024-02-20 21:07:17 +01:00
return result;
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;
};
2024-04-24 23:25:34 +02:00
layout(set=3)
2023-11-08 23:27:21 +01:00
ParameterBlock<LightEnv> pLightEnv;