1
0
Fork 0

Tweak shadow offset parameters and cascade ranges

This should hopefully fix some shadow acne edge cases.
This commit is contained in:
Fernando García Liñán 2021-01-05 11:24:19 +01:00
parent a5e8ffa450
commit 21a6e20701
3 changed files with 19 additions and 21 deletions

View file

@ -43,7 +43,7 @@
<pass n="1" include="csm-pass.xml">
<name>csm1</name>
<near-m>3.0</near-m>
<far-m>50.0</far-m>
<far-m>70.0</far-m>
<viewport>
<x>0.5</x>
<y>0.0</y>
@ -53,8 +53,8 @@
</pass>
<pass n="2" include="csm-pass.xml">
<name>csm2</name>
<near-m>50.0</near-m>
<far-m>400.0</far-m>
<near-m>70.0</near-m>
<far-m>300.0</far-m>
<viewport>
<x>0.0</x>
<y>0.5</y>
@ -64,8 +64,8 @@
</pass>
<pass n="3" include="csm-pass.xml">
<name>csm3</name>
<near-m>400.0</near-m>
<far-m>1500.0</far-m>
<near-m>300.0</near-m>
<far-m>1000.0</far-m>
<viewport>
<x>0.5</x>
<y>0.5</y>

View file

@ -9,7 +9,7 @@ varying vec4 lightSpacePos[4];
const bool DEBUG_CASCADES = false;
const float DEPTH_BIAS = 1.5;
const float DEPTH_BIAS = 2.0;
// Ideally these should be passed as an uniform, but we don't support uniform
// arrays yet

View file

@ -1,7 +1,6 @@
#version 120
uniform bool shadows_enabled;
uniform int sun_atlas_size;
uniform mat4 fg_LightMatrix_csm0;
uniform mat4 fg_LightMatrix_csm1;
@ -10,8 +9,7 @@ uniform mat4 fg_LightMatrix_csm3;
varying vec4 lightSpacePos[4];
const float NORMAL_OFFSET_SCALE = 200.0;
const float NORMAL_OFFSET_SCALES[4] = float[4](0.0, 0.1, 0.3, 1.0);
void setupShadows(vec4 eyeSpacePos)
{
@ -23,16 +21,18 @@ void setupShadows(vec4 eyeSpacePos)
vec3 toLight = normalize(gl_LightSource[0].position.xyz);
float costheta = dot(normal, toLight);
float slopeScale = clamp(1.0 - costheta, 0.0, 1.0);
float texelSize = 1.0 / sun_atlas_size;
float normalOffset = NORMAL_OFFSET_SCALE * slopeScale * texelSize;
vec4 offsetPos = eyeSpacePos + vec4(normal * normalOffset, 0.0);
vec4 offsetPos[4];
for (int i = 0; i < 4; i++) {
float normalOffset = NORMAL_OFFSET_SCALES[i] * slopeScale;
offsetPos[i] = eyeSpacePos + vec4(normal * normalOffset, 0.0);
}
vec4 offsets[4];
offsets[0] = fg_LightMatrix_csm0 * offsetPos;
offsets[1] = fg_LightMatrix_csm1 * offsetPos;
offsets[2] = fg_LightMatrix_csm2 * offsetPos;
offsets[3] = fg_LightMatrix_csm3 * offsetPos;
vec4 offsetPosLightSpace[4];
offsetPosLightSpace[0] = fg_LightMatrix_csm0 * offsetPos[0];
offsetPosLightSpace[1] = fg_LightMatrix_csm1 * offsetPos[1];
offsetPosLightSpace[2] = fg_LightMatrix_csm2 * offsetPos[2];
offsetPosLightSpace[3] = fg_LightMatrix_csm3 * offsetPos[3];
lightSpacePos[0] = fg_LightMatrix_csm0 * eyeSpacePos;
lightSpacePos[1] = fg_LightMatrix_csm1 * eyeSpacePos;
@ -40,8 +40,6 @@ void setupShadows(vec4 eyeSpacePos)
lightSpacePos[3] = fg_LightMatrix_csm3 * eyeSpacePos;
// Offset only in UV space
lightSpacePos[0].xy = offsets[0].xy;
lightSpacePos[1].xy = offsets[1].xy;
lightSpacePos[2].xy = offsets[2].xy;
lightSpacePos[3].xy = offsets[3].xy;
for (int i = 0; i < 4; i++)
lightSpacePos[i].xy = offsetPosLightSpace[i].xy;
}