Generate Procedural Mesh

tangent fix for wonky normal along tri edge

not sure if anyone is still using this,https://wiki.unrealengine.com/Procedural_Mesh_Generation
i am using a modified version, its just easier(dont need multiple materials) for what i am doing.
but.
there is a problem with the tangent calculator,

but i have a fix…
where you build the SceneProxy
replace:


			const FVector TangentX = Edge01.SafeNormal();
			const FVector TangentZ = (Edge02 ^ Edge01).SafeNormal();
			const FVector TangentY = (TangentX ^ TangentZ).SafeNormal();

with:



			//tangents werent working quite right here, so...
			float DeltaU1 = Tri.Vertex1.U - Tri.Vertex0.U;
			float DeltaV1 = Tri.Vertex1.V - Tri.Vertex0.V;
			float DeltaU2 = Tri.Vertex2.U - Tri.Vertex0.U;
			float DeltaV2 = Tri.Vertex2.V - Tri.Vertex0.V;
			
			float f = 1.0f / (DeltaV1 * DeltaU2 - DeltaU1 * DeltaV2);
			
			FVector Tangent;
			FVector BTangent;//not used
			
			Tangent = (Edge01 * DeltaU2   - Edge02 * DeltaU1)*f;
			BTangent = (Edge02 * DeltaV1   - Edge01 * DeltaV2)*f;//not used
			
			const FVector Normal = FVector::CrossProduct((Tri.Vertex1.Position - Tri.Vertex0.Position),(Tri.Vertex2.Position - Tri.Vertex0.Position));

			FVector NTangentX = Tangent;
			FVector NTangentZ = (Tangent ^ Normal);
			FVector NTangentY = (NTangentX ^ NTangentZ);
			NTangentX.Normalize();
			NTangentY.Normalize();
			NTangentZ.Normalize();
			const FVector TangentX = NTangentX;
			const FVector TangentZ = NTangentY; 
			const FVector TangentY = NTangentZ;

and you will go from this:


to this:

hope this helps someone.