1
0
Fork 0

Decrease shadow mapping bias and better 3rd cascade depth range

This commit is contained in:
Fernando García Liñán 2020-12-06 14:42:26 +01:00
parent 3e3593119c
commit 00b465b84f
3 changed files with 15 additions and 12 deletions

View file

@ -54,7 +54,7 @@
<pass n="2" include="csm-pass.xml"> <pass n="2" include="csm-pass.xml">
<name>csm2</name> <name>csm2</name>
<near-m>50.0</near-m> <near-m>50.0</near-m>
<far-m>150.0</far-m> <far-m>400.0</far-m>
<viewport> <viewport>
<x>0.0</x> <x>0.0</x>
<y>0.5</y> <y>0.5</y>
@ -64,7 +64,7 @@
</pass> </pass>
<pass n="3" include="csm-pass.xml"> <pass n="3" include="csm-pass.xml">
<name>csm3</name> <name>csm3</name>
<near-m>150.0</near-m> <near-m>400.0</near-m>
<far-m>1500.0</far-m> <far-m>1500.0</far-m>
<viewport> <viewport>
<x>0.5</x> <x>0.5</x>

View file

@ -9,7 +9,7 @@ varying vec4 lightSpacePos[4];
const bool DEBUG_CASCADES = false; const bool DEBUG_CASCADES = false;
const float depth_bias = 2.0; const float DEPTH_BIAS = 1.5;
// Ideally these should be passed as an uniform, but we don't support uniform // Ideally these should be passed as an uniform, but we don't support uniform
// arrays yet // arrays yet
@ -22,10 +22,10 @@ const vec2 uv_factor = vec2(0.5, 0.5);
float debugCascade(int cascade) float debugCascade(int cascade)
{ {
const mat2 bayer_matrix = mat2(0, 3, 2, 1); const mat2 bayer_matrix = mat2(0, 3, 2, 1);
const float scale = 1.0; const float scale = 0.2;
vec2 coords = mod(gl_FragCoord.xy * scale, 2.0); vec2 coords = mod(gl_FragCoord.xy * scale, 2.0);
int threshold = int(bayer_matrix[int(coords.y)][int(coords.x)]); int threshold = int(bayer_matrix[int(coords.x)][int(coords.y)]);
if (threshold <= cascade) if (threshold < cascade)
return 0.0; return 0.0;
return 1.0; return 1.0;
} }
@ -41,7 +41,7 @@ float sampleOffset(vec4 pos, vec2 offset, vec2 invTexelSize)
return shadow2DProj( return shadow2DProj(
shadow_tex, vec4( shadow_tex, vec4(
pos.xy + offset * invTexelSize * pos.w, pos.xy + offset * invTexelSize * pos.w,
pos.z - depth_bias * invTexelSize.x, pos.z - DEPTH_BIAS * invTexelSize.x,
pos.w)).r; pos.w)).r;
} }
@ -118,17 +118,18 @@ float getShadowing()
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
if (checkWithinBounds(lightSpacePos[i].xy, vec2(0.0), vec2(1.0)) > 0.0 && if (checkWithinBounds(lightSpacePos[i].xy, vec2(0.0), vec2(1.0)) > 0.0 &&
(lightSpacePos[i].z / lightSpacePos[i].w) <= 1.0) { (lightSpacePos[i].z / lightSpacePos[i].w) <= 1.0) {
float debug_value = 0.0;
if (DEBUG_CASCADES) if (DEBUG_CASCADES)
return debugCascade(i); debug_value = debugCascade(i);
if (checkWithinBounds(lightSpacePos[i].xy, bandBottomLeft, bandTopRight) < 1.0) { if (checkWithinBounds(lightSpacePos[i].xy, bandBottomLeft, bandTopRight) < 1.0) {
vec2 s = vec2 s =
smoothstep(vec2(0.0), bandBottomLeft, lightSpacePos[i].xy) - smoothstep(vec2(0.0), bandBottomLeft, lightSpacePos[i].xy) -
smoothstep(bandTopRight, vec2(1.0), lightSpacePos[i].xy); smoothstep(bandTopRight, vec2(1.0), lightSpacePos[i].xy);
float blend = 1.0 - s.x * s.y; float blend = 1.0 - s.x * s.y;
return mix(sampleShadowMap(i), sampleShadowMap(i+1), blend); return clamp(mix(sampleShadowMap(i), sampleShadowMap(i+1), blend) - debug_value, 0.0, 1.0);
} }
return sampleShadowMap(i); return clamp(sampleShadowMap(i) - debug_value, 0.0, 1.0);
} }
} }

View file

@ -1,6 +1,7 @@
#version 120 #version 120
uniform bool shadows_enabled; uniform bool shadows_enabled;
uniform int sun_atlas_size;
uniform mat4 fg_LightMatrix_csm0; uniform mat4 fg_LightMatrix_csm0;
uniform mat4 fg_LightMatrix_csm1; uniform mat4 fg_LightMatrix_csm1;
@ -9,7 +10,7 @@ uniform mat4 fg_LightMatrix_csm3;
varying vec4 lightSpacePos[4]; varying vec4 lightSpacePos[4];
const float normal_offset_scale = 0.1; const float NORMAL_OFFSET_SCALE = 200.0;
void setupShadows(vec4 eyeSpacePos) void setupShadows(vec4 eyeSpacePos)
@ -22,7 +23,8 @@ void setupShadows(vec4 eyeSpacePos)
vec3 toLight = normalize(gl_LightSource[0].position.xyz); vec3 toLight = normalize(gl_LightSource[0].position.xyz);
float costheta = dot(normal, toLight); float costheta = dot(normal, toLight);
float slopeScale = clamp(1.0 - costheta, 0.0, 1.0); float slopeScale = clamp(1.0 - costheta, 0.0, 1.0);
float normalOffset = normal_offset_scale * slopeScale; float texelSize = 1.0 / sun_atlas_size;
float normalOffset = NORMAL_OFFSET_SCALE * slopeScale * texelSize;
vec4 offsetPos = eyeSpacePos + vec4(normal * normalOffset, 0.0); vec4 offsetPos = eyeSpacePos + vec4(normal * normalOffset, 0.0);