Live2D Cubism Native Framework

repository·develop·Indexed 42 days ago

https://github.com/live2d/cubismnativeframework

A framework providing high-level logic for displaying and manipulating Live2D models in native applications. It works alongside the Live2D Cubism Core library to handle model lifecycles, rendering, motion playback, physics, and effects. Includes utilities like CubismCdiJson for parsing display information and CubismModelSettingJson for managing model metadata, textures, and motion groups.

Tokens
11.4K
Snippets
19
Records
51
Agent score
53%

What's inside Cubism Native Framework

  1. Overview of Cubism Native Framework

    develop

    The Cubism Native Framework is a framework designed for using models exported from the Live2D Cubism Editor within native applications. It provides the necessary functions for displaying, manipulating, and managing Live2D models.

    Important Integration Note: This framework is not a standalone solution; it must be used in conjunction with the Live2D Cubism Core library to load and process model data. The Core library is not included in this repository and must be downloaded separately.

  2. Explore Cubism Native Framework components

    develop

    The framework is organized into several functional components that handle different aspects of the Live2D model lifecycle and behavior:

    • Model: Core functions for handling the model lifecycle, including generate, update, and destroy.
    • Rendering: A renderer that implements graphics instructions to draw the model across various platforms.
    • Motion: Functions for motion playback and parameter blending.
    • Physics: Functions for applying transformation manipulations driven by physics to the model.
    • Effect: High-level functions for adding motion information, such as automatic blinking and lip sync.
    • Id: Provides unique types to manage parameter names, part names, and Drawable names set in the model.
    • Math: Arithmetic functions for model manipulation and drawing, including matrix and vector calculations.
    • Type: C++ type definitions used throughout the framework.
    • Utils: Utility functions including a JSON parser and log output.
    • Effect: Provides functions such as automatic blinking and lip sync to add motion information as an effect to the model.
  3. Use CubismViewMatrix to manage camera transformations

    develop

    The CubismViewMatrix class provides 4x4 matrix utilities specifically designed for managing camera position, translation, and scaling within the Live2D framework. It inherits from CubismMatrix44 and includes logic to constrain movement and scaling based on defined screen boundaries and scale limits.

    Key capabilities include:

    • Adjustable Translation: Moves the camera using AdjustTranslate, which automatically adjusts the movement amount relative to the screen size and maximum allowed range.
    • Constrained Scaling: Scales the view using AdjustScale, which respects defined minimum and maximum scaling factors.
    • Boundary Management: Defines the visible screen area (SetScreenRect) and the maximum allowable movable range (SetMaxScreenRect) using logical coordinates.
  4. Manage offscreen render targets with ICubismOffscreenManager

    develop

    The ICubismOffscreenManager<T> is a template class used to manage a pool of offscreen render targets (of type T). It handles the lifecycle of these targets, including creation, tracking their usage state, and releasing unused resources to optimize memory.

    To use the manager correctly within a rendering loop, you must follow the frame lifecycle:

    1. Call BeginFrameProcess() at the start of the frame.
    2. Perform rendering operations using the managed render targets.
    3. Call EndFrameProcess() at the end of the frame.

    Key lifecycle management tasks:

    • Releasing unused textures: Call ReleaseStaleRenderTextures() to free up render targets that are no longer being used, resizing the pool to the previous frame's maximum active count to prevent excessive allocation/deallocation.
    • Full cleanup: Call ReleaseAllRenderTextures() to destroy all managed render targets and clear the list.
    // Conceptual usage pattern
    ICubismOffscreenManager<MyRenderTargetType> manager;
    
    // Inside your render loop
    manager.BeginFrameProcess();
    
    // ... use render targets ...
    
    manager.EndFrameProcess();
    manager.ReleaseStaleRenderTextures();
  5. Use the ICubismModelSetting interface

    develop

    The ICubismModelSetting interface provides access to the configuration and metadata defined in a Live2D Model Settings File. It allows you to retrieve information about textures, hit areas, physics, motions, expressions, and other model-specific settings.

    If you need to extend model settings or implement a class that consumes model settings, you should inherit from this interface. The interface acts as a bridge between the raw settings files (like .moc3 and associated .json files) and the framework's runtime logic.

  6. Manage model state with CubismModel

    develop

    The CubismModel class is the primary interface for managing a Live2D model created from MOC data. It handles the model lifecycle, parameter updates, and provides access to model data such as parts, drawables, and offscreen sources.

    Key responsibilities include:

    • Updating State: Call Update() to recalculate the model state based on current parameter values.
    • Parameter Control: Set, add, or multiply parameter values to drive animations and expressions.
    • Part & Drawable Access: Retrieve hierarchy information, opacity, and vertex data for rendering.
    • Canvas Information: Access canvas dimensions and pixels per unit (PPU).
    • Parameter Persistence: Use SaveParameters() and LoadParameters() to temporarily store and restore parameter values.
    // Example conceptual usage
    void UpdateModel(Live2D::Cubism::Framework::CubismModel& model) {
        // Set a parameter value (e.g., Eye Open)
        model.SetParameterValue(parameterId, 1.0f);
        
        // Update the model state
        model.Update();
    }
  7. How Array and Map work in CubismJson

    develop

    In CubismJson, JSON arrays and objects are represented by the Array and Map classes, which both inherit from Value.

    Array

    • Accessing elements: Use the subscript operator operator[](csmInt32 index). If the index is out of bounds, it returns an error value.
    • Size: Use GetSize() to get the number of elements.
    • Iteration: Use GetVector() to retrieve the underlying csmVector<Value*>* for iteration.
    • Adding elements: Use Add(Value* v) to append a new value.

    Map (JSON Object)

    • Accessing elements: Use the subscript operator operator[](const csmString& s) or operator[](const csmChar* s). If the key does not exist, it returns a NullValue.
    • Keys: Use GetKeys() to retrieve a csmVector<csmString>& containing all keys in the map.
    • Size: Use GetSize() to get the number of key-value pairs.
    • Adding elements: Use Put(csmString& key, Value* v) to insert a new key-value pair.
    // Accessing an array
    Value& list = root["Elements"];
    if (list.IsArray()) {
        csmInt32 size = list.GetSize();
        for (csmInt32 i = 0; i < size; ++i) {
            Value& item = list[i];
            // Process item
        }
    }
    
    // Accessing a map
    Value& settings = root["Settings"];
    if (settings.IsMap()) {
        csmVector<csmString>& keys = settings.GetKeys();
        for (auto it = keys.Begin(); it != keys.End(); ++it) {
            Value& val = settings[(*it)];
            // Process val
        }
    }
  8. Configure CubismViewMatrix screen boundaries and scale limits

    develop

    To control how the camera moves and scales, you must set the logical coordinate boundaries and scale constraints on the CubismViewMatrix instance.

    Setting Boundaries

    • SetScreenRect(left, right, bottom, top): Sets the range of logical coordinates currently visible on the device.
    • SetMaxScreenRect(left, right, bottom, top): Sets the maximum movable range for the camera.

    Setting Scale Limits

    • SetMaxScale(maxScale): Sets the maximum allowed scaling factor.
    • SetMinScale(minScale): Sets the minimum allowed scaling factor.

    Querying State

    • Use IsMaxScale() and IsMinScale() to check if the current scale has reached its limits.
    • Use GetScreenLeft(), GetScreenRight(), GetScreenBottom(), and GetScreenTop() to retrieve the current visible rectangle.
    • Use GetMaxLeft(), GetMaxRight(), GetMaxBottom(), and GetMaxTop() to retrieve the maximum movable boundaries.
  9. Parse Model Settings JSON with CubismModelSettingJson

    develop

    The CubismModelSettingJson class is used to parse and access data from a Live2D Model Settings JSON file. You initialize it by passing a buffer containing the JSON data and its size. This class provides high-level access to model metadata such as texture paths, motion groups, expressions, hit areas, and physics settings.

    // Assuming 'buffer' is a csmByte* containing the JSON data and 'size' is its length
    Live2D::Cubism::Framework::CubismModelSettingJson modelSetting(buffer, size);
    
    // Example: Accessing the MOC3 file name
    const csmChar* moc3File = modelSetting.GetModelFileName();