Advertisement
cfox04

build_game_mesh

Mar 11th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1.     newGeometry = Geometry()
  2.     colors = {}
  3.     uvs = {}
  4.     loops = len(mesh.loops)
  5.     verts = len(mesh.vertices)
  6.     visit = verts * [False]
  7.     # Loop through each vertex color layer
  8.     for vertex_color in mesh.vertex_colors:
  9.         # Look into each loop's vertex
  10.         for loop in range(loops):
  11.             v = mesh.loops[loop].vertex_index
  12.             c = vertex_color.data[loop].color
  13.             if not visit[v]:
  14.                 colors[v] = c
  15.                 visit[v] = True
  16.     # Loop through each uv layer
  17.     for uv_layer in mesh.uv_layers:
  18.         for loop in range(loops):
  19.             vertex = mesh.loops[loop].vertex_index
  20.             uv = uv_layer.data[loop].uv
  21.             uvs[vertex] = uv
  22.  
  23.     # Loop through mesh vertices
  24.     for vertex in mesh.vertices:
  25.         i = vertex.index
  26.         newVertex = Vertex()
  27.         newVertex.Position = Vector3(vertex.co[0], vertex.co[1], vertex.co[2])
  28.         newVertex.Normal = Vector3(vertex.normal[0], vertex.normal[1], vertex.normal[2])
  29.         if len(colors) > 0:
  30.             newVertex.Color = RGBA(
  31.                 colors[i][0], colors[i][1], colors[i][2], colors[i][3]
  32.             )
  33.         newVertex.UV = Vector2(uvs[i][0], uvs[i][1])
  34.         newGeometry.Vertices.append(newVertex)
  35.  
  36.     mesh.calc_loop_triangles()
  37.     # Loop through mesh faces in order to get vertex indices
  38.     for tri in mesh.loop_triangles:
  39.         for i in tri.vertices:
  40.             newGeometry.Indices.append(i)
  41.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement