Skip to content

Commit

Permalink
Merge pull request #47540 from volzhs/ensure-normal-gles2
Browse files Browse the repository at this point in the history
Make ensure_correct_normals working on GLES2
  • Loading branch information
akien-mga authored Apr 14, 2021
2 parents 9df1ed3 + da4cec6 commit 81f8b40
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 1 addition & 3 deletions drivers/gles2/shader_compiler_gles2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
14 changes: 14 additions & 0 deletions drivers/gles2/shaders/scene.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 81f8b40

Please sign in to comment.