-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfmt_SMWOS_Mesh.py
More file actions
617 lines (473 loc) · 20 KB
/
fmt_SMWOS_Mesh.py
File metadata and controls
617 lines (473 loc) · 20 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# author: haru233
from inc_noesis import *
import struct
def registerNoesisTypes():
handle = noesis.register("Spider-Man: Web of Shadows", ".standalone_mesh")
noesis.setHandlerTypeCheck(handle, checkType)
noesis.setHandlerLoadModel(handle, LoadModel)
# noesis.setHandlerWriteModel(handle, WriteModel)
return 1
def checkType(data):
return 1
class D3D9_DATATYPE:
FLOAT1 = 0,
FLOAT2 = 1,
FLOAT3 = 2,
FLOAT4 = 3,
D3DCOLOR = 4,
UBYTE4 = 5,
SHORT2 = 6,
SHORT4 = 7,
UBYTE4N = 8,
SHORT2N = 9,
SHORT4N = 10,
USHORT2N = 11,
USHORT4N = 12,
UDEC3 = 13,
DEC3N = 14,
FLOAT16_2 = 15,
FLOAT16_4 = 16,
UNUSED = 17
D3D9_ELEMENTTYPE = {
0: "POSITION",
1: "BLENDWEIGHT",
2: "BLENDINDICES",
3: "NORMAL",
4: "PSIZE",
5: "TEXCOORD",
6: "TANGENT",
7: "BINORMAL",
8: "TESSFACTOR",
9: "POSITIONT",
10: "COLOR",
11: "FOG",
12: "DEPTH",
13: "SAMPLE"
}
def half_to_float(h):
s = (h >> 15) & 1
e = (h >> 10) & 0x1F
m = h & 0x3FF
if e == 0:
return ((-1)**s) * (m / 2**10) * 2**-14
if e == 31:
return float('inf') * ((-1)**s) if m == 0 else float('nan')
return ((-1)**s) * (1 + m / 2**10) * 2**(e-15)
# Map datatype to size (bytes) and struct format for unpacking
def Get_DataType(dt):
if dt == 0: # FLOAT1
return (4, "<f")
elif dt == 1: # FLOAT2
return (8, "<2f")
elif dt == 2: # FLOAT3
return (12, "<3f")
elif dt == 3: # FLOAT4
return (16, "<4f")
elif dt == 4: # D3DCOLOR (ARGB)
return (4, "<4B")
elif dt == 5: # UBYTE4
return (4, "<4B")
elif dt == 6: # SHORT2
return (4, "<2h")
elif dt == 7: # SHORT4
return (8, "<4h")
elif dt == 8: # UBYTE4N (normalized 0-1)
return (4, "<4B")
elif dt == 9: # SHORT2N (normalized -1..1)
return (4, "<2h")
elif dt == 10: # SHORT4N (normalized -1..1)
return (8, "<4h")
elif dt == 11: # USHORT2N (normalized 0..1)
return (4, "<2H")
elif dt == 12: # USHORT4N (normalized 0..1)
return (8, "<4H")
elif dt == 13: # UDEC3 (10:10:10 unsigned packed)
return (4, "<I")
elif dt == 14: # DEC3N (10:10:10 signed normalized)
return (4, "<I")
elif dt == 15: # FLOAT16_2 (half 2 floats)
return (4, "<2H")
elif dt == 16: # FLOAT16_4 (half 4 floats)
return (8, "<4H")
elif dt == 17: # UNUSED
return (0, "")
else:
return (0, "")
def LoadModel(data, mdlList):
bs = NoeBitStream(data)
# Mesh Header
bs.seek(4, 0)
Filename_Hash = bs.readUInt()
bs.seek(4, 1)
Submesh_Count = bs.readUInt()
MeshTableOffset = bs.readUInt()
bs.seek(12, 1) # Unknown
BoundingBoxSphere = bs.readBytes(16)
BoundingBoxMax_X = bs.readFloat()
BoundingBoxMax_Y = bs.readFloat()
BoundingBoxMax_Z = bs.readFloat()
BoundingBoxMax_W = bs.readFloat() # Always 0
bs.seek(MeshTableOffset, 0)
Mesh_Table = bs.readBytes(8 * Submesh_Count)
MeshInfoOffsets = []
for i in range(Submesh_Count):
entry_offset = i*8 + 4
MeshInfoOffset = struct.unpack_from("<I", Mesh_Table, entry_offset)[0]
MeshInfoOffsets.append(MeshInfoOffset)
# Mesh Declaration
Submeshes = []
for i in range(Submesh_Count):
bs.seek(MeshInfoOffsets[i], 0)
bs.seek(32, 1)
bs.seek(4, 1) # Unknown
bonepaletteoffset = bs.readUInt()
bone_palette_count = bs.readUInt()
bs.seek(8, 1) # Unknown
Vertices_Count = bs.readUInt()
bs.seek(12, 1) #Unknown
Indices_Count = bs.readUInt()
Index_Size = bs.readUInt() # How many bytes each index takes
VertexStrideOffset = bs.readUInt()
bs.seek(8, 1)
bs.seek(bonepaletteoffset, 0)
bone_palette = []
for j in range(bone_palette_count):
bone_palette_index = bs.readUShort()
bone_palette.append(bone_palette_index)
bs.seek(VertexStrideOffset, 0)
Vertex_Stride = bs.readUInt()
FVFOffset = bs.readUInt()
bs.seek(FVFOffset, 0)
# FVF (Flexible Vertex Format) Layout // Vertex Declarations
VertexLayout = []
while True:
next8 = bs.readBytes(8)
if next8 == b'\xFF\x00\x00\x00\x11\x00\x00\x00':
# Already consumed the sentinel
break
# Unpack 4 ushorts from next8
chan, pos, data, etype = struct.unpack("<4H", next8)
VertexLayout.append({
"Offset": pos,
"DataType": data,
"ElementType": etype
})
Submeshes.append((Vertices_Count, Indices_Count, Vertex_Stride, VertexLayout))
bs.seek(4, 1) # PHYS
# # === Find the companion .1.mesh file ===
# component0_path = rapi.getInputName()
# baseName = component0_path.replace(".0.wosmesh", "")
# component1_path = baseName + ".1.wosmesh"
# if not os.path.exists(component1_path):
# print("ERROR: Companion .1.wosmesh file not found:", component1_path)
# return 0
# # === Load pixel data ===
# ModelData = rapi.loadIntoByteArray(component1_path)
# Model_BS = NoeBitStream(ModelData)
submesh_buffers = []
for sm_index, submesh in enumerate(Submeshes):
Vertices_Count, Indices_Count, Vertex_Stride, VertexLayout = submesh
print(Vertices_Count)
print(Indices_Count)
# --- Read vertex and index buffers ---
vertex_data_offset = bs.tell()
Vertex_Bytes = bs.readBytes(Vertices_Count * Vertex_Stride)
index_data_offset = bs.tell()
if Index_Size == 2:
IndexData = [bs.readUShort() for _ in range(Indices_Count)]
elif Index_Size == 4:
IndexData = [bs.readUInt() for _ in range(Indices_Count)]
current_pos = bs.tell()
padding = (4 - (current_pos % 4)) % 4
file_length = len(bs.getBuffer())
if padding and current_pos < file_length:
bs.seek(padding, 1)
# SAVE all info for later rendering
submesh_buffers.append((Vertex_Bytes, IndexData, Vertex_Stride, VertexLayout, Vertices_Count, Index_Size))
submesh_vertices = []
for v in range(Vertices_Count):
vertex_base = v * Vertex_Stride
vertex_dict = {}
for elem in VertexLayout:
dt = elem['DataType']
offset = elem['Offset']
size, fmt = Get_DataType(dt)
start = vertex_base + offset
end = start + size
raw = Vertex_Bytes[start:end]
value = struct.unpack(fmt, raw)
# Normalize as needed
if dt == 8: # UBYTE4N
value = tuple(x / 255.0 for x in value)
elif dt in (9, 10): # SHORT2N / SHORT4N
# Normalize to [-1, 1]
value = [x / 32767.0 for x in value[:3]]
elif dt in (11, 12):
value = tuple(x / 65535.0 for x in value)
elif dt in (15, 16): # HALF precision
value = tuple(half_to_float(x) for x in value)
vertex_dict[D3D9_ELEMENTTYPE.get(elem['ElementType'], "UNKNOWN")] = value
submesh_vertices.append(vertex_dict)
# Creating RPG Context for Model
ctx = rapi.rpgCreateContext()
for i, (Vertex_Bytes, IndexData, Vertex_Stride, VertexLayout, Vertices_Count, Index_Size) in enumerate(submesh_buffers):
# ----------------------------
# Build buffers
# ----------------------------
# Submesh Name
rapi.rpgSetName("Submesh_{}".format(i))
D3D_TO_RPG = {
0: (noesis.RPGEODATA_FLOAT, 4), # FLOAT1
1: (noesis.RPGEODATA_FLOAT, 8), # FLOAT2
2: (noesis.RPGEODATA_FLOAT, 12), # FLOAT3
3: (noesis.RPGEODATA_FLOAT, 16), # FLOAT4
5: (noesis.RPGEODATA_UBYTE, 4), # UBYTE4
6: (noesis.RPGEODATA_SHORT, 4), # SHORT2
7: (noesis.RPGEODATA_SHORT, 8), # SHORT4
8: (noesis.RPGEODATA_UBYTE, 4), # UBYTE4N
9: (noesis.RPGEODATA_SHORT, 4), # SHORT2N
10:(noesis.RPGEODATA_SHORT, 8), # SHORT4N
11:(noesis.RPGEODATA_USHORT, 4), # USHORT2N
12:(noesis.RPGEODATA_USHORT, 8), # USHORT4N
15:(noesis.RPGEODATA_HALFFLOAT, 4), # FLOAT16_2
16:(noesis.RPGEODATA_HALFFLOAT, 8) # FLOAT16_4
}
stride = Vertex_Stride
for elem in VertexLayout:
dtype = elem['DataType'] # D3DDECLTYPE
usage = elem['ElementType'] # D3DDECLUSAGE
offset = elem['Offset']
rpg_type, size = D3D_TO_RPG.get(dtype, (noesis.RPGEODATA_FLOAT, 4))
usage_name = D3D9_ELEMENTTYPE.get(usage, None)
# Bind based on usage
if usage_name == 'BINORMAL':
continue
elif usage_name == 'POSITION':
rapi.rpgBindPositionBufferOfs(Vertex_Bytes, rpg_type, stride, offset)
elif usage_name == 'NORMAL':
rapi.rpgBindNormalBufferOfs(Vertex_Bytes, rpg_type, stride, offset)
elif usage_name == 'TEXCOORD':
rapi.rpgBindUV1BufferOfs(Vertex_Bytes, rpg_type, stride, offset)
elif usage_name == 'COLOR':
continue
elif usage_name == 'TANGENT':
continue
elif usage_name == 'BLENDWEIGHT':
continue
elif usage_name == 'BLENDINDICES':
continue
# Commit indices once
if Index_Size == 2:
sx = BoundingBoxMax_X
sy = BoundingBoxMax_Y
sz = BoundingBoxMax_Z
# Build a 4x3 scale matrix (three axes vectors + translation)
scaleMat = NoeMat43((
NoeVec3((sx, 0.0, 0.0)), # X axis
NoeVec3((0.0, sy, 0.0)), # Y axis
NoeVec3((0.0, 0.0, sz)), # Z axis
NoeVec3((0.0, 0.0, 0.0)) # Translation
))
# Apply it
rapi.rpgSetTransform(scaleMat)
indexBuffer = struct.pack("<%dH" % len(IndexData), *IndexData)
rapi.rpgCommitTriangles(indexBuffer, noesis.RPGEODATA_USHORT, len(IndexData), noesis.RPGEO_TRIANGLE_STRIP)
elif Index_Size == 4:
sx = BoundingBoxMax_X
sy = BoundingBoxMax_Y
sz = BoundingBoxMax_Z
# Build a 4x3 scale matrix (three axes vectors + translation)
scaleMat = NoeMat43((
NoeVec3((sx, 0.0, 0.0)), # X axis
NoeVec3((0.0, sy, 0.0)), # Y axis
NoeVec3((0.0, 0.0, sz)), # Z axis
NoeVec3((0.0, 0.0, 0.0)) # Translation
))
# Apply it
rapi.rpgSetTransform(scaleMat)
# Build a scale matrix from your bounding box
scaleMat = noesis.buildMat43Scale((BoundingBoxMax_X, BoundingBoxMax_Y, BoundingBoxMax_Z))
rapi.rpgSetTransform(scaleMat)
indexBuffer = struct.pack("<%dI" % len(IndexData), *IndexData)
rapi.rpgCommitTriangles(indexBuffer, noesis.RPGEODATA_UINT, len(IndexData), noesis.RPGEO_TRIANGLE_STRIP)
# --- Clear binds so they don’t leak to the next submesh ---
rapi.rpgClearBufferBinds()
# Build model and append
mdl = rapi.rpgConstructModel()
mdlList.append(mdl)
return 1
def getBoundingBox(mesh):
if not hasattr(mesh, 'positions') or not mesh.positions:
return None
first = mesh.positions[0]
max_x, max_y, max_z = first
for v in mesh.positions:
x, y, z = v
max_x = max(max_x, x)
max_y = max(max_y, y)
max_z = max(max_z, z)
return (max_x, max_y, max_z)
def has_skinning(mesh):
return len(mesh.boneWeights) > 0 if hasattr(mesh, "boneWeights") else False
def build_bone_palette(mesh):
"""
Returns a list of unique bone indices that influence this mesh.
If the mesh is not skinned, returns an empty list.
"""
bone_palette = set()
if hasattr(mesh, "boneIndices") and mesh.boneIndices:
for v_bone_indices in mesh.boneIndices:
bone_palette.update(v_bone_indices) # add all indices from this vertex
return list(bone_palette)
import struct
def float_to_half(f):
# convert float to 16-bit half float
# returns unsigned short
s = struct.pack('>f', f)
i = struct.unpack('>I', s)[0]
sign = (i >> 16) & 0x8000
exponent = ((i >> 23) & 0xff) - 127 + 15
mantissa = (i >> 13) & 0x3ff
if exponent <= 0:
exponent = 0
mantissa = 0
elif exponent >= 31:
exponent = 31
mantissa = 0
return sign | (exponent << 10) | mantissa
def simple_stripify(triangles):
"""
Convert a list of triangles into a single triangle strip with minimal degenerates.
triangles: list of tuples/lists of 3 vertex indices
Returns: flat list of indices
"""
if not triangles:
return []
strip = list(triangles[0]) # start with first triangle
for tri in triangles[1:]:
tri_list = list(tri) # convert tuple to list
last2 = strip[-2:]
if len(set(last2 + tri_list)) < 5:
# Triangles share vertices, no degenerate needed
strip.extend(tri_list)
else:
# Insert minimal degenerate bridge
strip.append(strip[-1]) # repeat last vertex
strip.append(tri_list[0]) # start new triangle
strip.extend(tri_list) # add the new triangle
return strip
def is_degenerate(tri):
return len(set(tri)) < 3
def remove_degenerates(triangle_strip):
cleaned = []
# Walk the strip and convert to triangles
for i in range(len(triangle_strip) - 2):
tri = [triangle_strip[i], triangle_strip[i+1], triangle_strip[i+2]]
if not is_degenerate(tri):
cleaned.extend(tri)
return cleaned
def WriteModel(mdl, bs):
with open("D://Testing SMWOS Exporter//HEARTNOESIS.0.wosmesh", "wb") as out:
# Mesh Header
out.write(b'\x00' * 4)
out.write(b'\x00' * 4)
out.write(b'\x00' * 4)
out.write(struct.pack("<I", len(mdl.meshes)))
out.write(b'\x00' * 20)
out.write(b'\x00' * 12)
bmax = getBoundingBox(mdl.meshes[0])
out.write(struct.pack("<3f", bmax[0], bmax[1], bmax[2]))
out.write(b'\x00'* 20)
out.write(b'\x00' * 8 * len(mdl.meshes))
current_pos = out.tell() # current position in the stream
padding = (16 - (current_pos % 16)) % 16 # compute how many bytes to pad
if padding:
out.write(b'\x00' * padding)
# Mesh Declaration
for i, mesh in enumerate(mdl.meshes):
out.write(b'\x00' * 32) # Unknown
out.write(b'\x00' * 8) # Unknown
# For a submesh
mesh = mdl.meshes[i]
unique_bones = set()
if hasattr(mesh, "boneIndices") and mesh.boneIndices:
for bone_index in mesh.boneIndices:
if bone_index != -1:
unique_bones.add(bone_index)
palette = build_bone_palette(mesh)
bone_palette_count = len(palette)
out.write(struct.pack("<I", bone_palette_count))
out.write(b'\x00' * 8) # Unknown
out.write(struct.pack("<I", len(mesh.positions)))
triangle_list = [mesh.indices[i:i+3] for i in range(0, len(mesh.indices), 3)]
triangle_strip = simple_stripify(triangle_list)
cleaned_strip = remove_degenerates(triangle_strip)
mesh._cleaned_strip = cleaned_strip
out.write(b'\x00' * 12) #Unknown
out.write(struct.pack("<I", len(cleaned_strip)))
out.write(b'\x00' * 16) #Unknown
for j in range(bone_palette_count):
out.write(struct.pack("<H", palette[j]))
current_pos = out.tell() # current position in the stream
padding = (4 - (current_pos % 4)) % 4 # compute how many bytes to pad
if padding:
out.write(b'\x00' * padding)
if has_skinning(mesh):
Stride = 44
out.write(struct.pack("<I", Stride))
else:
Stride = 24
out.write(struct.pack("<I", Stride))
out.write(b'\x00' * 8) # Unknown
# FVF (Flexible Vertex Format) Layout // Vertex Declarations
if has_skinning(mesh):
vertex_layout = [
(0, 0, 15, 5),
(0, 4, 16, 6),
(0, 12, 10, 0),
(0, 20, 16, 3),
(0, 28, 8, 1),
(0, 32, 5, 2),
(0, 36, 16, 7)
]
for entry in vertex_layout:
chan, pos, data, ctype = entry
out.write(struct.pack("<4H", chan, pos, data, ctype))
out.write(b'\xFF\x00\x00\x00\x11\x00\x00\x00')
current_pos = out.tell() # current position in the stream
padding = (16 - (current_pos % 16)) % 16 # compute how many bytes to pad
if padding:
out.write(b'\x00' * padding)
else:
vertex_layout_non_skinned = [
(0, 0, 15, 5),
(0, 4, 2, 0),
(0, 12, 16, 3)
]
for entry in vertex_layout_non_skinned:
chan, pos, data, ctype = entry
out.write(struct.pack("<4H", chan, pos, data, ctype))
out.write(b'\xFF\x00\x00\x00\x11\x00\x00\x00')
current_pos = out.tell() # current position in the stream
padding = (16 - (current_pos % 16)) % 16 # compute how many bytes to pad
if padding:
out.write(b'\x00' * padding)
with open("D://Testing SMWOS Exporter//HEARTNOESIS.1.wosmesh", "wb") as out:
for mesh in mdl.meshes:
if not has_skinning(mesh):
for i, pos in enumerate(mesh.positions):
# UV as FLOAT16_2
u, v = mesh.uvs[i][:2]
out.write(struct.pack("<H", float_to_half(u)))
out.write(struct.pack("<H", float_to_half(v)))
x, y, z = pos
out.write(struct.pack("<3f", x, y, z)) # position (FLOAT3)
nx, ny, nz = mesh.normals[i]
w = 0.0 # padding
out.write(struct.pack("<H", float_to_half(nx)))
out.write(struct.pack("<H", float_to_half(ny)))
out.write(struct.pack("<H", float_to_half(nz)))
out.write(struct.pack("<H", float_to_half(w)))
for idx in mesh._cleaned_strip:
out.write(struct.pack("<H", idx))
return 1