From da4cec620e16bfb36510cf7395afb570e125401f Mon Sep 17 00:00:00 2001 From: volzhs Date: Thu, 1 Apr 2021 16:33:35 +0900 Subject: [PATCH] Make ensure_correct_normals working on GLES2 --- drivers/gles2/shader_compiler_gles2.cpp | 4 +--- drivers/gles2/shaders/scene.glsl | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/gles2/shader_compiler_gles2.cpp b/drivers/gles2/shader_compiler_gles2.cpp index 60bbecfecd09..cee86753e872 100644 --- a/drivers/gles2/shader_compiler_gles2.cpp +++ b/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"; diff --git a/drivers/gles2/shaders/scene.glsl b/drivers/gles2/shaders/scene.glsl index e4d0620cbab3..0735839c75f7 100644 --- a/drivers/gles2/shaders/scene.glsl +++ b/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);