Skip to content

Commit

Permalink
Merge pull request #21203 from mrdoob/glfrontfacing
Browse files Browse the repository at this point in the history
ShaderChunks: isFrontFacing boolean to faceDirection float
  • Loading branch information
mrdoob authored Feb 4, 2021
2 parents 37724a5 + a9a3212 commit f99dfaa
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default /* glsl */`
}
vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy, bool isFrontFacing ) {
vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy, float faceDirection ) {
// Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
Expand All @@ -33,9 +33,7 @@ export default /* glsl */`
vec3 R1 = cross( vSigmaY, vN );
vec3 R2 = cross( vN, vSigmaX );
float fDet = dot( vSigmaX, R1 );
fDet *= ( float( isFrontFacing ) * 2.0 - 1.0 );
float fDet = dot( vSigmaX, R1 ) * faceDirection;
vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );
return normalize( abs( fDet ) * surf_norm - vGrad );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default /* glsl */`
#else
clearcoatNormal = perturbNormal2Arb( - vViewPosition, clearcoatNormal, clearcoatMapN, isFrontFacing );
clearcoatNormal = perturbNormal2Arb( - vViewPosition, clearcoatNormal, clearcoatMapN, faceDirection );
#endif
Expand Down
12 changes: 6 additions & 6 deletions src/renderers/shaders/ShaderChunk/normal_fragment_begin.glsl.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default /* glsl */`
bool isFrontFacing = true;
float faceDirection = 1.0;
#ifdef FLAT_SHADED
Expand All @@ -9,7 +9,7 @@ bool isFrontFacing = true;
vec3 fdy = vec3( dFdy( vViewPosition.x ), dFdy( vViewPosition.y ), dFdy( vViewPosition.z ) );
vec3 normal = normalize( cross( fdx, fdy ) );
isFrontFacing = dot( vec3( 0, 0, 1 ), normal ) > 0.0;
faceDirection = sign( dot( vec3( 0, 0, 1 ), normal ) );
#else
Expand All @@ -25,9 +25,9 @@ bool isFrontFacing = true;
// Workaround for Adreno GPUs broken gl_FrontFacing implementation
// https://stackoverflow.com/a/32621243
isFrontFacing = dot( normal, normalize( cross( fdx, fdy ) ) ) > 0.0;
faceDirection = sign( dot( normal, normalize( cross( fdx, fdy ) ) ) );
normal = normal * ( float( isFrontFacing ) * 2.0 - 1.0 );
normal = normal * faceDirection;
#endif
Expand All @@ -38,8 +38,8 @@ bool isFrontFacing = true;
#ifdef DOUBLE_SIDED
tangent = tangent * ( float( isFrontFacing ) * 2.0 - 1.0 );
bitangent = bitangent * ( float( isFrontFacing ) * 2.0 - 1.0 );
tangent = tangent * faceDirection;
bitangent = bitangent * faceDirection;
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default /* glsl */`
#ifdef DOUBLE_SIDED
// We can't compute isFrontFacing if the model doesn't have normals
// We can't compute faceDirection if the model doesn't have normals
normal = normal * ( float( gl_FrontFacing ) * 2.0 - 1.0 );
Expand All @@ -31,13 +31,13 @@ export default /* glsl */`
#else
normal = perturbNormal2Arb( -vViewPosition, normal, mapN, isFrontFacing );
normal = perturbNormal2Arb( -vViewPosition, normal, mapN, faceDirection );
#endif
#elif defined( USE_BUMPMAP )
normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd(), isFrontFacing );
normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd(), faceDirection );
#endif
`;
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default /* glsl */`
// Per-Pixel Tangent Space Normal Mapping
// http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html
vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec3 mapN, bool isFrontFacing ) {
vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec3 mapN, float faceDirection ) {
// Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
Expand All @@ -34,7 +34,7 @@ export default /* glsl */`
mat3 tsn = mat3( S, T, N );
mapN.xy *= ( float( isFrontFacing ) * 2.0 - 1.0 );
mapN.xy *= faceDirection;
return normalize( tsn * mapN );
Expand Down

0 comments on commit f99dfaa

Please sign in to comment.