Morph Target Normals Before Tangent Lighting in glTF
In glTF asset rendering, morph targets alter a mesh's geometry dynamically by blending displacements for positions, normals, and tangents. Morph target normals must be accumulated and normalized before computing tangent space lighting because the tangent-bitangent-normal (TBN) basis relies directly on the final deformed surface orientation. Calculating tangent space lighting or deriving the TBN matrix using un-morphed normals causes normal maps to perturb the surface relative to the wrong reference frame, resulting in distorted highlights, inverted shadows, and visible shading seams during mesh deformation.
Understanding Tangent Space Lighting
Tangent space lighting relies on a local coordinate system constructed at each vertex or fragment, defined by three orthogonal axes:
- Normal (\(N\)): The vector perpendicular to the surface.
- Tangent (\(T\)): A vector lying along the surface, typically aligned with the horizontal texture coordinate (\(U\)).
- Bitangent (\(B\)): The cross product of the normal and tangent (adjusted for handedness), aligned with the vertical texture coordinate (\(V\)).
Normal maps encode surface micro-details as offsets relative to this \(TBN\) frame. When an engine evaluates lighting in tangent space or transforms normal-map samples into world space, the validity of the lighting depends entirely on the \(TBN\) frame accurately reflecting the actual orientation of the triangle's surface.
The Mechanism of glTF Morph Targets
In the glTF 2.0 specification, morph targets are represented as sparse or dense displacement vectors added to the base mesh attributes:
\[\text{Final Normal} = \text{Base Normal} + \sum_{i} (w_i \times \text{Target Normal}_i)\]
A morph target rarely displaces vertices evenly; facial expressions, muscle flexing, and cloth deformation twist and rotate surface polygons. When vertex positions shift, the surface normal changes angle to remain perpendicular to the deformed face.
Why the Order of Operations Matters
If tangent space lighting is calculated using the base normal instead of the fully morphed normal, two critical mathematical failures occur:
- Orthogonality Breakdown: The tangent vector and normal vector must be orthogonal to construct an accurate coordinate frame (often finalized via Gram-Schmidt orthogonalization: \(T' = \text{normalize}(T - N \cdot (N \cdot T))\)). If an undeformed normal is used with a deformed tangent—or if the normal is updated after the \(TBN\) matrix is assembled—the frame shears, breaking the coordinate system.
- Misdirected Normal Map Perturbations: A normal map assumes the \(Z\)-axis of tangent space points directly outward along the current macroscopic surface normal. If a morph target twists a surface \(45^\circ\), the macroscopic normal rotates \(45^\circ\). If the normal map is applied to the old, unrotated basis, the lighting vectors (light direction and view direction) interact with surface details facing the wrong direction. Specular highlights will skate across the surface unnaturally, and diffuse shading will fail to match the dynamic contours.
The Correct Vertex Pipeline
To maintain lighting integrity according to the glTF specification, the graphics pipeline must process vertex attributes in the following strict order:
- Morph Attribute Blending: Apply morph target weights to base positions, normals, and (if present) tangents.
- Normalization: Normalize the resulting deformed normal and tangent vectors, as linear blending can reduce vector magnitude.
- Skinning (if applicable): Apply joint matrices to the morphed attributes.
- TBN Construction: Generate the bitangent via the cross product (\(B = (N \times T) \times \text{sign}\)) and construct the \(TBN\) matrix from the updated vectors.
- Lighting Evaluation: Transform lighting vectors into tangent space (or transform sampled normal-map vectors into world space using the \(TBN\) matrix) and evaluate the material's BRDF.
Applying normal morph targets prior to establishing the tangent space ensures that tangent-space details scale and rotate cohesively with dynamic mesh deformations.