Live2D Cubism Web Framework

repository·develop·Indexed 42 days ago

https://github.com/live2d/cubismwebframework

A JavaScript library for displaying and manipulating Live2D Cubism models in web applications. It provides high-level abstractions for rendering, physics, motion, and effects. This framework requires the Live2D Cubism Core library to load models. Key components include CubismBreath for breathing effects, CubismMath for vector and matrix calculations, and CubismMatrix44 for 4x4 transformations.

Tokens
15K
Snippets
42
Records
71
Agent score
52%

What's inside Cubism Web Framework

  1. Overview of Cubism Web Framework

    develop

    The Cubism Web Framework is a framework for using models output by the Live2D Cubism Editor in applications. It provides functions for displaying and manipulating models and is intended to be used as a JavaScript library in the browser.

    Important Requirement: This framework is not a standalone solution. It must be used in conjunction with the Live2D Cubism Core library to load models. The Core library is not included in this repository and must be downloaded separately from the Cubism SDK for Web.

  2. Core components of the Cubism Web Framework

    develop

    The framework is organized into several functional components:

    • effect: Provides functions to add motion-based effects to models, such as automatic blinking and lip-sync.
    • id: Manages parameter names, part names, and Drawable names set on the model using unique types.
    • math: Provides arithmetic operations required for model manipulation and rendering, including matrix and vector calculations.
    • model: Provides core functions for handling models, including creation, updating, and destruction.
    • motion: Provides functions for applying motion data to models, such as motion playback and parameter blending.
    • physics: Provides functions to apply deformation operations to models via physics calculations.
    • rendering: Provides a renderer that implements graphics commands for drawing the model.
    • type: Provides type definitions used throughout the framework.
    • utils: Provides utility functions such as JSON parsers and log output.
  3. Available framework components

    develop

    The framework is organized into several functional components:

    ComponentDescription
    effectAdds motion information like automatic blinking and lip sync to the model.
    idManages parameter, part, and Drawable names using unique types.
    mathProvides arithmetic functions for model manipulation and drawing (e.g., matrix and vector calculations).
    modelHandles model lifecycle: generate, update, and destroy.
    motionHandles motion playback and parameter blending.
    physicsApplies physics-based transformation manipulations to the model.
    renderingProvides a renderer that implements graphics instructions for drawing the model.
    typeContains type definitions used throughout the framework.
    utilsProvides utility functions like JSON parsers and log output.
  4. Set up the Cubism Web Framework development environment

    develop

    To set up the development environment, follow these steps:

    1. Install Node.js and Visual Studio Code.
    2. Open this project in Visual Studio Code and install the recommended extensions (search for @recommended in the Extensions tab).
    3. Open the Command Palette (View > Command Palette...) and run >Tasks: Run Task.
    4. Select npm: install to download the necessary dependency packages.

    Supported Versions:

    • Node.js: 25.8.2 or 24.14.1
    • TypeScript: 5.9.3
  5. Setup the development environment

    develop

    To set up the development environment for the Cubism Web Framework, follow these steps:

    1. Install [Node.js] and [Visual Studio Code].
    2. Open this project in Visual Studio Code and install the recommended extensions (accessible via the @recommended command in the Extensions tab).
    3. Open the Command Palette (View > Command Palette...) and enter >Tasks: Run Task.
    4. Select npm: install to download the dependent packages.

    Required Versions:

    • Node.js: 25.8.2 or 24.14.1
    • TypeScript: 5.9.3
  6. Extend CubismUserModel to implement a Live2D model

    develop
    The CubismUserModel class is the base class for user-implemented models. You should extend this class to create your own model implementation. It manages the lifecycle of the Live2D model, including loading assets (MOC3, motions, expressions, physics, etc.), handling rendering via WebGL, and managing animation states like breathing, blinking, and poses.
  7. Use CubismMath for mathematical utilities

    develop

    The CubismMath class provides a collection of static utility methods for common mathematical operations required for Live2D model calculations, including trigonometry, vector math, and equation solving. It also defines a constant Epsilon for floating-point comparisons.

    Key capabilities include:

    • Clamping and Range: Restricting values within a specific range.
    • Trigonometry: Sine, cosine, and conversions between degrees and radians.
    • Vector Math: Calculating directions and angles between CubismVector2 objects.
    • Equation Solving: Solving quadratic and cubic equations (specifically using Cardano's algorithm for Bezier curves).
    • Easing: Sine-based easing for fade-in/out effects.
    import { CubismMath } from './math/cubismmath';
    
    // Example: Clamping a value
    const clamped = CubismMath.clamp(15, 0, 10); // returns 10
    
    // Example: Converting degrees to radians
    const rad = CubismMath.degreesToRadian(180);
    
    // Example: Sine easing
    const eased = CubismMath.getEasingSine(0.5);
  8. Manage update cycles with CubismUpdateScheduler

    develop

    The CubismUpdateScheduler is responsible for managing and executing a list of ICubismUpdater instances. It ensures that updaters are executed in the correct order by automatically sorting them based on their internal execution order before the next update cycle occurs. This is typically used during the late update phase of a model's lifecycle.

    Key behaviors:

    • Automatic Sorting: When an updater is added or its execution order changes (via onUpdaterChanged), the scheduler marks the list as needing a sort. The list is sorted using ICubismUpdater.sortFunction during the next call to onLateUpdate.
    • Lifecycle Management: Use release() or clearUpdatableList() to remove all updaters and clean up listeners to prevent memory leaks.
    • Duplicate Prevention: addUpdatableList will not add the same ICubismUpdater instance multiple times.
    import { CubismUpdateScheduler } from './cubismupdatescheduler';
    
    const scheduler = new CubismUpdateScheduler();
    
    // Add updaters (must implement ICubismUpdater)
    // scheduler.addUpdatableList(myUpdater);
    
    // In your animation/render loop:
    // scheduler.onLateUpdate(model, deltaTimeSeconds);
    
    // Cleanup
    // scheduler.release();
  9. Parse physics3.json with CubismPhysicsJson

    develop

    The CubismPhysicsJson class is used to parse and access configuration data from a physics3.json file. You can instantiate it by providing an ArrayBuffer containing the JSON data and its size.

    Note: You must manually call .release() to free the underlying JSON resources when you are finished with the object to prevent memory leaks.

    const physicsJson = new CubismPhysicsJson(buffer, size);
    
    // ... use the object ...
    
    physicsJson.release();
  10. ESLint configuration and ignored files

    develop

    The project uses ESLint with TypeScript support. The configuration is set up to target ecmaVersion: 2020 and uses ./tsconfig.json for type-aware linting.

    File Ignoring Pattern: By default, the configuration ignores all files (**/*.*) except for TypeScript files within the src directory (!src/**/*.ts).

    ignores: [
      '**/*.*',
      '!src/**/*.ts',
    ],
  11. Available npm tasks

    develop

    The following tasks can be executed via the VS Code Command Palette (>Tasks: Run Task) or via npm:

    • npm: build: Builds the source files and outputs them to the dist directory. Configuration is managed via tsconfig.json.
    • npm: test: Performs a TypeScript type check test. Configuration is managed via tsconfig.json.
    • npm: lint: Performs static analysis of TypeScript files in the src directory. Configuration is managed via eslint.config.mjs.
    • npm: lint:fix: Performs static analysis and automatically modifies TypeScript files in the src directory. Configuration is managed via eslint.config.mjs.
    • npm: clean: Deletes the dist build directory.
  12. Available development tasks via npm

    develop

    You can execute various tasks using the VS Code Command Palette (>Tasks: Run Task).

    TaskDescription
    npm: buildBuilds source files and outputs them to the dist directory. Configuration can be modified via tsconfig.json.
    npm: testPerforms TypeScript type checking tests. Configuration can be modified via tsconfig.json.
    npm: lintPerforms static analysis of TypeScript files in the src directory. Configuration can be modified via eslint.config.mjs.
    npm: lint:fixPerforms static analysis and automatically fixes issues in the src directory. Configuration can be modified via eslint.config.mjs.
    npm: cleanDeletes the build output directory (dist).