-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfmt_sh3_ps2_map.py
239 lines (205 loc) · 7.95 KB
/
fmt_sh3_ps2_map.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
# Authors:
# Laurynas Zubavičius (Sparagas)
# Rodolfo Nuñez (roocker666)
#
# Buffer reading idea:
# Murugo - https://github.com/Murugo/Misc-Game-Research/blob/main/PS2/Silent%20Hill%202%2B3/Blender/addons/io_sh2_sh3/import_map.py
from inc_noesis import *
def registerNoesisTypes():
handle = noesis.register("Silent Hill 3 (PS2)", ".map")
noesis.setHandlerTypeCheck(handle, CheckType)
noesis.setHandlerLoadModel(handle, LoadModel)
# noesis.logPopup()
return 1
def CheckType(data):
if len(data) < 4:
return 0
if data[:4] != b'\xff\xff\xff\xff':
return 0
return 1
def LoadModel(data, mdlList):
bs = NoeBitStream(data)
ctx = rapi.rpgCreateContext()
class Head:
def __init__(self):
self.next_ofs = bs.readUInt()
self.data_start_ofs = bs.readUInt()
self.total_size = bs.readUInt()
self.pad = bs.readUInt()
class MeshGroup:
def __init__(self):
self.mesh_group_h = MeshGroupH()
self.mesh_group_b = MeshGroupB()
class MeshGroupH:
def __init__(self):
self.head = Head()
self.unk = bs.readUInt()
self.arr_id = bs.readUInt()
self.num_mesh = bs.readUShort()
self.unk = bs.readUShort()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
class MeshGroupB:
def __init__(self):
self.i = 0
self.mesh = Mesh(self.i)
while self.mesh.mesh_h.head.next_ofs > 0:
bs.seek(self.mesh.mesh_h.head.next_ofs)
self.i += 1
self.mesh = Mesh(self.i)
class Mesh:
def __init__(self, i):
self.i = i
self.mesh_h = MeshH()
self.mesh_b = MeshB(self.i)
class MeshH:
def __init__(self):
self.head = Head()
self.clut_index = bs.readUShort()
self.unk = bs.readUShort()
self.unk = bs.readUShort()
self.flag = bs.readUShort()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
class MeshB:
def __init__(self, i):
self.i = i
self.j = 0
self.sub_mesh = SubMesh(self.i, self.j)
while self.sub_mesh.sub_mesh_h.head.next_ofs > 0:
bs.seek(self.sub_mesh.sub_mesh_h.head.next_ofs)
self.j += 1
self.sub_mesh = SubMesh(self.i, self.j)
class SubMesh:
def __init__(self, i, j):
self.i = i
self.j = j
self.sub_mesh_h = SubMeshH()
self.sub_mesh_b = SubMeshB(self.i, self.j)
class SubMeshH:
def __init__(self):
self.head = Head()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readFloat()
self.unk = bs.readFloat()
self.unk = bs.readFloat()
self.unk = bs.readFloat()
class SubMeshB:
def __init__(self, i, j):
self.i = i
self.j = j
self.k = 0
self.shape = Shape()
rapi.rpgSetName('mesh{}_{}_{}'.format(self.i, self.j, self.k))
while self.shape.shape_h.head.next_ofs > 0:
bs.seek(self.shape.shape_h.head.next_ofs)
self.k += 1
self.shape = Shape()
rapi.rpgSetName('mesh{}_{}_{}'.format(self.i, self.j, self.k))
class Shape:
def __init__(self):
# self.vbuf = bytearray()
# self.uvbuf = bytearray()
self.shape_h = ShapeH()
vbuf = b''
ibuf = b''
vnbuf = b''
uvbuf = b''
vcolbuf = b''
reverse = False
for i in range(self.shape_h.num_vertex):
vbuf += bs.readBytes(6)
vn_vcol_x = bs.readShort()
uv_flag = [bs.readShort(), bs.readShort()]
vn_vcol = (vn_vcol_x, bs.readShort(), bs.readShort())
uvbuf += NoeVec3([uv_flag[0] / 0x8000, 1.0 - uv_flag[1] / 0x8000, 0]).toBytes()
vnbuf += NoeVec3([(v & ~0x3F) / -0x8000 for v in vn_vcol]).normalize().toBytes()
vcolbuf += NoeVec3([(v & 0x3F) / 0x20 for v in vn_vcol]).toBytes()
flag = uv_flag[0] & 0x1
if not flag:
if reverse:
ibuf += struct.pack('3H', i, i - 1, i - 2)
else:
ibuf += struct.pack('3H', i - 2, i - 1, i)
reverse = not reverse
rapi.rpgBindPositionBuffer(vbuf, noesis.RPGEODATA_SHORT, 6)
rapi.rpgBindNormalBuffer(vnbuf, noesis.RPGEODATA_FLOAT, 12)
rapi.rpgBindUV1Buffer(uvbuf, noesis.RPGEODATA_FLOAT, 12)
rapi.rpgBindColorBuffer(vcolbuf, noesis.RPGEODATA_FLOAT, 12, 3)
rapi.rpgCommitTriangles(ibuf, noesis.RPGEODATA_USHORT, len(ibuf) // 2, noesis.RPGEO_TRIANGLE)
# for _ in range(self.shape_h.num_vertex):
# self.shape_b = ShapeB()
# self.vbuf.extend(self.shape_b.vpos)
# self.uvbuf.extend(self.shape_b.uv_u)
# self.uvbuf.extend(self.shape_b.uv_v)
# rapi.rpgBindPositionBuffer(self.vbuf, noesis.RPGEODATA_SHORT, 6)
# rapi.rpgBindUV1Buffer(self.uvbuf, noesis.RPGEODATA_BYTE, 2)
# rapi.rpgBindColorBuffer(vcolbuf, noesis.RPGEODATA_BYTE, 3, 3)
# rapi.rpgCommitTriangles(None, noesis.RPGEODATA_UINT, self.shape_h.num_vertex, noesis.RPGEO_POINTS) # for point cloud
# rapi.rpgCommitTriangles(None, noesis.RPGEODATA_UINT, self.shape_h.num_vertex, noesis.RPGEO_TRIANGLE_STRIP) # for triangles
class ShapeH:
def __init__(self):
self.head = Head()
self.num_vertex = bs.readUInt()
self.transform_index = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readUShort()
self.unk = bs.readUShort()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.unk = bs.readUInt()
self.pad = bs.readUInt()
self.pad = bs.readUInt()
self.pad = bs.readUInt()
self.pad = bs.readUInt()
class ShapeB:
def __init__(self):
self.vpos = bs.readBytes(3 * 2)
self.vn_vcol_x = bs.readBytes(2)
self.tri_flag = bs.readUByte()
self.uv_u = bs.readBytes(1)
self.pad = bs.readByte()
self.uv_v = bs.readBytes(1)
self.vn_vcol = bs.readBytes(2 * 2)
class MeshBlock:
def __init__(self):
self.mesh_group = MeshGroup()
while self.mesh_group.mesh_group_h.head.next_ofs > 0:
bs.seek(self.mesh_group.mesh_group_h.head.next_ofs)
self.mesh_group = MeshGroup()
bs.seek(7 * 4)
mesh_group_ofs0 = bs.readUInt()
mesh_group_ofs1 = bs.readUInt()
mesh_group_ofs2 = bs.readUInt()
bs.seek(176)
bbox = bs.readBytes(128)
rapi.rpgBindPositionBuffer(bbox, noesis.RPGEODATA_FLOAT, 16)
rapi.rpgCommitTriangles(None, noesis.RPGEODATA_UINT, 8, noesis.RPGEO_POINTS)
bbox = rapi.rpgConstructModel()
if mesh_group_ofs0 > 0:
bs.seek(mesh_group_ofs0)
mesh_group0 = MeshBlock()
'''
if mesh_group_ofs1 > 0:
bs.seek(mesh_group_ofs1)
mesh_group1 = MeshGroup()
if mesh_group_ofs2 > 0:
bs.seek(mesh_group_ofs2)
mesh_group2 = MeshGroup()
'''
mdl = rapi.rpgConstructModel()
# mdl.setModelMaterials(NoeModelMaterials([], [NoeMaterial('mat0','')]))
mdlList.append(mdl)
mdlList.append(bbox)
return 1