refactor animation_codegen.lua

This commit is contained in:
MihailRis 2026-10-07 19:55:19 +03:00
parent fdd62e2e19
commit b21f4d4439

View file

@ -136,6 +136,32 @@ local is_multiplier = {
[animation.CH_ZOOM] = true, [animation.CH_ZOOM] = true,
} }
local function codegen_line(raw_track, lineset, line, index, keysets)
if line.expression then
return "(" .. process_expression(line.expression,
is_multiplier[line.channel] and "mul" or "add") .. ")"
elseif line.keys then
local target_keysets = keysets[lineset.target_name]
if not target_keysets then
target_keysets = {}
keysets[lineset.target_name] = target_keysets
end
target_keysets[index] = line.keys
if line.curve_func then
local valueat = string.format("curves[%s]", string.escape(line.curve_func))
return string.format("value_at_custom(keysets['%s'][%d], t * %s %% %s, %s)",
lineset.target_name, index, raw_track.fps, line.period, valueat)
elseif line.channel == animation.CH_TEXTURE then
return string.format("string_at(keysets['%s'][%d], t * %s %% %s)",
lineset.target_name, index, raw_track.fps, line.period)
else
return string.format("value_at(keysets['%s'][%d], t * %s %% %s, %s)",
lineset.target_name, index, raw_track.fps, line.period, line.interp)
end
end
end
local function codegen_track(raw_track, lineset, keysets, use_tsf) local function codegen_track(raw_track, lineset, keysets, use_tsf)
local lines = lineset.lines local lines = lineset.lines
local code = "" local code = ""
@ -144,34 +170,8 @@ local function codegen_track(raw_track, lineset, keysets, use_tsf)
local rotation = {false, false, false} local rotation = {false, false, false}
local scale = {false, false, false} local scale = {false, false, false}
for i, line in ipairs(lines) do for i, line in ipairs(lines) do
if line.expression then code = code .. string.format("\n local l%d = ", i)
code = code .. "\n local l" .. i .. " = (" .. .. codegen_line(raw_track, lineset, line, i, keysets)
process_expression(line.expression,
is_multiplier[line.channel] and "mul" or "add") .. ")"
elseif line.keys then
local target_keysets = keysets[lineset.target_name]
if not target_keysets then
target_keysets = {}
keysets[lineset.target_name] = target_keysets
end
target_keysets[i] = line.keys
if line.curve_func then
local valueat = string.format("curves[%s]", string.escape(line.curve_func))
code = code .. string.format(
"\n local l%d = value_at_custom(keysets['%s'][%d], t * %s %% %s, %s)",
i, lineset.target_name, i, raw_track.fps, line.period, valueat)
elseif line.channel == animation.CH_TEXTURE then
code = code .. string.format(
"\n local l%d = string_at(keysets['%s'][%d], t * %s %% %s)",
i, lineset.target_name, i, raw_track.fps, line.period)
else
code = code .. string.format(
"\n local l%d = value_at(keysets['%s'][%d], t * %s %% %s, %s)",
i, lineset.target_name, i, raw_track.fps, line.period, line.interp)
end
end
if line.channel == animation.CH_TRANSLATE then if line.channel == animation.CH_TRANSLATE then
translation[line.axis] = i translation[line.axis] = i
@ -185,7 +185,8 @@ local function codegen_track(raw_track, lineset, keysets, use_tsf)
elseif line.channel == animation.CH_ZOOM then elseif line.channel == animation.CH_ZOOM then
code = code .. "\n zoom = l" .. i code = code .. "\n zoom = l" .. i
elseif line.channel == animation.CH_TEXTURE then elseif line.channel == animation.CH_TEXTURE then
code = code .. string.format("\n target:set_texture(%s, %s)", lineset.target_name:escape(), "l"..i) code = code .. string.format("\n target:set_texture(%s, %s)",
lineset.target_name:escape(), "l"..i)
end end
end end
@ -270,11 +271,25 @@ local function codegen_camera_target(raw_track, context)
end end
local function has_transform_directive(raw_track)
for _, lineset in pairs(raw_track.linesets) do
for _, line in ipairs(lineset.lines) do
if line.channel == animation.CH_TRANSLATE or
line.channel == animation.CH_ROTATE or
line.channel == animation.CH_SCALE then
return true
end
end
end
return false
end
function internals.compile_animation_track(raw_track, track_name) function internals.compile_animation_track(raw_track, track_name)
local code = "" local code = ""
local context = { local context = {
keysets = {}, keysets = {},
curves = {}, curves = {},
has_tsf = has_transform_directive(raw_track),
} }
for name, curve in pairs(raw_track.curves) do for name, curve in pairs(raw_track.curves) do
context.curves[name] = load(string.format( context.curves[name] = load(string.format(
@ -283,12 +298,14 @@ function internals.compile_animation_track(raw_track, track_name)
), "<curve>", "t", env)() ), "<curve>", "t", env)()
end end
if context.has_tsf then
code = code .. codegen_rig_target(raw_track, context) code = code .. codegen_rig_target(raw_track, context)
code = code .. codegen_object_target(raw_track, context) code = code .. codegen_object_target(raw_track, context)
end
code = code .. codegen_camera_target(raw_track, context) code = code .. codegen_camera_target(raw_track, context)
local src = "return function(target, t, intensity, m)\n" local src = "return function(target, t, intensity, m)\n"
.. "m = m or 1\n intensity = intensity or 1.0\n" .. " m = m or 1\n intensity = intensity or 1.0\n"
.. code .. "\nend" .. code .. "\nend"
if animation.TRACE_CODEGEN then if animation.TRACE_CODEGEN then