Hi. I have flat surface that I am planing to apply a normal map to. As its flat and always faces up I am just setting the normals in the vert shader like so…
v.normal = float3(0,1,0);
I need to set the tangents as well. Now I know the tangent is perpendicular to the normal but on the x axis or the z axis?
Tangents can actually be made based on any basis you like, but the standard thing to do is to align the tangent and bitangent (the lattter is computed in the shader) so that they’re perpendicular to the normal, and correlate with your U and V coordinates rotated into object space. That way, you can “read” your normal maps, or use any other stock normal map, because that’s how everybody else does it.
Bitangents are calculated in the shader by cross(normal, tangent.xyz) * tangent.w. tangent.w is normally 1, making it redundant in most cases. However, your tangents will point in (-U) if your UVs are mirrored so that their derivative in U is negative In that case, tangent.w is -1, in order to preserve the normal map’s y component, even though its x has to be flipped. Similarly, tangent.w will have to be -1 if UV’s derivative in V is negative: the tangent still points along U, but the bitangent has to be flipped. If the derivatives in both U and V are negative, however, tangent.w is 1. You can think of tangent.w as switching from the right- to the left-hand rule, or being a way to do cross(tangent.xyz, normal) instead of cross(normal, tangent.xyz).
Keep in mind that some 3D modeling apps do actually use the equivalent of cross(tangent.xyz, normal) * tangent.w, so the green channel of their baked normal maps needs to be inverted for use in Unity. Or at least, I’ve had to deal with that before, but it’s been a while. Maybe everything’s standardized by now.