#version 430
layout(std140) uniform;
uniform UnifDirLight
{
mat4 mWVPMatrix; // camera world-view-projection matrix
mat4 mVPMatrix; // the spotlights view-projection matrix
vec4 mLightColor;
vec4 mLightDir;
vec4 mGamma;
vec2 mScreenSize;
} UnifDirLightPass;
layout (binding = 2) uniform sampler2D unifPositionTexture;
layout (binding = 3) uniform sampler2D unifNormalTexture;
layout (binding = 4) uniform sampler2D unifDiffuseTexture;
layout (binding = 5) uniform sampler2D unifShadowTexture;
out vec4 fragColor;
void main()
{
vec2 texcoord = gl_FragCoord.xy / UnifDirLightPass.mScreenSize;
vec3 worldPos = texture(unifPositionTexture, texcoord).xyz;
vec3 normal = normalize(texture(unifNormalTexture, texcoord).xyz);
vec3 diffuse = texture(unifDiffuseTexture, texcoord).xyz;
vec4 lightClipPos = UnifDirLightPass.mVPMatrix * vec4(worldPos, 1.0);
vec3 projCoords = lightClipPos.xyz / lightClipPos.w;
projCoords.x = 0.5 * projCoords.x + 0.5;
projCoords.y = 0.5 * projCoords.y + 0.5;
projCoords.z = 0.5 * projCoords.z + 0.5;
float depthValue = texture(unifShadowTexture, projCoords.xy).x;
float shadowFactor = 0.0;
if (depthValue >= (projCoords.z + 0.0001))
shadowFactor = 1.0;
float angleNormal = clamp(dot(normal, UnifDirLightPass.mLightDir.xyz), 0, 1);
fragColor = vec4(diffuse, 1.0) * shadowFactor * angleNormal *
UnifDirLightPass.mLightColor;
}