Skip to content

Commit

Permalink
Merge pull request #14 from bgsamm/animation-importing
Browse files Browse the repository at this point in the history
Animation importing
  • Loading branch information
StarsMmd authored Jun 10, 2024
2 parents 5da1c7a + ee00857 commit 1e32f18
Show file tree
Hide file tree
Showing 4 changed files with 626 additions and 37 deletions.
29 changes: 25 additions & 4 deletions importer/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,29 @@ def __init__(self, f, matID):
self.materialIndex = matID

class Bone:
def __init__(self, i, name, trans, mat):
def __init__(self, i, name, type, pivots, trans, mat, brot, rot, sca, pos, nodeFlags, boneFlags):
self.index = i
self.name = name
self.type = type

self.ScalePivot = pivots[0]
self.ScalePivotTranslate = pivots[1]
self.RotatePivot = pivots[2]
self.RotationPivotTranslate = pivots[3]

self.inverseBindMatrix = mat

self.initialRot = rot
self.initialScale = sca
self.initialTrans = pos
self.bindRotation = brot
self.nodeFlags = nodeFlags
self.boneFlags = boneFlags

self.parentRelativeBind = None

self.localTransform = trans
# self.globalTransform calculated by Skeleton
self.inverseBindMatrix = mat

self.childIndices = []
self.parentIndex = None
Expand All @@ -66,8 +82,13 @@ def __init__(self, name, numBones, bones):

self.calcGlobalTransforms(0, Matrix.Identity(4))

def calcGlobalTransforms(self, idx, parentTransform):
def calcGlobalTransforms(self, idx, parentTransform, invparentBind = Matrix.Identity(4)):
bone = self.bones[idx]
s = invparentBind.to_scale()
#invparentBindWithoutScale = Matrix.Diagonal((1 / s[0], 1 / s[1], 1 / s[2], 1.0)) @ invparentBind
bone.invparentBind = invparentBind
bone.parentRelativeBind = invparentBind @ bone.inverseBindMatrix.inverted()
bone.globalTransform = parentTransform @ bone.localTransform

for childIndex in bone.childIndices:
self.calcGlobalTransforms(childIndex, bone.globalTransform)
self.calcGlobalTransforms(childIndex, bone.globalTransform, bone.inverseBindMatrix)
55 changes: 50 additions & 5 deletions importer/gtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,48 @@ def bytesToRGB5A3(b1, b2):

def interpolate(v1, v2, weight):
return [int(a * (1 - weight) + b * weight) for a, b in zip(v1, v2)]

def generateTLUT(byte_arr, maxPaletteSize, palEncoding):
colors = []
paletteSize = min(maxPaletteSize, len(byte_arr) // 2)
if palEncoding == 'IA8':
for i in range(paletteSize):
b = byte_arr[i * 2 : (i + 1) * 2]
colors.append([b[1]]*3 + [b[0]])
elif palEncoding == 'RGB565':
for i in range(paletteSize):
b = byte_arr[i * 2 : (i + 1) * 2]
colors.append(bytesToRGB565(*b))
elif palEncoding == 'RGB5A3':
for i in range(paletteSize):
b = byte_arr[i * 2 : (i + 1) * 2]
colors.append(bytesToRGB5A3(*b))
return colors

def parseImageData(byte_arr, img_width, img_height, encoding):
def parseImageData(byte_arr, img_width, img_height, encoding, palEncoding, palOffset):
if encoding == 'RGBA32':
return parseRGBA32Data(byte_arr, img_width, img_height)
elif encoding == 'CMPR':
return parseCMPRData(byte_arr, img_width, img_height)

if encoding == 'I4':
if encoding in ['C4', 'C8', 'C14X2']:
if encoding == 'C4': # 4 bit indices
maxPaletteSize = 0x10
img_width /= 2
block_width = 4
block_height = 8
px_sz = 1
elif encoding == 'C8': # 8 bit indices
maxPaletteSize = 0x100
block_width = 8
block_height = 4
px_sz = 1
elif encoding == 'C14X2': # 10 bit indices
maxPaletteSize = 0x400
block_width = block_height = 4
px_sz = 2
tlut = generateTLUT(byte_arr[palOffset:], maxPaletteSize, palEncoding)
elif encoding == 'I4':
img_width /= 2
block_width = 4
block_height = 8
Expand Down Expand Up @@ -78,6 +112,17 @@ def parseImageData(byte_arr, img_width, img_height, encoding):
rgba += bytesToRGB565(*b)
elif encoding == 'RGB5A3':
rgba += bytesToRGB5A3(*b)
elif encoding == 'C4':
idx1 = (b[0] & 0xF0) >> 4
idx2 = b[0] & 0xF
rgba += tlut[idx1]
rgba += tlut[idx2]
elif encoding == 'C8':
idx = b[0]
rgba += tlut[idx]
elif encoding == 'C14X2':
idx = ((b[0] << 8) | b[1]) & 0x4FFF
rgba += tlut[idx]
return rgba

def parseRGBA32Data(byte_arr, img_width, img_height):
Expand Down Expand Up @@ -141,7 +186,7 @@ def parseCMPRData(byte_arr, img_width, img_height):
rgba += colors[index]
return rgba

def decompress(byte_arr, img_width, img_height, encoding):
rgba = parseImageData(byte_arr, img_width, img_height, encoding)
assert len(rgba) / 4 == img_width * img_height
def decompress(byte_arr, img_width, img_height, encoding, palEncoding, palOffset):
rgba = parseImageData(byte_arr, img_width, img_height, encoding, palEncoding, palOffset)
#assert len(rgba) / 4 == img_width * img_height
return rgba
Loading

0 comments on commit 1e32f18

Please sign in to comment.