Skip to content

Animation networks - #25

Draft
filipppavlov wants to merge 29 commits into
mainfrom
statemachines
Draft

filipppavlov wants to merge 29 commits into
mainfrom
statemachines

Conversation

@filipppavlov

Copy link
Copy Markdown
Member

Summary

Animation networks, the CAN format, and the CanEdit editor

This branch adds a runtime for blending animations with a node graph, a binary file format for those graphs (CAN), and CanEdit, an ImGui editor for building them. To let the editor reuse the viewer's renderer, the rendering code moves out of the viewer into a static library.

Library (CarbonMesh)

Building networks

  • AnimationNetworkBuilder / AnimationGraphBuilder (include/cmf/animationnetworkbuilder.h) is the editable model. It holds nodes, edges, control parameters, subgraphs, expressions, state machines and asset references.
  • Editing calls return a ValidationResult. Calls that would make a type mismatch or a cycle are rejected.
  • GetErrors() lists problems that stop the network from being evaluated. GetWarnings() lists problems the network works around: a missing animation plays the rest pose, a missing bone mask gives full bone weights, and missing bones are ignored.
  • GetSaveLayout() lists graphs and nodes in the order they are saved, so tools can attach their own data to nodes. GetAnimationNodeTypeInfo() gives each node type's name and its fixed sockets.
  • EmbedAsset() makes a network self-contained. Save() stores only what the network uses from an embedded file: the skeleton and the sampled animations, not meshes or unused animations. Saving fails if a referenced skeleton or animation is missing from an embedded file.
  • Control parameters are typed as float, boolean or integer.

Node types

  • Output, SubgraphInput, Parameter, AnimationSample, RestPose
  • Blend, AdditiveBlend, Switch, RandomSwitch
  • NamedBoneWeights, BoneWeights, Subgraph, Expression, StateMachine

State machines

  • Each state is its own graph. A transition fires on one of three conditions:
    • an expression over the parameters and stateTime
    • the state's animation finishing
    • a timeout
  • A transition can blend into the new state over a set duration. Up to eight states can be fading at once.
  • A state machine either restarts from its start state when it becomes active again or, with "resume on activate", carries on in the state it was in.
  • Entering a state restarts the playheads in its graph.

Runtime

  • AnimationNetwork (include/cmf/animationnetwork.h) loads a CAN file in place, without allocating per node. It resolves assets through an application-provided AnimationAssetLoader or from embedded CMF sections.
  • When a network is ready it is compiled once into a single memory block that all instances share:
    • subgraphs are inlined
    • producers are sorted into evaluation order
    • curves are bound to skeleton bones
    • bone weights are precomputed
    • pose slots are reused based on liveness
  • AnimationNetworkInstance evaluates a network for one animated object. It makes one allocation up front and none per frame. It supports stateful playheads, skips branches that don't contribute, and rerolls switches on cycle and on activation.

CAN file format (include/cmf/can.h, namespace cmf::can::v1)

  • A header with a CRC, a network section with a data table per node type, and optional embedded CMF sections. Malformed files are rejected during validation.
  • CAN files are for playback only and can't be loaded back into a builder. Editors keep the editable network in their own files.

Shared internals and fixes

  • Pose blending math moves into internal kernels (posekernels.h). Curve sampling helpers move to animationinternal.h and span validation to validationutils.h.
  • Behavior change: the public blend functions now set outPose.skeleton, which they used to leave unset.
  • Adds tests for BlendPoses, ExtractBoneWeights, and weighted BlendAdditivePose, which had no coverage before.
  • Adding a node type is safe by construction. Switches over node and operation types have no default, and the library builds with missing enum cases reported (MSVC C4062, -Wswitch). Combined with warnings-as-errors, a forgotten case fails the build.

Dependencies

  • Adds carbon-parser (and re2c to build it) from the carbon vcpkg registry, for the expression parser. It is linked privately into CarbonMesh.

Render library (src/render, CarbonMeshRender)

  • The Vulkan renderer, renderables, camera, shaders, and CMF content loading move from src/viewer into a private static library that the viewer and CanEdit both use.
  • Renderables no longer depend on the viewer's AppState; they have plain setters instead. A new ViewerBinding forwards AppState changes to them and passes LOD and morph weights back to the UI.
  • New: RenderTarget for offscreen rendering, and ImGuiLayer, which wraps the ImGui GLFW/Vulkan backends.
  • AnimationState can apply a pose evaluated elsewhere, matching bones by name.
  • The log functions are now type-safe templates over a single printf sink.

CanEdit (src/canedit, requires -DBUILD_TOOLS=ON)

  • CanEditCore holds the editing logic with no UI, and is covered by canedittest:
    • documents are JSON .cane files that hold the network and editor state, and export to .can
    • snapshot-based undo/redo
    • a clipboard that works between graphs, documents and editor instances
    • mapping of path prefixes (res:/...) to directories
    • a shared asset loader
    • automatic layout for documents that have no positions
    • persisted settings
  • CanEdit is the ImGui application:
    • a graph canvas with zoom/pan, selection, node dragging, connections, context menus, drag and drop, and subgraph navigation
    • a state machine canvas for states and transitions
    • palette, properties, parameters and problems/assets panels
    • a live preview that rebuilds the network after each edit and poses one or more preview meshes, or the skeleton, with play controls and preview parameter values

Tooling and docs

  • AGENTS.md and scripts/run_clang_tidy.py, which runs clang-format and then clang-tidy on changed lines only.
  • .clang-tidy:
    • adds naming rules for types, enums, template parameters and aliases
    • mutes cppcoreguidelines-pro-bounds-avoid-unchecked-container-access and bugprone-easily-swappable-parameters, which were too noisy
  • New docs:
    • doc/source/cmf/animationNetwork.rst (concepts, node reference, building and evaluating networks, state machines, asset embedding)
    • doc/source/cmf/animationNetworkFormat.rst (the CAN file layout)
    • doc/source/canedit/canedit.rst (CanEdit user guide)

