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-11-15 13:34:51 +01:00
|
|
|
#ifdef _CSM
|
|
|
|
uniform vec4 casData[shadowmapCascades * 4 + 4];
|
|
|
|
#endif
|
|
|
|
|
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-11-20 12:38:35 +01:00
|
|
|
float shadowLerp(const vec2 uv, const float compare, const vec2 smSize){
|
2017-11-15 13:34:51 +01:00
|
|
|
const vec2 texelSize = vec2(1.0) / smSize;
|
|
|
|
vec2 f = fract(uv * smSize + 0.5);
|
|
|
|
vec2 centroidUV = floor(uv * smSize + 0.5) / smSize;
|
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-11-20 12:38:35 +01:00
|
|
|
float PCF(const vec2 uv, const float compare, const vec2 smSize) {
|
|
|
|
float result = shadowLerp(uv + (vec2(-1.0, -1.0) / smSize), compare, smSize);
|
|
|
|
result += shadowLerp(uv + (vec2(-1.0, 0.0) / smSize), compare, smSize);
|
|
|
|
result += shadowLerp(uv + (vec2(-1.0, 1.0) / smSize), compare, smSize);
|
|
|
|
result += shadowLerp(uv + (vec2(0.0, -1.0) / smSize), compare, smSize);
|
|
|
|
result += shadowLerp(uv, compare, smSize);
|
|
|
|
result += shadowLerp(uv + (vec2(0.0, 1.0) / smSize), compare, smSize);
|
|
|
|
result += shadowLerp(uv + (vec2(1.0, -1.0) / smSize), compare, smSize);
|
|
|
|
result += shadowLerp(uv + (vec2(1.0, 0.0) / smSize), compare, smSize);
|
|
|
|
result += shadowLerp(uv + (vec2(1.0, 1.0) / smSize), compare, smSize);
|
2016-10-17 17:39:40 +02:00
|
|
|
return result / 9.0;
|
|
|
|
}
|
2017-03-17 18:34:03 +01:00
|
|
|
|
2017-11-15 16:43:46 +01:00
|
|
|
float lpToDepth(vec3 lp, const vec2 lightProj) {
|
2017-03-18 18:44:21 +01:00
|
|
|
lp = abs(lp);
|
|
|
|
float zcomp = max(lp.x, max(lp.y, lp.z));
|
2017-11-15 16:43:46 +01:00
|
|
|
zcomp = lightProj.x - lightProj.y / zcomp;
|
2017-03-18 18:44:21 +01:00
|
|
|
return zcomp * 0.5 + 0.5;
|
|
|
|
}
|
|
|
|
|
2017-11-15 16:43:46 +01:00
|
|
|
float PCFCube(const vec3 lp, vec3 ml, const float bias, const vec2 lightProj, const vec3 n) {
|
|
|
|
// return float(texture(shadowMapCube, ml).r + bias > lpToDepth(lp, lightProj));
|
2017-10-31 22:49:48 +01:00
|
|
|
const float s = shadowmapCubePcfSize; // 0.001 TODO: incorrect...
|
2017-11-15 16:43:46 +01:00
|
|
|
// float compare = lpToDepth(lp, lightProj) - bias;
|
|
|
|
float compare = lpToDepth(lp - n * 0.003, lightProj) - bias * 0.4;
|
|
|
|
ml = ml + n * 0.003;
|
|
|
|
float result = step(compare, texture(shadowMapCube, ml).r);
|
2017-03-18 18:44:21 +01:00
|
|
|
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
|
|
|
}
|
2017-11-15 13:34:51 +01:00
|
|
|
|
2017-11-20 12:38:35 +01:00
|
|
|
float shadowTest(const vec3 lPos, const float shadowsBias, const vec2 smSize) {
|
2017-11-15 13:34:51 +01:00
|
|
|
|
|
|
|
// float cosAngle = max(1.0 - dotNL, 0.0);
|
|
|
|
// vec3 noff = n * shadowsBias * cosAngle;
|
|
|
|
// vec4 lPos = LWVP * vec4(p + noff, 1.0);
|
|
|
|
|
|
|
|
// Out of bounds
|
|
|
|
if (lPos.x < 0.0 || lPos.y < 0.0 || lPos.x > 1.0 || lPos.y > 1.0) return 1.0;
|
|
|
|
|
2017-11-20 12:38:35 +01:00
|
|
|
return PCF(lPos.xy, lPos.z - shadowsBias, smSize);
|
2017-11-15 13:34:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _CSM
|
|
|
|
mat4 getCascadeMat(const float d, out int casi, out int casIndex) {
|
|
|
|
const int c = shadowmapCascades;
|
|
|
|
|
|
|
|
// Get cascade index
|
|
|
|
// TODO: use bounding box slice selection instead of sphere
|
|
|
|
const vec4 ci = vec4(float(c > 0), float(c > 1), float(c > 2), float(c > 3));
|
|
|
|
// int ci;
|
|
|
|
// if (d < casData[c * 4].x) ci = 0;
|
|
|
|
// else if (d < casData[c * 4].y) ci = 1 * 4;
|
|
|
|
// else if (d < casData[c * 4].z) ci = 2 * 4;
|
|
|
|
// else ci = 3 * 4;
|
|
|
|
// Splits
|
|
|
|
vec4 comp = vec4(
|
|
|
|
float(d > casData[c * 4].x),
|
|
|
|
float(d > casData[c * 4].y),
|
|
|
|
float(d > casData[c * 4].z),
|
|
|
|
float(d > casData[c * 4].w));
|
|
|
|
casi = int(min(dot(ci, comp), c));
|
|
|
|
|
|
|
|
// Get cascade mat
|
|
|
|
casIndex = casi * 4;
|
|
|
|
return mat4(
|
|
|
|
casData[casIndex + 0],
|
|
|
|
casData[casIndex + 1],
|
|
|
|
casData[casIndex + 2],
|
|
|
|
casData[casIndex + 3]);
|
|
|
|
}
|
|
|
|
|
2017-11-20 12:38:35 +01:00
|
|
|
float shadowTestCascade(const vec3 eye, const vec3 p, const float shadowsBias, const vec2 smSize) {
|
2017-11-15 13:34:51 +01:00
|
|
|
const int c = shadowmapCascades;
|
|
|
|
float d = distance(eye, p);
|
|
|
|
|
|
|
|
int casi;
|
|
|
|
int casIndex;
|
|
|
|
mat4 LWVP = getCascadeMat(d, casi, casIndex);
|
|
|
|
|
|
|
|
vec4 lPos = LWVP * vec4(p, 1.0);
|
|
|
|
float visibility = 1.0;
|
2017-11-20 12:38:35 +01:00
|
|
|
if (lPos.w > 0.0) visibility = shadowTest(lPos.xyz / lPos.w, shadowsBias, smSize);
|
2017-11-15 13:34:51 +01:00
|
|
|
|
|
|
|
// Blend cascade
|
|
|
|
// https://github.com/TheRealMJP/Shadows
|
|
|
|
const float blendThres = 0.15;
|
|
|
|
float nextSplit = casData[c * 4][casi];
|
|
|
|
float splitSize = casi == 0 ? nextSplit : nextSplit - casData[c * 4][casi - 1];
|
|
|
|
float splitDist = (nextSplit - d) / splitSize;
|
|
|
|
if (splitDist <= blendThres && casi != c - 1) {
|
|
|
|
int casIndex2 = casIndex + 4;
|
|
|
|
mat4 LWVP2 = mat4(
|
|
|
|
casData[casIndex2 + 0],
|
|
|
|
casData[casIndex2 + 1],
|
|
|
|
casData[casIndex2 + 2],
|
|
|
|
casData[casIndex2 + 3]);
|
|
|
|
|
|
|
|
vec4 lPos2 = LWVP2 * vec4(p, 1.0);
|
|
|
|
float visibility2 = 1.0;
|
2017-11-20 12:38:35 +01:00
|
|
|
if (lPos2.w > 0.0) visibility2 = shadowTest(lPos2.xyz / lPos2.w, shadowsBias, smSize);
|
2017-11-15 13:34:51 +01:00
|
|
|
|
|
|
|
float lerpAmt = smoothstep(0.0, blendThres, splitDist);
|
|
|
|
return mix(visibility2, visibility, lerpAmt);
|
|
|
|
}
|
|
|
|
return visibility;
|
|
|
|
|
|
|
|
// Visualize cascades
|
|
|
|
// if (ci == 0) albedo.rgb = vec3(1.0, 0.0, 0.0);
|
|
|
|
// if (ci == 4) albedo.rgb = vec3(0.0, 1.0, 0.0);
|
|
|
|
// if (ci == 8) albedo.rgb = vec3(0.0, 0.0, 1.0);
|
|
|
|
// if (ci == 12) albedo.rgb = vec3(1.0, 1.0, 0.0);
|
|
|
|
}
|
|
|
|
#endif
|