From: Mikko Rasa Date: Mon, 11 Oct 2021 13:05:16 +0000 (+0300) Subject: Apply shadow depth bias in a different way X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b733c793381e78637f52c7f77cc4f69e914918e1;p=libs%2Fgl.git Apply shadow depth bias in a different way It cannot be properly applied through the matrix for perspective projection. --- diff --git a/shaderlib/shadow.glsl b/shaderlib/shadow.glsl index 2fa1fd81..438af68d 100644 --- a/shaderlib/shadow.glsl +++ b/shaderlib/shadow.glsl @@ -6,6 +6,7 @@ struct ShadowParameters float darkness; int matrix_index; vec4 region; + float bias; }; uniform ShadowMap @@ -26,7 +27,10 @@ virtual float get_shadow_factor(int index, vec4 world_pos) int type = shadows[index].type; vec3 shadow_coord; if(type==1) + { shadow_coord = (shd_world_matrix[shadows[index].matrix_index]*world_pos).xyz; + shadow_coord.z -= shadows[index].bias; + } else return 1.0; @@ -34,7 +38,7 @@ virtual float get_shadow_factor(int index, vec4 world_pos) return 1.0; vec4 region = shadows[index].region; - float shadow_sample = texture(shadow_map, shadow_coord*vec3(region.zw, 1.0)+vec3(region.xy, 0.0)); + float shadow_sample = texture(shadow_map, vec3(shadow_coord.xy*region.zw+region.xy, shadow_coord.z)); return mix(1.0, shadow_sample, shadows[index].darkness); } else diff --git a/source/effects/shadowmap.cpp b/source/effects/shadowmap.cpp index 4cc0475d..46524397 100644 --- a/source/effects/shadowmap.cpp +++ b/source/effects/shadowmap.cpp @@ -30,6 +30,7 @@ ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l): shdata.uniform(base+".darkness", 1.0f); shdata.uniform(base+".matrix_index", 0); shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f)); + shdata.uniform(base+".bias", 0.0f); } Matrix dummy_matrix; @@ -160,6 +161,13 @@ void ShadowMap::setup_frame(Renderer &renderer) for(const ShadowedLight &l: lights) l.shadow_caster->setup_frame(renderer); + for(const ShadowedLight &l: lights) + { + string base = format("shadows[%d]", l.index); + if(l.type==DIRECTIONAL) + shdata.uniform(base+".bias", depth_bias/l.region.width); + } + vector shadow_matrices; shadow_matrices.reserve(views.size()); for(ShadowView &v: views) @@ -174,7 +182,7 @@ void ShadowMap::setup_frame(Renderer &renderer) v.camera.set_depth_clip(-radius, radius); } - Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f-depth_bias/light.region.width)).scale(0.5f); + Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f)).scale(0.5f); shadow_matrices.push_back(to_texcoord*v.camera.get_projection_matrix()*v.camera.get_view_matrix()); }