Merge pull request #1016 from MihailRis/upgrade-vca

upgrade vca
This commit is contained in:
MihailRis 2026-10-09 00:52:27 +03:00 • committed by GitHub
commit 1b971b49a5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 215 additions and 113 deletions

View file

@ -28,6 +28,9 @@ Two kinds of curve definitions are currently available, each producing a value `
- `@scale` - scales an object/bone (multiplier) - `@scale` - scales an object/bone (multiplier)
- `@zoom` - camera zoom (multiplier) - `@zoom` - camera zoom (multiplier)
- `@texture` - change of a dynamically assigned texture (see [skeleton:set_texture](scripting/ecs.md#skeleton)) - `@texture` - change of a dynamically assigned texture (see [skeleton:set_texture](scripting/ecs.md#skeleton))
- `@model` - bone model replacement (see [skeleton:set_model](scripting/ecs.md#skeleton))
- `@show` - visibility of a bone or the entire skeleton/object (if no bone is specified)
- `@color` - color of a bone or the entire skeleton/object (if no bone is specified) (RGBA)
## Metadata ## Metadata
@ -96,6 +99,23 @@ If you do not wish to limit the animation duration via `configure` but want to l
} }
``` ```
Example of using flag directives like `show`:
```vcd
# Specific bone
@show bone head period 20 {
@key frame 0 value on
@key frame 10 value off
}
# Entire skeleton
@show period 30 {
@key frame 0 value on
@key frame 10 value off
}
```
## Expression curves ## Expression curves
The attribute describes a function `f(t) = x`, where t is time in seconds. The attribute describes a function `f(t) = x`, where t is time in seconds.

View file

@ -28,6 +28,9 @@ VCA файл состоит из набора директив, порядок
- `@scale` - масштабирование объекта/кости (множитель) - `@scale` - масштабирование объекта/кости (множитель)
- `@zoom` - масштаб камеры (множитель) - `@zoom` - масштаб камеры (множитель)
- `@texture` - смена динамически-назначаемой текстуры (см. [skeleton:set_texture](scripting/ecs.md#skeleton)) - `@texture` - смена динамически-назначаемой текстуры (см. [skeleton:set_texture](scripting/ecs.md#skeleton))
- `@model` - назначение замены модели кости (см. [skeleton:set_model](scripting/ecs.md#skeleton))
- `@show` - видимость кости или всего скелета / объекта (если не указана кость)
- `@color` - цвет кости или всего скелета / объекта (если не указана кость) (RGBA)
## Мета-информация ## Мета-информация
@ -96,6 +99,23 @@ VCA файл состоит из набора директив, порядок
} }
``` ```
Пример использования директив-флагов, таких как `show`:
```vcd
# Конкретная кость
@show bone head period 20 {
@key frame 0 value on
@key frame 10 value off
}
# Весь скелет
@show period 30 {
@key frame 0 value on
@key frame 10 value off
}
```
## Кривые через выражения ## Кривые через выражения
Через атрибут описывается функция `f(t) = x` где t - время в секундах. Через атрибут описывается функция `f(t) = x` где t - время в секундах.

View file

@ -39,12 +39,15 @@ local function bezier_interpolation(k0, k1, t)
return bezier(k0.value, k0.ry + k0.value, k1.ly + k1.value, k1.value, u) return bezier(k0.value, k0.ry + k0.value, k1.ly + k1.value, k1.value, u)
end end
local this = { local M = {
CH_TRANSLATE = 1, CH_TRANSLATE = 1,
CH_ROTATE = 2, CH_ROTATE = 2,
CH_SCALE = 3, CH_SCALE = 3,
CH_ZOOM = 4, CH_ZOOM = 4,
CH_TEXTURE = 5, CH_TEXTURE = 5,
CH_SHOW = 6,
CH_MODEL = 7,
CH_COLOR = 8,
INT_CONST = 1, INT_CONST = 1,
INT_LINEAR = 2, INT_LINEAR = 2,
@ -53,7 +56,7 @@ local this = {
MAX_DURATION = 1e9, MAX_DURATION = 1e9,
MAX_FRAMES = 1e9, MAX_FRAMES = 1e9,
TRACE_CODEGEN = false, TRACE_CODEGEN = true,
maths = { maths = {
bezier_interpolation = bezier_interpolation bezier_interpolation = bezier_interpolation
@ -67,14 +70,14 @@ function internals.store_animation(name, track)
loaded_tracks[name] = track loaded_tracks[name] = track
end end
function this.get_track(identifier) function M.get_track(identifier)
return loaded_tracks[identifier] return loaded_tracks[identifier]
end end
local running_actions = {} local running_actions = {}
local playing_tracks = {} local playing_tracks = {}
function this.action(func) function M.action(func)
table.insert(running_actions, coroutine.create(func)) table.insert(running_actions, coroutine.create(func))
end end
@ -98,7 +101,7 @@ local PlayingTrack = {
} }
} }
function this.play(name, target) function M.play(name, target)
if target and target.reset_pose then if target and target.reset_pose then
target:reset_pose() target:reset_pose()
end end
@ -156,4 +159,4 @@ function internals.restore_animation_backup()
loaded_tracks, backup_tracks = backup_tracks, {} loaded_tracks, backup_tracks = backup_tracks, {}
end end
return this return M

View file

@ -15,7 +15,7 @@ local exclude_patters = {
} }
-- TODO: replace with actual expression -> lua translator -- TODO: replace with actual expression -> lua translator
local function process_expression(src, memoised, mode) local function process_expression(src, mode)
if mode == 'mul' then if mode == 'mul' then
src = string.format("(%s) * intensity + (1.0 - intensity)", src) src = string.format("(%s) * intensity + (1.0 - intensity)", src)
elseif mode == "add" then elseif mode == "add" then
@ -30,7 +30,6 @@ local function process_expression(src, memoised, mode)
for i, pattern in ipairs(patterns) do for i, pattern in ipairs(patterns) do
local pattern_safe = string.pattern_safe(pattern.pattern) local pattern_safe = string.pattern_safe(pattern.pattern)
if src:find(pattern_safe) then if src:find(pattern_safe) then
memoised[pattern.name] = pattern.pattern
src = src:gsub(pattern_safe, pattern.name) src = src:gsub(pattern_safe, pattern.name)
end end
end end
@ -76,7 +75,7 @@ local env = {
return keys[left].value return keys[left].value
end end
left = keys[left] left = keys[left]
if interp == INT_CONST then if not interp or interp == INT_CONST then
return left.value return left.value
end end
right = keys[right] right = keys[right]
@ -137,42 +136,48 @@ local is_multiplier = {
[animation.CH_ZOOM] = true, [animation.CH_ZOOM] = true,
} }
local function codegen_track(raw_track, lineset, memoised, keysets, use_tsf) 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, target_type)
if lineset.target_type ~= target_type then
return ""
end
local lines = lineset.lines local lines = lineset.lines
local code = "" local code = ""
local has_tsf = false local has_tsf = false
local translation = {false, false, false} local translation = {false, false, false}
local rotation = {false, false, false} local rotation = {false, false, false}
local scale = {false, false, false} local scale = {false, false, false}
local color = {false, 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, memoised,
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
@ -186,7 +191,31 @@ local function codegen_track(raw_track, lineset, memoised, 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, l%d)",
lineset.target_name:escape(), i)
elseif line.channel == animation.CH_MODEL then
code = code .. string.format("\n target:set_model(bone_index, l%d)", i)
elseif line.channel == animation.CH_SHOW then
if target_type == "bone" then
code = code .. string.format("\n target:set_visible(bone_index, l%d)", i)
else
code = code .. string.format("\n target:set_visible(l%d)", i)
end
elseif line.channel == animation.CH_COLOR then
color[line.axis] = i
end
end
if color[1] or color[2] or color[3] or color[4] then
local color_vector = "{" ..
(color[1] and ("l" .. color[1]) or '1').. ", " ..
(color[2] and ("l" .. color[2]) or '1').. ", " ..
(color[3] and ("l" .. color[3]) or '1').. ", " ..
(color[4] and ("l" .. color[4]) or '1').. "}"
if target_type == "bone" then
code = code .. "\n target:set_color(" .. color_vector .. ", bone_index)"
else
code = code .. "\n target:set_color(" .. color_vector .. ")"
end end
end end
@ -229,14 +258,15 @@ local function codegen_rig_target(raw_track, context)
if lineset.target_type ~= "bone" and lineset.target_type ~= "texture" then if lineset.target_type ~= "bone" and lineset.target_type ~= "texture" then
goto continue goto continue
end end
local lineset_code = codegen_track( local lineset_code = codegen_track(raw_track, lineset, context.keysets, true, "bone")
raw_track, lineset, context.memoised, context.keysets, true)
code = code .. string.format("\n local bone_index = target:index(%s)", string.escape(bone))
if context.has_tsf then
code = code .. "\n local dst = target:get_matrix(bone_index)"
end
code = code code = code
.. string.format("\n local bone_index = target:index(%s)"
.. "\n local dst = target:get_matrix(bone_index)", string.escape(bone))
.. "\n do" .. lineset_code .. "\n end\n" .. "\n do" .. lineset_code .. "\n end\n"
if lineset.target_type == "bone" then if lineset.target_type == "bone" and context.has_tsf then
code = code .. code = code ..
" target:set_matrix(bone_index, dst)\n" " target:set_matrix(bone_index, dst)\n"
end end
@ -246,17 +276,18 @@ local function codegen_rig_target(raw_track, context)
end end
local function codegen_object_target(raw_track, context) local function codegen_object_target(raw_track, context)
local code = "\n if target.set_pos then\n" local code = " local dst = mat4.idt()\n"
code = code .. " local dst = mat4.idt()\n"
local lineset = raw_track.linesets[""] local lineset = raw_track.linesets[""]
if not lineset then if not lineset then
return "" return ""
end end
local lineset_code = codegen_track( local lineset_code = codegen_track(raw_track, lineset, context.keysets, true)
raw_track, lineset, context.memoised, context.keysets, true)
code = code .. "\n do" .. lineset_code .. "\n end\n" code = code .. "\n do" .. lineset_code .. "\n end\n"
.. " set_matrix(target, dst)\n"
return code .. " end" if context.has_tsf then
code = code .. " set_matrix(target, dst)\n"
end
return code
end end
local function codegen_camera_target(raw_track, context) local function codegen_camera_target(raw_track, context)
@ -266,25 +297,37 @@ local function codegen_camera_target(raw_track, context)
if not lineset then if not lineset then
return "" return ""
end end
local lineset_code = codegen_track( local lineset_code = codegen_track(raw_track, lineset, context.keysets, false, "camera")
raw_track, lineset, context.memoised, context.keysets, false)
code = code .. "\n do" .. lineset_code .. "\n end\n" code = code .. "\n do" .. lineset_code .. "\n end\n"
.. " target:set_zoom(zoom)\n" .. " target:set_zoom(zoom)\n"
return code .. " end" return code .. " end"
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 = {
memoised = {},
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(
"return function(kl, kr, t) return %s end", "return function(kl, kr, t) return %s end",
process_expression(curve.func, context.memoised, "curve") process_expression(curve.func, "curve")
), "<curve>", "t", env)() ), "<curve>", "t", env)()
end end
@ -292,17 +335,8 @@ function internals.compile_animation_track(raw_track, track_name)
code = code .. codegen_object_target(raw_track, context) code = code .. codegen_object_target(raw_track, context)
code = code .. codegen_camera_target(raw_track, context) code = code .. codegen_camera_target(raw_track, context)
local memoised_code = "" local src = "return function(target, t, intensity, m)\n"
for name, expression in pairs(context.memoised) do .. " m = m or 1\n intensity = intensity or 1.0\n"
memoised_code = memoised_code .. "\n local " .. name .. " = "
.. expression
end
if #memoised_code > 0 then
code = memoised_code .. "\n" .. code
end
local src = "return function(target, t, intensity, m)\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

View file

@ -8,6 +8,9 @@ local action_to_channel = {
scale = animation.CH_SCALE, scale = animation.CH_SCALE,
zoom = animation.CH_ZOOM, zoom = animation.CH_ZOOM,
texture = animation.CH_TEXTURE, texture = animation.CH_TEXTURE,
show = animation.CH_SHOW,
model = animation.CH_MODEL,
color = animation.CH_COLOR,
} }
local curve_to_interp = { local curve_to_interp = {
@ -16,6 +19,10 @@ local curve_to_interp = {
bezier = animation.INT_BEZIER, bezier = animation.INT_BEZIER,
} }
local boolean_actions = {
show = true
}
local function parse_configure(raw_track, node) local function parse_configure(raw_track, node)
if node.fps then if node.fps then
raw_track.fps = tonumber(node.fps) raw_track.fps = tonumber(node.fps)
@ -62,6 +69,70 @@ local function parse_simple_frames(line, node)
end end
end end
local function parse_boolean_frames(line, node)
line.keys = {}
for j, key_node in ipairs(node) do
local keyframe = {
frame = tonumber(key_node.frame),
value = key_node.value == "on",
}
table.insert(line.keys, keyframe)
end
end
local function parse_directive(node, raw_track)
local linesets = raw_track.linesets
local tag = node['#']
if tag == "configure" then
parse_configure(raw_track, node)
return
elseif tag == "curve" then
raw_track.curves[node.name] = node
return
end
local target_type = nil
if node.bone then
target_type = "bone"
elseif node.zoom then
target_type = "camera"
elseif tag == "texture" then
target_type = "texture"
end
local target_name = node.bone or node.name or ""
local lineset = linesets[target_name]
if not lineset then
lineset = {
lines = {},
target_type = target_type,
target_name = target_name,
flag = node.flag,
}
linesets[target_name] = lineset
end
local channel = action_to_channel[tag]
if not channel then
error("unknown directive " .. tag:escape())
end
local line = {
axis = node.by and (("xyz"):find(node.by) or ("rgba"):find(node.by)) or "",
channel = channel,
period = node.period or animation.MAX_FRAMES
}
if node.func then
line.expression = node.func
elseif node.curve then
parse_curve(line, node)
elseif boolean_actions[tag] then
parse_boolean_frames(line, node)
else
parse_simple_frames(line, node)
end
table.insert(lineset.lines, line)
end
local function parse_track(root) local function parse_track(root)
local raw_track = { local raw_track = {
duration = math.huge, duration = math.huge,
@ -69,56 +140,10 @@ local function parse_track(root)
linesets = {}, linesets = {},
curves = {}, curves = {},
} }
local linesets = raw_track.linesets
for i, node in ipairs(root) do for i, node in ipairs(root) do
if type(node) == "string" then if type(node) == 'table' then
goto continue parse_directive(node, raw_track)
end end
local tag = node['#']
if tag == "configure" then
parse_configure(raw_track, node)
goto continue
elseif tag == "curve" then
raw_track.curves[node.name] = node
goto continue
end
local target_type = nil
if node.bone then
target_type = "bone"
elseif tag == "texture" then
target_type = "texture"
end
local target_name = node.bone or node.name or ""
local lineset = linesets[target_name]
if not lineset then
lineset = {
lines = {},
target_type = target_type,
target_name = target_name
}
linesets[target_name] = lineset
end
local channel = action_to_channel[tag]
if not channel then
error("unknown directive " .. tag:escape())
end
local line = {
axis = node.by and ("xyz"):find(node.by) or "",
channel = channel,
period = node.period or animation.MAX_FRAMES
}
if node.func then
line.expression = node.func
elseif node.curve then
parse_curve(line, node)
else
parse_simple_frames(line, node)
end
table.insert(lineset.lines, line)
::continue::
end end
return raw_track return raw_track
end end