Compatibility

  • The CMF on-disk format (v1.h) and FILE_VERSION are unchanged.
  • The new public headers are animationnetwork.h, animationnetworkbuilder.h, animationnetworkinstance.h, and can.h.
  • The blend functions now fill in outPose.skeleton (see above).

Testing

  • New cmftest suites: animationnetwork, animationnetworkbuilder, animationnetworkinstance, animationstatemachine, plus additions to animation.
  • New canedittest suites: document, session, clipboard, loader, settings, state machine. These are registered with CTest when BUILD_TOOLS=ON.
  • python scripts/run_clang_tidy.py is clean
  • Builds with BUILD_TOOLS=ON with no warnings
  • ctest passes

AI assistance

1000%

filipppavlov and others added 29 commits November 24, 2026 09:57
…d bugprone-easily-swappable-parameters as they are too noisy
- Add carbon-parser (and its re2c build dependency) from the carbon
  vcpkg registry and link it privately into CarbonMesh.
- Move pose blending math into internal kernels (posekernels.h) shared
  by the public blend functions; the blend functions now set
  outPose.skeleton, which they previously left unset.
- Move curve sampling helpers into animationinternal.h and the span
  pointer validator into validationutils.h so they can be reused.
- Add tests for BlendPoses, ExtractBoneWeights and weighted
  BlendAdditivePose, which had no coverage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- AnimationNetworkBuilder / AnimationGraphBuilder: editable network model
  (nodes, edges, parameters, subgraphs, expressions, asset references)
  with validation, saving to and loading from CAN files.
- AnimationNetwork: read-only runtime network that loads a CAN file in
  place without per-node allocations and resolves assets through an
  AnimationAssetLoader or embedded CMF sections.
- CAN file format (cmf::can::v1): header with CRC, network section with
  per-node-type data tables, embedded CMF sections; malformed files are
  rejected by validation.
- .clang-tidy: CamelCase enums, template parameters and type aliases;
  ignore all-public structs in the non-private member checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Compile ready networks once into a single memory block shared by all
  instances: subgraphs are inlined, producers are sorted in evaluation
  order, curves are bound to skeleton bones, bone weights are
  precomputed and pose slots are reused based on liveness.
- AnimationNetworkInstance evaluates a network per animated object with
  one allocation and no allocations per frame: stateful playheads,
  lazy evaluation of unused branches, Switch/RandomSwitch selection with
  reroll on cycle and on activation, expressions and parameters.
- Make adding node types safe: per-type node descriptions drive the
  socket layout, compiler and instance use one handler per type, and
  switches over node and operation types have no default case, with
  missing cases reported as errors (MSVC C4062, -Wswitch).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Move the Vulkan renderer, renderables, camera, shaders and CMF content
loading from src/viewer to a private static library in src/render so
that other tools can reuse it.

- Renderables no longer depend on the viewer AppState; they expose plain
  setters. A new ViewerBinding in the viewer forwards AppState changes
  to them and reflects LOD and morph weights back for the UI.
- Add RenderTarget for offscreen rendering, and an ImGuiLayer wrapping
  the ImGui GLFW/Vulkan backends (also used by the viewer).
- AnimationState can apply an externally evaluated pose, matching bones
  by name.
- Log functions are type safe templates over a single printf sink.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rors

- CAN files may contain one EditorData section with opaque data of
  editing tools. The builder keeps it (Set/GetEditorData) and writes it
  as the last section; the runtime ignores it. FILE_VERSION is unchanged.
- AnimationNetworkBuilder::GetSaveLayout returns graphs and nodes in the
  order Save stores them, so that tools can key data by file location.
- GetAnimationNodeTypeInfo exposes node type names and fixed sockets.
- Missing animations, bone masks and bones are warnings (GetWarnings)
  instead of errors: the network evaluates with rest poses, full bone
  weights and ignored bones respectively.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Editing logic of the animation network editor, independent of the UI:
- PathResolver maps protocol prefixes ("res:/...") to directories.
- FileAssetLoader loads network assets from files, shares them between
  networks and remembers load errors.
- Document keeps node positions, graph views and the preview mesh, and
  stores them as JSON in the EditorData section of CAN files. Networks
  without editor data are laid out automatically.
- EditorSession provides the open graph, selection, snapshot based
  undo/redo and editing operations, including clipboard copy/paste.
- EditorSettings persists prefixes, recent files and panel sizes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ImGui application on top of CanEditCore and CarbonMeshRender:
- Graph canvas with zoom/pan, box and click selection, node dragging,
  socket connections, edge selection, context menus, drag and drop from
  the palette and parameters, clipboard and subgraph navigation.
- Palette, properties, parameters and problems/assets panels in a fixed
  layout with resizable splitters; path prefix settings.
- Preview that rebuilds the runtime network after edits and poses the
  preview mesh (or the skeleton) in an offscreen render target, with
  play controls and preview parameter values.
- Networks with a missing skeleton can only have their skeleton fixed.

Also: ImGuiLayer can clear the swapchain, and EditorSession::Reload
requests assets again after path prefixes change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the CanEdit user guide and describe the new render library and
editor components in AGENTS.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Update now applies parameters, advances time, takes state machine
transitions, rerolls RandomSwitch nodes and marks the operations that
contribute to the pose. Evaluate only computes the decided pose and has
no side effects, so it can be skipped or called repeatedly.

Parameters set with SetParameter take effect at the next Update. The
CanEdit preview calls Update( 0 ) while paused to apply edited values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant