Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Fix tangent transformation for meshes with non-uniform scale

Arseny Kapoulkine573546a71e69
diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl
index cd210e8304c..c69b04bfc80 100644
--- a/drivers/gles3/shaders/scene.glsl
+++ b/drivers/gles3/shaders/scene.glsl
@@ -698,14 +698,14 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
vertex = (model_matrix * vec4(vertex, 1.0)).xyz;
#ifdef NORMAL_USED
+ // For correct non-uniform scale handling, normal has to be transformed by normal matrix, but tangent vectors need to use model matrix as is
normal = model_normal_matrix * normal;
#endif
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
-
- tangent = model_normal_matrix * tangent;
- binormal = model_normal_matrix * binormal;
-
+ // For non-uniform scale, this produces non-orthogonal TBNs; ideally binormal should be reconstructed in fragment shader with cross
+ tangent = mat3(model_matrix) * tangent;
+ binormal = mat3(model_matrix) * binormal;
#endif
#endif
@@ -730,14 +730,16 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
#if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
vertex = (modelview * vec4(vertex, 1.0)).xyz;
+
#ifdef NORMAL_USED
+ // For correct non-uniform scale handling, normal has to be transformed by normal matrix, but tangent vectors need to use model matrix as is
normal = modelview_normal * normal;
#endif
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
-
- binormal = modelview_normal * binormal;
- tangent = modelview_normal * tangent;
+ // For non-uniform scale, this produces non-orthogonal TBNs; ideally binormal should be reconstructed in fragment shader with cross
+ tangent = mat3(modelview) * tangent;
+ binormal = mat3(modelview) * binormal;
#endif
#endif // !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
@@ -757,8 +759,7 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
vertex_interp = vertex;
- // Normalize TBN vectors before interpolation, per MikkTSpace.
- // See: http://www.mikktspace.com/
+ // Normalize TBN vectors to account for model/normal transforms that may have scale
#ifdef NORMAL_USED
normal_interp = normalize(normal);
#endif
diff --git a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl
index 1e54da8a60b..6d883150456 100644
--- a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl
+++ b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl
@@ -396,14 +396,14 @@ void vertex_shader(vec3 vertex_input,
vertex = (model_matrix * vec4(vertex, 1.0)).xyz;
#ifdef NORMAL_USED
+ // For correct non-uniform scale handling, normal has to be transformed by normal matrix, but tangent vectors need to use model matrix as is
normal_highp = model_normal_matrix * normal_highp;
#endif
#ifdef TANGENT_USED
-
- tangent = model_normal_matrix * tangent;
- binormal = model_normal_matrix * binormal;
-
+ // For non-uniform scale, this produces non-orthogonal TBNs; ideally binormal should be reconstructed in fragment shader with cross
+ tangent = mat3(model_matrix) * tangent;
+ binormal = mat3(model_matrix) * binormal;
#endif
#endif
@@ -461,13 +461,14 @@ void vertex_shader(vec3 vertex_input,
vertex = (modelview * vec4(vertex, 1.0)).xyz;
#ifdef NORMAL_USED
+ // For correct non-uniform scale handling, normal has to be transformed by normal matrix, but tangent vectors need to use model matrix as is
normal = modelview_normal * normal;
#endif
#ifdef TANGENT_USED
-
- binormal = modelview_normal * binormal;
- tangent = modelview_normal * tangent;
+ // For non-uniform scale, this produces non-orthogonal TBNs; ideally binormal should be reconstructed in fragment shader with cross
+ tangent = mat3(modelview) * tangent;
+ binormal = mat3(modelview) * binormal;
#endif
#endif // !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
@@ -487,8 +488,7 @@ void vertex_shader(vec3 vertex_input,
vertex_interp = vertex;
- // Normalize TBN vectors before interpolation, per MikkTSpace.
- // See: http://www.mikktspace.com/
+ // Normalize TBN vectors to account for model/normal transforms that may have scale
#ifdef NORMAL_USED
normal_interp = normalize(normal);
#endif
diff --git a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl
index 7845f5df464..cb3504e0c71 100644
--- a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl
+++ b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl
@@ -421,14 +421,14 @@ void vertex_shader(in vec3 vertex,
vertex = (model_matrix * vec4(vertex, 1.0)).xyz;
#ifdef NORMAL_USED
+ // For correct non-uniform scale handling, normal has to be transformed by normal matrix, but tangent vectors need to use model matrix as is
normal_highp = model_normal_matrix * normal_highp;
#endif
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
-
- tangent_highp = model_normal_matrix * tangent_highp;
- binormal_highp = model_normal_matrix * binormal_highp;
-
+ // For non-uniform scale, this produces non-orthogonal TBNs; ideally binormal should be reconstructed in fragment shader with cross
+ tangent_highp = mat3(model_matrix) * tangent_highp;
+ binormal_highp = mat3(model_matrix) * binormal_highp;
#endif
#endif
@@ -487,13 +487,14 @@ void vertex_shader(in vec3 vertex,
vertex = (modelview * vec4(vertex, 1.0)).xyz;
#ifdef NORMAL_USED
+ // For correct non-uniform scale handling, normal has to be transformed by normal matrix, but tangent vectors need to use model matrix as is
normal_highp = modelview_normal * normal_highp;
#endif
#if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
-
- binormal_highp = modelview_normal * binormal_highp;
- tangent_highp = modelview_normal * tangent_highp;
+ // For non-uniform scale, this produces non-orthogonal TBNs; ideally binormal should be reconstructed in fragment shader with cross
+ tangent_highp = mat3(modelview) * tangent_highp;
+ binormal_highp = mat3(modelview) * binormal_highp;
#endif
#endif // !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
@@ -513,8 +514,7 @@ void vertex_shader(in vec3 vertex,
vertex_interp = vertex;
- // Normalize TBN vectors before interpolation, per MikkTSpace.
- // See: http://www.mikktspace.com/
+ // Normalize TBN vectors to account for model/normal transforms that may have scale
#ifdef NORMAL_USED
normal_interp = hvec3(normalize(normal_highp));
#endif