-
Notifications
You must be signed in to change notification settings - Fork 2
/
anim9.lua
416 lines (362 loc) · 9.73 KB
/
anim9.lua
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
local cpml = require "cpml"
local cpml_mat4 = cpml.mat4
local cpml_vec3 = cpml.vec3
local cpml_quat = cpml.quat
local cpml_utils = cpml.utils
local anim = {
_LICENSE = "anim9 is distributed under the terms of the MIT license. See LICENSE.md.",
_URL = "https://github.com/excessive/anim9",
_VERSION = "0.2.0",
_DESCRIPTION = "Animation library for LÖVE3D.",
}
anim.__index = anim
local calc_bone_matrix = cpml_mat4.from_transform
local function bind_pose(skeleton)
local pose = {}
for i = 1, #skeleton do
pose[i] = {
translate = skeleton[i].position,
rotate = skeleton[i].rotation,
scale = skeleton[i].scale
}
end
return pose
end
local function is_child(skeleton, bone, which)
local next = skeleton[bone]
if bone == which then
return true
elseif next.parent < which then
return false
else
return is_child(skeleton, next.parent, which)
end
end
local function mix_poses(skeleton, p1, p2, weight, start)
local new_pose = {}
for i = 1, #skeleton do
if not p1[i] or not p2[i] then
goto continue
end
local mix = weight
if start > 1 then
if not is_child(skeleton, i, start) then
mix = 0
end
end
local r = cpml_quat.slerp(p1[i].rotate, p2[i].rotate, mix)
r = r:normalize()
new_pose[i] = {
translate = cpml_vec3.lerp(p1[i].translate, p2[i].translate, mix),
rotate = r,
scale = cpml_vec3.lerp(p1[i].scale, p2[i].scale, mix)
}
::continue::
end
return new_pose
end
local function update_matrices(skeleton, base, pose, indices)
local animation_buffer = {}
local transform = {}
local bone_lookup = {}
local identity = cpml_mat4()
for i, joint in ipairs(skeleton) do
local m = identity
if pose[i] then
m = calc_bone_matrix(pose[i].translate, pose[i].rotate, pose[i].scale)
else
m = calc_bone_matrix(joint.position, joint.rotation, joint.scale)
end
local render
if joint.parent > 0 then
assert(joint.parent < i)
transform[i] = transform[joint.parent] * m
render = transform[i] * base[i]
else
transform[i] = m
render = m * base[i]
end
bone_lookup[joint.name] = transform[i]
animation_buffer[indices[joint.name]] = render:to_vec4s()
end
return animation_buffer, bone_lookup
end
local function new(data, anims, markers)
if not data.skeleton then return end
local t = {
time = 0,
animations = {},
timeline = {},
skeleton = {},
inverse_base = {},
index_map = {},
bind_pose = {}
}
local o = setmetatable(t, anim)
if anims ~= nil and not anims then
return o
end
o:rebind(data)
for _, v in ipairs(anims or data) do
if markers then
o:add_animation(v, data.frames, markers[v.name])
else
o:add_animation(v, data.frames)
end
end
return o
end
function anim:rebind(data)
self.skeleton = data.skeleton
self.bind_pose = bind_pose(self.skeleton)
self.inverse_base = {}
self.index_map = {}
-- Calculate inverse base pose.
for i, bone in ipairs(data.skeleton) do
local m = calc_bone_matrix(bone.position, bone.rotation, bone.scale)
local inv = cpml_mat4():invert(m)
if bone.parent > 0 then
assert(bone.parent < i)
self.inverse_base[i] = inv * self.inverse_base[bone.parent]
else
self.inverse_base[i] = inv
end
self.index_map[i] = i
self.index_map[bone.name] = i
end
end
function anim:find_index(bone_name)
for i, bone in ipairs(self.skeleton) do
if bone.name == bone_name then
return i
end
end
return 1
end
--- Add animation to anim object
-- @param animation Animation data
-- @param frames Frame data
function anim:add_animation(animation, frames, markers)
if animation.frames then
for _, v in ipairs(animation) do
-- if markers then
-- o:add_animation(v, data.frames, markers[v.name])
-- else
self:add_animation(v, animation.frames)
end
return
end
local new_anim = {
name = animation.name,
frames = {},
length = animation.last - animation.first,
framerate = animation.framerate,
loop = animation.loop,
markers = markers or {}
}
for i = animation.first, animation.last do
table.insert(new_anim.frames, frames[i])
end
self.animations[new_anim.name] = new_anim
end
--- Create a new track
-- @param name Name of animation for track
-- @param weight Percentage of total timeline blending being given to track
-- @param rate Playback rate of animation
-- @param callback Function to call after non-looping animation ends
-- @param lock Stops track from being affected by transition
-- @return table Track object
function anim:new_track(name, weight, rate, callback, lock, early)
if not self.animations[name] then
return nil
end
local t = {
name = assert(name),
offset = self.time,
time = self.time,
weight = weight or 1,
rate = rate or 1,
callback = callback or false,
lock = lock or false,
early = early or false,
playing = false,
active = true,
frame = 0,
marker = 0,
blend = 1,
base = 1
}
return t
end
--- Add track to timeline
-- @param track Track object to play
-- @return table Track object
function anim:play(track)
assert(type(track) == "table")
assert(self.timeline[track] == nil)
track.playing = true
track.offset = self.time
track.time = self.time
table.insert(self.timeline, track)
self.timeline[track] = track
return track
end
--- Remove track from timeline
-- @param track Track to remove from timeline (optional). If not specified, removes all.
function anim:stop(track)
if track ~= nil then
assert(self.timeline[track])
end
for i = #self.timeline, 1, -1 do
if self.timeline[i] == track or track == nil then
self.timeline[self.timeline[i]] = nil
table.remove(self.timeline, i)
break
end
end
end
--- Get length of animation
-- @param name Name of animation
-- @return number Length of animation (in seconds)
function anim:length(name)
local _anim = assert(self.animations[name], string.format("Invalid animation: \'%s\'", name))
return _anim.length / _anim.framerate
end
--- Update animations
-- @param _dt Delta time
function anim:update(dt)
self.time = self.time + dt
for _, track in ipairs(self.timeline) do
track.time = track.time + (dt * track.rate)
end
-- Transition from one animation to the next
if self.transitioning then
local t = self.transitioning
t.time = t.time + dt * t.track.rate
local progress = math.min(t.time / t.length, 1)
-- fade new animation in
t.track.blend = cpml_utils.lerp(0, 1, progress)
-- fade old animations out
for _, track in ipairs(self.timeline) do
if track ~= t.track and not track.lock then
track.blend = cpml_utils.lerp(0, 1, 1-progress)
end
end
-- remove dead animations
if progress == 1 then
for _, track in ipairs(self.timeline) do
if track.blend == 0 and not track.lock then
self:stop(track)
-- Call callback on early exit if flagged
if track.early and type(track.callback) == "function" then
track.callback(self)
end
end
end
self.transitioning = nil
end
end
local pose = self.bind_pose
for _, track in ipairs(self.timeline) do
if not track.playing then
track.offset = track.offset + dt
end
if not track.active then
goto continue
end
local time = track.time - track.offset
local _anim = self.animations[track.name]
local frame = time * _anim.framerate
if _anim.loop then
frame = frame % _anim.length
else
if frame >= _anim.length and not track.lock then
self:stop(track)
if type(track.callback) == "function" then
track.callback(self)
end
goto continue
end
frame = math.min(_anim.length, frame)
end
frame = math.max(frame, 0)
local f1, f2 = math.floor(frame), math.ceil(frame)
track.frame = f1
-- make sure f2 doesn't exceed anim length or wrongly loop
if _anim.loop then
f2 = f2 % _anim.length
else
f2 = math.min(_anim.length, f2)
end
-- Update the final pose
local interp = mix_poses(
self.skeleton,
_anim.frames[f1+1],
_anim.frames[f2+1],
frame - f1,
track.base
)
pose = mix_poses(self.skeleton, pose, interp, track.weight * track.blend, track.base)
::continue::
end
self.current_pose, self.current_matrices = update_matrices(
self.skeleton, self.inverse_base, pose, self.index_map
)
end
--- Reset animations
-- @param clear_locked Flag to clear even locked tracks
function anim:reset(clear_locked)
self.time = 0
self.transitioning = nil
for i = #self.timeline, 1, -1 do
local track = self.timeline[i]
if not track.lock or clear_locked then
table.remove(self.timeline, i)
self.timeline[track] = nil
end
end
end
--- Transition from one animation to another
-- @param track Track object to transition to
-- @param length Length of transition (in seconds)
function anim:transition(track, length)
assert(track)
if self.transitioning and self.transitioning.track == track then
return
end
if not self.timeline[track] then
self:play(track)
end
self.transitioning = {
track = track,
length = length or 0.2,
time = 0
}
track.offset = self.time
track.time = self.time
end
--- Find track in timeline
-- @param track Track to locate
-- @return boolean true if found, false if not found
function anim:find_track(track)
if self.timeline[track] then
return true
end
return false
end
return setmetatable({
new = new
}, {
__call = function(_, ...) return new(...) end
})
--- @table Track
-- @field name Name of animation
-- @field offset Offset from timeline time for track to start play
-- @field weight Blend weight
-- @field rate Playback rate of animation
-- @field callback Function to call after non-looping animation ends
-- @field lock Stop track from being affected by transitions
-- @field playing Determine if animation is playing
-- @field active Toggle influence of track (used for debugging animations)
-- @field blend Fade in/out during transition
-- @field base Starting bone to be used