summaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorx2048 <codeforsmile@gmail.com>2022-04-07 22:13:50 +0200
committerGitHub <noreply@github.com>2022-04-07 22:13:50 +0200
commit48f7c5603e5b2dfca941d228e76e452bc269a1fa (patch)
tree7983bb05e762792e38ba22a59e8718cac5381747 /src/client
parent0b5b2b2633609f646a534d353a2c587af4be46fa (diff)
downloadminetest-48f7c5603e5b2dfca941d228e76e452bc269a1fa.tar.gz
minetest-48f7c5603e5b2dfca941d228e76e452bc269a1fa.tar.bz2
minetest-48f7c5603e5b2dfca941d228e76e452bc269a1fa.zip
Adjust shadowmap distortion to use entire SM texture (#12166)
Diffstat (limited to 'src/client')
-rw-r--r--src/client/clientmap.cpp36
-rw-r--r--src/client/clientmap.h2
-rw-r--r--src/client/shader.cpp6
-rw-r--r--src/client/shadows/dynamicshadows.cpp58
-rw-r--r--src/client/shadows/dynamicshadows.h10
-rw-r--r--src/client/shadows/dynamicshadowsrender.cpp11
-rw-r--r--src/client/shadows/shadowsshadercallbacks.cpp6
-rw-r--r--src/client/shadows/shadowsshadercallbacks.h5
8 files changed, 73 insertions, 61 deletions
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;
};