From 537964f8d8c89bc5434fe2400699963018b12803 Mon Sep 17 00:00:00 2001 From: Alexei Kotov Date: Thu, 18 Apr 2024 11:23:21 +0300 Subject: [PATCH] Reconstruct normal Z *properly* --- files/shaders/compatibility/bs/default.frag | 5 +++-- files/shaders/compatibility/groundcover.frag | 5 +++-- files/shaders/compatibility/objects.frag | 5 +++-- files/shaders/compatibility/terrain.frag | 5 +++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/files/shaders/compatibility/bs/default.frag b/files/shaders/compatibility/bs/default.frag index d2c8de0b22..3c665752d1 100644 --- a/files/shaders/compatibility/bs/default.frag +++ b/files/shaders/compatibility/bs/default.frag @@ -77,10 +77,11 @@ void main() vec3 specularColor = getSpecularColor().xyz; #if @normalMap vec4 normalTex = texture2D(normalMap, normalMapUV); + vec3 normal = normalTex.xyz * 2.0 - 1.0; #if @reconstructNormalZ - normalTex.z = sqrt(1.0 - dot(normalTex.xy, normalTex.xy)); + normal.z = sqrt(1.0 - dot(normal.xy, normal.xy)); #endif - vec3 viewNormal = normalToView(normalTex.xyz * 2.0 - 1.0); + vec3 viewNormal = normalToView(normal); specularColor *= normalTex.a; #else vec3 viewNormal = normalize(gl_NormalMatrix * passNormal); diff --git a/files/shaders/compatibility/groundcover.frag b/files/shaders/compatibility/groundcover.frag index aab37d465d..96a79c8793 100644 --- a/files/shaders/compatibility/groundcover.frag +++ b/files/shaders/compatibility/groundcover.frag @@ -60,10 +60,11 @@ void main() #if @normalMap vec4 normalTex = texture2D(normalMap, normalMapUV); + vec3 normal = normalTex.xyz * 2.0 - 1.0; #if @reconstructNormalZ - normalTex.z = sqrt(1.0 - dot(normalTex.xy, normalTex.xy)); + normal.z = sqrt(1.0 - dot(normal.xy, normal.xy)); #endif - vec3 viewNormal = normalToView(normalTex.xyz * 2.0 - 1.0); + vec3 viewNormal = normalToView(normal); #else vec3 viewNormal = normalToView(normalize(passNormal)); #endif diff --git a/files/shaders/compatibility/objects.frag b/files/shaders/compatibility/objects.frag index eb5b79a0c2..7f163580cc 100644 --- a/files/shaders/compatibility/objects.frag +++ b/files/shaders/compatibility/objects.frag @@ -168,10 +168,11 @@ vec2 screenCoords = gl_FragCoord.xy / screenRes; #if @normalMap vec4 normalTex = texture2D(normalMap, normalMapUV + offset); + vec3 normal = normalTex.xyz * 2.0 - 1.0; #if @reconstructNormalZ - normalTex.z = sqrt(1.0 - dot(normalTex.xy, normalTex.xy)); + normal.z = sqrt(1.0 - dot(normal.xy, normal.xy)); #endif - vec3 viewNormal = normalToView(normalTex.xyz * 2.0 - 1.0); + vec3 viewNormal = normalToView(normal); #else vec3 viewNormal = normalize(gl_NormalMatrix * passNormal); #endif diff --git a/files/shaders/compatibility/terrain.frag b/files/shaders/compatibility/terrain.frag index f45f1f024e..9d89217f35 100644 --- a/files/shaders/compatibility/terrain.frag +++ b/files/shaders/compatibility/terrain.frag @@ -64,10 +64,11 @@ void main() #if @normalMap vec4 normalTex = texture2D(normalMap, adjustedUV); + vec3 normal = normalTex.xyz * 2.0 - 1.0; #if @reconstructNormalZ - normalTex.z = sqrt(1.0 - dot(normalTex.xy, normalTex.xy)); + normal.z = sqrt(1.0 - dot(normal.xy, normal.xy)); #endif - vec3 viewNormal = normalToView(normalTex.xyz * 2.0 - 1.0); + vec3 viewNormal = normalToView(normal); #else vec3 viewNormal = normalize(gl_NormalMatrix * passNormal); #endif