transformDirection
Correctly transforms direction vectors (like normals) between different coordinate spaces (e.g., model space to view space), properly handling transformations under non-uniform scaling and automatically normalizing the result.
Core Advantages
Ensures the correctness of direction vectors like normals after transformation with non-uniform scaling, which is crucial for accurate lighting, while simplifying shader code through encapsulation and automatic normalization.
Common Uses
Dynamic lighting calculations
Environment map reflection and refraction
Anisotropic shading
How to adjust
Adjust by changing the input `direction` vector or `matrix`. Swapping the matrix from `normalMatrix` to `modelWorldMatrix` causes incorrect lighting with non-uniform scaling. Negating the input normal (`normalLocal.negate()`) inverts the lighting. Using a constant vector instead of `normalLocal` can create stylized shading based on the object's own orientation.
Code Examples
1// Transform local normal to view space using transformDirection
2// normalLocal: The local space normal vector from the geometry
3// normalMatrix: The special matrix for correctly transforming normals
4const normalView = transformDirection( normalLocal, normalMatrix );