]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_mesh.py
Use low-level handles when computing descriptor set hashes
[libs/gl.git] / blender / io_mspgl / export_mesh.py
1 import itertools
2 import os
3 import bpy
4 import mathutils
5
6 class MeshExporter:
7         def export_mesh(self, context, mesh_or_obj, progress):
8                 from .mesh import Mesh, create_mesh_from_object
9
10                 if type(mesh_or_obj)==Mesh:
11                         mesh = mesh_or_obj
12                 else:
13                         progress.push_task("", 0.0, 0.9)
14                         mesh = create_mesh_from_object(context, mesh_or_obj, progress)
15                         progress.pop_task()
16
17                 from .datafile import Resource, Statement, Token
18                 resource = Resource(mesh.name+".mesh", "mesh")
19                 statements = resource.statements
20
21                 st = Statement("vertices", Token("VERTEX3_FLOAT"))
22                 stride = 12
23                 if mesh.vertices[0].color:
24                         st.append(Token("COLOR4_UBYTE"))
25                         stride += 4
26                 if mesh.uv_layers:
27                         for u in mesh.uv_layers:
28                                 size = len(u.uvs[0])
29                                 min_val = min(*u.uvs[0])
30                                 max_val = max(*u.uvs[1])
31                                 for c in u.uvs:
32                                         min_val = min(min_val, *c)
33                                         max_val = max(max_val, *c)
34                                 uv_type = "USHORT" if min_val>=0.0 and max_val<=1.0 else "FLOAT"
35                                 if uv_type=="FLOAT" and stride%4:
36                                         pad = 4-stride%4
37                                         st.append(Token("PADDING{}".format(pad)))
38                                         stride += pad
39                                 if u.unit==0:
40                                         st.append(Token("TEXCOORD{}_{}".format(size, uv_type)))
41                                 else:
42                                         st.append(Token("TEXCOORD{}_{}_{}".format(size, u.unit, uv_type)))
43                                 stride += (2 if uv_type=="USHORT" else 4)*size
44                 if mesh.vertex_groups:
45                         st.append(Token("WEIGHT{}_USHORT".format(mesh.max_groups_per_vertex)))
46                         st.append(Token("GROUP{}_UBYTE".format(mesh.max_groups_per_vertex)))
47                         stride += 3*mesh.max_groups_per_vertex
48                 st.append(Token("NORMAL3_BYTE"))
49                 stride += 3
50                 if mesh.uv_layers and mesh.tangent_vecs:
51                         st.append(Token("TANGENT3_BYTE"))
52                         stride += 3
53                 if stride%4:
54                         pad = 4-stride%4
55                         st.append(Token("PADDING{}_UBYTE".format(pad)))
56                         stride += pad
57
58                 normal = None
59                 color = None
60                 uvs = [None]*len(mesh.uv_layers)
61                 tan = None
62                 group = None
63                 weight = None
64                 for v in mesh.vertices:
65                         if v.normal!=normal:
66                                 st.sub.append(Statement("normal", *v.normal))
67                                 normal = v.normal
68                         if v.color!=color:
69                                 st.sub.append(Statement("color", *v.color))
70                                 color = v.color
71                         for i, u in enumerate(mesh.uv_layers):
72                                 if v.uvs[i]!=uvs[i]:
73                                         if u.unit==0:
74                                                 st.sub.append(Statement("texcoord", *v.uvs[i]))
75                                         else:
76                                                 st.sub.append(Statement("multitexcoord", u.unit, *v.uvs[i]))
77                                         uvs[i] = v.uvs[i]
78                         if mesh.tangent_vecs:
79                                 if v.tan!=tan:
80                                         st.sub.append(Statement("tangent", *v.tan))
81                                         tan = v.tan
82                         if mesh.vertex_groups:
83                                 v_group = [g.group for g in v.groups]
84                                 v_weight = [g.weight for g in v.groups]
85                                 if v_group!=group:
86                                         st.sub.append(Statement("group", *v_group))
87                                         group = v_group
88                                 if v_weight!=weight:
89                                         st.sub.append(Statement("weight", *v_weight))
90                                         weight = v_weight
91                         st.sub.append(Statement("vertex", *v.co))
92
93                 statements.append(st)
94
95                 if mesh.use_strips:
96                         for s in mesh.vertex_sequence:
97                                 st = Statement("batch", Token("TRIANGLE_STRIP"))
98                                 for i in range(0, len(s), 32):
99                                         st.sub.append(Statement("indices", *(v.index for v in s[i:i+32])))
100                                 statements.append(st)
101                 else:
102                         st = Statement("batch", Token('TRIANGLES'))
103                         for f in mesh.faces:
104                                 st.sub.append(Statement("indices", *(v.index for v in f.vertices)))
105                         statements.append(st)
106
107                 if mesh.lines:
108                         st = Statement("batch", Token('LINES'))
109                         for l in mesh.lines:
110                                 st.sub.append(Statement("indices", *(v.index for v in l.vertices)))
111                         statements.append(st)
112
113                 if mesh.winding_test:
114                         statements.append(Statement("winding", Token('COUNTERCLOCKWISE')))
115
116                 progress.set_progress(1.0)
117
118                 return resource