From fac2e5dc671fb497616f8a1ccf4bd3dd989ab742 Mon Sep 17 00:00:00 2001 From: Brennen Green Date: Wed, 5 May 2021 10:25:32 -0400 Subject: [PATCH] Manually unpacked normal mapping to fix issue with refraction being offset rather than distorted. (cherry picked from commit 31f2d946ad6abcfe82ff54bb36e84d20ac6c62a9) --- scene/resources/material.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index a4e2ab099ef..a9ef274214e 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -861,7 +861,10 @@ void SpatialMaterial::_update_shader() { if (features[FEATURE_REFRACTION]) { if (features[FEATURE_NORMAL_MAPPING]) { - code += "\tvec3 ref_normal = normalize( mix(NORMAL,TANGENT * NORMALMAP.x + BINORMAL * NORMALMAP.y + NORMAL * NORMALMAP.z,NORMALMAP_DEPTH) );\n"; + code += "\tvec3 unpacked_normal = NORMALMAP;\n"; + code += "\tunpacked_normal.xy = unpacked_normal.xy * 2.0 - 1.0;\n"; + code += "\tunpacked_normal.z = sqrt(max(0.0, 1.0 - dot(unpacked_normal.xy, unpacked_normal.xy)));\n"; + code += "\tvec3 ref_normal = normalize( mix(NORMAL,TANGENT * unpacked_normal.x + BINORMAL * unpacked_normal.y + NORMAL * unpacked_normal.z,NORMALMAP_DEPTH) );\n"; } else { code += "\tvec3 ref_normal = NORMAL;\n"; }