-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimCurve.lua
More file actions
430 lines (332 loc) · 10.4 KB
/
AnimCurve.lua
File metadata and controls
430 lines (332 loc) · 10.4 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
AnimCurve = {}
function linearInterpolator1(first, second, alpha)
return second[1] + alpha * (first[1] - second[1])
end
function linearInterpolator2(first, second, alpha)
local oneMinusAlpha = 1 - alpha
return first[1] * alpha + second[1] * oneMinusAlpha, first[2] * alpha + second[2] * oneMinusAlpha
end
function linearInterpolator3(first, second, alpha)
local oneMinusAlpha = 1 - alpha
return first[1] * alpha + second[1] * oneMinusAlpha, first[2] * alpha + second[2] * oneMinusAlpha, first[3] * alpha + second[3] * oneMinusAlpha
end
function linearInterpolator4(first, second, alpha)
local oneMinusAlpha = 1 - alpha
return first[1] * alpha + second[1] * oneMinusAlpha, first[2] * alpha + second[2] * oneMinusAlpha, first[3] * alpha + second[3] * oneMinusAlpha, first[4] * alpha + second[4] * oneMinusAlpha
end
function linearInterpolatorN(first, second, alpha)
local oneMinusAlpha = 1 - alpha
local ret = {}
for i, v in ipairs(first) do
table.insert(ret, v * alpha + second[i] * oneMinusAlpha)
end
return ret
end
function linearInterpolatorTransRot(first, second, alpha)
local oneMinusAlpha = 1 - alpha
return first.x * alpha + second.x * oneMinusAlpha, first.y * alpha + second.y * oneMinusAlpha, first.z * alpha + second.z * oneMinusAlpha, first.rx * alpha + second.rx * oneMinusAlpha, first.ry * alpha + second.ry * oneMinusAlpha, first.rz * alpha + second.rz * oneMinusAlpha
end
function linearInterpolatorTransRotScale(first, second, alpha)
local oneMinusAlpha = 1 - alpha
return first.x * alpha + second.x * oneMinusAlpha, first.y * alpha + second.y * oneMinusAlpha, first.z * alpha + second.z * oneMinusAlpha, first.rx * alpha + second.rx * oneMinusAlpha, first.ry * alpha + second.ry * oneMinusAlpha, first.rz * alpha + second.rz * oneMinusAlpha, first.sx * alpha + second.sx * oneMinusAlpha, first.sy * alpha + second.sy * oneMinusAlpha, first.sz * alpha + second.sz * oneMinusAlpha
end
function catmullRomInterpolator1(p1, p2, p0, p3, t)
t = 1 - t
local t2 = t * t
local t3 = t2 * t
local p0v = nil
if p0 == nil then
p0v = 2 * p1.v - p2.v
else
p0v = p0.v
end
local p3v = nil
if p3 == nil then
p3v = 2 * p2.v - p1.v
else
p3v = p3.v
end
local v = 0.5 * (2 * p1.v + (-p0v + p2.v) * t + (2 * p0v - 5 * p1.v + 4 * p2.v - p3v) * t2 + (-p0v + 3 * p1.v - 3 * p2.v + p3v) * t3)
return v
end
function catmullRomInterpolator3(p1, p2, p0, p3, t)
t = 1 - t
local t2 = t * t
local t3 = t2 * t
local p0x, p0y, p0z = nil
if p0 == nil then
p0x = 2 * p1.x - p2.x
p0y = 2 * p1.y - p2.y
p0z = 2 * p1.z - p2.z
else
p0x = p0.x
p0y = p0.y
p0z = p0.z
end
local p3x, p3y, p3z = nil
if p3 == nil then
p3x = 2 * p2.x - p1.x
p3y = 2 * p2.y - p1.y
p3z = 2 * p2.z - p1.z
else
p3x = p3.x
p3y = p3.y
p3z = p3.z
end
local x = 0.5 * (2 * p1.x + (-p0x + p2.x) * t + (2 * p0x - 5 * p1.x + 4 * p2.x - p3x) * t2 + (-p0x + 3 * p1.x - 3 * p2.x + p3x) * t3)
local y = 0.5 * (2 * p1.y + (-p0y + p2.y) * t + (2 * p0y - 5 * p1.y + 4 * p2.y - p3y) * t2 + (-p0y + 3 * p1.y - 3 * p2.y + p3y) * t3)
local z = 0.5 * (2 * p1.z + (-p0z + p2.z) * t + (2 * p0z - 5 * p1.z + 4 * p2.z - p3z) * t2 + (-p0z + 3 * p1.z - 3 * p2.z + p3z) * t3)
return x, y, z
end
function quaternionInterpolator(p1, p2, t)
return MathUtil.nlerpQuaternionShortestPath(p2.x, p2.y, p2.z, p2.w, p1.x, p1.y, p1.z, p1.w, t)
end
function quaternionInterpolator2(p1, p2, p0, p3, t)
t = 1 - t
local w0 = (1 - t) * 0.6
local w3 = t * 0.6
if p0 == nil then
p0 = p1
w0 = 0
end
if p3 == nil then
p3 = p2
w3 = 0
end
local w1 = 1 - t + w3
local w2 = t + w0
local x = p0.x * w0
local y = p0.y * w0
local z = p0.z * w0
local w = p0.w * w0
x, y, z, w = MathUtil.quaternionMadShortestPath(x, y, z, w, p1.x, p1.y, p1.z, p1.w, w1)
x, y, z, w = MathUtil.quaternionMadShortestPath(x, y, z, w, p2.x, p2.y, p2.z, p2.w, w2)
x, y, z, w = MathUtil.quaternionMadShortestPath(x, y, z, w, p3.x, p3.y, p3.z, p3.w, w3)
return MathUtil.quaternionNormalized(x, y, z, w)
end
local AnimCurve_mt = Class(AnimCurve)
function AnimCurve.new(interpolator, interpolatorDegree)
local self = setmetatable({}, AnimCurve_mt)
self.keyframes = {}
self.interpolator = interpolator
self.interpolatorDegree = Utils.getNoNil(interpolatorDegree, 2)
self.currentTime = 0
self.maxTime = 0
self.numKeyframes = 0
return self
end
function AnimCurve:delete()
end
function AnimCurve:reset()
table.clear(self.keyframes)
self.numKeyframes = 0
self.currentTime = 0
self.maxTime = 0
end
function AnimCurve:addKeyframe(keyframe, xmlFile, key)
local numKeys = self.numKeyframes
if numKeys > 0 and keyframe.time < self.keyframes[numKeys].time then
if xmlFile ~= nil then
if type(xmlFile) == "number" then
xmlFile = g_xmlManager:getFileByHandle(xmlFile)
end
if xmlFile ~= nil then
Logging.xmlError(xmlFile, "keyframes not strictly monotonic increasing at %s", key)
else
Logging.error("keyframes not strictly monotonic increasing at %s", key)
end
else
print("Error: keyframes not strictly monotonic increasing")
end
end
table.insert(self.keyframes, keyframe)
self.maxTime = keyframe.time
self.numKeyframes = numKeys + 1
end
function AnimCurve:removeKeyframe(index)
if index ~= nil and (index < 1 or index > #self.keyframes) then
return
end
for i = #self.keyframes - 1, index, -1 do
self.keyframes[i + 1].time = self.keyframes[i].time
end
table.remove(self.keyframes, index)
self.maxTime = self.keyframes[#self.keyframes] and self.keyframes[#self.keyframes].time or 0
self.numKeyframes = self.numKeyframes - 1
end
function AnimCurve:getMaximum()
local numKeys = #self.keyframes
if numKeys == 0 then
return 0, 0
elseif numKeys == 1 then
return self:getFromKeyframes(self.keyframes[1], self.keyframes[1], 1, 1, 0), self.keyframes[1].time
end
local maxValue = self:getFromKeyframes(self.keyframes[1], self.keyframes[2], 1, 2, 0)
local maxTime = self.keyframes[1].time
for i = 1, numKeys - 1 do
local value = self:getFromKeyframes(self.keyframes[i], self.keyframes[i + 1], i, i + 1, 1)
if maxValue < value then
maxValue = value
maxTime = self.keyframes[i + 1].time
end
end
return maxValue, maxTime
end
function AnimCurve:get(time)
local numKeys = self.numKeyframes
if numKeys == 0 then
return
end
local first, second, firstI, secondI = nil
if numKeys >= 2 and self.keyframes[1].time <= time then
if time < self.maxTime then
for i = 2, numKeys do
second = self.keyframes[i]
secondI = i
if time <= second.time then
first = self.keyframes[i - 1]
firstI = i - 1
break
end
end
else
first = self.keyframes[numKeys]
second = first
firstI = numKeys
secondI = numKeys
end
else
first = self.keyframes[1]
second = first
end
local time0 = first.time
local time1 = second.time
local alpha = nil
if time0 < time1 then
alpha = (time1 - time) / (time1 - time0)
else
alpha = 0
end
if self.segmentTimes ~= nil and firstI < numKeys then
local timesOffset = (firstI - 1) * (self.numTimesPerKeyframe + 1) + 1
local segmentT = time - first.time
local segmentLow, segmentHi = self:getInterval(segmentT, self.segmentTimes, timesOffset, self.numTimesPerKeyframe + 1)
alpha = segmentLow
local l = self.segmentTimes[segmentHi + timesOffset] - self.segmentTimes[segmentLow + timesOffset]
if l > 0 then
local p = segmentT - self.segmentTimes[segmentLow + timesOffset]
alpha = alpha + p / l
end
alpha = alpha / self.numTimesPerKeyframe
alpha = 1 - alpha
end
return self:getFromKeyframes(first, second, firstI, secondI, alpha)
end
function AnimCurve:getFromKeyframes(first, second, firstI, secondI, alpha)
if self.interpolatorDegree == 2 then
return self.interpolator(first, second, alpha)
elseif self.interpolatorDegree == 3 then
local beforeFirst = nil
if firstI > 1 then
beforeFirst = self.keyframes[firstI - 1]
end
local afterSecond = nil
local numKeys = #self.keyframes
if secondI < numKeys then
afterSecond = self.keyframes[secondI + 1]
end
return self.interpolator(first, second, beforeFirst, afterSecond, alpha)
end
end
function AnimCurve:getInterval(time, times, timesOffset, numTimes)
local low = 0
local hi = numTimes
while hi - low > 1 do
local kk = math.floor((hi + low) / 2)
if time < times[kk + timesOffset] then
hi = kk
else
low = kk
end
end
return low, hi
end
function AnimCurve:loadCurveFromXML(xmlFile, baseKey, loadFunc)
local i = 0
while true do
local key = string.format("%s.key(%d)", baseKey, i)
if not hasXMLProperty(xmlFile, key) then
break
end
local keyFrame = loadFunc(xmlFile, key)
if keyFrame ~= nil then
self:addKeyframe(keyFrame)
end
i = i + 1
end
end
function loadInterpolator1Curve(xmlFile, key)
local time = getXMLString(xmlFile, key .. "#time")
local value = getXMLString(xmlFile, key .. "#value")
if value ~= nil then
return {
Utils.evaluateFormula(value),
time = Utils.evaluateFormula(time)
}
end
return nil
end
function loadInterpolator2Curve(xmlFile, key)
local time = getXMLString(xmlFile, key .. "#time")
local values = string.split(getXMLString(xmlFile, key .. "#values"), " ")
if values ~= nil then
return {
Utils.evaluateFormula(values[1]),
Utils.evaluateFormula(values[2]),
time = Utils.evaluateFormula(time)
}
end
return nil
end
function loadInterpolator3Curve(xmlFile, key)
local time = getXMLString(xmlFile, key .. "#time")
local values = string.split(getXMLString(xmlFile, key .. "#values"), " ")
if values ~= nil then
return {
Utils.evaluateFormula(values[1]),
Utils.evaluateFormula(values[2]),
Utils.evaluateFormula(values[3]),
time = Utils.evaluateFormula(time)
}
end
return nil
end
function loadInterpolator4Curve(xmlFile, key)
local time = getXMLString(xmlFile, key .. "#time")
local values = string.split(getXMLString(xmlFile, key .. "#values"), " ")
if values ~= nil then
return {
Utils.evaluateFormula(values[1]),
Utils.evaluateFormula(values[2]),
Utils.evaluateFormula(values[3]),
Utils.evaluateFormula(values[4]),
time = Utils.evaluateFormula(time)
}
end
return nil
end
function getLoadNamedInterpolatorCurve(names)
return function (xmlFile, key)
local time = getXMLString(xmlFile, key .. "#time")
local values = {}
for _, name in ipairs(names) do
local value = getXMLString(xmlFile, key .. "#" .. name)
if value == nil then
return nil
end
table.insert(values, Utils.evaluateFormula(value))
end
values.time = Utils.evaluateFormula(time)
return values
end
end