directionToFaceDirection
Converts a direction vector into a face-aware one based on the material side: flips for BackSide, multiplies by faceDirection (+1/-1 per fragment) for DoubleSide, unchanged for FrontSide.
Core Advantages
Keeps direction-dependent terms (N·V, Fresnel, rim) consistent on back faces or double-sided materials without branching.
Common Uses
Wrap viewDirection or a light vector on DoubleSide materials to stabilize N·V, Fresnel, or rim lighting.
On BackSide materials, flip the direction once to reuse front-face formulas.
Use with reflect/refract or custom half vectors so reflection/refraction respects face orientation.
Pair with faceDirection to replace frontFacing-based if/else.
How to adjust
Set material.side to control behavior: FrontSide leaves the vector as is; BackSide flips it; DoubleSide multiplies by faceDirection per fragment. Combine with normalize, dot, reflect, or refract. Avoid double flipping if your input is already face corrected.
Code Examples
1// Make view direction stable across faces
2material.side = THREE.DoubleSide;
3
4const V = normalize( directionToFaceDirection( viewDirection ) );
5const rim = pow( saturate( 1.0 - dot( normalView, V ) ), 3.0 );
6material.emissiveNode = vec3( 1.0, 0.6, 0.2 ).mul( rim );