A surface normal must stay perpendicular to its surface after nonuniform scaling.
dotnet add package RecursiveAI.OneMathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMath; var matrix = Matrix3x3D.CreateScale( new(2, 1, 1)); if (!matrix.TryInvert(out var inverse)) throw new InvalidOperationException( "Singular matrix"); Console.WriteLine(inverse.Transposed() * new Vector3D(1, 1, 0));
(0.5, 1, 0)Unzip the example, then dotnet run.
The full program
The full program also transforms a perpendicular tangent. Their dot product remains zero with inverse transpose, but becomes 3 if the original matrix is incorrectly used for the normal. A singular matrix returns false from TryInvert; the snippet at the top turns that failure into an exception.
using RecursiveAI.OneMath; var linear = Matrix3x3D.CreateScale(new(2, 1, 1)); if (!linear.TryInvert(out var inverse)) throw new InvalidOperationException("Singular matrix"); var normal = new Vector3D(1, 1, 0); var tangent = new Vector3D(1, -1, 0); var transformedNormal = inverse.Transposed() * normal; var transformedTangent = linear * tangent; Console.WriteLine(transformedNormal); Console.WriteLine(Vector3D.Dot( transformedNormal, transformedTangent)); Console.WriteLine(Vector3D.Dot( linear * normal, transformedTangent)); var singular = Matrix3x3D.CreateScale(new(0, 1, 1)); Console.WriteLine(singular.TryInvert(out _));
(0.5, 1, 0) 0 3 False
Variations
Normalize after transforming
using RecursiveAI.OneMath; var matrix = Matrix3x3D.CreateScale( new(2, 1, 1)); if (!matrix.TryInvert(out var inverse)) throw new InvalidOperationException( "Singular matrix"); var normal = inverse.Transposed() * new Vector3D(1, 1, 0); Console.WriteLine(normal.Normalized().Length());
0.9999999999999999Related recipes
Compose affine scale and shear · Invert a matrix
The short answer
For an invertible three-by-three linear matrix L, transform a surface normal with (L⁻¹)ᵀ, not L. In OneMath, call TryInvert, check its Boolean result, then call Transposed on the inverse and multiply by the original normal. Normalize the result afterward if the next operation requires a unit vector.
The example deliberately uses a nonuniform scale: x doubles while y and z stay unchanged. The normal (1, 1, 0) becomes (0.5, 1, 0). It is not yet unit length. Keeping that unnormalized result in the first example makes the reciprocal scale visible rather than hiding it in rounded unit-vector components.
Why transform normals with the transpose of the inverse matrix?
A normal describes a perpendicular direction to a surface. Pick a tangent t on that surface and a normal n with dot product zero. After transforming the surface by L, the tangent becomes Lt. The transformed normal must still have zero dot product with that transformed tangent.
Choosing n′ = (L⁻¹)ᵀn preserves the dot product: n′ᵀLt = nᵀL⁻¹Lt = nᵀt. See the normal-transformation derivation for the same perpendicularity argument.
In the download, the tangent starts at (1, −1, 0). The scaled tangent is (2, −1, 0), whose dot product with (0.5, 1, 0) is exactly zero for these inputs. Incorrectly scaling the normal directly gives (2, 1, 0), whose dot product with that tangent is 3.
How do you build an inverse transpose matrix in C#?
Use the linear three-by-three matrix from your object transform. For an Affine3D, that is its Linear field. Translation belongs to point transformation and is absent from this normal calculation. A tangent or displacement uses the ordinary linear matrix; the surface normal uses its inverse transpose.
OneMath’s matrix documentation defines column-vector multiplication and the instance methods TryInvert and Transposed. The snippet at the top follows that convention directly. If many normals share one transform, compute the inverse-transpose matrix once and reuse it for those normals rather than inverting separately for every vertex.
Inverse transpose preserves the perpendicular relation but does not generally preserve length. Normalize after applying it when a lighting calculation or other consumer expects unit normals. The variation prints a length just below one because its result is represented in floating point.
Can you transform normals with a singular matrix?
A zero axis scale can collapse a surface and makes the linear matrix singular. There is no inverse-transpose matrix for that transform. The full program explicitly tries a zero x scale and prints False from TryInvert.
Decide what collapse means for your geometry: skip the surface, reject the transform, or derive a normal from remaining transformed edges when those edges still define a plane. Do not continue with the failed inverse. OneMath documents failure when the determinant is zero or nonfinite and returns a zero matrix in that case. It does not test conditioning, so an invertible matrix very close to singular still needs an application-specific accuracy decision.