From 4801bdf45aaaa9238bc52a157e1d25c9d477d81a Mon Sep 17 00:00:00 2001 From: Dmitry Kostenko Date: Sun, 20 Feb 2022 00:04:48 +0100 Subject: Correct normal bias for entities Remove use of magic constants. Apply cameraOffset Calculate distance projected on SM plane --- client/shaders/object_shader/opengl_vertex.glsl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'client/shaders/object_shader') diff --git a/client/shaders/object_shader/opengl_vertex.glsl b/client/shaders/object_shader/opengl_vertex.glsl index 12078f532..185551c58 100644 --- a/client/shaders/object_shader/opengl_vertex.glsl +++ b/client/shaders/object_shader/opengl_vertex.glsl @@ -3,6 +3,7 @@ uniform vec3 dayLight; uniform vec3 eyePosition; uniform float animationTimer; uniform vec4 emissiveColor; +uniform vec3 cameraOffset; varying vec3 vNormal; @@ -110,10 +111,13 @@ void main(void) // Calculate normal offset scale based on the texel size adjusted for // curvature of the SM texture. This code must be change together with // getPerspectiveFactor or any light-space transformation. - float distanceToPlayer = length((eyePosition - worldPosition).xyz) / f_shadowfar; + vec3 eyeToVertex = worldPosition - eyePosition + cameraOffset; + // Distance from the vertex to the player + float distanceToPlayer = length(eyeToVertex - v_LightDirection * dot(eyeToVertex, v_LightDirection)) / f_shadowfar; + // perspective factor estimation according to the float perspectiveFactor = distanceToPlayer * bias0 + bias1; - float texelSize = 1.0 / f_textureresolution; - texelSize *= f_shadowfar * perspectiveFactor / (bias1 / perspectiveFactor - texelSize * bias0) * 0.15; + float texelSize = f_shadowfar * perspectiveFactor * perspectiveFactor / + (f_textureresolution * bias1 - perspectiveFactor * bias0); float slopeScale = clamp(pow(1.0 - cosLight*cosLight, 0.5), 0.0, 1.0); normalOffsetScale = texelSize * slopeScale; -- cgit v1.2.3