From 8bd21199e78616b139cd5f7a18523181ebdfc8d4 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 31 Jul 2024 00:36:11 +0300 Subject: [PATCH] Align shadow camera to shadow map texels This avoids swimming shadows when the target area moves smoothly. --- source/effects/shadowmap.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/effects/shadowmap.cpp b/source/effects/shadowmap.cpp index 0345ac1f..d945ea6b 100644 --- a/source/effects/shadowmap.cpp +++ b/source/effects/shadowmap.cpp @@ -209,7 +209,10 @@ void ShadowMap::setup_frame(Renderer &renderer) if(light.type==DIRECTIONAL) { v.camera.set_object_matrix(*light.light->get_matrix()); - v.camera.set_position(target); + Vector4 pos = v.camera.get_view_matrix()*compose(target, 0.0f); + LinAl::Vector texel_size = { radius*2.0f/light.region.width, radius*2.0f/light.region.height }; + pos = { round(pos.x/texel_size.x)*texel_size.x, round(pos.y/texel_size.y)*texel_size.y, pos.z, 0.0f }; + v.camera.set_position((v.camera.get_object_matrix()*pos).slice<3>(0)); v.camera.set_orthographic(radius*2, radius*2); v.camera.set_depth_clip(-radius, radius); } -- 2.45.2