2018-06-24 22:40:52 +00:00
#define SHADOWS @shadows_enabled
#if SHADOWS
@foreach shadow_texture_unit_index @shadow_texture_unit_list
2023-02-05 00:40:33 +00:00
uniform mat4 shadowSpaceMatrix@shadow_texture_unit_index;
2018-06-24 22:40:52 +00:00
uniform int shadowTextureUnit@shadow_texture_unit_index;
varying vec4 shadowSpaceCoords@shadow_texture_unit_index;
2018-12-08 20:39:41 +00:00
#if @perspectiveShadowMaps
uniform mat4 validRegionMatrix@shadow_texture_unit_index;
varying vec4 shadowRegionCoords@shadow_texture_unit_index;
#endif
2018-06-24 22:40:52 +00:00
@endforeach
2019-01-30 22:28:00 +00:00
// Enabling this may reduce peter panning. Probably unnecessary.
const bool onlyNormalOffsetUV = false;
2018-06-24 22:40:52 +00:00
#endif // SHADOWS
2019-01-30 22:28:00 +00:00
void setupShadowCoords(vec4 viewPos, vec3 viewNormal)
2018-06-24 22:40:52 +00:00
{
#if SHADOWS
// This matrix has the opposite handedness to the others used here, so multiplication must have the vector to the left. Alternatively it could be transposed after construction, but that's extra work for the GPU just to make the code look a tiny bit cleaner.
2019-01-30 22:28:00 +00:00
vec4 shadowOffset;
2018-06-24 22:40:52 +00:00
@foreach shadow_texture_unit_index @shadow_texture_unit_list
2018-12-08 20:39:41 +00:00
#if @perspectiveShadowMaps
shadowRegionCoords@shadow_texture_unit_index = validRegionMatrix@shadow_texture_unit_index * viewPos;
#endif
2023-02-05 00:40:33 +00:00
2019-01-30 22:28:00 +00:00
#if @disableNormalOffsetShadows
2023-02-05 00:40:33 +00:00
shadowSpaceCoords@shadow_texture_unit_index = shadowSpaceMatrix@shadow_texture_unit_index * viewPos;
2019-01-30 22:28:00 +00:00
#else
shadowOffset = vec4(viewNormal * @shadowNormalOffset, 0.0);
if (onlyNormalOffsetUV)
{
vec4 lightSpaceXY = viewPos + shadowOffset;
2023-02-05 00:40:33 +00:00
lightSpaceXY = shadowSpaceMatrix@shadow_texture_unit_index * lightSpaceXY;
2019-01-30 22:28:00 +00:00
shadowSpaceCoords@shadow_texture_unit_index.xy = lightSpaceXY.xy;
}
else
{
vec4 offsetViewPosition = viewPos + shadowOffset;
2023-02-05 00:40:33 +00:00
shadowSpaceCoords@shadow_texture_unit_index = shadowSpaceMatrix@shadow_texture_unit_index * offsetViewPosition;
2019-01-30 22:28:00 +00:00
}
#endif
2018-06-24 22:40:52 +00:00
@endforeach
#endif // SHADOWS
}