diff options
author | x2048 <codeforsmile@gmail.com> | 2022-04-07 22:13:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-07 22:13:50 +0200 |
commit | 48f7c5603e5b2dfca941d228e76e452bc269a1fa (patch) | |
tree | 7983bb05e762792e38ba22a59e8718cac5381747 | |
parent | 0b5b2b2633609f646a534d353a2c587af4be46fa (diff) | |
download | minetest-48f7c5603e5b2dfca941d228e76e452bc269a1fa.tar.gz minetest-48f7c5603e5b2dfca941d228e76e452bc269a1fa.tar.bz2 minetest-48f7c5603e5b2dfca941d228e76e452bc269a1fa.zip |
Adjust shadowmap distortion to use entire SM texture (#12166)
-rw-r--r-- | client/shaders/nodes_shader/opengl_fragment.glsl | 25 | ||||
-rw-r--r-- | client/shaders/object_shader/opengl_fragment.glsl | 89 | ||||
-rw-r--r-- | client/shaders/shadow_shaders/pass1_trans_vertex.glsl | 10 | ||||
-rw-r--r-- | client/shaders/shadow_shaders/pass1_vertex.glsl | 10 | ||||
-rw-r--r-- | src/client/clientmap.cpp | 36 | ||||
-rw-r--r-- | src/client/clientmap.h | 2 | ||||
-rw-r--r-- | src/client/shader.cpp | 6 | ||||
-rw-r--r-- | src/client/shadows/dynamicshadows.cpp | 58 | ||||
-rw-r--r-- | src/client/shadows/dynamicshadows.h | 10 | ||||
-rw-r--r-- | src/client/shadows/dynamicshadowsrender.cpp | 11 | ||||
-rw-r--r-- | src/client/shadows/shadowsshadercallbacks.cpp | 6 | ||||
-rw-r--r-- | src/client/shadows/shadowsshadercallbacks.h | 5 |
12 files changed, 156 insertions, 112 deletions
diff --git a/client/shaders/nodes_shader/opengl_fragment.glsl b/client/shaders/nodes_shader/opengl_fragment.glsl index 3dc66bdb3..4d0d107d1 100644 --- a/client/shaders/nodes_shader/opengl_fragment.glsl +++ b/client/shaders/nodes_shader/opengl_fragment.glsl @@ -17,6 +17,7 @@ uniform float animationTimer; uniform mat4 m_ShadowViewProj; uniform float f_shadowfar; uniform float f_shadow_strength; + uniform vec4 CameraPos; varying float normalOffsetScale; varying float adj_shadow_strength; varying float cosLight; @@ -53,12 +54,13 @@ uniform float zPerspectiveBias; 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; - - shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPerspectiveBias); - + l /= pFactor; + shadowPosition.xy = CameraPos.xy * (1.0 - l) + s * l; + shadowPosition.z *= zPerspectiveBias; return shadowPosition; } @@ -171,13 +173,13 @@ float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDis 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 } 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) @@ -185,7 +187,6 @@ float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDist float baseLength = getBaseLength(smTexCoord); float perspectiveFactor; - if (PCFBOUND == 0.0) return 0.0; // Return fast if sharp shadows are requested if (PCFBOUND == 0.0) return 0.0; @@ -489,11 +490,13 @@ void main(void) vec3 shadow_color = vec3(0.0, 0.0, 0.0); 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); if (distance_rate > 1e-7) { - + #ifdef COLORED_SHADOWS vec4 visibility; if (cosLight > 0.0) @@ -527,7 +530,7 @@ void main(void) } shadow_int *= f_adj_shadow_strength; - + // calculate fragment color from components: col.rgb = adjusted_night_ratio * col.rgb + // artificial light diff --git a/client/shaders/object_shader/opengl_fragment.glsl b/client/shaders/object_shader/opengl_fragment.glsl index 2b9a59cd7..2611bf8ef 100644 --- a/client/shaders/object_shader/opengl_fragment.glsl +++ b/client/shaders/object_shader/opengl_fragment.glsl @@ -6,8 +6,33 @@ uniform vec4 skyBgColor; uniform float fogDistance; uniform vec3 eyePosition; +// The cameraOffset is the current center of the visible world. +uniform vec3 cameraOffset; +uniform float animationTimer; +#ifdef ENABLE_DYNAMIC_SHADOWS + // shadow texture + uniform sampler2D ShadowMapSampler; + // shadow uniforms + uniform vec3 v_LightDirection; + uniform float f_textureresolution; + uniform mat4 m_ShadowViewProj; + uniform float f_shadowfar; + uniform float f_shadow_strength; + uniform vec4 CameraPos; + varying float normalOffsetScale; + varying float adj_shadow_strength; + varying float cosLight; + varying float f_normal_length; +#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 @@ -15,32 +40,15 @@ 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 - // shadow texture - uniform sampler2D ShadowMapSampler; - // shadow uniforms - uniform vec3 v_LightDirection; - uniform float f_textureresolution; - uniform mat4 m_ShadowViewProj; - uniform float f_shadowfar; - uniform float f_timeofday; - uniform float f_shadow_strength; - varying float normalOffsetScale; - varying float adj_shadow_strength; - varying float cosLight; - varying float f_normal_length; -#endif + #ifdef ENABLE_DYNAMIC_SHADOWS uniform float xyPerspectiveBias0; @@ -49,15 +57,22 @@ uniform float zPerspectiveBias; 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; - - shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPerspectiveBias); - + l /= pFactor; + shadowPosition.xy = CameraPos.xy * (1.0 - l) + s * l; + shadowPosition.z *= zPerspectiveBias; 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() { vec4 pLightSpace; @@ -161,13 +176,13 @@ float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDis 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 } 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) @@ -178,7 +193,7 @@ float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDist // Return fast if sharp shadows are requested if (PCFBOUND == 0.0) return 0.0; - + if (SOFTSHADOWRADIUS <= 1.0) { perspectiveFactor = getDeltaPerspectiveFactor(baseLength); return max(2 * length(smTexCoord.xy) * 2048 / f_textureresolution / pow(perspectiveFactor, 3), SOFTSHADOWRADIUS); @@ -418,6 +433,7 @@ float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance) #endif #if ENABLE_TONE_MAPPING + /* Hable's UC2 Tone mapping parameters A = 0.22; B = 0.30; @@ -448,12 +464,14 @@ vec4 applyToneMapping(vec4 color) } #endif + + void main(void) { vec3 color; 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 // 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. @@ -467,8 +485,7 @@ void main(void) #endif color = base.rgb; - vec4 col = vec4(color.rgb, base.a); - col.rgb *= varColor.rgb; + vec4 col = vec4(color.rgb * varColor.rgb, 1.0); col.rgb *= vIDiff; #ifdef ENABLE_DYNAMIC_SHADOWS @@ -477,11 +494,13 @@ void main(void) vec3 shadow_color = vec3(0.0, 0.0, 0.0); 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); if (distance_rate > 1e-7) { - + #ifdef COLORED_SHADOWS vec4 visibility; if (cosLight > 0.0) @@ -506,8 +525,8 @@ void main(void) // Power ratio was measured on torches in MTG (brightness = 14). float adjusted_night_ratio = pow(max(0.0, nightRatio), 0.6); - // cosine of the normal-to-light angle when - // we start to apply self-shadowing + // Apply self-shadowing when light falls at a narrow angle to the surface + // Cosine of the cut-off angle. const float self_shadow_cutoff_cosine = 0.14; 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); @@ -541,5 +560,7 @@ void main(void) float clarity = clamp(fogShadingParameter - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0); col = mix(skyBgColor, col, clarity); - gl_FragColor = vec4(col.rgb, base.a); + col = vec4(col.rgb, base.a); + + gl_FragColor = col; } diff --git a/client/shaders/shadow_shaders/pass1_trans_vertex.glsl b/client/shaders/shadow_shaders/pass1_trans_vertex.glsl index 6d2877d18..c2f575876 100644 --- a/client/shaders/shadow_shaders/pass1_trans_vertex.glsl +++ b/client/shaders/shadow_shaders/pass1_trans_vertex.glsl @@ -1,4 +1,5 @@ uniform mat4 LightMVP; // world matrix +uniform vec4 CameraPos; varying vec4 tPos; #ifdef COLORED_SHADOWS varying vec3 varColor; @@ -10,10 +11,13 @@ uniform float zPerspectiveBias; 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; - shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPerspectiveBias); - + l /= pFactor; + shadowPosition.xy = CameraPos.xy * (1.0 - l) + s * l; + shadowPosition.z *= zPerspectiveBias; return shadowPosition; } diff --git a/client/shaders/shadow_shaders/pass1_vertex.glsl b/client/shaders/shadow_shaders/pass1_vertex.glsl index 3873ac6e6..38aef3619 100644 --- a/client/shaders/shadow_shaders/pass1_vertex.glsl +++ b/client/shaders/shadow_shaders/pass1_vertex.glsl @@ -1,4 +1,5 @@ uniform mat4 LightMVP; // world matrix +uniform vec4 CameraPos; // camera position varying vec4 tPos; uniform float xyPerspectiveBias0; @@ -7,10 +8,13 @@ uniform float zPerspectiveBias; 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; - shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPerspectiveBias); - + l /= pFactor; + shadowPosition.xy = CameraPos.xy * (1.0 - l) + s * l; + shadowPosition.z *= zPerspectiveBias; return shadowPosition; } diff --git a/src/client/clientmap.cpp b/src/client/clientmap.cpp index f070a58bb..8a059c922 100644 --- a/src/client/clientmap.cpp +++ b/src/client/clientmap.cpp @@ -862,20 +862,14 @@ void ClientMap::renderMapShadows(video::IVideoDriver *driver, /* 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); - const v3f camera_position = shadow_light_pos; - 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 cam_pos_nodes = floatToInt(shadow_light_pos, BS); v3s16 p_blocks_min; 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; @@ -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. // FIXME: This could be removed if we figure out why they are missing // from the light frustum. - for (auto &i : m_drawlist) { - i.second->refGrab(); - m_drawlist_shadow[i.first] = i.second; - } + // for (auto &i : m_drawlist) { + // i.second->refGrab(); + // m_drawlist_shadow[i.first] = i.second; + // } // Number of blocks currently loaded by the client u32 blocks_loaded = 0; @@ -919,23 +913,13 @@ void ClientMap::updateDrawListShadow(const v3f &shadow_light_pos, const v3f &sha continue; } - float range = shadow_range; - - float d = 0.0; - if (!isBlockInSight(block->getPos(), camera_position, - camera_direction, camera_fov, range, &d)) + 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); + if (projection.getDistanceFrom(block_pos) > radius) continue; 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. block->resetUsageTimer(); diff --git a/src/client/clientmap.h b/src/client/clientmap.h index 4edad0d20..7bd7af266 100644 --- a/src/client/clientmap.h +++ b/src/client/clientmap.h @@ -116,7 +116,7 @@ public: void getBlocksInViewRange(v3s16 cam_pos_nodes, v3s16 *p_blocks_min, v3s16 *p_blocks_max, float range=-1.0f); 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. bool needsUpdateDrawList() { return m_needs_update_drawlist; } void renderMap(video::IVideoDriver* driver, s32 pass); diff --git a/src/client/shader.cpp b/src/client/shader.cpp index d9c1952c4..1be9ef128 100644 --- a/src/client/shader.cpp +++ b/src/client/shader.cpp @@ -220,6 +220,7 @@ class MainShaderConstantSetter : public IShaderConstantSetter CachedPixelShaderSetting<f32> m_shadow_strength; CachedPixelShaderSetting<f32> m_time_of_day; CachedPixelShaderSetting<f32> m_shadowfar; + CachedPixelShaderSetting<f32, 4> m_camera_pos; CachedPixelShaderSetting<s32> m_shadow_texture; CachedVertexShaderSetting<f32> m_perspective_bias0_vertex; CachedPixelShaderSetting<f32> m_perspective_bias0_pixel; @@ -252,6 +253,7 @@ public: , m_shadow_strength("f_shadow_strength") , m_time_of_day("f_timeofday") , m_shadowfar("f_shadowfar") + , m_camera_pos("CameraPos") , m_shadow_texture("ShadowMapSampler") , m_perspective_bias0_vertex("xyPerspectiveBias0") , m_perspective_bias0_pixel("xyPerspectiveBias0") @@ -321,6 +323,10 @@ public: f32 shadowFar = shadow->getMaxShadowFar(); 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 // MAX_TEXTURE - 1 or somthing like that?? s32 TextureLayerID = 3; diff --git a/src/client/shadows/dynamicshadows.cpp b/src/client/shadows/dynamicshadows.cpp index ddec3a5d5..ca2d3ce37 100644 --- a/src/client/shadows/dynamicshadows.cpp +++ b/src/client/shadows/dynamicshadows.cpp @@ -29,7 +29,6 @@ using m4f = core::matrix4; void DirectionalLight::createSplitMatrices(const Camera *cam) { - float radius; v3f newCenter; v3f look = cam->getDirection(); @@ -42,17 +41,16 @@ void DirectionalLight::createSplitMatrices(const Camera *cam) float sfFar = adjustDist(future_frustum.zFar, cam->getFovY()); // adjusted camera positions - v3f camPos2 = cam->getPosition(); - v3f camPos = v3f(camPos2.X - cam->getOffset().X * BS, - camPos2.Y - cam->getOffset().Y * BS, - camPos2.Z - cam->getOffset().Z * BS); - camPos += look * sfNear; - camPos2 += look * sfNear; + v3f cam_pos_world = cam->getPosition(); + v3f cam_pos_scene = v3f(cam_pos_world.X - cam->getOffset().X * BS, + cam_pos_world.Y - cam->getOffset().Y * BS, + cam_pos_world.Z - cam->getOffset().Z * BS); + cam_pos_scene += look * sfNear; + cam_pos_world += look * sfNear; // center point of light frustum - float end = sfNear + sfFar; - newCenter = camPos + look * (sfNear + 0.05f * end); - v3f world_center = camPos2 + look * (sfNear + 0.05f * end); + v3f center_scene = cam_pos_scene + look * 0.35 * (sfFar - sfNear); + v3f center_world = cam_pos_world + look * 0.35 * (sfFar - sfNear); // Create a vector to the frustum far corner const v3f &viewUp = cam->getCameraNode()->getUpVector(); @@ -60,22 +58,21 @@ void DirectionalLight::createSplitMatrices(const Camera *cam) v3f farCorner = (look + viewRight * tanFovX + viewUp * tanFovY).normalize(); // Compute the frustumBoundingSphere radius - v3f boundVec = (camPos + farCorner * sfFar) - newCenter; - radius = boundVec.getLength(); - // boundVec.getLength(); - float vvolume = radius; - v3f frustumCenter = newCenter; - v3f eye_displacement = direction * vvolume; + v3f boundVec = (cam_pos_scene + farCorner * sfFar) - center_scene; + float radius = boundVec.getLength(); + float length = radius * 3.0f; + v3f eye_displacement = direction * length; // we must compute the viewmat with the position - the camera offset // but the future_frustum position must be the actual world position - v3f eye = frustumCenter - eye_displacement; - future_frustum.position = world_center - eye_displacement; - future_frustum.length = vvolume; - future_frustum.ViewMat.buildCameraLookAtMatrixLH(eye, frustumCenter, v3f(0.0f, 1.0f, 0.0f)); - future_frustum.ProjOrthMat.buildProjectionMatrixOrthoLH(future_frustum.length, - future_frustum.length, -future_frustum.length, - future_frustum.length,false); + v3f eye = center_scene - eye_displacement; + future_frustum.player = cam_pos_scene; + future_frustum.position = center_world - eye_displacement; + future_frustum.length = length; + future_frustum.radius = radius; + future_frustum.ViewMat.buildCameraLookAtMatrixLH(eye, center_scene, v3f(0.0f, 1.0f, 0.0f)); + future_frustum.ProjOrthMat.buildProjectionMatrixOrthoLH(radius, radius, + 0.0f, length, false); 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 zFar = getMaxFarValue(); 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 @@ -105,7 +102,7 @@ void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool fo createSplitMatrices(cam); // get the draw list for shadows client->getEnv().getClientMap().updateDrawListShadow( - getPosition(), getDirection(), future_frustum.length); + getPosition(), getDirection(), future_frustum.radius, future_frustum.length); should_update_map_shadow = true; dirty = true; @@ -115,6 +112,7 @@ void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool fo v3f rotated_offset; 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.player += intToFloat(shadow_frustum.camera_offset - cam->getOffset(), BS); shadow_frustum.camera_offset = cam_offset; } } @@ -139,6 +137,16 @@ v3f DirectionalLight::getPosition() const 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 { return shadow_frustum.ViewMat; diff --git a/src/client/shadows/dynamicshadows.h b/src/client/shadows/dynamicshadows.h index 03dd36014..70574aa6c 100644 --- a/src/client/shadows/dynamicshadows.h +++ b/src/client/shadows/dynamicshadows.h @@ -29,12 +29,14 @@ class Client; struct shadowFrustum { - float zNear{0.0f}; - float zFar{0.0f}; - float length{0.0f}; + f32 zNear{0.0f}; + f32 zFar{0.0f}; + f32 length{0.0f}; + f32 radius{0.0f}; core::matrix4 ProjOrthMat; core::matrix4 ViewMat; v3f position; + v3f player; v3s16 camera_offset; }; @@ -57,6 +59,8 @@ public: return direction; }; v3f getPosition() const; + v3f getPlayerPos() const; + v3f getFuturePlayerPos() const; /// Gets the light's matrices. const core::matrix4 &getViewMatrix() const; diff --git a/src/client/shadows/dynamicshadowsrender.cpp b/src/client/shadows/dynamicshadowsrender.cpp index 262711221..1dfc90d1c 100644 --- a/src/client/shadows/dynamicshadowsrender.cpp +++ b/src/client/shadows/dynamicshadowsrender.cpp @@ -158,7 +158,6 @@ void ShadowRenderer::setShadowIntensity(float shadow_intensity) disable(); } - void ShadowRenderer::addNodeToShadowList( scene::ISceneNode *node, E_SHADOW_MODE shadowMode) { @@ -261,8 +260,9 @@ void ShadowRenderer::updateSMTextures() cb->MaxFar = (f32)m_shadow_map_max_distance * BS; cb->PerspectiveBiasXY = getPerspectiveBiasXY(); cb->PerspectiveBiasZ = getPerspectiveBiasZ(); + cb->CameraPos = light.getFuturePlayerPos(); } - + // set the Render Target // right now we can only render in usual RTT, not // Depth texture is available in irrlicth maybe we @@ -322,9 +322,10 @@ void ShadowRenderer::update(video::ITexture *outputTarget) if (!m_shadow_node_array.empty() && !m_light_list.empty()) { for (DirectionalLight &light : m_light_list) { - // Static shader values. - m_shadow_depth_cb->MapRes = (f32)m_shadow_map_texture_size; - m_shadow_depth_cb->MaxFar = (f32)m_shadow_map_max_distance * BS; + // Static shader values for entities are set in updateSMTextures + // SM texture for entities is not updated incrementally and + // must by updated using current player position. + m_shadow_depth_entity_cb->CameraPos = light.getPlayerPos(); // render shadows for the n0n-map objects. m_driver->setRenderTarget(shadowMapTextureDynamicObjects, true, diff --git a/src/client/shadows/shadowsshadercallbacks.cpp b/src/client/shadows/shadowsshadercallbacks.cpp index 51ea8aa93..b571ea939 100644 --- a/src/client/shadows/shadowsshadercallbacks.cpp +++ b/src/client/shadows/shadowsshadercallbacks.cpp @@ -26,6 +26,10 @@ void ShadowDepthShaderCB::OnSetConstants( core::matrix4 lightMVP = driver->getTransform(video::ETS_PROJECTION); lightMVP *= driver->getTransform(video::ETS_VIEW); + + f32 cam_pos[4]; + lightMVP.transformVect(cam_pos, CameraPos); + lightMVP *= driver->getTransform(video::ETS_WORLD); m_light_mvp_setting.set(lightMVP.pointer(), services); @@ -39,4 +43,6 @@ void ShadowDepthShaderCB::OnSetConstants( m_perspective_bias1.set(&bias1, services); f32 zbias = PerspectiveBiasZ; m_perspective_zbias.set(&zbias, services); + + m_cam_pos_setting.set(cam_pos, services); } diff --git a/src/client/shadows/shadowsshadercallbacks.h b/src/client/shadows/shadowsshadercallbacks.h index d00f59c37..87833c0ec 100644 --- a/src/client/shadows/shadowsshadercallbacks.h +++ b/src/client/shadows/shadowsshadercallbacks.h @@ -33,7 +33,8 @@ public: m_color_map_sampler_setting("ColorMapSampler"), m_perspective_bias0("xyPerspectiveBias0"), m_perspective_bias1("xyPerspectiveBias1"), - m_perspective_zbias("zPerspectiveBias") + m_perspective_zbias("zPerspectiveBias"), + m_cam_pos_setting("CameraPos") {} void OnSetMaterial(const video::SMaterial &material) override {} @@ -43,6 +44,7 @@ public: f32 MaxFar{2048.0f}, MapRes{1024.0f}; f32 PerspectiveBiasXY {0.9f}, PerspectiveBiasZ {0.5f}; + v3f CameraPos; private: CachedVertexShaderSetting<f32, 16> m_light_mvp_setting; @@ -52,4 +54,5 @@ private: CachedVertexShaderSetting<f32> m_perspective_bias0; CachedVertexShaderSetting<f32> m_perspective_bias1; CachedVertexShaderSetting<f32> m_perspective_zbias; + CachedVertexShaderSetting<f32, 4> m_cam_pos_setting; }; |