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)
- `@zoom` - camera zoom (multiplier)
- `@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
@ -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
The attribute describes a function `f(t) = x`, where t is time in seconds.

View file

@ -28,6 +28,9 @@ VCA файл состоит из набора директив, порядок
- `@scale` - масштабирование объекта/кости (множитель)
- `@zoom` - масштаб камеры (множитель)
- `@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 - время в секундах.

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

View file

@ -15,7 +15,7 @@ local exclude_patters = {
}
-- TODO: replace with actual expression -> lua translator
local function process_expression(src, memoised, mode)
local function process_expression(src, mode)
if mode == 'mul' then
src = string.format("(%s) * intensity + (1.0 - intensity)", src)
elseif mode == "add" then
@ -30,7 +30,6 @@ local function process_expression(src, memoised, mode)
for i, pattern in ipairs(patterns) do
local pattern_safe = string.pattern_safe(pattern.pattern)
if src:find(pattern_safe) then
memoised[pattern.name] = pattern.pattern
src = src:gsub(pattern_safe, pattern.name)
end
end
@ -76,7 +75,7 @@ local env = {
return keys[left].value
end
left = keys[left]
if interp == INT_CONST then
if not interp or interp == INT_CONST then
return left.value
end
right = keys[right]
@ -137,42 +136,48 @@ local is_multiplier = {
[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 code = ""
local has_tsf = false
local translation = {false, false, false}
local rotation = {false, false, false}
local scale = {false, false, false}
local color = {false, false, false, false}
for i, line in ipairs(lines) do
if line.expression then
code = code .. "\n local l" .. i .. " = (" ..
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
code = code .. string.format("\n local l%d = ", i)
.. codegen_line(raw_track, lineset, line, i, keysets)
if line.channel == animation.CH_TRANSLATE then
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
code = code .. "\n zoom = l" .. i
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
@ -229,14 +258,15 @@ local function codegen_rig_target(raw_track, context)
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, true)
local lineset_code = codegen_track(raw_track, lineset, context.keysets, true, "bone")
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
.. 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"
if lineset.target_type == "bone" then
if lineset.target_type == "bone" and context.has_tsf then
code = code ..
" target:set_matrix(bone_index, dst)\n"
end
@ -246,17 +276,18 @@ local function codegen_rig_target(raw_track, context)
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"
local code = " local dst = mat4.idt()\n"
local lineset = raw_track.linesets[""]
if not lineset then
return ""
end
local lineset_code = codegen_track(
raw_track, lineset, context.memoised, context.keysets, true)
local lineset_code = codegen_track(raw_track, lineset, context.keysets, true)
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
local function codegen_camera_target(raw_track, context)
@ -266,25 +297,37 @@ local function codegen_camera_target(raw_track, context)
if not lineset then
return ""
end
local lineset_code = codegen_track(
raw_track, lineset, context.memoised, context.keysets, false)
local lineset_code = codegen_track(raw_track, lineset, context.keysets, false, "camera")
code = code .. "\n do" .. lineset_code .. "\n end\n"
.. " target:set_zoom(zoom)\n"
return code .. " 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)
local code = ""
local context = {
memoised = {},
keysets = {},
curves = {},
has_tsf = has_transform_directive(raw_track),
}
for name, curve in pairs(raw_track.curves) do
context.curves[name] = load(string.format(
"return function(kl, kr, t) return %s end",
process_expression(curve.func, context.memoised, "curve")
process_expression(curve.func, "curve")
), "<curve>", "t", env)()
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_camera_target(raw_track, context)
local memoised_code = ""
for name, expression in pairs(context.memoised) do
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"
local src = "return function(target, t, intensity, m)\n"
.. " m = m or 1\n intensity = intensity or 1.0\n"
.. code .. "\nend"
if animation.TRACE_CODEGEN then

View file

@ -8,6 +8,9 @@ local action_to_channel = {
scale = animation.CH_SCALE,
zoom = animation.CH_ZOOM,
texture = animation.CH_TEXTURE,
show = animation.CH_SHOW,
model = animation.CH_MODEL,
color = animation.CH_COLOR,
}
local curve_to_interp = {
@ -16,6 +19,10 @@ local curve_to_interp = {
bezier = animation.INT_BEZIER,
}
local boolean_actions = {
show = true
}
local function parse_configure(raw_track, node)
if node.fps then
raw_track.fps = tonumber(node.fps)
@ -62,6 +69,70 @@ local function parse_simple_frames(line, node)
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 raw_track = {
duration = math.huge,
@ -69,56 +140,10 @@ local function parse_track(root)
linesets = {},
curves = {},
}
local linesets = raw_track.linesets
for i, node in ipairs(root) do
if type(node) == "string" then
goto continue
if type(node) == 'table' then
parse_directive(node, raw_track)
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
return raw_track
end