From 48f7c5603e5b2dfca941d228e76e452bc269a1fa Mon Sep 17 00:00:00 2001 From: x2048 Date: Thu, 7 Apr 2022 22:13:50 +0200 Subject: Adjust shadowmap distortion to use entire SM texture (#12166) --- src/client/shadows/dynamicshadows.cpp | 58 +++++++++++++++------------ src/client/shadows/dynamicshadows.h | 10 +++-- src/client/shadows/dynamicshadowsrender.cpp | 11 ++--- src/client/shadows/shadowsshadercallbacks.cpp | 6 +++ src/client/shadows/shadowsshadercallbacks.h | 5 ++- 5 files changed, 56 insertions(+), 34 deletions(-) (limited to 'src/client/shadows') 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 m_light_mvp_setting; @@ -52,4 +54,5 @@ private: CachedVertexShaderSetting m_perspective_bias0; CachedVertexShaderSetting m_perspective_bias1; CachedVertexShaderSetting m_perspective_zbias; + CachedVertexShaderSetting m_cam_pos_setting; }; -- cgit v1.2.3