From f6beb386d0ac005dc1b4f88ff93cd8030443b97b Mon Sep 17 00:00:00 2001 From: David Labode Date: Wed, 4 Nov 2020 18:46:59 +0100 Subject: [PATCH 1/4] Apply ambient occlusion only to diffuse part of IBL. Not to direct lighting or specular. --- src/shaders/pbr.frag | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/shaders/pbr.frag b/src/shaders/pbr.frag index dc9ffa07..8444e664 100644 --- a/src/shaders/pbr.frag +++ b/src/shaders/pbr.frag @@ -537,6 +537,13 @@ void main() #endif #endif + float ao = 1.0; + // Apply optional PBR terms for additional (optional) shading +#ifdef HAS_OCCLUSION_MAP + ao = texture(u_OcclusionSampler, getOcclusionUV()).r; + f_diffuse = mix(f_diffuse, f_diffuse * ao, u_OcclusionStrength); +#endif + #ifdef USE_PUNCTUAL for (int i = 0; i < LIGHT_COUNT; ++i) { @@ -645,13 +652,6 @@ void main() color = (f_emissive + diffuse + f_specular + f_subsurface + (1.0 - reflectance) * f_sheen) * (1.0 - clearcoatFactor * clearcoatFresnel) + f_clearcoat * clearcoatFactor; - float ao = 1.0; - // Apply optional PBR terms for additional (optional) shading -#ifdef HAS_OCCLUSION_MAP - ao = texture(u_OcclusionSampler, getOcclusionUV()).r; - color = mix(color, color * ao, u_OcclusionStrength); -#endif - #ifndef DEBUG_OUTPUT // no debug #ifdef ALPHAMODE_MASK From b642b7175b734e263eeb5aaeb97431676d34f1b2 Mon Sep 17 00:00:00 2001 From: Moritz Willy Becher Date: Thu, 5 Nov 2020 11:03:27 +0100 Subject: [PATCH 2/4] self document backfacing code --- src/shaders/pbr.frag | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/shaders/pbr.frag b/src/shaders/pbr.frag index a90742d5..825ed9dd 100644 --- a/src/shaders/pbr.frag +++ b/src/shaders/pbr.frag @@ -164,10 +164,13 @@ NormalInfo getNormalInfo(vec3 v) #endif // For a back-facing surface, the tangential basis vectors are negated. - float facing = step(0.0, dot(v, ng)) * 2.0 - 1.0; - t *= facing; - b *= facing; - ng *= facing; + float facing = sign(dot(v, ng)); + if (facing < 0.0) + { + t *= facing; + b *= facing; + ng *= facing; + } // Due to anisoptry, the tangent can be further rotated around the geometric normal. vec3 direction; From 0144dd1ecfeb6e6060bd3ad469917ef2f4304829 Mon Sep 17 00:00:00 2001 From: Moritz Willy Becher Date: Thu, 5 Nov 2020 11:30:22 +0100 Subject: [PATCH 3/4] fix backfacing detection for weird mesh normals --- src/shaders/pbr.frag | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/shaders/pbr.frag b/src/shaders/pbr.frag index 825ed9dd..9ed07330 100644 --- a/src/shaders/pbr.frag +++ b/src/shaders/pbr.frag @@ -164,12 +164,11 @@ NormalInfo getNormalInfo(vec3 v) #endif // For a back-facing surface, the tangential basis vectors are negated. - float facing = sign(dot(v, ng)); - if (facing < 0.0) + if (gl_FrontFacing == false) { - t *= facing; - b *= facing; - ng *= facing; + t *= -1.0; + b *= -1.0; + ng *= -1.0; } // Due to anisoptry, the tangent can be further rotated around the geometric normal. From a6ec8b438cb8b821cf4ec9eae4aaa83fd50232a7 Mon Sep 17 00:00:00 2001 From: Moritz Willy Becher Date: Thu, 5 Nov 2020 11:33:51 +0100 Subject: [PATCH 4/4] debug view for geometry normals --- src/renderer.js | 3 +++ src/rendering_parameters.js | 1 + src/shaders/pbr.frag | 6 +++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/renderer.js b/src/renderer.js index 5f7a92cf..7dc193f8 100644 --- a/src/renderer.js +++ b/src/renderer.js @@ -436,6 +436,9 @@ class gltfRenderer case(DebugOutput.WORLDSPACENORMAL): fragDefines.push("DEBUG_WORLDSPACE_NORMAL 1"); break; + case(DebugOutput.GEOMETRYNORMAL): + fragDefines.push("DEBUG_GEOMETRY_NORMAL 1"); + break; case(DebugOutput.TANGENT): fragDefines.push("DEBUG_TANGENT 1"); break; diff --git a/src/rendering_parameters.js b/src/rendering_parameters.js index 201fcb09..3fed2f7c 100644 --- a/src/rendering_parameters.js +++ b/src/rendering_parameters.js @@ -50,6 +50,7 @@ const DebugOutput = ROUGHNESS: "Roughness", NORMAL: "Normal", WORLDSPACENORMAL: "Worldspace Normal", + GEOMETRYNORMAL: "Geometry Normal", TANGENT: "Tangent", BITANGENT: "Bitangent", BASECOLOR: "Base Color", diff --git a/src/shaders/pbr.frag b/src/shaders/pbr.frag index 9ed07330..14ab81e5 100644 --- a/src/shaders/pbr.frag +++ b/src/shaders/pbr.frag @@ -686,8 +686,12 @@ void main() #endif #endif + #ifdef DEBUG_GEOMETRY_NORMAL + g_finalColor.rgb = (normalInfo.ng + 1.0) / 2.0; + #endif + #ifdef DEBUG_WORLDSPACE_NORMAL - g_finalColor.rgb = (n + 1.0) / 2.0; + g_finalColor.rgb = (n + 1.0) / 2.0; #endif #ifdef DEBUG_TANGENT