#version 430 core out vec4 FragColor; in VS_OUT { vec3 FragPos; vec4 FragPosLightSpace; vec2 TexCoords; vec3 TangentLightDir; vec3 TangentViewPos; vec3 TangentFragPos; } fs_in; layout (binding = 0) uniform sampler2D albedoMap; layout (binding = 1) uniform sampler2D normalMap; layout (binding = 2) uniform sampler2D depthMap; layout (binding = 3) uniform sampler2D shadowMap; uniform float bump_depth; uniform float number_of_steps; uniform float number_of_refinement_steps; vec2 get_parallax_offset_uv(vec2 uv, vec3 view_direction) { float layer_depth = 1.0 / number_of_steps; float refinement_layer_depth = layer_depth / number_of_refinement_steps; float current_layer_depth = 0.0; // the amount to shift the texture coordinates per layer (from vector total_uv_shift) vec2 total_uv_shift = view_direction.xy / view_direction.z * bump_depth; vec2 uv_shift_per_layer = total_uv_shift / number_of_steps; vec2 uv_refine_shift_per_layer = total_uv_shift / (number_of_steps * number_of_refinement_steps); // Initial values vec2 current_uv = uv; float current_bump_value = 1.0 - texture(depthMap, current_uv).r; // Loop until our depth is greater than the value in the bump map, meaning our ray collided while(current_layer_depth < current_bump_value) { current_uv -= uv_shift_per_layer; current_bump_value = 1.0 - texture(depthMap, current_uv).r; current_layer_depth += layer_depth; } // Reverse the last operations so we're at the point before the collision current_uv += uv_shift_per_layer; current_bump_value = 1.0 - texture(depthMap, current_uv).r; // Loop again for the refinement steps while(current_layer_depth < current_bump_value) { current_uv -= uv_refine_shift_per_layer; current_bump_value = 1.0 - texture(depthMap, current_uv).r; current_layer_depth += refinement_layer_depth; } // Referse the UV operation again and return the result current_uv += uv_refine_shift_per_layer; return current_uv; } // Return a linear interpolation (0..1) of value between low and high, or 0.0 / 1.0 if value is below / above the bounds float linear_step(float low, float high, float value) { return clamp((value - low) / (high - low), 0.0, 1.0); } float get_shadow(vec4 fragPosLightSpace, vec3 normal) { // perform perspective divide mediump vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; // transform to [0,1] range projCoords = projCoords * 0.5 + 0.5; // If we're outside of [0.0, 1.0] in the coordinates, return 0 if (projCoords.x < 0.0 || projCoords.y < 0.0 || projCoords.x > 1.0 || projCoords.y > 1.0) return 0.0; // Variance Shadow Map Calculation vec2 moments = texture(shadowMap, projCoords.xy).rg; // get closest depth value from light's perspective (using [0,1] range fragPosLight as coords) mediump float closestDepth = moments.r; // get depth of current fragment from light's perspective mediump float currentDepth = projCoords.z; // check whether current frag pos is in shadow float p = step(projCoords.z, moments.x); // We divide by this later, so make sure it's not exactly 0 // It seems like it should always be 0.0, but due to interpolation it's not -- it increases with the deviation! // A larger second parameter to max() means more blur (but also more light bleeding) float variance = max(moments.y - moments.x * moments.x, (-0.01 + (currentDepth - closestDepth) * 0.7) * 0.1); float d = projCoords.z - moments.x * 1.0; // Use linear_step to prevent light bleeding float p_max = linear_step(0.2, 1.0, variance / (variance + d * d)); // If this pixel is exactly in the light, p is 1, so make sure we return that in that case // min() to make sure that it doesn't get greater than 1.0 return 1.0 - min(max(p, p_max), 1.0); } void main() { // Offset texture coordinates with Parallax Mapping vec3 view_direction = normalize(fs_in.TangentViewPos - fs_in.TangentFragPos); vec2 uv = fs_in.TexCoords; uv = get_parallax_offset_uv(uv, view_direction); // Discard if the parallax offset moved us outside of the texture // TODO: Do this only if the mode is not REPEAT //if (uv.x > 1.0 || uv.y > 1.0 || uv.x < 0.0 || uv.y < 0.0) // discard; // Get normal from normal map and scale it to -1..1 vec3 normal = texture(normalMap, uv).rgb; normal = normalize(normal * 2.0 - 1.0); // Get albedo color vec3 color = texture(albedoMap, uv).rgb; // Ambient lighting vec3 ambient = 0.1 * color; // Apply albedo with intensity based on the dot product between the light direction and the normal here vec3 light_direction = fs_in.TangentLightDir; float light_normal_dot = max(dot(light_direction, normal), 0.0); vec3 albedo = light_normal_dot * color; // Specular lighting vec3 halfway_reflected_light_direction = normalize(light_direction + view_direction); float spec = pow(max(dot(normal, halfway_reflected_light_direction), 0.0), 32.0); vec3 specular = vec3(0.2) * spec; float shadow = get_shadow(fs_in.FragPosLightSpace, normal); // Apply FragColor = vec4(ambient + albedo + specular - shadow * 0.3, 1.0); }