protot/data/shaders/fs_deferred_lighting.glsl

134 lines
4.4 KiB
Plaintext
Raw Permalink Normal View History

#version 150 core
2018-07-07 23:09:34 +02:00
uniform sampler2D uPosition;
uniform sampler2D uDepth;
2018-07-07 23:09:34 +02:00
uniform sampler2D uNormal;
uniform sampler2D uColor;
2018-06-22 17:31:53 +02:00
uniform sampler2D uAmbientOcclusion;
const int NUM_SPLITS = 4;
2018-07-07 23:09:34 +02:00
#define USE_SAMPLER2D_SHADOW 1
#ifdef USE_SAMPLER2D_SHADOW
2018-07-07 23:09:34 +02:00
uniform sampler2DShadow uShadowMap[NUM_SPLITS];
#else
2018-07-07 23:09:34 +02:00
uniform sampler2D uShadowMaps[NUM_SPLITS];
#endif
2018-07-07 23:09:34 +02:00
uniform mat4 uViewToLightMatrix[NUM_SPLITS];
2018-07-07 23:09:34 +02:00
uniform float uNear;
uniform float uFar;
2018-07-26 14:07:06 +02:00
uniform float uShowCascadesAlpha;
2018-07-07 23:09:34 +02:00
uniform vec4 uShadowSplits;
uniform vec4 uShadowSplitBias;
uniform vec3 uLightDirection;
2018-06-25 16:55:01 +02:00
uniform float uShadowBias;
in vec2 ioFragTexCoords;
out vec3 outColor;
2018-07-07 23:09:34 +02:00
#ifdef USE_SAMPLER2D_SHADOW
float ShadowCalculationPCF(sampler2DShadow shadow_map, vec4 frag_pos_light_space, vec3 frag_normal_light_space, float shadow_bias) {
2018-07-07 23:09:34 +02:00
#else
float ShadowCalculationPCF(sampler2D shadow_map, vec4 frag_pos_light_space, vec3 frag_normal_light_space, float shadow_bias) {
2018-07-07 23:09:34 +02:00
#endif
vec3 projected_coordinates = frag_pos_light_space.xyz / frag_pos_light_space.w;
projected_coordinates = projected_coordinates * 0.5 + 0.5;
if (abs(projected_coordinates.z) > 1.0 ) {
return 0.0;
}
float current_depth = projected_coordinates.z;
float bias = 0.00;
bias = max(shadow_bias * (1.0 - dot(frag_normal_light_space, uLightDirection)), shadow_bias);
float shadow = 0.0;
2018-07-07 23:09:34 +02:00
vec2 texel_size = 1.0 / textureSize(shadow_map, 0);
for (int x = -1; x <= 1; ++x) {
for (int y = -1; y <= 1; ++y) {
#ifdef USE_SAMPLER2D_SHADOW
vec2 coordinate = projected_coordinates.xy + vec2(x, y) * texel_size;
2018-07-07 23:09:34 +02:00
float pcf_depth = texture(shadow_map, vec3(coordinate, current_depth - bias));
#else
2018-07-07 23:09:34 +02:00
float pcf_depth = texture(shadow_map, projected_coordinates.xy).r;
#endif
shadow += current_depth - bias > pcf_depth ? 1.0 : 0.0;
}
}
shadow /= 9.0;
return shadow;
}
vec3 get_cascade_color (float depth) {
2018-07-26 14:07:06 +02:00
if (depth < uShadowSplits[0]) {
return vec3 (1.0, 0.0, 0.0);
2018-07-26 14:07:06 +02:00
} else if (depth < uShadowSplits[1]) {
return vec3 (1.0, 1.0, 0.0);
2018-07-07 23:09:34 +02:00
} else if (depth < uShadowSplits[2]) {
return vec3 (0.0, 1.0, 0.0);
2018-07-26 14:07:06 +02:00
}
return vec3 (0.0, 0.0, 1.0);
}
void main() {
vec3 color = texture(uColor, ioFragTexCoords).rgb;
vec3 normal = texture (uNormal, ioFragTexCoords).xyz;
float depth = texture (uDepth, ioFragTexCoords).r;
vec3 position = texture (uPosition, ioFragTexCoords).xyz;
2018-06-22 17:31:53 +02:00
float ambient_occlusion = texture(uAmbientOcclusion, ioFragTexCoords).r;
// ambient lighting
float ambient_strength = 0.2;
vec3 ambient = ambient_strength * color;
vec3 light_dir = -uLightDirection;
// diffuse lighting
float diff = max(dot(normal, light_dir), 0.0);
vec3 diffuse = diff * color;
// specular lighting
vec3 view_dir = normalize(-position);
vec3 halfway_dir = normalize(light_dir + view_dir);
float spec = pow(max(dot(normal, halfway_dir), 0.0), 32);
vec3 specular = spec * vec3(0.5);
2018-07-07 23:09:34 +02:00
float shadow = 0;
float normalized_depth = (depth - uNear) / (uFar - uNear);
if (-position.z < uShadowSplits[0]) {
2018-07-07 23:09:34 +02:00
// shadow (need to transform position and normal to light space)
vec4 position_light_space = uViewToLightMatrix[0] * vec4(position, 1.0);
vec3 normal_light_space = (transpose(inverse(uViewToLightMatrix[0])) * vec4(normal, 1.0)).xyz;
shadow = ShadowCalculationPCF(uShadowMap[0], position_light_space, normal, uShadowSplitBias[0]);
} else if (-position.z< uShadowSplits[1]) {
2018-07-07 23:09:34 +02:00
vec4 position_light_space = uViewToLightMatrix[1] * vec4(position, 1.0);
vec3 normal_light_space = (transpose(inverse(uViewToLightMatrix[1])) * vec4(normal, 1.0)).xyz;
shadow = ShadowCalculationPCF(uShadowMap[1], position_light_space, normal, uShadowSplitBias[1]);
} else if (-position.z< uShadowSplits[2]) {
2018-07-07 23:09:34 +02:00
vec4 position_light_space = uViewToLightMatrix[2] * vec4(position, 1.0);
vec3 normal_light_space = (transpose(inverse(uViewToLightMatrix[2])) * vec4(normal, 1.0)).xyz;
shadow = ShadowCalculationPCF(uShadowMap[2], position_light_space, normal, uShadowSplitBias[2]);
} else {
vec4 position_light_space = uViewToLightMatrix[3] * vec4(position, 1.0);
vec3 normal_light_space = (transpose(inverse(uViewToLightMatrix[3])) * vec4(normal, 1.0)).xyz;
shadow = ShadowCalculationPCF(uShadowMap[3], position_light_space, normal, uShadowSplitBias[3]);
2018-07-07 23:09:34 +02:00
}
2018-07-26 14:07:06 +02:00
vec3 cascade = get_cascade_color(-position.z);
ambient = (uShowCascadesAlpha * cascade) + (1.0 - uShowCascadesAlpha) * ambient;
outColor = (ambient + (1.0 - shadow) * (diffuse + specular)) * ambient_occlusion;
2018-07-26 14:07:06 +02:00
2018-07-07 23:09:34 +02:00
// outColor = diffuse;
2018-06-22 17:31:53 +02:00
// outColor = (ambient + (diffuse + specular)) * ambient_occlusion;
}