diff options
author | Brennen Green <brennengreen@outlook.com> | 2021-05-05 10:25:32 -0400 |
---|---|---|
committer | Brennen Green <brennengreen@outlook.com> | 2021-05-07 08:38:36 -0400 |
commit | 31f2d946ad6abcfe82ff54bb36e84d20ac6c62a9 (patch) | |
tree | 1a21c57b19fafddd7bb0a76ba9b58137f5768b0c | |
parent | 7449d6c752a032024fa9e862efc96c3d36034369 (diff) |
Manually unpacked normal mapping to fix issue with refraction being offset rather than distorted.
-rw-r--r-- | scene/resources/material.cpp | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index e8157c7165..aaa9f426bc 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -1032,7 +1032,10 @@ void BaseMaterial3D::_update_shader() { if (features[FEATURE_REFRACTION]) { if (features[FEATURE_NORMAL_MAPPING]) { - code += "\tvec3 ref_normal = normalize( mix(NORMAL,TANGENT * NORMAL_MAP.x + BINORMAL * NORMAL_MAP.y + NORMAL * NORMAL_MAP.z,NORMAL_MAP_DEPTH) );\n"; + code += "\tvec3 unpacked_normal = NORMAL_MAP;\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,NORMAL_MAP_DEPTH) );\n"; } else { code += "\tvec3 ref_normal = NORMAL;\n"; } |