2025-11-23 18:40:48 +00:00
|
|
|
#version 330 core
|
|
|
|
|
|
2025-11-29 23:22:17 +00:00
|
|
|
precision highp float;
|
|
|
|
|
precision highp sampler2D;
|
|
|
|
|
|
2025-11-23 18:40:48 +00:00
|
|
|
in vec4 v_normal;
|
|
|
|
|
in vec4 color;
|
2025-11-23 23:43:48 +00:00
|
|
|
in vec3 light_direction;
|
2025-11-29 23:22:17 +00:00
|
|
|
in vec4 v_light_space_position;
|
2025-11-28 20:39:09 +00:00
|
|
|
|
2025-11-23 18:40:48 +00:00
|
|
|
uniform int render_normals_bool;
|
2025-11-28 20:39:09 +00:00
|
|
|
uniform int render_shadows_bool;
|
2025-11-29 23:22:17 +00:00
|
|
|
|
|
|
|
|
uniform float shadow_max_bias;
|
|
|
|
|
uniform float shadow_min_bias;
|
|
|
|
|
uniform float shadow_fac_bias;
|
|
|
|
|
uniform int shadow_step_size;
|
|
|
|
|
uniform int shadow_step_count;
|
|
|
|
|
|
|
|
|
|
uniform highp sampler2D shadow_map;
|
2025-11-23 18:40:48 +00:00
|
|
|
|
|
|
|
|
out vec4 FragColor;
|
|
|
|
|
|
|
|
|
|
const float ambient_strenght = 0.3;
|
|
|
|
|
const float diffuse_strenght = 0.7;
|
|
|
|
|
|
2025-11-28 20:39:09 +00:00
|
|
|
/**
|
|
|
|
|
* Calculates the shadow factor (0.0 for shadow, 1.0 for lit).
|
|
|
|
|
*/
|
|
|
|
|
float calculate_shadow() {
|
2025-11-29 23:22:17 +00:00
|
|
|
float cosTheta = dot(normalize(v_normal.xyz), normalize(light_direction));
|
|
|
|
|
float bias = shadow_fac_bias * tan(acos(cosTheta));
|
|
|
|
|
bias = clamp(bias, shadow_min_bias, shadow_max_bias);
|
|
|
|
|
// float bias = max(shadow_max_bias * (1.0 - dot_product), shadow_min_bias);
|
2025-11-28 20:39:09 +00:00
|
|
|
|
|
|
|
|
// 1. Perspective divide to get Normalized Device Coordinates (NDC)
|
|
|
|
|
vec3 proj_coords = v_light_space_position.xyz / v_light_space_position.w;
|
2025-11-29 23:22:17 +00:00
|
|
|
proj_coords = proj_coords * 0.5 + 0.5;
|
2025-11-28 20:39:09 +00:00
|
|
|
|
2025-11-29 23:22:17 +00:00
|
|
|
float current_depth = proj_coords.z;
|
2025-11-28 20:39:09 +00:00
|
|
|
|
2025-11-29 23:22:17 +00:00
|
|
|
// Deal with coords outside texture or infinite distance (no objects found)
|
|
|
|
|
if (proj_coords.x > 1.0 || proj_coords.x < 0.0 || proj_coords.y > 1.0 || proj_coords.y < 0.0 || current_depth > 1.0) {
|
2025-11-28 20:39:09 +00:00
|
|
|
return 1.0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-29 23:22:17 +00:00
|
|
|
vec2 texelSize = 1.0 / textureSize(shadow_map, 0);
|
|
|
|
|
|
2025-12-04 17:32:53 +00:00
|
|
|
float proj_depth = texture(shadow_map, proj_coords.xy).r;
|
|
|
|
|
// int show_shadow = current_depth - bias > proj_depth ? 0 : 1;
|
|
|
|
|
// if (show_shadow == 1) {
|
|
|
|
|
// return 1.0;
|
|
|
|
|
// }
|
|
|
|
|
float shadow = 0.0;
|
2025-11-29 23:22:17 +00:00
|
|
|
|
|
|
|
|
int range = (shadow_step_count - 1) / 2;
|
|
|
|
|
for (int x = -range; x <= range; x++)
|
|
|
|
|
{
|
|
|
|
|
for (int y = -range; y <= range; y++)
|
|
|
|
|
{
|
|
|
|
|
float pcfDepth = texture(shadow_map, proj_coords.xy + (vec2(x, y) * texelSize) * shadow_step_size).r;
|
|
|
|
|
shadow += current_depth - bias > pcfDepth ? 0.0 : 1.0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
shadow /= shadow_step_count * shadow_step_count;
|
|
|
|
|
|
|
|
|
|
return shadow;
|
2025-11-28 20:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-23 18:40:48 +00:00
|
|
|
void main() {
|
2025-11-23 23:43:48 +00:00
|
|
|
float diff = max(dot(normalize(vec3(v_normal.x, v_normal.y, v_normal.z)), normalize(light_direction)), 0) * diffuse_strenght;
|
2025-11-29 23:22:17 +00:00
|
|
|
vec4 app_color;
|
2025-11-23 18:40:48 +00:00
|
|
|
if (render_normals_bool == 1) {
|
2025-11-29 23:22:17 +00:00
|
|
|
app_color = normalize(abs(v_normal));
|
2025-11-23 18:40:48 +00:00
|
|
|
}
|
2025-11-29 23:22:17 +00:00
|
|
|
else {
|
2025-12-04 08:01:54 +00:00
|
|
|
app_color = color / 256;
|
2025-11-23 18:40:48 +00:00
|
|
|
}
|
2025-11-28 20:39:09 +00:00
|
|
|
|
2025-11-29 23:22:17 +00:00
|
|
|
if (render_shadows_bool == 1) {
|
|
|
|
|
float shadow_factor = calculate_shadow();
|
2025-11-28 20:39:09 +00:00
|
|
|
|
2025-11-29 23:22:17 +00:00
|
|
|
float lighting = ambient_strenght + diff * shadow_factor;
|
2025-11-28 20:39:09 +00:00
|
|
|
|
2025-11-29 23:22:17 +00:00
|
|
|
FragColor = app_color * lighting;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
float lighting = ambient_strenght + diff;
|
|
|
|
|
FragColor = app_color * lighting;
|
|
|
|
|
}
|
2025-11-23 18:40:48 +00:00
|
|
|
}
|