Adjust shadowmap distortion to use entire SM texture (#12166)

This commit is contained in:
x2048 2022-04-07 22:13:50 +02:00 committed by GitHub
parent 0b5b2b2633
commit 48f7c5603e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 159 additions and 115 deletions

@ -17,6 +17,7 @@ uniform float animationTimer;
uniform mat4 m_ShadowViewProj; uniform mat4 m_ShadowViewProj;
uniform float f_shadowfar; uniform float f_shadowfar;
uniform float f_shadow_strength; uniform float f_shadow_strength;
uniform vec4 CameraPos;
varying float normalOffsetScale; varying float normalOffsetScale;
varying float adj_shadow_strength; varying float adj_shadow_strength;
varying float cosLight; varying float cosLight;
@ -53,12 +54,13 @@ uniform float zPerspectiveBias;
vec4 getPerspectiveFactor(in vec4 shadowPosition) vec4 getPerspectiveFactor(in vec4 shadowPosition)
{ {
vec2 s = vec2(shadowPosition.x > CameraPos.x ? 1.0 : -1.0, shadowPosition.y > CameraPos.y ? 1.0 : -1.0);
float pDistance = length(shadowPosition.xy); vec2 l = s * (shadowPosition.xy - CameraPos.xy) / (1.0 - s * CameraPos.xy);
float pDistance = length(l);
float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1; float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
l /= pFactor;
shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPerspectiveBias); shadowPosition.xy = CameraPos.xy * (1.0 - l) + s * l;
shadowPosition.z *= zPerspectiveBias;
return shadowPosition; return shadowPosition;
} }
@ -171,13 +173,13 @@ float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDis
float getBaseLength(vec2 smTexCoord) float getBaseLength(vec2 smTexCoord)
{ {
float l = length(2.0 * smTexCoord.xy - 1.0); // length in texture coords float l = length(2.0 * smTexCoord.xy - 1.0 - CameraPos.xy); // length in texture coords
return xyPerspectiveBias1 / (1.0 / l - xyPerspectiveBias0); // return to undistorted coords return xyPerspectiveBias1 / (1.0 / l - xyPerspectiveBias0); // return to undistorted coords
} }
float getDeltaPerspectiveFactor(float l) float getDeltaPerspectiveFactor(float l)
{ {
return 0.1 / (xyPerspectiveBias0 * l + xyPerspectiveBias1); // original distortion factor, divided by 10 return 0.04 * pow(512.0 / f_textureresolution, 0.4) / (xyPerspectiveBias0 * l + xyPerspectiveBias1); // original distortion factor, divided by 10
} }
float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDistance, float multiplier) float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDistance, float multiplier)
@ -185,7 +187,6 @@ float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDist
float baseLength = getBaseLength(smTexCoord); float baseLength = getBaseLength(smTexCoord);
float perspectiveFactor; float perspectiveFactor;
if (PCFBOUND == 0.0) return 0.0;
// Return fast if sharp shadows are requested // Return fast if sharp shadows are requested
if (PCFBOUND == 0.0) if (PCFBOUND == 0.0)
return 0.0; return 0.0;
@ -489,7 +490,9 @@ void main(void)
vec3 shadow_color = vec3(0.0, 0.0, 0.0); vec3 shadow_color = vec3(0.0, 0.0, 0.0);
vec3 posLightSpace = getLightSpacePosition(); vec3 posLightSpace = getLightSpacePosition();
float distance_rate = (1 - pow(clamp(2.0 * length(posLightSpace.xy - 0.5),0.0,1.0), 50.0)); float distance_rate = (1.0 - pow(clamp(2.0 * length(posLightSpace.xy - 0.5),0.0,1.0), 10.0));
if (max(abs(posLightSpace.x - 0.5), abs(posLightSpace.y - 0.5)) > 0.5)
distance_rate = 0.0;
float f_adj_shadow_strength = max(adj_shadow_strength-mtsmoothstep(0.9,1.1, posLightSpace.z),0.0); float f_adj_shadow_strength = max(adj_shadow_strength-mtsmoothstep(0.9,1.1, posLightSpace.z),0.0);
if (distance_rate > 1e-7) { if (distance_rate > 1e-7) {

@ -6,26 +6,9 @@ uniform vec4 skyBgColor;
uniform float fogDistance; uniform float fogDistance;
uniform vec3 eyePosition; uniform vec3 eyePosition;
varying vec3 vNormal; // The cameraOffset is the current center of the visible world.
varying vec3 vPosition; uniform vec3 cameraOffset;
varying vec3 worldPosition; uniform float animationTimer;
varying lowp vec4 varColor;
#ifdef GL_ES
varying mediump vec2 varTexCoord;
#else
centroid varying vec2 varTexCoord;
#endif
varying vec3 eyeVec;
varying float nightRatio;
varying float vIDiff;
const float e = 2.718281828459;
const float BS = 10.0;
const float fogStart = FOG_START;
const float fogShadingParameter = 1.0 / (1.0 - fogStart);
#ifdef ENABLE_DYNAMIC_SHADOWS #ifdef ENABLE_DYNAMIC_SHADOWS
// shadow texture // shadow texture
uniform sampler2D ShadowMapSampler; uniform sampler2D ShadowMapSampler;
@ -34,14 +17,39 @@ const float fogShadingParameter = 1.0 / (1.0 - fogStart);
uniform float f_textureresolution; uniform float f_textureresolution;
uniform mat4 m_ShadowViewProj; uniform mat4 m_ShadowViewProj;
uniform float f_shadowfar; uniform float f_shadowfar;
uniform float f_timeofday;
uniform float f_shadow_strength; uniform float f_shadow_strength;
uniform vec4 CameraPos;
varying float normalOffsetScale; varying float normalOffsetScale;
varying float adj_shadow_strength; varying float adj_shadow_strength;
varying float cosLight; varying float cosLight;
varying float f_normal_length; varying float f_normal_length;
#endif #endif
varying vec3 vNormal;
varying vec3 vPosition;
// World position in the visible world (i.e. relative to the cameraOffset.)
// This can be used for many shader effects without loss of precision.
// If the absolute position is required it can be calculated with
// cameraOffset + worldPosition (for large coordinates the limits of float
// precision must be considered).
varying vec3 worldPosition;
varying lowp vec4 varColor;
#ifdef GL_ES
varying mediump vec2 varTexCoord;
#else
centroid varying vec2 varTexCoord;
#endif
varying vec3 eyeVec;
varying float nightRatio;
varying float vIDiff;
const float fogStart = FOG_START;
const float fogShadingParameter = 1.0 / (1.0 - fogStart);
#ifdef ENABLE_DYNAMIC_SHADOWS #ifdef ENABLE_DYNAMIC_SHADOWS
uniform float xyPerspectiveBias0; uniform float xyPerspectiveBias0;
uniform float xyPerspectiveBias1; uniform float xyPerspectiveBias1;
@ -49,15 +57,22 @@ uniform float zPerspectiveBias;
vec4 getPerspectiveFactor(in vec4 shadowPosition) vec4 getPerspectiveFactor(in vec4 shadowPosition)
{ {
vec2 s = vec2(shadowPosition.x > CameraPos.x ? 1.0 : -1.0, shadowPosition.y > CameraPos.y ? 1.0 : -1.0);
float pDistance = length(shadowPosition.xy); vec2 l = s * (shadowPosition.xy - CameraPos.xy) / (1.0 - s * CameraPos.xy);
float pDistance = length(l);
float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1; float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
l /= pFactor;
shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPerspectiveBias); shadowPosition.xy = CameraPos.xy * (1.0 - l) + s * l;
shadowPosition.z *= zPerspectiveBias;
return shadowPosition; return shadowPosition;
} }
// assuming near is always 1.0
float getLinearDepth()
{
return 2.0 * f_shadowfar / (f_shadowfar + 1.0 - (2.0 * gl_FragCoord.z - 1.0) * (f_shadowfar - 1.0));
}
vec3 getLightSpacePosition() vec3 getLightSpacePosition()
{ {
vec4 pLightSpace; vec4 pLightSpace;
@ -161,13 +176,13 @@ float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDis
float getBaseLength(vec2 smTexCoord) float getBaseLength(vec2 smTexCoord)
{ {
float l = length(2.0 * smTexCoord.xy - 1.0); // length in texture coords float l = length(2.0 * smTexCoord.xy - 1.0 - CameraPos.xy); // length in texture coords
return xyPerspectiveBias1 / (1.0 / l - xyPerspectiveBias0); // return to undistorted coords return xyPerspectiveBias1 / (1.0 / l - xyPerspectiveBias0); // return to undistorted coords
} }
float getDeltaPerspectiveFactor(float l) float getDeltaPerspectiveFactor(float l)
{ {
return 0.1 / (xyPerspectiveBias0 * l + xyPerspectiveBias1); // original distortion factor, divided by 10 return 0.04 * pow(512.0 / f_textureresolution, 0.4) / (xyPerspectiveBias0 * l + xyPerspectiveBias1); // original distortion factor, divided by 10
} }
float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDistance, float multiplier) float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDistance, float multiplier)
@ -418,6 +433,7 @@ float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
#endif #endif
#if ENABLE_TONE_MAPPING #if ENABLE_TONE_MAPPING
/* Hable's UC2 Tone mapping parameters /* Hable's UC2 Tone mapping parameters
A = 0.22; A = 0.22;
B = 0.30; B = 0.30;
@ -448,12 +464,14 @@ vec4 applyToneMapping(vec4 color)
} }
#endif #endif
void main(void) void main(void)
{ {
vec3 color; vec3 color;
vec2 uv = varTexCoord.st; vec2 uv = varTexCoord.st;
vec4 base = texture2D(baseTexture, uv).rgba;
vec4 base = texture2D(baseTexture, uv).rgba;
// If alpha is zero, we can just discard the pixel. This fixes transparency // If alpha is zero, we can just discard the pixel. This fixes transparency
// on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa, // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa,
// and also on GLES 2, where GL_ALPHA_TEST is missing entirely. // and also on GLES 2, where GL_ALPHA_TEST is missing entirely.
@ -467,8 +485,7 @@ void main(void)
#endif #endif
color = base.rgb; color = base.rgb;
vec4 col = vec4(color.rgb, base.a); vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
col.rgb *= varColor.rgb;
col.rgb *= vIDiff; col.rgb *= vIDiff;
#ifdef ENABLE_DYNAMIC_SHADOWS #ifdef ENABLE_DYNAMIC_SHADOWS
@ -477,7 +494,9 @@ void main(void)
vec3 shadow_color = vec3(0.0, 0.0, 0.0); vec3 shadow_color = vec3(0.0, 0.0, 0.0);
vec3 posLightSpace = getLightSpacePosition(); vec3 posLightSpace = getLightSpacePosition();
float distance_rate = (1 - pow(clamp(2.0 * length(posLightSpace.xy - 0.5),0.0,1.0), 50.0)); float distance_rate = (1.0 - pow(clamp(2.0 * length(posLightSpace.xy - 0.5),0.0,1.0), 10.0));
if (max(abs(posLightSpace.x - 0.5), abs(posLightSpace.y - 0.5)) > 0.5)
distance_rate = 0.0;
float f_adj_shadow_strength = max(adj_shadow_strength-mtsmoothstep(0.9,1.1, posLightSpace.z),0.0); float f_adj_shadow_strength = max(adj_shadow_strength-mtsmoothstep(0.9,1.1, posLightSpace.z),0.0);
if (distance_rate > 1e-7) { if (distance_rate > 1e-7) {
@ -506,8 +525,8 @@ void main(void)
// Power ratio was measured on torches in MTG (brightness = 14). // Power ratio was measured on torches in MTG (brightness = 14).
float adjusted_night_ratio = pow(max(0.0, nightRatio), 0.6); float adjusted_night_ratio = pow(max(0.0, nightRatio), 0.6);
// cosine of the normal-to-light angle when // Apply self-shadowing when light falls at a narrow angle to the surface
// we start to apply self-shadowing // Cosine of the cut-off angle.
const float self_shadow_cutoff_cosine = 0.14; const float self_shadow_cutoff_cosine = 0.14;
if (f_normal_length != 0 && cosLight < self_shadow_cutoff_cosine) { if (f_normal_length != 0 && cosLight < self_shadow_cutoff_cosine) {
shadow_int = max(shadow_int, 1 - clamp(cosLight, 0.0, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine); shadow_int = max(shadow_int, 1 - clamp(cosLight, 0.0, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine);
@ -541,5 +560,7 @@ void main(void)
float clarity = clamp(fogShadingParameter float clarity = clamp(fogShadingParameter
- fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0); - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
col = mix(skyBgColor, col, clarity); col = mix(skyBgColor, col, clarity);
gl_FragColor = vec4(col.rgb, base.a); col = vec4(col.rgb, base.a);
gl_FragColor = col;
} }

@ -1,4 +1,5 @@
uniform mat4 LightMVP; // world matrix uniform mat4 LightMVP; // world matrix
uniform vec4 CameraPos;
varying vec4 tPos; varying vec4 tPos;
#ifdef COLORED_SHADOWS #ifdef COLORED_SHADOWS
varying vec3 varColor; varying vec3 varColor;
@ -10,10 +11,13 @@ uniform float zPerspectiveBias;
vec4 getPerspectiveFactor(in vec4 shadowPosition) vec4 getPerspectiveFactor(in vec4 shadowPosition)
{ {
float pDistance = length(shadowPosition.xy); vec2 s = vec2(shadowPosition.x > CameraPos.x ? 1.0 : -1.0, shadowPosition.y > CameraPos.y ? 1.0 : -1.0);
vec2 l = s * (shadowPosition.xy - CameraPos.xy) / (1.0 - s * CameraPos.xy);
float pDistance = length(l);
float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1; float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPerspectiveBias); l /= pFactor;
shadowPosition.xy = CameraPos.xy * (1.0 - l) + s * l;
shadowPosition.z *= zPerspectiveBias;
return shadowPosition; return shadowPosition;
} }

@ -1,4 +1,5 @@
uniform mat4 LightMVP; // world matrix uniform mat4 LightMVP; // world matrix
uniform vec4 CameraPos; // camera position
varying vec4 tPos; varying vec4 tPos;
uniform float xyPerspectiveBias0; uniform float xyPerspectiveBias0;
@ -7,10 +8,13 @@ uniform float zPerspectiveBias;
vec4 getPerspectiveFactor(in vec4 shadowPosition) vec4 getPerspectiveFactor(in vec4 shadowPosition)
{ {
float pDistance = length(shadowPosition.xy); vec2 s = vec2(shadowPosition.x > CameraPos.x ? 1.0 : -1.0, shadowPosition.y > CameraPos.y ? 1.0 : -1.0);
vec2 l = s * (shadowPosition.xy - CameraPos.xy) / (1.0 - s * CameraPos.xy);
float pDistance = length(l);
float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1; float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPerspectiveBias); l /= pFactor;
shadowPosition.xy = CameraPos.xy * (1.0 - l) + s * l;
shadowPosition.z *= zPerspectiveBias;
return shadowPosition; return shadowPosition;
} }

@ -862,20 +862,14 @@ void ClientMap::renderMapShadows(video::IVideoDriver *driver,
/* /*
Custom update draw list for the pov of shadow light. Custom update draw list for the pov of shadow light.
*/ */
void ClientMap::updateDrawListShadow(const v3f &shadow_light_pos, const v3f &shadow_light_dir, float shadow_range) void ClientMap::updateDrawListShadow(v3f shadow_light_pos, v3f shadow_light_dir, float radius, float length)
{ {
ScopeProfiler sp(g_profiler, "CM::updateDrawListShadow()", SPT_AVG); ScopeProfiler sp(g_profiler, "CM::updateDrawListShadow()", SPT_AVG);
const v3f camera_position = shadow_light_pos; v3s16 cam_pos_nodes = floatToInt(shadow_light_pos, BS);
const v3f camera_direction = shadow_light_dir;
// I "fake" fov just to avoid creating a new function to handle orthographic
// projection.
const f32 camera_fov = m_camera_fov * 1.9f;
v3s16 cam_pos_nodes = floatToInt(camera_position, BS);
v3s16 p_blocks_min; v3s16 p_blocks_min;
v3s16 p_blocks_max; v3s16 p_blocks_max;
getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max, shadow_range); getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max, radius + length);
std::vector<v2s16> blocks_in_range; std::vector<v2s16> blocks_in_range;
@ -889,10 +883,10 @@ void ClientMap::updateDrawListShadow(const v3f &shadow_light_pos, const v3f &sha
// they are not inside the light frustum and it creates glitches. // they are not inside the light frustum and it creates glitches.
// FIXME: This could be removed if we figure out why they are missing // FIXME: This could be removed if we figure out why they are missing
// from the light frustum. // from the light frustum.
for (auto &i : m_drawlist) { // for (auto &i : m_drawlist) {
i.second->refGrab(); // i.second->refGrab();
m_drawlist_shadow[i.first] = i.second; // m_drawlist_shadow[i.first] = i.second;
} // }
// Number of blocks currently loaded by the client // Number of blocks currently loaded by the client
u32 blocks_loaded = 0; u32 blocks_loaded = 0;
@ -919,23 +913,13 @@ void ClientMap::updateDrawListShadow(const v3f &shadow_light_pos, const v3f &sha
continue; continue;
} }
float range = shadow_range; v3f block_pos = intToFloat(block->getPos() * MAP_BLOCKSIZE, BS);
v3f projection = shadow_light_pos + shadow_light_dir * shadow_light_dir.dotProduct(block_pos - shadow_light_pos);
float d = 0.0; if (projection.getDistanceFrom(block_pos) > radius)
if (!isBlockInSight(block->getPos(), camera_position,
camera_direction, camera_fov, range, &d))
continue; continue;
blocks_in_range_with_mesh++; blocks_in_range_with_mesh++;
/*
Occlusion culling
*/
if (isBlockOccluded(block, cam_pos_nodes)) {
blocks_occlusion_culled++;
continue;
}
// This block is in range. Reset usage timer. // This block is in range. Reset usage timer.
block->resetUsageTimer(); block->resetUsageTimer();

@ -116,7 +116,7 @@ public:
void getBlocksInViewRange(v3s16 cam_pos_nodes, void getBlocksInViewRange(v3s16 cam_pos_nodes,
v3s16 *p_blocks_min, v3s16 *p_blocks_max, float range=-1.0f); v3s16 *p_blocks_min, v3s16 *p_blocks_max, float range=-1.0f);
void updateDrawList(); void updateDrawList();
void updateDrawListShadow(const v3f &shadow_light_pos, const v3f &shadow_light_dir, float shadow_range); void updateDrawListShadow(v3f shadow_light_pos, v3f shadow_light_dir, float radius, float length);
// Returns true if draw list needs updating before drawing the next frame. // Returns true if draw list needs updating before drawing the next frame.
bool needsUpdateDrawList() { return m_needs_update_drawlist; } bool needsUpdateDrawList() { return m_needs_update_drawlist; }
void renderMap(video::IVideoDriver* driver, s32 pass); void renderMap(video::IVideoDriver* driver, s32 pass);

@ -220,6 +220,7 @@ class MainShaderConstantSetter : public IShaderConstantSetter
CachedPixelShaderSetting<f32> m_shadow_strength; CachedPixelShaderSetting<f32> m_shadow_strength;
CachedPixelShaderSetting<f32> m_time_of_day; CachedPixelShaderSetting<f32> m_time_of_day;
CachedPixelShaderSetting<f32> m_shadowfar; CachedPixelShaderSetting<f32> m_shadowfar;
CachedPixelShaderSetting<f32, 4> m_camera_pos;
CachedPixelShaderSetting<s32> m_shadow_texture; CachedPixelShaderSetting<s32> m_shadow_texture;
CachedVertexShaderSetting<f32> m_perspective_bias0_vertex; CachedVertexShaderSetting<f32> m_perspective_bias0_vertex;
CachedPixelShaderSetting<f32> m_perspective_bias0_pixel; CachedPixelShaderSetting<f32> m_perspective_bias0_pixel;
@ -252,6 +253,7 @@ public:
, m_shadow_strength("f_shadow_strength") , m_shadow_strength("f_shadow_strength")
, m_time_of_day("f_timeofday") , m_time_of_day("f_timeofday")
, m_shadowfar("f_shadowfar") , m_shadowfar("f_shadowfar")
, m_camera_pos("CameraPos")
, m_shadow_texture("ShadowMapSampler") , m_shadow_texture("ShadowMapSampler")
, m_perspective_bias0_vertex("xyPerspectiveBias0") , m_perspective_bias0_vertex("xyPerspectiveBias0")
, m_perspective_bias0_pixel("xyPerspectiveBias0") , m_perspective_bias0_pixel("xyPerspectiveBias0")
@ -321,6 +323,10 @@ public:
f32 shadowFar = shadow->getMaxShadowFar(); f32 shadowFar = shadow->getMaxShadowFar();
m_shadowfar.set(&shadowFar, services); m_shadowfar.set(&shadowFar, services);
f32 cam_pos[4];
shadowViewProj.transformVect(cam_pos, light.getPlayerPos());
m_camera_pos.set(cam_pos, services);
// I dont like using this hardcoded value. maybe something like // I dont like using this hardcoded value. maybe something like
// MAX_TEXTURE - 1 or somthing like that?? // MAX_TEXTURE - 1 or somthing like that??
s32 TextureLayerID = 3; s32 TextureLayerID = 3;

@ -29,7 +29,6 @@ using m4f = core::matrix4;
void DirectionalLight::createSplitMatrices(const Camera *cam) void DirectionalLight::createSplitMatrices(const Camera *cam)
{ {
float radius;
v3f newCenter; v3f newCenter;
v3f look = cam->getDirection(); v3f look = cam->getDirection();
@ -42,17 +41,16 @@ void DirectionalLight::createSplitMatrices(const Camera *cam)
float sfFar = adjustDist(future_frustum.zFar, cam->getFovY()); float sfFar = adjustDist(future_frustum.zFar, cam->getFovY());
// adjusted camera positions // adjusted camera positions
v3f camPos2 = cam->getPosition(); v3f cam_pos_world = cam->getPosition();
v3f camPos = v3f(camPos2.X - cam->getOffset().X * BS, v3f cam_pos_scene = v3f(cam_pos_world.X - cam->getOffset().X * BS,
camPos2.Y - cam->getOffset().Y * BS, cam_pos_world.Y - cam->getOffset().Y * BS,
camPos2.Z - cam->getOffset().Z * BS); cam_pos_world.Z - cam->getOffset().Z * BS);
camPos += look * sfNear; cam_pos_scene += look * sfNear;
camPos2 += look * sfNear; cam_pos_world += look * sfNear;
// center point of light frustum // center point of light frustum
float end = sfNear + sfFar; v3f center_scene = cam_pos_scene + look * 0.35 * (sfFar - sfNear);
newCenter = camPos + look * (sfNear + 0.05f * end); v3f center_world = cam_pos_world + look * 0.35 * (sfFar - sfNear);
v3f world_center = camPos2 + look * (sfNear + 0.05f * end);
// Create a vector to the frustum far corner // Create a vector to the frustum far corner
const v3f &viewUp = cam->getCameraNode()->getUpVector(); const v3f &viewUp = cam->getCameraNode()->getUpVector();
@ -60,22 +58,21 @@ void DirectionalLight::createSplitMatrices(const Camera *cam)
v3f farCorner = (look + viewRight * tanFovX + viewUp * tanFovY).normalize(); v3f farCorner = (look + viewRight * tanFovX + viewUp * tanFovY).normalize();
// Compute the frustumBoundingSphere radius // Compute the frustumBoundingSphere radius
v3f boundVec = (camPos + farCorner * sfFar) - newCenter; v3f boundVec = (cam_pos_scene + farCorner * sfFar) - center_scene;
radius = boundVec.getLength(); float radius = boundVec.getLength();
// boundVec.getLength(); float length = radius * 3.0f;
float vvolume = radius; v3f eye_displacement = direction * length;
v3f frustumCenter = newCenter;
v3f eye_displacement = direction * vvolume;
// we must compute the viewmat with the position - the camera offset // we must compute the viewmat with the position - the camera offset
// but the future_frustum position must be the actual world position // but the future_frustum position must be the actual world position
v3f eye = frustumCenter - eye_displacement; v3f eye = center_scene - eye_displacement;
future_frustum.position = world_center - eye_displacement; future_frustum.player = cam_pos_scene;
future_frustum.length = vvolume; future_frustum.position = center_world - eye_displacement;
future_frustum.ViewMat.buildCameraLookAtMatrixLH(eye, frustumCenter, v3f(0.0f, 1.0f, 0.0f)); future_frustum.length = length;
future_frustum.ProjOrthMat.buildProjectionMatrixOrthoLH(future_frustum.length, future_frustum.radius = radius;
future_frustum.length, -future_frustum.length, future_frustum.ViewMat.buildCameraLookAtMatrixLH(eye, center_scene, v3f(0.0f, 1.0f, 0.0f));
future_frustum.length,false); future_frustum.ProjOrthMat.buildProjectionMatrixOrthoLH(radius, radius,
0.0f, length, false);
future_frustum.camera_offset = cam->getOffset(); future_frustum.camera_offset = cam->getOffset();
} }
@ -94,7 +91,7 @@ void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool fo
float zNear = cam->getCameraNode()->getNearValue(); float zNear = cam->getCameraNode()->getNearValue();
float zFar = getMaxFarValue(); float zFar = getMaxFarValue();
if (!client->getEnv().getClientMap().getControl().range_all) if (!client->getEnv().getClientMap().getControl().range_all)
zFar = MYMIN(zFar, client->getEnv().getClientMap().getControl().wanted_range * BS * 1.5); zFar = MYMIN(zFar, client->getEnv().getClientMap().getControl().wanted_range * BS);
/////////////////////////////////// ///////////////////////////////////
// update splits near and fars // update splits near and fars
@ -105,7 +102,7 @@ void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool fo
createSplitMatrices(cam); createSplitMatrices(cam);
// get the draw list for shadows // get the draw list for shadows
client->getEnv().getClientMap().updateDrawListShadow( client->getEnv().getClientMap().updateDrawListShadow(
getPosition(), getDirection(), future_frustum.length); getPosition(), getDirection(), future_frustum.radius, future_frustum.length);
should_update_map_shadow = true; should_update_map_shadow = true;
dirty = true; dirty = true;
@ -115,6 +112,7 @@ void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool fo
v3f rotated_offset; v3f rotated_offset;
shadow_frustum.ViewMat.rotateVect(rotated_offset, intToFloat(cam_offset - shadow_frustum.camera_offset, BS)); shadow_frustum.ViewMat.rotateVect(rotated_offset, intToFloat(cam_offset - shadow_frustum.camera_offset, BS));
shadow_frustum.ViewMat.setTranslation(shadow_frustum.ViewMat.getTranslation() + rotated_offset); shadow_frustum.ViewMat.setTranslation(shadow_frustum.ViewMat.getTranslation() + rotated_offset);
shadow_frustum.player += intToFloat(shadow_frustum.camera_offset - cam->getOffset(), BS);
shadow_frustum.camera_offset = cam_offset; shadow_frustum.camera_offset = cam_offset;
} }
} }
@ -139,6 +137,16 @@ v3f DirectionalLight::getPosition() const
return shadow_frustum.position; return shadow_frustum.position;
} }
v3f DirectionalLight::getPlayerPos() const
{
return shadow_frustum.player;
}
v3f DirectionalLight::getFuturePlayerPos() const
{
return future_frustum.player;
}
const m4f &DirectionalLight::getViewMatrix() const const m4f &DirectionalLight::getViewMatrix() const
{ {
return shadow_frustum.ViewMat; return shadow_frustum.ViewMat;

@ -29,12 +29,14 @@ class Client;
struct shadowFrustum struct shadowFrustum
{ {
float zNear{0.0f}; f32 zNear{0.0f};
float zFar{0.0f}; f32 zFar{0.0f};
float length{0.0f}; f32 length{0.0f};
f32 radius{0.0f};
core::matrix4 ProjOrthMat; core::matrix4 ProjOrthMat;
core::matrix4 ViewMat; core::matrix4 ViewMat;
v3f position; v3f position;
v3f player;
v3s16 camera_offset; v3s16 camera_offset;
}; };
@ -57,6 +59,8 @@ public:
return direction; return direction;
}; };
v3f getPosition() const; v3f getPosition() const;
v3f getPlayerPos() const;
v3f getFuturePlayerPos() const;
/// Gets the light's matrices. /// Gets the light's matrices.
const core::matrix4 &getViewMatrix() const; const core::matrix4 &getViewMatrix() const;

