If you are generating meshes or importing meshes at runtime you may stumble upon the problem of generating tangents for your mesh. Normal / bumped shaders require that tangent plane basis vectors be calculated for each vertex in your mesh to correctly light or effect your mesh in a consistant manner.

I did some digging and found this code on the unity3d forum (unfortunately their was a slight/tiny error on the c# version) so I thought I would post the working version here for reference. I have also added some ammendments which also apparently calculate bi-normals or bit-tangents should you ever find a need. I believe the code may have originated come from here: Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”. Terathon Software 3D Graphics Library, 2001.

Usage:- TangentSolver(Mesh myMesh); returns void

6394 Total Views 5 Views Today