Normal / Bump Mesh Tangent Generation

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.


By |February 16th, 2011|Unity 3D|3 Comments

Unity3D DAE, OBJ import at runtime 2010.

This code is now extremely out of date and very verbose  and I would now approach the task very differently, see my post on using object serialisation and xml. I now no longer can provide any support for this code, although as this web page keeps on getting hits I thought I better keep the files alive for those for some reason wanting to use this methodology.


By |January 24th, 2011|Unity 3D|4 Comments

Runtime Normal / Bump Maps in Unity3d

Continuing with runtime image manipulation I stumbled across some code on the Unity forums found here, all I had to add was a strength modifier to gain a little more control of the function. The function below “NormalMap” is written in c#, it takes 2 arguments and should be used as such:-

Returned Image(Texture 2d)= NormalMap(Source Image(Texture2D),Strength of Bump(Float));

By |January 24th, 2011|Unity 3D|4 Comments

Runtime Texture Scaling in Unity3d

If you are creating images or altering them in real-time you may need to resize them at some point, so armed with Unity3d’s new GetPixelBilinear(float,float) function we can do a reasonable job of scaling an image at runtime. The function below “ScaleTexture” is written in c#, it takes 3 arguments and should be used as such:-


By |January 24th, 2011|Unity 3D|10 Comments

Unity3d Terrain/Water Procedural Mesh

procedural-screenshot1-300x149The script below creates a simple procedural grid based mesh with height data taken from a procedural perlin noise map, it is a simple combination of two of the scripts supplied in the Unity3d procedural examples found here. For the script to run you will also need a copy of the perlin.cs plugin script file also found in the Unity3d procedural examples and to place it in a folder named Plugins in your project assets directory.


By |January 14th, 2011|Unity 3D|2 Comments