如何在 Blender 2.80 Python 中导出切线?
How to export tangents in Blender 2.80 Python?
我正在编写一个导出器并且已经可以使用法线、位置和纹理坐标,但是如何访问切线?这是我的出口商的代码示例:
def get_vertex_pnt( self, obj_prop, mesh, face, face_vi ):
# position
co = obj_prop[ OBJ.LOC ] + mathutils.Vector( obj_prop[ OBJ.ROT ] @ mathutils.Vector([ \
mesh.vertices[ face.vertices[ face_vi ] ].co[ 0 ] * obj_prop[ OBJ.SCA ][ 0 ], \
mesh.vertices[ face.vertices[ face_vi ] ].co[ 1 ] * obj_prop[ OBJ.SCA ][ 1 ], \
mesh.vertices[ face.vertices[ face_vi ] ].co[ 2 ] * obj_prop[ OBJ.SCA ][ 2 ] \
]))
# normal
if face.use_smooth:
if mesh.use_auto_smooth:
no = mathutils.Vector( obj_prop[ OBJ.ROT ] @ get_autosmooth_normal( mesh, face, face.vertices[ face_vi ] ))
else:
no = mathutils.Vector( obj_prop[ OBJ.ROT ] @ mesh.vertices[ face.vertices[ face_vi ] ].normal )
else:
no = mathutils.Vector( obj_prop[ OBJ.ROT ] @ face.normal )
color = ( 1.0, 1.0, 1.0, 1.0 )
outVertex = Vertex()
outVertex.co = co
outVertex.normal = no
outVertex.color = color
outVertex.tangent = (1.0, 0.0, 0.0, 1.0)
return outVertex
def readMeshes( self, context ):
...
for face in obj.data.loop_triangles:
for vertex_id in (0, 1, 2):
vertex_pnt = self.get_vertex_pnt(object, obj.data, face, vertex_id)
mesh.vertices.append( vertex_pnt )
tri = [ f * 3 + 0, f * 3 + 1, f * 3 + 2 ]
mesh.faces.append( tri )
f = f + 1
for uv_layer in obj.data.uv_layers:
for tri in obj.data.loop_triangles:
for loop_index in tri.loops:
mesh.vertices[ u ].uv = uv_layer.data[ loop_index ].uv
u = u + 1
我应该如何修改代码以导出切线?我正在使用 Blender 2.80。
我找到了阅读它们的方法:
for p in obj.data.loops:
print( "tangent: " + str( p.tangent ) + ", bitangent sign: " + str( p.bitangent_sign ) )
我正在编写一个导出器并且已经可以使用法线、位置和纹理坐标,但是如何访问切线?这是我的出口商的代码示例:
def get_vertex_pnt( self, obj_prop, mesh, face, face_vi ):
# position
co = obj_prop[ OBJ.LOC ] + mathutils.Vector( obj_prop[ OBJ.ROT ] @ mathutils.Vector([ \
mesh.vertices[ face.vertices[ face_vi ] ].co[ 0 ] * obj_prop[ OBJ.SCA ][ 0 ], \
mesh.vertices[ face.vertices[ face_vi ] ].co[ 1 ] * obj_prop[ OBJ.SCA ][ 1 ], \
mesh.vertices[ face.vertices[ face_vi ] ].co[ 2 ] * obj_prop[ OBJ.SCA ][ 2 ] \
]))
# normal
if face.use_smooth:
if mesh.use_auto_smooth:
no = mathutils.Vector( obj_prop[ OBJ.ROT ] @ get_autosmooth_normal( mesh, face, face.vertices[ face_vi ] ))
else:
no = mathutils.Vector( obj_prop[ OBJ.ROT ] @ mesh.vertices[ face.vertices[ face_vi ] ].normal )
else:
no = mathutils.Vector( obj_prop[ OBJ.ROT ] @ face.normal )
color = ( 1.0, 1.0, 1.0, 1.0 )
outVertex = Vertex()
outVertex.co = co
outVertex.normal = no
outVertex.color = color
outVertex.tangent = (1.0, 0.0, 0.0, 1.0)
return outVertex
def readMeshes( self, context ):
...
for face in obj.data.loop_triangles:
for vertex_id in (0, 1, 2):
vertex_pnt = self.get_vertex_pnt(object, obj.data, face, vertex_id)
mesh.vertices.append( vertex_pnt )
tri = [ f * 3 + 0, f * 3 + 1, f * 3 + 2 ]
mesh.faces.append( tri )
f = f + 1
for uv_layer in obj.data.uv_layers:
for tri in obj.data.loop_triangles:
for loop_index in tri.loops:
mesh.vertices[ u ].uv = uv_layer.data[ loop_index ].uv
u = u + 1
我应该如何修改代码以导出切线?我正在使用 Blender 2.80。
我找到了阅读它们的方法:
for p in obj.data.loops:
print( "tangent: " + str( p.tangent ) + ", bitangent sign: " + str( p.bitangent_sign ) )