-
Notifications
You must be signed in to change notification settings - Fork 3
/
bck.py
291 lines (246 loc) · 9.25 KB
/
bck.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from warnings import warn
import sys
from common import *
from struct import unpack, pack, Struct, error as StructError
from warnings import warn
from array import array
from enum import Enum
import math
from bisect import bisect
def convRotation(rots, scale):
for r in rots:
r.value *= scale
r.tangentIn *= scale
r.tangentOut *= scale
class LoopMode(Enum):
ONCE = 0
ONCE_AND_RESET = 1
REPEAT = 2
MIRRORED_ONCE = 3
MIRRORED_REPEAT = 4
class J3DAnmTransformKeyData(Section):
header = Struct('>BBHHHHHIIII')
fields = [
('loopMode', LoopMode), 'angleMultiplier', 'animationLength',
'numJoints', 'scaleCount', 'rotCount', 'transCount',
'offsetToJoints', 'offsetToScales', 'offsetToRots', 'offsetToTrans'
]
def read(self, fin, start, size):
super().read(fin, start, size)
scales = array('f')
fin.seek(start+self.offsetToScales)
scales.fromfile(fin, self.scaleCount)
if sys.byteorder == 'little': scales.byteswap()
rotations = array('h')
fin.seek(start+self.offsetToRots)
rotations.fromfile(fin, self.rotCount)
if sys.byteorder == 'little': rotations.byteswap()
translations = array('f')
fin.seek(start+self.offsetToTrans)
translations.fromfile(fin, self.transCount)
if sys.byteorder == 'little': translations.byteswap()
rotationScale = (1<<self.angleMultiplier)*math.pi/0x7FFF
fin.seek(start+self.offsetToJoints)
self.anims = [None]*self.numJoints
for i in range(self.numJoints):
joint = AnimatedJoint()
joint.read(fin)
anim = Animation()
anim.scalesX = readComp(scales, joint.x.s)
anim.scalesY = readComp(scales, joint.y.s)
anim.scalesZ = readComp(scales, joint.z.s)
anim.rotationsX = readComp(rotations, joint.x.r)
convRotation(anim.rotationsX, rotationScale)
anim.rotationsY = readComp(rotations, joint.y.r)
convRotation(anim.rotationsY, rotationScale)
anim.rotationsZ = readComp(rotations, joint.z.r)
convRotation(anim.rotationsZ, rotationScale)
anim.translationsX = readComp(translations, joint.x.t)
anim.translationsY = readComp(translations, joint.y.t)
anim.translationsZ = readComp(translations, joint.z.t)
self.anims[i] = anim
def write(self, fout):
maxRotation = max(
abs(v) for anim in self.anims
for key in anim.rotationsX+anim.rotationsY+anim.rotationsZ
for v in (key.value, key.tangentIn, key.tangentOut)
)
if maxRotation == 0:
self.angleMultiplier = 0
else:
self.angleMultiplier = max(0, math.ceil(math.log2(maxRotation/math.pi)))
rotationScale = (1<<self.angleMultiplier)*math.pi/0x7FFF
scales = array('f')
rotations = array('h')
translations = array('f')
joints = []
for anim in self.anims:
joint = AnimatedJoint()
addComp(joint.x.s, anim.scalesX, scales)
addComp(joint.y.s, anim.scalesY, scales)
addComp(joint.z.s, anim.scalesZ, scales)
addComp(joint.x.r, anim.rotationsX, rotations, rotationScale, int)
addComp(joint.y.r, anim.rotationsY, rotations, rotationScale, int)
addComp(joint.z.r, anim.rotationsZ, rotations, rotationScale, int)
addComp(joint.x.t, anim.translationsX, translations)
addComp(joint.y.t, anim.translationsY, translations)
addComp(joint.z.t, anim.translationsZ, translations)
joints.append(joint)
if sys.byteorder == 'little':
scales.byteswap()
rotations.byteswap()
translations.byteswap()
self.offsetToJoints = self.header.size+8
self.numJoints = len(self.anims)
self.offsetToScales = self.offsetToJoints+(9*AnimIndex.header.size*len(joints))
self.scaleCount = len(scales)
self.offsetToRots = self.offsetToScales+(scales.itemsize*len(scales))
self.rotCount = len(rotations)
self.offsetToTrans = self.offsetToRots+(rotations.itemsize*len(rotations))
self.transCount = len(translations)
super().write(fout)
for joint in joints:
joint.write(fout)
scales.tofile(fout)
rotations.tofile(fout)
translations.tofile(fout)
class Bck(BFile):
sectionHandlers = {b'ANK1': J3DAnmTransformKeyData}
class AnimatedJoint(Readable):
def __init__(self, f=None):
self.x = AnimComponent()
self.y = AnimComponent()
self.z = AnimComponent()
super().__init__(f)
def read(self, f):
self.x.read(f)
self.y.read(f)
self.z.read(f)
def write(self, f):
self.x.write(f)
self.y.write(f)
self.z.write(f)
class AnimComponent(Readable):
def __init__(self, f=None):
self.s = AnimIndex()
self.r = AnimIndex()
self.t = AnimIndex()
super().__init__(f)
def read(self, f):
self.s.read(f)
self.r.read(f)
self.t.read(f)
def write(self, f):
self.s.write(f)
self.r.write(f)
self.t.write(f)
class TangentType(Enum):
In = 0
InOut = 1
class AnimIndex(ReadableStruct):
header = Struct('>HHH')
fields = ["count", "index", ("tangent", TangentType)]
class Key(object):
time: float
value: float
tangentIn: float
tangentOut: float
class Animation(object):
scalesX: list[Key]
scalesY: list[Key]
scalesZ: list[Key]
rotationsX: list[Key]
rotationsY: list[Key]
rotationsZ: list[Key]
translationsX: list[Key]
translationsY: list[Key]
translationsZ: list[Key]
def readComp(src, index):
dst = [None]*index.count
if index.count <= 0:
warn("bck1: readComp(): count is <= 0")
return
elif index.count == 1:
k = Key()
k.time = 0
k.value = src[index.index]
k.tangentIn = 0
k.tangentOut = 0
dst[0] = k
else:
sz = {TangentType.In: 3, TangentType.InOut: 4}[index.tangent]
for j in range(index.count):
k = Key()
k.time = src[index.index + sz*j]
k.value = src[index.index + sz*j + 1]
k.tangentIn = src[index.index + sz*j + 2]
if index.tangent == TangentType.InOut:
k.tangentOut = src[index.index + sz*j + 3]
else:
k.tangentOut = k.tangentIn
dst[j] = k
dst.sort(key=lambda a: a.time)
return dst
def addComp(idx: AnimIndex, keys: list[Key], out, scale=1.0, cnv=float):
idx.count = len(keys)
tangentSimple = all([key.tangentIn == key.tangentOut for key in keys])
idx.tangent = TangentType.In if tangentSimple else TangentType.InOut
if idx.count == 1:
values = [cnv(key.value/scale) for key in keys]
elif tangentSimple:
values = [cnv(v) for key in keys for v in (key.time, key.value/scale, key.tangentIn/scale)]
else:
values = [cnv(v) for key in keys for v in (key.time, key.value/scale, key.tangentIn/scale, key.tangentOut/scale)]
idx.index = arrayStringSearch(out, values)
if idx.index is None:
idx.index = len(out)
out.extend(values)
def getPointCubic(cf, t):
return ((cf[0] * t + cf[1]) * t + cf[2]) * t + cf[3]
def getDerivativeCubic(cf, t):
return (3 * cf[0] * t + 2 * cf[1]) * t + cf[2]
def getCoeffHermite(p0, p1, s0, s1):
return (
(p0 * 2) + (p1 * -2) + (s0 * 1) + (s1 * 1),
(p0 * -3) + (p1 * 3) + (s0 * -2) + (s1 * -1),
(p0 * 0) + (p1 * 0) + (s0 * 1) + (s1 * 0),
(p0 * 1) + (p1 * 0) + (s0 * 0) + (s1 * 0)
)
def getPointHermite(p0, p1, s0, s1, t):
coeff = getCoeffHermite(p0, p1, s0, s1)
return getPointCubic(coeff, t)
def getDerivativeHermite(p0, p1, s0, s1, t):
coeff = getCoeffHermite(p0, p1, s0, s1)
return getDerivativeCubic(coeff, t)
def hermiteInterpolate(k0, k1, frame, tangents=False):
length = k1.time - k0.time
t = (frame - k0.time) / length
p0 = k0.value
p1 = k1.value
s0 = k0.tangentOut * length
s1 = k1.tangentIn * length
if tangents:
return getPointHermite(p0, p1, s0, s1, t), getDerivativeHermite(p0, p1, s0, s1, t)
else:
return getPointHermite(p0, p1, s0, s1, t)
def animateSingle(time, keyList, tangents=False):
timeList = [key.time for key in keyList]
i = bisect(timeList, time)
if i <= 0:
# the time is before any keys
if tangents:
return keyList[0].value, keyList[0].tangentIn
else:
return keyList[0].value
elif i >= len(keyList):
# the time is after all keys
if tangents:
return keyList[-1].value, keyList[0].tangentOut
else:
return keyList[-1].value
else:
keyBefore = keyList[i-1]
keyAfter = keyList[i]
return hermiteInterpolate(keyBefore, keyAfter, time, tangents)
def animate(time, keyListSet, tangents=False):
return (animateSingle(time, keyList, tangents) for keyList in keyListSet)