2019-11-03 19:52:22 +00:00
|
|
|
#version 120
|
|
|
|
|
2020-03-14 19:41:36 +00:00
|
|
|
uniform bool shadows_enabled;
|
|
|
|
|
2019-11-03 19:52:22 +00:00
|
|
|
uniform mat4 fg_LightMatrix_csm0;
|
|
|
|
uniform mat4 fg_LightMatrix_csm1;
|
|
|
|
uniform mat4 fg_LightMatrix_csm2;
|
|
|
|
uniform mat4 fg_LightMatrix_csm3;
|
|
|
|
|
|
|
|
varying vec4 lightSpacePos[4];
|
|
|
|
|
2021-01-05 10:24:19 +00:00
|
|
|
const float NORMAL_OFFSET_SCALES[4] = float[4](0.0, 0.1, 0.3, 1.0);
|
2019-11-03 19:52:22 +00:00
|
|
|
|
|
|
|
void setupShadows(vec4 eyeSpacePos)
|
|
|
|
{
|
2020-03-14 19:41:36 +00:00
|
|
|
if (!shadows_enabled)
|
|
|
|
return;
|
|
|
|
|
2020-11-10 00:45:54 +00:00
|
|
|
vec3 normal = gl_NormalMatrix * gl_Normal;
|
2019-11-03 19:52:22 +00:00
|
|
|
|
2020-11-10 00:45:54 +00:00
|
|
|
vec3 toLight = normalize(gl_LightSource[0].position.xyz);
|
|
|
|
float costheta = dot(normal, toLight);
|
|
|
|
float slopeScale = clamp(1.0 - costheta, 0.0, 1.0);
|
2019-11-03 19:52:22 +00:00
|
|
|
|
2021-01-05 10:24:19 +00:00
|
|
|
vec4 offsetPos[4];
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
float normalOffset = NORMAL_OFFSET_SCALES[i] * slopeScale;
|
|
|
|
offsetPos[i] = eyeSpacePos + vec4(normal * normalOffset, 0.0);
|
|
|
|
}
|
2019-11-03 19:52:22 +00:00
|
|
|
|
2021-01-05 10:24:19 +00:00
|
|
|
vec4 offsetPosLightSpace[4];
|
|
|
|
offsetPosLightSpace[0] = fg_LightMatrix_csm0 * offsetPos[0];
|
|
|
|
offsetPosLightSpace[1] = fg_LightMatrix_csm1 * offsetPos[1];
|
|
|
|
offsetPosLightSpace[2] = fg_LightMatrix_csm2 * offsetPos[2];
|
|
|
|
offsetPosLightSpace[3] = fg_LightMatrix_csm3 * offsetPos[3];
|
2019-11-03 19:52:22 +00:00
|
|
|
|
|
|
|
lightSpacePos[0] = fg_LightMatrix_csm0 * eyeSpacePos;
|
|
|
|
lightSpacePos[1] = fg_LightMatrix_csm1 * eyeSpacePos;
|
|
|
|
lightSpacePos[2] = fg_LightMatrix_csm2 * eyeSpacePos;
|
|
|
|
lightSpacePos[3] = fg_LightMatrix_csm3 * eyeSpacePos;
|
|
|
|
|
|
|
|
// Offset only in UV space
|
2021-01-05 10:24:19 +00:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
lightSpacePos[i].xy = offsetPosLightSpace[i].xy;
|
2019-11-03 19:52:22 +00:00
|
|
|
}
|