Browse Source

Merge pull request #47540 from volzhs/ensure-normal-gles2

Make ensure_correct_normals working on GLES2
Rémi Verschelde 4 years ago
parent
commit
81f8b404f2
2 changed files with 15 additions and 3 deletions
  1. 1 3
      drivers/gles2/shader_compiler_gles2.cpp
  2. 14 0
      drivers/gles2/shaders/scene.glsl

+ 1 - 3
drivers/gles2/shader_compiler_gles2.cpp

@@ -1073,9 +1073,7 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
 
 	actions[VS::SHADER_SPATIAL].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
 	actions[VS::SHADER_SPATIAL].render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";
-
-	// Defined in GLES3, could be implemented in GLES2 too if there's a need for it
-	//actions[VS::SHADER_SPATIAL].render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n";
+	actions[VS::SHADER_SPATIAL].render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n";
 	// Defined in GLES3, might not be possible in GLES2 as gl_FrontFacing is not available
 	//actions[VS::SHADER_SPATIAL].render_mode_defines["cull_front"] = "#define DO_SIDE_CHECK\n";
 	//actions[VS::SHADER_SPATIAL].render_mode_defines["cull_disabled"] = "#define DO_SIDE_CHECK\n";

+ 14 - 0
drivers/gles2/shaders/scene.glsl

@@ -10,6 +10,10 @@ precision highp float;
 precision highp int;
 #endif
 
+#if defined(ENSURE_CORRECT_NORMALS)
+#define INVERSE_USED
+#endif
+
 /* clang-format on */
 #include "stdlib.glsl"
 /* clang-format off */
@@ -365,7 +369,12 @@ void main() {
 
 #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
 	vertex = world_matrix * vertex;
+#if defined(ENSURE_CORRECT_NORMALS)
+	mat3 normal_matrix = mat3(transpose(inverse(world_matrix)));
+	normal = normal_matrix * normal;
+#else
 	normal = normalize((world_matrix * vec4(normal, 0.0)).xyz);
+#endif
 #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
 
 	tangent = normalize((world_matrix * vec4(tangent, 0.0)).xyz);
@@ -439,7 +448,12 @@ VERTEX_SHADER_CODE
 	// use local coordinates
 #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
 	vertex = modelview * vertex;
+#if defined(ENSURE_CORRECT_NORMALS)
+	mat3 normal_matrix = mat3(transpose(inverse(modelview)));
+	normal = normal_matrix * normal;
+#else
 	normal = normalize((modelview * vec4(normal, 0.0)).xyz);
+#endif
 
 #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
 	tangent = normalize((modelview * vec4(tangent, 0.0)).xyz);