Skip to content

Commit

Permalink
Add Support for TRI16
Browse files Browse the repository at this point in the history
  • Loading branch information
SE2Dev committed Jan 6, 2018
1 parent a78b8cf commit 80140af
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
24 changes: 21 additions & 3 deletions xbin.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,16 @@ def LoadTriangleBlock(file):
result = struct.unpack('BB', data)
return result

@staticmethod
def LoadTriangle16Block(file):
file.seek(file.tell() + 2) # Skip padding
data = file.read(4)
result = struct.unpack('HH', data)
return result

@staticmethod
def LoadColorBlock(file):
file.seek(file.tell() + 2)
file.seek(file.tell() + 2) # Skip padding
data = file.read(4)
r, g, b, a = struct.unpack('BBBB', data)
return (r / 255.0, g / 255.0, b / 255.0, a / 255.0)
Expand Down Expand Up @@ -370,7 +377,11 @@ def WriteVertexWeightBlock(file, weight):

@staticmethod
def WriteFaceInfoBlock(file, face):
data = struct.pack('HBB', 0x562F, face.mesh_id, face.material_id)
if face.mesh_id > 255 or face.material_id > 255:
data = struct.pack('HHHH', 0x6711, 0x0,
face.mesh_id, face.material_id)
else:
data = struct.pack('HBB', 0x562F, face.mesh_id, face.material_id)
file.write(data)

@staticmethod
Expand Down Expand Up @@ -618,6 +629,13 @@ def LoadTriInfo(file):
dummy_mesh.faces.append(tri)
state.active_tri = tri

def LoadTri16Info(file):
object_index, material_index = XBlock.LoadTriangle16Block(file)
tri = XModel.Face(object_index, material_index)
tri.indices = []
dummy_mesh.faces.append(tri)
state.active_tri = tri

def LoadTriVertNormal(file):
state.active_thing.normal = XBlock.LoadShortVec3Block(file)

Expand Down Expand Up @@ -744,6 +762,7 @@ def LoadNoteFrame(file):

0xBE92: ("Number of faces block", LoadTriCount),
0x562F: ("Triangle info block", LoadTriInfo),
0x6711: ("Triangle info (16) block", LoadTri16Info),
0x89EC: ("Normal info", LoadTriVertNormal),
0x6DD8: ("Color info", LoadTriVertColor),
0x1AD4: ("UV info", LoadTriVertUV),
Expand Down Expand Up @@ -781,7 +800,6 @@ def LoadNoteFrame(file):

# Misc (Unimplemented)
0xBCD4: ("FIRSTFRAME", None),
0x6711: ("TRI16", None),
0x1FC2: ("NUMSBONES", None),
0xB35E: ("NUMSWEIGHTS", None),
0xEF69: ("QUATERNION", None),
Expand Down
12 changes: 9 additions & 3 deletions xmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ def __load_face__(self, file, version, face_count, vert_tok='VERT'):
if split[-1:] == ',':
line_split[i] = split.rstrip(",")

if state == 0 and line_split[0] == "TRI":
# Support both TRI & TRI16
if state == 0 and line_split[0].startswith("TRI"):
tri_number += 1
self.mesh_id = int(line_split[1])
self.material_id = int(line_split[2])
Expand Down Expand Up @@ -240,8 +241,13 @@ def __load_face__(self, file, version, face_count, vert_tok='VERT'):
return lines_read

def save(self, file, version, index_offset, vert_tok_suffix=""):
file.write("TRI %d %d %d %d\n" %
(self.mesh_id, self.material_id, 0, 0))
# Only use TRI16 if we're using version 7 or newer, etc.
if version >= 7 and (self.mesh_id > 255 or self.material_id > 255):
token = "TRI16"
else:
token = "TRI"
file.write("%s %d %d %d %d\n" %
(token, self.mesh_id, self.material_id, 0, 0))
for i in range(3):
self.indices[i].save(file, version, index_offset,
vert_tok_suffix=vert_tok_suffix)
Expand Down

0 comments on commit 80140af

Please sign in to comment.