-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy paththreedo.py
243 lines (208 loc) · 8.46 KB
/
threedo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import io
import re
SUBSECTION_RE = re.compile(br'(.+)\s+\d+\Z') # [ITEM TYPE] [COUNT]EOL
ITEM_RE = re.compile(br'(\d+):') # [IDX]: ...
MAT_RE = re.compile(br'(\d+):\s+(\S+)') # [IDX]: [FILENAME]
INTEGER_VALUE_RE = re.compile(br'([\D ]*)\s+(-?\d+)\Z')
FLOAT_FRAGMENT2 = r'-?\d*(?:\.\d+)?(?:[eE][-+]?\d+)?'
VERTEX_XYZI_RE = re.compile(
fr'(\d+):\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})'.encode())
VERTEX_UV_RE = re.compile(
fr'(\d+):\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})'.encode())
NORMAL_RE = re.compile(
fr'(\d+):\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})'.encode())
FACE_RE = re.compile(
fr'(\d+):\s+(-?\d+?)\s+(0x[0-9a-fA-F]+)\s+(-?\d+?)\s+(-?\d+?)\s+(-?\d+?)\s+({FLOAT_FRAGMENT2})\s+(\d+)'.encode())
NODE_RE = re.compile(
fr'(\d+):\s+(0x[0-9a-fA-F]+)\s+(0x[0-9a-fA-F]+)\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})\s+({FLOAT_FRAGMENT2})\s+(\D+)'.encode())
def _get_face_rest_re(nverts):
text = br'\s+'
text += br'(-?\d+),\s*(-?\d+)\s+' * (nverts - 1)
text += br'(-?\d+),\s*(-?\d+)' # no trailing whitespace
rest_re = re.compile(text)
return rest_re
class ThreedoFile:
def __init__(self, sections):
self._read_materials(sections[b'modelresource'])
self._read_geometry(sections[b'geometrydef'])
self._read_hierarchy(sections[b'hierarchydef'])
def _read_materials(self, lines):
mats = {}
for line in lines:
match = MAT_RE.match(line)
if match:
key = int(match.group(1))
mats[key] = match.group(2)
self.materials = mats
def _read_hierarchy(self, lines):
nodes = {}
for line in lines:
match = NODE_RE.match(line)
if match:
key = int(match.group(1))
# flags = int(match.group(2)[2:], 16)
# ntype = int(match.group(3)[2:], 16)
mesh = int(match.group(4))
parent = int(match.group(5))
# child = int(match.group(6))
# sibling = int(match.group(7))
# num_children = int(match.group(8))
x = float(match.group(9))
y = float(match.group(10))
z = float(match.group(11))
pitch = float(match.group(12))
yaw = float(match.group(13))
roll = float(match.group(14))
pivot_x = float(match.group(15))
pivot_y = float(match.group(16))
pivot_z = float(match.group(17))
# name = match.group(18)
nodes[key] = {
'mesh': mesh,
'offset': (x, y, z),
'rot': (pitch, yaw, roll),
'pivot': (pivot_x, pivot_y, pivot_z),
'children': [],
'parent': parent
}
# build hierarchy and find root
root_nodes = []
for _, n in nodes.items():
p = n['parent']
if p >= 0:
nodes[p]['children'].append(n)
else:
root_nodes.append(n)
del n['parent'] # no longer need this bit
if not root_nodes:
raise Exception("No root nodes!")
self.root_nodes = root_nodes
def _read_geometry(self, lines):
geosets = {}
curgeoset = {}
curmesh = {}
vdata = {}
target = ''
for line in lines:
sline = line.lstrip()
if sline.startswith(b'VERTICES'):
target = 'xyzi'
continue
elif sline.startswith(b'TEXTURE VERTICES'):
target = 'uv'
continue
elif sline.startswith(b'VERTEX NORMALS'):
target = 'norm'
continue
elif sline.startswith(b'FACES'):
target = 'faces'
continue
elif sline.startswith(b'FACE NORMALS'):
target = 'fnorm'
continue
match = INTEGER_VALUE_RE.match(line)
if match:
target = ''
name = match.group(1)
value = int(match.group(2))
if name == b'GEOSET':
geosets[value] = curgeoset = {}
elif name == b'MESH':
curgeoset[value] = curmesh = {}
vdata = {'xyzi': {}, 'uv': {}, 'norm': {}}
if target == 'xyzi':
match = VERTEX_XYZI_RE.match(line)
if not match:
continue
key = int(match.group(1))
x = float(match.group(2))
y = float(match.group(3))
z = float(match.group(4))
i = float(match.group(5))
vdata[target][key] = (x, y, z, i)
elif target == 'uv':
match = VERTEX_UV_RE.match(line)
if not match:
continue
key = int(match.group(1))
u = float(match.group(2))
v = float(match.group(3))
vdata[target][key] = (u, v)
elif target == 'norm':
match = NORMAL_RE.match(line)
if not match:
continue
key = int(match.group(1))
x = float(match.group(2))
y = float(match.group(3))
z = float(match.group(4))
vdata[target][key] = (x, y, z)
elif target == 'faces':
match = FACE_RE.match(line)
if not match:
continue
key = int(match.group(1))
mat = int(match.group(2))
ftype = int(match.group(3)[2:], 16)
geo = int(match.group(4))
# light = int(match.group(5))
# tex = int(match.group(6))
extra_light = float(match.group(7))
nverts = int(match.group(8))
rest_re = _get_face_rest_re(nverts)
rest = line[match.end(8):]
match = rest_re.match(rest)
if not match:
continue
twosided = (ftype & 0x1) != 0
translucent = (ftype & 0x2) != 0
vertices = []
for i in range(nverts):
xyzi_idx = int(match.group(2 * i + 1))
uv_idx = int(match.group(2 * i + 2))
xyzi = vdata['xyzi'][xyzi_idx]
pos = xyzi[0:3]
norm = vdata['norm'][xyzi_idx]
# check for valid UV index to load skin https://www.massassi.net/levels/files/564.shtml
uv = vdata['uv'][uv_idx] if geo == 4 and uv_idx in vdata['uv'] else (0, 0)
diffuse = [extra_light + xyzi[3]] * 3
vertices.append([pos, uv, diffuse, norm])
if twosided:
for v in reversed(vertices[1:-1]):
v = list(v) # copy vertex to flip normal
v[3] = (-v[3][0], -v[3][1], -v[3][2])
vertices.append(v)
curmesh[key] = {'vertices': vertices, 'geo': geo,
'material': mat, 'translucent': translucent}
# keep only the highest resolution (geoset 0)
self.meshes = geosets[0]
def _strip(line):
start = line.find(b'#')
if start != -1:
line = line[:start]
return line.strip()
def _defines_section(line):
return len(line) > 8 and line[:8].lower() == b'section:'
def _ends_section(line):
return line == b'end' or _defines_section(line)
def _parse_section(lines, f, section):
for line in f:
line = _strip(line)
if not line:
continue
elif _ends_section(line):
return line
lines.append(line)
return b''
def read_from_file(f):
sections = {}
for line in f:
line = _strip(line)
while _defines_section(line):
section = line[8:].strip().lower()
section_lines = []
line = _parse_section(section_lines, f, section)
sections[section] = section_lines
return ThreedoFile(sections)
def read_from_bytes(b):
return read_from_file(io.BytesIO(b))