From 88c401224220010cb71426fafc5173661c4d48a3 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 11 Mar 2022 23:48:17 +0200 Subject: [PATCH] Flip normal if a surface faces away from the camera This gives correct two-sided lighting for non-closed meshes, which may be used for tree leaves and such. --- shaderlib/common.glsl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/shaderlib/common.glsl b/shaderlib/common.glsl index 7a57b067..557d4d76 100644 --- a/shaderlib/common.glsl +++ b/shaderlib/common.glsl @@ -78,10 +78,12 @@ layout(location=0) out vec4 frag_color; virtual vec3 get_fragment_normal() { + vec3 normal; + float sgn = (gl_FrontFacing ? 1.0 : -1.0); if(use_normal_map) - return normalize(world_tbn_matrix*(texture(normal_map, texcoord.xy).xyz*2.0-1.0)); + return sgn*normalize(world_tbn_matrix*(texture(normal_map, texcoord.xy).xyz*2.0-1.0)); else - return normalize(world_normal); + return sgn*normalize(world_normal); } virtual IncomingLight get_incoming_light(int index, vec3 world_pos) -- 2.43.0