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

73 lines
2.0 KiB
Plaintext

import Common;
import BRDF;
import MaterialParameter;
interface ILightEnv
{
float3 illuminate<B:IBRDF>(LightingParameter input, B brdf);
};
struct DirectionalLight : ILightEnv
{
float4 color;
float4 direction;
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
{
float3 lightDir_TS = mul(params.tbn, direction.xyz);
return brdf.evaluate(params.tbn, params.viewDir_TS, -normalize(lightDir_TS), color.xyz);
}
};
struct PointLight : ILightEnv
{
float4 position_WS;
float4 colorRange;
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
{
float3 lightDir_WS = params.position_WS - position_WS.xyz;
float3 lightDir_TS = mul(params.tbn, lightDir_WS);
float d = length(lightDir_TS);
float illuminance = max(1 - d / colorRange.w, 0);
return illuminance * brdf.evaluate(params.tbn, params.viewDir_TS, -normalize(lightDir_TS), colorRange.xyz);
}
float3 getClipPosition()
{
float4 position_CS = mul(pViewParams.projectionMatrix, mul(pViewParams.viewMatrix, position_WS));
return position_CS.xyz / position_CS.w;
}
bool insidePlane(Plane plane, float3 position_CS)
{
return dot(plane.n, position_CS) - plane.d < -colorRange.w;
}
bool insideFrustum(Frustum frustum, float3 position_CS, float minDepth, float maxDepth)
{
bool result = true;
if(position_CS.z - colorRange.w > minDepth || position_CS.z + colorRange.w < maxDepth)
{
result = false;
}
for(int i = 0; i < 4 && result; ++i)
{
if(insidePlane(frustum.sides[i], position_CS))
{
result = false;
}
}
return result;
}
};
struct LightEnv
{
StructuredBuffer<DirectionalLight> directionalLights;
uint numDirectionalLights;
StructuredBuffer<PointLight> pointLights;
uint numPointLights;
};
layout(set=3)
ParameterBlock<LightEnv> pLightEnv;