Skip to content

Commit edeb393

Browse files
Normal mapping - Flip normal direction for backfaces
1 parent bab3c0b commit edeb393

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/shaders/common/closest_hit.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ bool ClosestHit(Ray r, inout State state, inout LightSampleRec lightSampleRec)
241241
vec3 normal = normalize(n0.xyz * bary.x + n1.xyz * bary.y + n2.xyz * bary.z);
242242

243243
state.normal = normalize(transpose(inverse(mat3(transform))) * normal);
244-
state.ffnormal = dot(state.normal, r.direction) <= 0.0 ? state.normal : state.normal * -1.0;
244+
state.ffnormal = dot(state.normal, r.direction) <= 0.0 ? state.normal : -state.normal;
245245

246246
// Calculate tangent and bitangent
247247
vec3 deltaPos1 = vert1.xyz - vert0.xyz;

src/shaders/common/pathtrace.glsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ void GetMaterials(inout State state, in Ray r)
8080
vec3 texNormal = texture(textureMapsArrayTex, vec3(texUV, texIDs.z)).rgb;
8181
texNormal = normalize(texNormal * 2.0 - 1.0);
8282

83-
state.normal = normalize(state.tangent * texNormal.x + state.bitangent * texNormal.y + state.ffnormal * texNormal.z);
84-
state.ffnormal = normalize(state.normal);
83+
vec3 origNormal = state.normal;
84+
state.normal = normalize(state.tangent * texNormal.x + state.bitangent * texNormal.y + state.normal * texNormal.z);
85+
state.ffnormal = dot(origNormal, r.direction) <= 0.0 ? state.normal : -state.normal;
8586
}
8687

8788
// Emission Map

0 commit comments

Comments
 (0)