]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Correct normal bias for entities
authorDmitry Kostenko <codeforsmile@gmail.com>
Sat, 19 Feb 2022 23:04:48 +0000 (00:04 +0100)
committerx2048 <codeforsmile@gmail.com>
Mon, 7 Mar 2022 22:45:26 +0000 (23:45 +0100)
Remove use of magic constants.
Apply cameraOffset
Calculate distance projected on SM plane

client/shaders/object_shader/opengl_vertex.glsl
src/client/shadows/dynamicshadows.cpp
src/client/shadows/dynamicshadows.h
src/client/shadows/dynamicshadowsrender.cpp

index 12078f532e251a669ce651e757f483aca2154c83..185551c5837317417f47d683e64207423f36ae75 100644 (file)
@@ -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;
        
index 6ef5a4f1db4e60613cc75b087475b26c95f9355e..a45bf64fed04aaf7e49453666c25aef2240bdae9 100644 (file)
@@ -58,15 +58,13 @@ void DirectionalLight::createSplitMatrices(const Camera *cam)
        const v3f &viewUp = cam->getCameraNode()->getUpVector();
        v3f viewRight = look.crossProduct(viewUp);
 
-       v3f farCorner = look + viewRight * tanFovX + viewUp * tanFovY;
+       v3f farCorner = (look + viewRight * tanFovX + viewUp * tanFovY).normalize();
        // Compute the frustumBoundingSphere radius
        v3f boundVec = (camPos + farCorner * sfFar) - newCenter;
-       radius = boundVec.getLength() * 2.0f;
+       radius = boundVec.getLength();
        // boundVec.getLength();
-       float vvolume = radius * 2.0f;
-
+       float vvolume = radius;
        v3f frustumCenter = newCenter;
-       // probar radius multipliacdor en funcion del I, a menor I mas multiplicador
        v3f eye_displacement = direction * vvolume;
 
        // we must compute the viewmat with the position - the camera offset
index d8be66be89bd67032d289553666c12060a647fa5..03dd360140f168b6b5fe0139ff3d920386bb33a5 100644 (file)
@@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "irrlichttypes_bloated.h"
 #include <matrix4.h>
 #include "util/basic_macros.h"
+#include "constants.h"
 
 class Camera;
 class Client;
@@ -67,7 +68,7 @@ class DirectionalLight
        /// Gets the light's far value.
        f32 getMaxFarValue() const
        {
-               return farPlane;
+               return farPlane * BS;
        }
 
 
index a913a9290af468a8210c516ebf646146891281bf..528415aafd39bd925ddbe4acd879954508155c25 100644 (file)
@@ -118,12 +118,8 @@ size_t ShadowRenderer::getDirectionalLightCount() const
 f32 ShadowRenderer::getMaxShadowFar() const
 {
        if (!m_light_list.empty()) {
-               float wanted_range = m_client->getEnv().getClientMap().getWantedRange();
-
-               float zMax = m_light_list[0].getMaxFarValue() > wanted_range
-                                            ? wanted_range
-                                            : m_light_list[0].getMaxFarValue();
-               return zMax * MAP_BLOCKSIZE;
+               float zMax = m_light_list[0].getMaxFarValue();
+               return zMax;
        }
        return 0.0f;
 }