1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Correct normal bias for entities

Remove use of magic constants.
Apply cameraOffset
Calculate distance projected on SM plane
This commit is contained in:
Dmitry Kostenko 2022-02-20 00:04:48 +01:00 committed by x2048
parent 25c1974e0d
commit 4801bdf45a
4 changed files with 14 additions and 15 deletions

View 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;