2020-08-06 00:54:43 +02:00
|
|
|
import MaterialParameter;
|
2020-06-02 11:46:18 +02:00
|
|
|
import BRDF;
|
|
|
|
|
import Common;
|
|
|
|
|
|
|
|
|
|
interface ILightEnv
|
|
|
|
|
{
|
2023-02-27 13:52:57 +01:00
|
|
|
float3 illuminate<B:IBRDF>(MaterialFragmentParameter 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-02-27 13:52:57 +01:00
|
|
|
float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf)
|
2022-03-19 22:45:30 +01:00
|
|
|
{
|
2023-02-27 13:52:57 +01:00
|
|
|
float3 lightDir_TS = input.transformWorldToTangent(normalize(direction.xyz));
|
|
|
|
|
return brdf.evaluate(input.viewDir_TS, -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-02-27 13:52:57 +01:00
|
|
|
float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf)
|
2022-03-19 22:45:30 +01:00
|
|
|
{
|
2023-02-27 13:52:57 +01:00
|
|
|
float3 position_TS = input.transformWorldToTangent(position_WS.xyz);
|
|
|
|
|
float3 lightDir_TS = position_TS - input.position_TS;
|
|
|
|
|
float d = length(lightDir_TS);
|
2020-10-03 11:00:10 +02:00
|
|
|
float illuminance = max(1 - d / colorRange.w, 0);
|
2023-02-27 13:52:57 +01:00
|
|
|
return illuminance * brdf.evaluate(input.viewDir_TS, normalize(lightDir_TS), colorRange.xyz);
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-19 22:45:30 +01:00
|
|
|
bool insidePlane(Plane plane)
|
|
|
|
|
{
|
2022-02-24 22:38:26 +01:00
|
|
|
return dot(plane.n, getViewPos().xyz) - plane.d < -colorRange.w;
|
2022-03-19 22:45:30 +01:00
|
|
|
}
|
2020-06-02 11:46:18 +02:00
|
|
|
|
2022-03-19 22:45:30 +01:00
|
|
|
bool insideFrustum(Frustum frustum, float zNear, float zFar)
|
|
|
|
|
{
|
2020-06-02 11:46:18 +02:00
|
|
|
bool result = true;
|
|
|
|
|
|
2022-03-19 22:45:30 +01:00
|
|
|
if(getViewPos().z - colorRange.w > zNear || getViewPos().z + colorRange.w < zFar)
|
|
|
|
|
{
|
|
|
|
|
//result = false;
|
|
|
|
|
}
|
|
|
|
|
for(int i = 0; i < 4 && result; ++i)
|
|
|
|
|
{
|
|
|
|
|
if(insidePlane(frustum.planes[i]))
|
|
|
|
|
{
|
|
|
|
|
//result = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
float3 getViewPos()
|
|
|
|
|
{
|
2023-02-27 13:52:57 +01:00
|
|
|
return mul(gViewParams.viewMatrix, position_WS).xyz;
|
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;
|
|
|
|
|
StructureBuffer<PointLight> pointLights;
|
|
|
|
|
uint numPointLights;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParameterBlock<LightEnv> gLightEnv;
|
|
|
|
|
|
|
|
|
|
//layout(set = INDEX_LIGHT_ENV, binding = 0, std430)
|
|
|
|
|
//ShaderBuffer<DirectionalLight> directionalLights;
|
|
|
|
|
//layout(set = INDEX_LIGHT_ENV, binding = 1, std430)
|
|
|
|
|
//ConstantBuffer<uint> numDirectionalLights;
|
|
|
|
|
//layout(set = INDEX_LIGHT_ENV, binding = 2, std430)
|
|
|
|
|
//ShaderBuffer<PointLight> pointLights;
|
|
|
|
|
//layout(set = INDEX_LIGHT_ENV, binding = 3, std430)
|
|
|
|
|
//ConstantBuffer<uint> numPointLights;
|