faceForward
Orients a normal vector n to face towards an incident vector i, using a reference normal nRef to determine the orientation.
It's typically used in shading calculations to ensure a normal vector consistently points "outward" from a surface, relative to an incident vector (like a view vector or light vector).
The logic is as follows:
If the dot product of the reference normal
nRefand the incident vectoriis negative (dot(nRef, i) < 0f), it meansiis hitting the "front side" of the surface (the side from whichnRefis pointing away). In this case, the original normalnis returned, assuming it's already oriented correctly for the front side.Otherwise (
dot(nRef, i) >= 0f),iis hitting the "back side" of the surface (the sidenRefis pointing towards), or is tangent to it. In this case, the negated normal-nis returned, effectively flippingnto orient it correctly for this side.
The goal is to produce a normal that, when dotted with i, would typically yield a negative value if i is considered a direction towards the surface (e.g., a view ray).
Return
Returns n if dot(nRef, i) < 0f, otherwise returns -n.
Parameters
The normal vector to be potentially oriented. This could be a geometric normal, a shading normal, or any normal that needs consistent orientation.
The incident vector (e.g., a vector from the camera to the surface, or from a light source to the surface).
The reference normal, typically the true geometric normal of the surface. It determines which side is considered "front" or "back".