I'm writing a script that exports a mesh from Blender to a game format. The format stores, per vertex, their normals, tangents, and what i assume is a bitangent sign. The latter is a single signed byte that is either 127 or -128, and the block is referred to as "binormal".
As i understand, Blender doesn't normally store tangents, and that they first have to be calculated by calling calc_tangents() on the mesh. But once they are calculated, they, as well as smooth\custom normals, are stored per loop rather than per vertex. Then a vertex can have multiple normals\tangents, depending on how many loops it is a part of.
So how do i get per vertex normals\tangents out of that? Do i just calculate an average vector for each vertex? So far i tried this:
def getAveragedNormals(obj):
msh = obj.data
msh.calc_tangents()
loopVertsData = [ list() for i in range( len(msh.vertices) ) ]
for loop in msh.loops:
loopVertsData[loop.vertex_index].append(
[loop.normal, loop.tangent, loop.bitangent, loop.bitangent_sign]
)
averagedNormals = [ list() for i in range( len(msh.vertices) ) ]
for i in range( len(loopVertsData) ):
avgNormals = [
Vector( [0.0, 0.0, 0.0] ),
Vector( [0.0, 0.0, 0.0] ),
Vector( [0.0, 0.0, 0.0] ),
0.0
]
avgFactor = ( 1.0 / len(loopVertsData[i]) )
for vd in loopVertsData[i]:
for j in range(4):
avgNormals[j] = ( avgNormals[j] + (vd[j] * avgFactor) )
averagedNormals[i] = avgNormals
return averagedNormals
The result seems to look ok in game, but when there are issues, it's not like immediately obvious. So i was hoping to get an input from someone more knowledgeable on this topic. Is that how i'm supposed to calculate these values, or is there another way? And what is bitangent sign?