armory/Shaders/std/shadows.glsl

70 lines
2.9 KiB
Text
Raw Normal View History

2016-10-17 17:39:40 +02:00
#include "../compiled.glsl"
uniform sampler2D shadowMap;
2017-03-17 18:34:03 +01:00
uniform samplerCube shadowMapCube;
2016-10-17 17:39:40 +02:00
2017-03-18 18:44:21 +01:00
float shadowCompare(const vec2 uv, const float compare){
2017-02-28 14:13:50 +01:00
float depth = texture(shadowMap, uv).r;
2016-10-17 17:39:40 +02:00
return step(compare, depth);
}
2017-03-18 18:44:21 +01:00
float shadowLerp(const vec2 uv, const float compare){
2016-10-17 17:39:40 +02:00
const vec2 texelSize = vec2(1.0) / shadowmapSize;
vec2 f = fract(uv * shadowmapSize + 0.5);
vec2 centroidUV = floor(uv * shadowmapSize + 0.5) / shadowmapSize;
2017-03-18 18:44:21 +01:00
float lb = shadowCompare(centroidUV, compare);
float lt = shadowCompare(centroidUV + texelSize * vec2(0.0, 1.0), compare);
float rb = shadowCompare(centroidUV + texelSize * vec2(1.0, 0.0), compare);
float rt = shadowCompare(centroidUV + texelSize, compare);
2016-10-17 17:39:40 +02:00
float a = mix(lb, lt, f.y);
float b = mix(rb, rt, f.y);
float c = mix(a, b, f.x);
return c;
}
2017-03-17 18:34:03 +01:00
float PCF(const vec2 uv, const float compare) {
2016-10-17 17:39:40 +02:00
// float result = 0.0;
// for (int x = -1; x <= 1; x++){
// for(int y = -1; y <= 1; y++){
// vec2 off = vec2(x, y) / shadowmapSize;
2017-03-18 18:44:21 +01:00
// result += shadowLerp(shadowmapSize, uv + off, compare);
float result = shadowLerp(uv + (vec2(-1.0, -1.0) / shadowmapSize), compare);
result += shadowLerp(uv + (vec2(-1.0, 0.0) / shadowmapSize), compare);
result += shadowLerp(uv + (vec2(-1.0, 1.0) / shadowmapSize), compare);
result += shadowLerp(uv + (vec2(0.0, -1.0) / shadowmapSize), compare);
result += shadowLerp(uv, compare);
result += shadowLerp(uv + (vec2(0.0, 1.0) / shadowmapSize), compare);
result += shadowLerp(uv + (vec2(1.0, -1.0) / shadowmapSize), compare);
result += shadowLerp(uv + (vec2(1.0, 0.0) / shadowmapSize), compare);
result += shadowLerp(uv + (vec2(1.0, 1.0) / shadowmapSize), compare);
2016-10-17 17:39:40 +02:00
// }
// }
return result / 9.0;
}
2017-03-17 18:34:03 +01:00
2017-03-18 18:44:21 +01:00
float lpToDepth(vec3 lp, const vec2 lightPlane) {
float d = lightPlane.y - lightPlane.x;
lp = abs(lp);
float zcomp = max(lp.x, max(lp.y, lp.z));
zcomp = (lightPlane.y + lightPlane.x) / (d) - (2.0 * lightPlane.y * lightPlane.x) / (d) / zcomp;
return zcomp * 0.5 + 0.5;
}
float PCFCube(const vec3 lp, const vec3 ml, const float bias, const vec2 lightPlane) {
// return float(texture(shadowMapCube, ml).r + bias > lpToDepth(lp));
const float s = 0.001; // TODO: incorrect...
float compare = lpToDepth(lp, lightPlane) - bias;
float result = step(compare, texture(shadowMapCube, ml).r);
result += step(compare, texture(shadowMapCube, ml + vec3(s, s, s)).r);
result += step(compare, texture(shadowMapCube, ml + vec3(-s, s, s)).r);
result += step(compare, texture(shadowMapCube, ml + vec3(s, -s, s)).r);
result += step(compare, texture(shadowMapCube, ml + vec3(s, s, -s)).r);
result += step(compare, texture(shadowMapCube, ml + vec3(-s, -s, s)).r);
result += step(compare, texture(shadowMapCube, ml + vec3(s, -s, -s)).r);
result += step(compare, texture(shadowMapCube, ml + vec3(-s, s, -s)).r);
result += step(compare, texture(shadowMapCube, ml + vec3(-s, -s, -s)).r);
result /= 9.0;
return result;
2017-03-17 18:34:03 +01:00
}