v4k-git-backup/engine/art/shaderlib/shadowmap.glsl

68 lines
2.0 KiB
Plaintext
Raw Normal View History

2024-08-29 15:32:34 +00:00
in vec4 vpeye;
in vec4 vneye;
uniform bool u_shadow_receiver;
uniform samplerCube shadowMap[MAX_LIGHTS];
2024-08-24 17:32:25 +00:00
2024-08-29 15:32:34 +00:00
//// From http://fabiensanglard.net/shadowmappingVSM/index.php
float chebyshevUpperBound(float distance, vec3 dir, int light_index) {
distance = distance/20;
vec2 moments = texture(shadowMap[light_index], dir).rg;
2024-08-24 13:24:44 +00:00
2024-08-29 15:32:34 +00:00
// Surface is fully lit. as the current fragment is before the light occluder
if (distance <= moments.x) {
return 1.0;
}
2024-08-24 13:24:44 +00:00
2024-08-29 15:32:34 +00:00
// The fragment is either in shadow or penumbra. We now use chebyshev's upperBound to check
// How likely this pixel is to be lit (p_max)
float variance = moments.y - (moments.x*moments.x);
//variance = max(variance, 0.000002);
variance = max(variance, 0.00002);
float d = distance - moments.x;
float p_max = variance / (variance + d*d);
return p_max;
}
vec4 shadowmap(in vec4 peye, in vec4 neye) {
float shadowFactor = 0.0;
vec3 fragment = vec3(peye);
int total_casters = 0;
for (int i = 0; i < u_num_lights; i++) {
light_t light = u_lights[i];
float factor = 0.0;
if (light.type == LIGHT_DIRECTIONAL) {
// shadowFactor = chebyshevUpperBound(distance, light.dir);
} else if (light.type == LIGHT_POINT) {
total_casters++;
vec3 light_pos = (view * vec4(light.pos, 1.0)).xyz;
vec3 dir = light_pos - fragment;
vec4 sc = inv_view * vec4(dir, 0.0);
factor += chebyshevUpperBound(length(dir), -sc.xyz, i);
} else if (light.type == LIGHT_SPOT) {
// shadowFactor = chebyshevUpperBound(distance, light.pos);
2024-08-24 17:32:25 +00:00
}
2024-08-29 15:32:34 +00:00
shadowFactor += factor;
}
if (u_num_lights == 0) {
shadowFactor = 1.0;
} else {
shadowFactor /= total_casters;
}
return vec4(vec3(shadowFactor), 1.0);
}
vec4 shadowing() {
if (u_shadow_receiver) {
return shadowmap(vpeye, vneye);
} else {
return vec4(1.0);
}
}