@ -158,7 +158,6 @@ void ShadowRenderer::setShadowIntensity(float shadow_intensity)
disable(); disable();
} }
void ShadowRenderer::addNodeToShadowList( void ShadowRenderer::addNodeToShadowList(
scene::ISceneNode *node, E_SHADOW_MODE shadowMode) scene::ISceneNode *node, E_SHADOW_MODE shadowMode)
{ {
@ -261,6 +260,7 @@ void ShadowRenderer::updateSMTextures()
cb->MaxFar = (f32)m_shadow_map_max_distance * BS; cb->MaxFar = (f32)m_shadow_map_max_distance * BS;
cb->PerspectiveBiasXY = getPerspectiveBiasXY(); cb->PerspectiveBiasXY = getPerspectiveBiasXY();
cb->PerspectiveBiasZ = getPerspectiveBiasZ(); cb->PerspectiveBiasZ = getPerspectiveBiasZ();
cb->CameraPos = light.getFuturePlayerPos();
} }
// set the Render Target // set the Render Target
@ -322,9 +322,10 @@ void ShadowRenderer::update(video::ITexture *outputTarget)
if (!m_shadow_node_array.empty() && !m_light_list.empty()) { if (!m_shadow_node_array.empty() && !m_light_list.empty()) {
for (DirectionalLight &light : m_light_list) { for (DirectionalLight &light : m_light_list) {
// Static shader values. // Static shader values for entities are set in updateSMTextures
m_shadow_depth_cb->MapRes = (f32)m_shadow_map_texture_size; // SM texture for entities is not updated incrementally and
m_shadow_depth_cb->MaxFar = (f32)m_shadow_map_max_distance * BS; // must by updated using current player position.
m_shadow_depth_entity_cb->CameraPos = light.getPlayerPos();
// render shadows for the n0n-map objects. // render shadows for the n0n-map objects.
m_driver->setRenderTarget(shadowMapTextureDynamicObjects, true, m_driver->setRenderTarget(shadowMapTextureDynamicObjects, true,

@ -26,6 +26,10 @@ void ShadowDepthShaderCB::OnSetConstants(
core::matrix4 lightMVP = driver->getTransform(video::ETS_PROJECTION); core::matrix4 lightMVP = driver->getTransform(video::ETS_PROJECTION);
lightMVP *= driver->getTransform(video::ETS_VIEW); lightMVP *= driver->getTransform(video::ETS_VIEW);
f32 cam_pos[4];
lightMVP.transformVect(cam_pos, CameraPos);
lightMVP *= driver->getTransform(video::ETS_WORLD); lightMVP *= driver->getTransform(video::ETS_WORLD);
m_light_mvp_setting.set(lightMVP.pointer(), services); m_light_mvp_setting.set(lightMVP.pointer(), services);
@ -39,4 +43,6 @@ void ShadowDepthShaderCB::OnSetConstants(
m_perspective_bias1.set(&bias1, services); m_perspective_bias1.set(&bias1, services);
f32 zbias = PerspectiveBiasZ; f32 zbias = PerspectiveBiasZ;
m_perspective_zbias.set(&zbias, services); m_perspective_zbias.set(&zbias, services);
m_cam_pos_setting.set(cam_pos, services);
} }

@ -33,7 +33,8 @@ public:
m_color_map_sampler_setting("ColorMapSampler"), m_color_map_sampler_setting("ColorMapSampler"),
m_perspective_bias0("xyPerspectiveBias0"), m_perspective_bias0("xyPerspectiveBias0"),
m_perspective_bias1("xyPerspectiveBias1"), m_perspective_bias1("xyPerspectiveBias1"),
m_perspective_zbias("zPerspectiveBias") m_perspective_zbias("zPerspectiveBias"),
m_cam_pos_setting("CameraPos")
{} {}
void OnSetMaterial(const video::SMaterial &material) override {} void OnSetMaterial(const video::SMaterial &material) override {}
@ -43,6 +44,7 @@ public:
f32 MaxFar{2048.0f}, MapRes{1024.0f}; f32 MaxFar{2048.0f}, MapRes{1024.0f};
f32 PerspectiveBiasXY {0.9f}, PerspectiveBiasZ {0.5f}; f32 PerspectiveBiasXY {0.9f}, PerspectiveBiasZ {0.5f};
v3f CameraPos;
private: private:
CachedVertexShaderSetting<f32, 16> m_light_mvp_setting; CachedVertexShaderSetting<f32, 16> m_light_mvp_setting;
@ -52,4 +54,5 @@ private:
CachedVertexShaderSetting<f32> m_perspective_bias0; CachedVertexShaderSetting<f32> m_perspective_bias0;
CachedVertexShaderSetting<f32> m_perspective_bias1; CachedVertexShaderSetting<f32> m_perspective_bias1;
CachedVertexShaderSetting<f32> m_perspective_zbias; CachedVertexShaderSetting<f32> m_perspective_zbias;
CachedVertexShaderSetting<f32, 4> m_cam_pos_setting;
}; };