Generate Procedural Mesh

I’m still having problems with the vertex tangents. The current algorithm seems to use 2 edge lengths to generate the tangents which give opposite results for 2 triangles that share the same edge. Can someone point me in the right direction for calculating tangents for a quad (or preferably several quads)? Thanks.