-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcl_boneanimlib.lua
More file actions
348 lines (294 loc) · 11.6 KB
/
cl_boneanimlib.lua
File metadata and controls
348 lines (294 loc) · 11.6 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
if SERVER or GetLuaAnimations ~= nil then return end
include("sh_boneanimlib.lua")
include("sh_mocap.lua")
local ANIMATIONFADEOUTTIME = 0.125
net.Receive("bal_reset", function(length)
local ent = net.ReadEntity()
local anim = net.ReadString()
local time = net.ReadFloat()
local power = net.ReadFloat()
local timescale = net.ReadFloat()
if ent:IsValid() then
ent:ResetLuaAnimation(anim, time ~= -1 and time, power ~= -1 and power, timescale ~= -1 and timescale)
end
end)
net.Receive("bal_set", function(length)
local ent = net.ReadEntity()
local anim = net.ReadString()
local time = net.ReadFloat()
local power = net.ReadFloat()
local timescale = net.ReadFloat()
if ent:IsValid() then
ent:SetLuaAnimation(anim, time ~= -1 and time, power ~= -1 and power, timescale ~= -1 and timescale)
end
end)
net.Receive("bal_stop", function(length)
local ent = net.ReadEntity()
local anim = net.ReadString()
local tim = net.ReadFloat()
if tim == 0 then tim = nil end
if ent:IsValid() then
ent:StopLuaAnimation(anim, tim)
end
end)
net.Receive("bal_stopgroup", function(length)
local ent = net.ReadEntity()
local animgroup = net.ReadString()
local tim = net.ReadFloat()
if tim == 0 then tim = nil end
if ent:IsValid() then
ent:StopLuaAnimationGroup(animgroup, tim)
end
end)
net.Receive("bal_stopall", function(length)
local ent = net.ReadEntity()
local tim = net.ReadFloat()
if tim == 0 then tim = nil end
if ent:IsValid() then
ent:StopAllLuaAnimations(tim)
end
end)
local TYPE_GESTURE = TYPE_GESTURE
local TYPE_POSTURE = TYPE_POSTURE
local TYPE_STANCE = TYPE_STANCE
local TYPE_SEQUENCE = TYPE_SEQUENCE
local Animations = GetLuaAnimations()
local function AdvanceFrame(tGestureTable, tFrameData)
tGestureTable.FrameDelta = tGestureTable.FrameDelta + FrameTime() * tFrameData.FrameRate * tGestureTable.TimeScale
if tGestureTable.FrameDelta > 1 then
tGestureTable.Frame = tGestureTable.Frame + 1
tGestureTable.FrameDelta = math.min(1, tGestureTable.FrameDelta - 1)
if tGestureTable.Frame > #tGestureTable.FrameData then
tGestureTable.Frame = math.min(tGestureTable.RestartFrame or 1, #tGestureTable.FrameData)
return true
end
end
return false
end
local function LinearInterpolation(y1, y2, mu)
return y1 * (1 - mu) + y2 * mu
end
local function CosineInterpolation(y1, y2, mu)
local mu2 = (1 - math.cos(mu * math.pi)) / 2
return y1 * (1 - mu2) + y2 * mu2
end
local function CubicInterpolation(y0, y1, y2, y3, mu)
local mu2 = mu * mu
local a0 = y3 - y2 - y0 + y1
return a0 * mu * mu2 + (y0 - y1 - a0) * mu2 + (y2 - y0) * mu + y1
end
local EMPTYBONEINFO = {MU = 0, MR = 0, MF = 0, RU = 0, RR = 0, RF = 0}
local function GetFrameBoneInfo(pl, tGestureTable, iFrame, iBoneID)
local tPrev = tGestureTable.FrameData[iFrame]
if tPrev then
return tPrev.BoneInfo[iBoneID] or tPrev.BoneInfo[pl:GetBoneName(iBoneID)] or EMPTYBONEINFO
end
return EMPTYBONEINFO
end
local function DoCurrentFrame(tGestureTable, tFrameData, iCurFrame, pl, fAmount, fFrameDelta, fPower, bNoInterp, tBuffer)
for iBoneID, tBoneInfo in pairs(tFrameData.BoneInfo) do
if type(iBoneID) ~= "number" then
iBoneID = pl:LookupBone(iBoneID)
end
if not iBoneID then continue end
if not tBuffer[iBoneID] then tBuffer[iBoneID] = Matrix() end
local mBoneMatrix = tBuffer[iBoneID]
local vCurBonePos, aCurBoneAng = mBoneMatrix:GetTranslation(), mBoneMatrix:GetAngles()
if not tBoneInfo.Callback or not tBoneInfo.Callback(pl, mBoneMatrix, iBoneID, vCurBonePos, aCurBoneAng, fFrameDelta, fPower) then
local vUp = aCurBoneAng:Up()
local vRight = aCurBoneAng:Right()
local vForward = aCurBoneAng:Forward()
local iInterp = tGestureTable.Interpolation
if iInterp == INTERP_LINEAR or bNoInterp then
local bi1 = GetFrameBoneInfo(pl, tGestureTable, iCurFrame - 1, iBoneID)
mBoneMatrix:Translate(LinearInterpolation(bi1.MU * vUp + bi1.MR * vRight + bi1.MF * vForward, tBoneInfo.MU * vUp + tBoneInfo.MR * vRight + tBoneInfo.MF * vForward, fFrameDelta) * fPower)
mBoneMatrix:Rotate(LinearInterpolation(Angle(bi1.RR, bi1.RU, bi1.RF), Angle(tBoneInfo.RR, tBoneInfo.RU, tBoneInfo.RF), fFrameDelta) * fPower)
elseif iInterp == INTERP_CUBIC and tGestureTable.FrameData[iCurFrame - 2] and tGestureTable.FrameData[iCurFrame + 1] then
local bi0 = GetFrameBoneInfo(pl, tGestureTable, iCurFrame - 2, iBoneID)
local bi1 = GetFrameBoneInfo(pl, tGestureTable, iCurFrame - 1, iBoneID)
local bi3 = GetFrameBoneInfo(pl, tGestureTable, iCurFrame + 1, iBoneID)
mBoneMatrix:Translate(CosineInterpolation(bi1.MU * vUp + bi1.MR * vRight + bi1.MF * vForward, tBoneInfo.MU * vUp + tBoneInfo.MR * vRight + tBoneInfo.MF * vForward, fFrameDelta) * fPower)
mBoneMatrix:Rotate(CubicInterpolation(Angle(bi0.RR, bi0.RU, bi0.RF),
Angle(bi1.RR, bi1.RU, bi1.RF),
Angle(tBoneInfo.RR, tBoneInfo.RU, tBoneInfo.RF),
Angle(bi3.RR, bi3.RU, bi3.RF),
fFrameDelta) * fPower)
else -- Default is Cosine
local bi1 = GetFrameBoneInfo(pl, tGestureTable, iCurFrame - 1, iBoneID)
mBoneMatrix:Translate(CosineInterpolation(bi1.MU * vUp + bi1.MR * vRight + bi1.MF * vForward, tBoneInfo.MU * vUp + tBoneInfo.MR * vRight + tBoneInfo.MF * vForward, fFrameDelta) * fPower)
mBoneMatrix:Rotate(CosineInterpolation(Angle(bi1.RR, bi1.RU, bi1.RF), Angle(tBoneInfo.RR, tBoneInfo.RU, tBoneInfo.RF), fFrameDelta) * fPower)
end
end
end
end
local function BuildBonePositions(pl)
local tBuffer = {}
local tLuaAnimations = pl.LuaAnimations
for sGestureName, tGestureTable in pairs(tLuaAnimations) do
local iCurFrame = tGestureTable.Frame
local tFrameData = tGestureTable.FrameData[iCurFrame]
local fFrameDelta = tGestureTable.FrameDelta
local fDieTime = tGestureTable.DieTime
local fPower = tGestureTable.Power
if fDieTime and fDieTime - ANIMATIONFADEOUTTIME <= CurTime() then
fPower = fPower * (fDieTime - CurTime()) / ANIMATIONFADEOUTTIME
end
local fAmount = fPower * fFrameDelta
DoCurrentFrame(tGestureTable, tFrameData, iCurFrame, pl, fAmount, fFrameDelta, fPower, tGestureTable.Type == TYPE_POSTURE, tBuffer)
if tGestureTable.DisplayCallback then
tGestureTable.DisplayCallback(pl, sGestureName, tGestureTable, iCurFrame, tFrameData, fFrameDelta, fPower)
end
end
for iBoneID, mMatrix in pairs(tBuffer) do
pl:ManipulateBonePosition(iBoneID, mMatrix:GetTranslation())
pl:ManipulateBoneAngles(iBoneID, mMatrix:GetAngles())
end
end
local function ProcessAnimations(pl)
pl:ResetBoneMatrix()
local tLuaAnimations = pl.LuaAnimations
for sGestureName, tGestureTable in pairs(tLuaAnimations) do
local iCurFrame = tGestureTable.Frame
local tFrameData = tGestureTable.FrameData[iCurFrame]
local fFrameDelta = tGestureTable.FrameDelta
local fDieTime = tGestureTable.DieTime
local fPower = tGestureTable.Power
if fDieTime and fDieTime - ANIMATIONFADEOUTTIME <= CurTime() then
fPower = fPower * (fDieTime - CurTime()) / ANIMATIONFADEOUTTIME
end
local fAmount = fPower * fFrameDelta
if fDieTime and fDieTime <= CurTime() then
pl:StopLuaAnimation(sGestureName)
elseif not tGestureTable.PreCallback or not tGestureTable.PreCallback(pl, sGestureName, tGestureTable, iCurFrame, tFrameData, fFrameDelta) then
if tGestureTable.ShouldPlay and not tGestureTable.ShouldPlay(pl, sGestureName, tGestureTable, iCurFrame, tFrameData, fFrameDelta, fPower) then
pl:StopLuaAnimation(sGestureName, 0.2)
end
if tGestureTable.Type == TYPE_GESTURE then
if AdvanceFrame(tGestureTable, tFrameData) then
pl:StopLuaAnimation(sGestureName)
end
elseif tGestureTable.Type == TYPE_POSTURE then
if fFrameDelta < 1 and tGestureTable.TimeToArrive then
fFrameDelta = math.min(1, fFrameDelta + FrameTime() * (1 / tGestureTable.TimeToArrive))
tGestureTable.FrameDelta = fFrameDelta
end
else
AdvanceFrame(tGestureTable, tFrameData)
end
end
end
if pl.LuaAnimations then
BuildBonePositions(pl)
end
end
hook.Add("Think", "BoneAnimThink", function()
for _, pl in pairs(player.GetAll()) do
if pl.LuaAnimations and pl:IsValid() then
ProcessAnimations(pl)
end
end
end)
hook.Add("CalcMainActivity", "LuaAnimationSequence", function(pl)
if pl.InSequence then
pl:ResetInSequence()
return 0, 0
end
end)
local meta = FindMetaTable("Entity")
if not meta then return end
function meta:ResetBoneMatrix()
for i=0, self:GetBoneCount() - 1 do
self:ManipulateBoneAngles(i, angle_zero)
self:ManipulateBonePosition(i, vector_origin)
end
end
function meta:ResetLuaAnimation(sAnimation, fDieTime, fPower, fTimeScale)
local animtable = Animations[sAnimation]
if animtable then
self.LuaAnimations = self.LuaAnimations or {}
self.LuaAnimations[sAnimation] = {Frame = animtable.StartFrame or 1, FrameDelta = animtable.Type == TYPE_POSTURE and not animtable.TimeToArrive and 1 or 0, FrameData = animtable.FrameData,
TimeScale = fTimeScale or animtable.TimeScale or 1, Type = animtable.Type, RestartFrame = animtable.RestartFrame, TimeToArrive = animtable.TimeToArrive, Callback = animtable.Callback,
ShouldPlay = animtable.ShouldPlay, PreCallback = animtable.PreCallback, Power = fPower or animtable.Power or 1, DieTime = fDieTime or animtable.DieTime, Group = animtable.Group,
UseReferencePose = animtable.UseReferencePose, Interpolation = animtable.Interpolation}
self:ResetLuaAnimationProperties()
end
end
function meta:SetLuaAnimation(sAnimation, fDieTime, fPower, fTimeScale)
if self.LuaAnimations and self.LuaAnimations[sAnimation] then return end
self:ResetLuaAnimation(sAnimation, fDieTime, fPower, fTimeScale)
end
function meta:SetLuaAnimationPower(sAnimation, fPower)
if self.LuaAnimations and self.LuaAnimations[sAnimation] then
self.LuaAnimations[sAnimation].Power = fPower
end
end
function meta:SetLuaAnimationTimeScale(sAnimation, fTimeScale)
if self.LuaAnimations and self.LuaAnimations[sAnimation] then
self.LuaAnimations[sAnimation].TimeScale = fTimeScale
end
end
function meta:SetLuaAnimationDieTime(sAnimation, fTime)
if self.LuaAnimations and self.LuaAnimations[sAnimation] then
if self.LuaAnimations[sAnimation].DieTime then
self.LuaAnimations[sAnimation].DieTime = math.min(self.LuaAnimations[sAnimation].DieTime, fTime)
else
self.LuaAnimations[sAnimation].DieTime = fTime
end
end
end
function meta:ResetInSequence()
local anims = self.LuaAnimations
if anims then
for sAnimation, tAnimTab in pairs(anims) do
if tAnimTab.Type == TYPE_SEQUENCE and (not tAnimTab.DieTime or CurTime() < tAnimTab.DieTime - ANIMATIONFADEOUTTIME) or tAnimTab.UseReferencePose then
self.InSequence = true
return
end
end
self.InSequence = nil
end
end
function meta:ResetLuaAnimationProperties()
local anims = self.LuaAnimations
if anims and table.Count(anims) > 0 then
self:SetIK(false)
self:ResetInSequence()
else
--self:SetIK(true)
self.LuaAnimations = nil
self.InSequence = nil
self:ResetBoneMatrix()
end
end
-- Time is optional, sets the die time to CurTime() + fTime
function meta:StopLuaAnimation(sAnimation, fTime)
local anims = self.LuaAnimations
if anims and anims[sAnimation] then
if fTime then
if anims[sAnimation].DieTime then
anims[sAnimation].DieTime = math.min(anims[sAnimation].DieTime, CurTime() + fTime)
else
anims[sAnimation].DieTime = CurTime() + fTime
end
else
anims[sAnimation] = nil
end
self:ResetLuaAnimationProperties()
end
end
function meta:StopLuaAnimationGroup(sGroup, fTime)
if self.LuaAnimations then
for animname, animtable in pairs(self.LuaAnimations) do
if animtable.Group == sGroup then
self:StopLuaAnimation(animname, fTime)
end
end
end
end
function meta:StopAllLuaAnimations(fTime)
if self.LuaAnimations then
for name in pairs(self.LuaAnimations) do
self:StopLuaAnimation(name, fTime)
end
end
end