optimize animator

This commit is contained in:
Andrey Tazetdinov 2026-10-14 16:35:21 +02:00
parent 17bdbb1a38
commit fd17ce32ab
11 changed files with 294 additions and 2 deletions

View file

@ -5,6 +5,9 @@ local INT_BEZIER = animation.INT_BEZIER
local bezier_interpolation = animation.maths.bezier_interpolation
local register_track = __skeleton and __skeleton.register_track
local MAX_NATIVE_VALUES = 150
local patterns = {}
local exclude_patters = {
"end",
@ -121,7 +124,8 @@ local env = {
end
end
end,
dump = debug.print
dump = debug.print,
apply_track = __skeleton and __skeleton.apply_track,
}
local math_funcs = {
@ -245,6 +249,85 @@ local function codegen_rig_target(raw_track, context)
return code .. " end"
end
-- the same as codegen_rig_target for the entities' skeletons: one
-- apply_track call instead of the matrix calls of every bone
local function codegen_native_rig_target(raw_track, context, track_name)
if not register_track or not track_name then
return nil
end
local layout = {}
local values = 0
local code = ""
local axis_indices = {X=1, Y=2, Z=3}
local rotation_order = raw_track.rotation_order or "XYZ"
for bone, lineset in pairs(raw_track.linesets) do
if lineset.target_type ~= "bone" and lineset.target_type ~= "texture" then
goto continue
end
local lineset_code = codegen_track(
raw_track, lineset, context.memoised, context.keysets, false)
local exports = ""
if lineset.target_type == "bone" then
-- the last line of a channel and axis wins, as in codegen_track
local channels = {
[animation.CH_TRANSLATE] = {},
[animation.CH_ROTATE] = {},
[animation.CH_SCALE] = {},
}
for i, line in ipairs(lineset.lines) do
local by_axis = channels[line.channel]
if by_axis and type(line.axis) == "number" then
by_axis[line.axis] = i
end
end
local function export(i)
values = values + 1
exports = exports .. string.format("\n v%d = l%d", values, i)
return values
end
local entry = {name = bone}
local function vector(by_axis)
if not (by_axis[1] or by_axis[2] or by_axis[3]) then
return nil
end
local v = {0, 0, 0}
for axis = 1, 3 do
if by_axis[axis] then v[axis] = export(by_axis[axis]) end
end
return v
end
entry.t = vector(channels[animation.CH_TRANSLATE])
local rotation = channels[animation.CH_ROTATE]
for i = 1, 3 do
local axis = axis_indices[rotation_order[i]]
if rotation[axis] then
entry.r = entry.r or {}
table.insert(entry.r, axis - 1)
table.insert(entry.r, export(rotation[axis]))
end
end
entry.s = vector(channels[animation.CH_SCALE])
if entry.t or entry.r or entry.s then
table.insert(layout, entry)
end
end
code = code .. "\n do" .. lineset_code .. exports .. "\n end\n"
::continue::
end
if values > MAX_NATIVE_VALUES then
return nil
end
local id = register_track(track_name, layout)
local names = {}
for i = 1, values do
names[i] = "v" .. i
end
local args = #names > 0 and (", " .. table.concat(names, ", ")) or ""
local declare = #names > 0 and ("\n local " .. table.concat(names, ", ")) or ""
return "\n if target.__native_rig then" .. declare .. code
.. string.format(" apply_track(target.eid, %d%s)\n", id, args)
end
local function codegen_object_target(raw_track, context)
local code = "\n if target.set_pos then\n"
code = code .. " local dst = mat4.idt()\n"
@ -288,7 +371,11 @@ function internals.compile_animation_track(raw_track, track_name)
), "<curve>", "t", env)()
end
code = code .. codegen_rig_target(raw_track, context)
local native = codegen_native_rig_target(raw_track, context, track_name)
local rig = codegen_rig_target(raw_track, context)
rig = native .. " elseif" .. rig:sub(5)
end
code = code .. rig
code = code .. codegen_object_target(raw_track, context)
code = code .. codegen_camera_target(raw_track, context)