-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmt_ntwm_anb.py
275 lines (199 loc) · 8.59 KB
/
fmt_ntwm_anb.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
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
from inc_noesis import *
def registerNoesisTypes():
handle = noesis.register( "Nosferatu The Wrath of Malachi (2003) model", ".anb")
noesis.setHandlerTypeCheck(handle, ntwmModelCheckType)
noesis.setHandlerLoadModel(handle, ntwmModelLoadModel)
noesis.setHandlerWriteModel(handle, ntwmModelWriteModel)
return 1
class Vector3UI16:
def read(self, reader):
self.x = reader.readShort()
self.y = reader.readShort()
self.z = reader.readShort()
def getStorage(self):
return (self.x, self.y, self.z)
def toBytes(self):
result = bytearray()
result += self.x.to_bytes(2, byteorder='little')
result += self.y.to_bytes(2, byteorder='little')
result += self.z.to_bytes(2, byteorder='little')
return result
class Vector3F:
def read(self, reader):
self.x = reader.readFloat()
self.y = reader.readFloat()
self.z = reader.readFloat()
def getStorage(self):
return (self.x, self.y, self.z)
def toBytes(self):
result = bytearray()
result += bytearray(struct.pack("f", self.x))
result += bytearray(struct.pack("f", self.y))
result += bytearray(struct.pack("f", self.z))
return result
class Vector2F:
def read(self, reader):
self.x = reader.readFloat()
self.y = reader.readFloat()
def getStorage(self):
return (self.x, self.y)
def toBytes(self):
result = bytearray()
result += bytearray(struct.pack("f", self.x))
result += bytearray(struct.pack("f", self.y))
return result
class NTWMMesh:
def __init__(self):
self.vertexCoordinates = []
self.facesIndexes = []
self.uvIndexes = []
self.uvCoordinates = []
def read(self, reader):
self.vertexCount = reader.readUInt()
self.uvCount = reader.readUInt()
self.faceCount = reader.readUInt()
for vertexIndex in range(self.vertexCount):
coordinates = Vector3F()
coordinates.read(reader)
normal = Vector3F()
normal.read(reader)
self.vertexCoordinates.append(coordinates)
for faceIndex in range(self.faceCount):
faceIndexes = Vector3UI16()
faceIndexes.read(reader)
self.facesIndexes.append(faceIndexes)
for uvCoordIndex in range(self.uvCount):
uvCoordinates = Vector2F()
uvCoordinates.read(reader)
self.uvCoordinates.append(uvCoordinates)
for uvIndex in range(self.faceCount):
uvIndexes = Vector3UI16()
uvIndexes.read(reader)
self.uvIndexes.append(uvIndexes)
class NTWMFrameMesh:
def __init__(self):
self.positions = bytearray()
self.normals = bytearray()
class NTWMMorphFrame:
def __init__(self):
self.frameMeshes = []
class NTWMCharacterModel:
def __init__(self, reader):
self.reader = reader
self.textures = []
self.materials = []
self.meshes = []
self.morphFrames= []
def readHeader(self, reader):
self.morphFrameCount = reader.readUInt()
self.meshCount = reader.readUInt()
def readModelData(self, reader):
for i in range(self.meshCount):
mesh = NTWMMesh()
mesh.read(reader)
self.meshes.append(mesh)
def readFrame(self, reader):
morphFrame = NTWMMorphFrame()
for mesh in self.meshes:
frameMesh = NTWMFrameMesh()
for vertexIndex in range(mesh.vertexCount):
coordinates = Vector3F()
coordinates.read(reader)
normal = Vector3F()
normal.read(reader)
frameMesh.positions += coordinates.toBytes()
frameMesh.normals += normal.toBytes()
morphFrame.frameMeshes.append(frameMesh)
return morphFrame
def readMorphFrames(self, reader):
for frameIndex in range(self.morphFrameCount - 1):
self.morphFrames.append(self.readFrame(reader))
def read(self):
self.readHeader(self.reader)
self.readModelData(self.reader)
self.readMorphFrames(self.reader)
def ntwmModelCheckType(data):
return 1
# ToDo: texture coordinates
def ntwmModelLoadModel(data, mdlList):
ntwmModel = NTWMCharacterModel(NoeBitStream(data))
ntwmModel.read()
ctx = rapi.rpgCreateContext()
materials = []
textures = []
textureName = ""
textureName = "F:/Games/Nosferatu - The Wrath of Malachi/Version/Data/corpse.jpg"
texture = rapi.loadExternalTex(textureName)
if texture == None:
texture = NoeTexture(textureName, 0, 0, bytearray())
textures.append(texture)
material = NoeMaterial(textureName, textureName)
material.setFlags(noesis.NMATFLAG_TWOSIDED, 1)
materials.append(material)
noesis.logPopup()
index = 0
for mesh in ntwmModel.meshes:
rapi.rpgSetMaterial(textureName)
rapi.rpgSetName("mesh " + str(index))
# for i in range(len(mesh.facesIndexes)):
# rapi.immBegin(noesis.RPGEO_TRIANGLE)
# for idx in range(3):
# uvIndex = mesh.uvIndexes[i].getStorage()[idx]
# rapi.immUV2(mesh.uvCoordinates[uvIndex].getStorage())
# vertexIndex = mesh.facesIndexes[i].getStorage()[idx]
# rapi.immVertex3(mesh.vertexCoordinates[vertexIndex].getStorage())
# rapi.immEnd()
positions = bytearray()
for vertex in mesh.vertexCoordinates:
positions += vertex.toBytes()
uvs = bytearray()
for uvIndexes in mesh.uvIndexes:
uvs += uvIndexes.toBytes()
for uvIndexes in mesh.uvIndexes:
for i in uvIndexes.getStorage():
uvs += mesh.uvCoordinates[i].toBytes()
triangles = bytearray()
for triangle in mesh.facesIndexes:
triangles += triangle.toBytes()
rapi.rpgBindPositionBuffer(positions, noesis.RPGEODATA_FLOAT, 12)
#rapi.rpgBindUV1Buffer(uvs, noesis.RPGEODATA_FLOAT, 8)
for frame in ntwmModel.morphFrames:
frameMesh = frame.frameMeshes[index]
rapi.rpgFeedMorphTargetPositions(frameMesh.positions, noesis.RPGEODATA_FLOAT, 12)
rapi.rpgFeedMorphTargetNormals(frameMesh.normals, noesis.RPGEODATA_FLOAT, 12)
rapi.rpgCommitMorphFrame(len(frameMesh.positions) // 12)
rapi.rpgCommitMorphFrameSet()
rapi.rpgCommitTriangles(triangles, noesis.RPGEODATA_USHORT, len(mesh.facesIndexes)*3, noesis.RPGEO_TRIANGLE, 1)
rapi.rpgClearBufferBinds()
index += 1
rapi.rpgOptimize()
mdl = rapi.rpgConstructModelSlim()
mdl.setModelMaterials(NoeModelMaterials(textures, materials))
mdlList.append(mdl)
return 1
def ntwmModelWriteModel(mdl, bs):
# writing header
bs.writeInt(len(mdl.meshes[0].morphList)) # number of frames
bs.writeInt(len(mdl.meshes)) # number of meshes
# meshes
for mesh in mdl.meshes:
bs.writeInt(len(mesh.positions))
bs.writeInt(len(mesh.uvs))
bs.writeInt(len(mesh.indices)//3)
for i in range(len(mesh.positions)):
bs.writeBytes(mesh.positions[i].toBytes())
bs.writeBytes(mesh.normals[i].toBytes())
for idx in mesh.indices:
bs.writeShort(idx)
for vcmp in mesh.uvs:
bs.writeBytes(vcmp.toBytes()[0:8])
for idx in mesh.indices:
bs.writeShort(idx)
# morphs
for frameIndex in range(len(mdl.meshes[0].morphList)):
for mesh in mdl.meshes:
mf = mesh.morphList[frameIndex]
for i in range(len(mf.positions)):
bs.writeBytes(mf.positions[i].toBytes())
bs.writeBytes(mf.normals[i].toBytes())
return 1