-
Notifications
You must be signed in to change notification settings - Fork 0
/
lk3d_skeleton.lua
317 lines (236 loc) · 7.4 KB
/
lk3d_skeleton.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
LK3D = LK3D or {}
-- helper lk3d module for "skeletal" models
-- these are really a bunch of different models being translated cleverly
-- horribly optimized TODO: optimize!
LK3D.SkeletalOriginLUT = {}
function LK3D.DeclareSkeletalOriginPos(mdlname, pos)
LK3D.SkeletalOriginLUT[mdlname] = pos
end
local function make_bone_obj(data)
return {
idx = data.idx,
parent = data.parent,
pos = data.pos * 1,
end_pos = data.end_pos * 1,
ang = Angle(data.ang),
children = {}
}
end
local function recursive_bone_insert(data, bone, transform_pointers)
for k, v in ipairs(data) do
if v.idx == bone.parent then
local bone_object = make_bone_obj(bone)
v.children[#v.children + 1] = bone_object
transform_pointers[bone.idx] = bone_object
return true
else
local ret = recursive_bone_insert(v.children, bone, transform_pointers)
if ret then
return true
end
end
end
end
-- creates a skeletal model
function LK3D.DeclareSkeleton(object, data)
local obj_ptr = LK3D.CurrUniv["objects"][object]
if not obj_ptr then
return
end
-- build the tree
local tree_gen = {}
local transform_pointers = {}
for k, v in ipairs(data) do
if v.parent == -1 then
local bone_object = make_bone_obj(v)
tree_gen[#tree_gen + 1] = bone_object
transform_pointers[v.idx] = bone_object
else
-- search the tree and insert it in
recursive_bone_insert(tree_gen, make_bone_obj(v), transform_pointers)
end
end
obj_ptr.skeleton = tree_gen
obj_ptr.skeleton_transforms = transform_pointers
obj_ptr.parented_obj_ids = {}
obj_ptr.calc_bones = {}
obj_ptr.skeleton_anim_rate = 1
obj_ptr.skeleton_anim_interval = 0.0
obj_ptr.skeleton_anims = {}
return transform_pointers
end
function LK3D.AddSkeletonParentedObject(object, otherobj, boneparent)
if not boneparent then
return
end
local obj_ptr = LK3D.CurrUniv["objects"][object]
if not obj_ptr then
return
end
if not obj_ptr.skeleton then
return
end
local other_obj_ptr = LK3D.CurrUniv["objects"][otherobj]
if not other_obj_ptr then
return
end
if not obj_ptr.parented_obj_ids then
obj_ptr.parented_obj_ids = {}
end
local parented_ids = obj_ptr.parented_obj_ids
if not parented_ids[boneparent] then
parented_ids[boneparent] = {}
end
local parented_bp = parented_ids[boneparent]
parented_bp[#parented_bp + 1] = otherobj
end
-- removes a whole skeleton (base object + parented objects)
function LK3D.RemoveSkeletonObject(object)
if not object then
return
end
local obj_ptr = LK3D.CurrUniv["objects"][object]
if not obj_ptr then
return
end
if not obj_ptr.parented_obj_ids then
return
end
for k, v in pairs(obj_ptr.parented_obj_ids) do
if LK3D.CurrUniv["objects"][v] then
LK3D.RemoveObjectFromUniverse(v)
end
end
LK3D.RemoveObjectFromUniverse(object)
end
function LK3D.DeclareSkeletonAnim(object, anim, func)
if not func then
return
end
local obj_ptr = LK3D.CurrUniv["objects"][object]
if not obj_ptr then
return
end
obj_ptr.skeleton_anims[anim] = func
end
function LK3D.SetSkeletonAnim(object, targetanim)
if not targetanim then
return
end
local obj_ptr = LK3D.CurrUniv["objects"][object]
if not obj_ptr then
return
end
obj_ptr.skeleton_curr_anim = targetanim
end
function LK3D.SetSkeletonAnimPlayRate(object, rate)
if not rate then
return
end
local obj_ptr = LK3D.CurrUniv["objects"][object]
if not obj_ptr then
return
end
obj_ptr.skeleton_anim_rate = rate
end
-- because recalculating matrices is slow
function LK3D.SetSkeletonUpdateInterval(object, interval)
if not interval then
return
end
local obj_ptr = LK3D.CurrUniv["objects"][object]
if not obj_ptr then
return
end
obj_ptr.skeleton_anim_interval = interval
end
local function recursive_recalculate_bones(bones, object, l_matrix)
for k, v in ipairs(bones) do
local l_mtrx_cpy = Matrix(l_matrix)
local real_matrix = Matrix(l_matrix)
real_matrix:Translate(v.pos)
object.calc_bones[v.idx] = real_matrix
if #v.children ~= 0 then
l_mtrx_cpy:Translate(v.pos)
l_mtrx_cpy:Translate(v.end_pos)
l_mtrx_cpy:Rotate(v.ang)
recursive_recalculate_bones(v.children, object, l_mtrx_cpy)
end
end
end
local function object_update_bone_objects(object)
if CurTime() < (object.next_calculate or 0) then
return
end
object.next_calculate = CurTime() + object.skeleton_anim_interval
if (not object["NO_VW_CULLING"]) and ((object.pos - LK3D.CamPos):Dot(LK3D.CamAng:Forward()) > 0) then
recursive_recalculate_bones(object.skeleton, object, Matrix())
end
if not object.parented_obj_ids then
return
end
local matrix_obj = Matrix()
matrix_obj:SetTranslation(object.pos)
matrix_obj:SetAngles(object.ang)
for k, v in pairs(object.parented_obj_ids) do
local count = #v
if count == 0 then
continue
end
for i = 1, count do
local obj_idex = v[i]
local obj_ptr = LK3D.CurrUniv["objects"][obj_idex]
local b_calc_idx = (Matrix(object.calc_bones[k]) or Matrix()) -- matrix
b_calc_idx:Rotate(obj_ptr.bone_ang) -- war crime war crime
b_calc_idx = matrix_obj * b_calc_idx
--local world_p, world_a = LocalToWorld(b_calc_pos, b_calc_ang, object.pos, object.ang)
LK3D.SetObjectPosAng(obj_idex, b_calc_idx:GetTranslation(), b_calc_idx:GetAngles())
end
end
end
function LK3D.UpdateSkeletons()
for k, v in pairs(LK3D.CurrUniv["objects"]) do
if v.skeleton then
object_update_bone_objects(v)
end
end
end
-- debug
local lif = .01
local function recursive_render_children(bones, l_matrix)
for k, v in ipairs(bones) do
local l_mtrx_cpy = Matrix(l_matrix)
-- translate with MATRIX
local of_pos = l_mtrx_cpy * (v.pos * 1)
local of_pos_end = l_mtrx_cpy * (v.pos + v.end_pos * 1)
local real_angle = Matrix(l_mtrx_cpy)
real_angle:Rotate(v.ang)
real_angle = real_angle:GetAngles()
LK3D.DebugUtils.Box(of_pos_end, Vector(.005, .005, .005), real_angle, lif, Color(0, 255, 0))
--LK3D.DebugUtils.Cross(of_pos_end, .025, lif, Color(0, 0, 255))
LK3D.DebugUtils.Line(of_pos, of_pos_end, lif, Color(255, 0, 0))
if #v.children ~= 0 then
l_mtrx_cpy:Translate(v.pos)
l_mtrx_cpy:Translate(v.end_pos)
l_mtrx_cpy:Rotate(v.ang)
recursive_render_children(v.children, l_mtrx_cpy)
end
end
end
function LK3D.RenderSkeleton(object)
if not LK3D.Debug then
return
end
local obj_ptr = LK3D.CurrUniv["objects"][object]
if not obj_ptr then
return
end
if not obj_ptr.skeleton then
return
end
local tree = obj_ptr.skeleton
local mat_mv = Matrix()
mat_mv:SetTranslation(obj_ptr.pos)
mat_mv:SetAngles(obj_ptr.ang)
recursive_render_children(tree, mat_mv)
end