pixi-live2d-display

repository·master·Indexed 23 days ago

https://github.com/guansss/pixi-live2d-display

A universal Live2D framework for the web that integrates Live2D models into PixiJS v6. It provides a high-level, unified interface supporting all versions of Live2D, including Cubism 2.1 and Cubism 4. The library includes the Live2DModel class for model management, as well as managers for motions and expressions, and experimental support for loading models from zip files or uploaded file arrays.

Tokens
14.8K
Snippets
38
Records
75
Agent score
80%

What's inside pixi-live2d-display

  1. Identify core classes and modules in pixi-live2d-display

    master

    The library is organized into two main modules: index (the main entry point that re-exports everything) and CubismWebFramework.

    Key classes for interacting with Live2D models include:

    • Live2DModel: The primary class for managing Live2D models.
    • InternalModel: Represents the underlying model structure.
    • ModelSettings: Handles configuration and settings for the model.
    • MotionManager: Manages playing motions/animations.
    • ExpressionManager: Manages facial expressions.
    • config: Provides configuration options for the library.
  2. Understand the Live2DModel loading lifecycle and architecture

    master

    The Live2DModel is loaded using the Live2DModel.fromSync(source) method. The source can be a URL, a settingsJSON object, ModelSettings, a Pose, Physics configuration, an array of Texture[], or an InternalModel.

    Loading follows a specific event lifecycle:

    1. The process begins with fromSync(source).
    2. Depending on the source type, specific events are emitted: settingsJSONLoaded, settingsLoaded, poseLoaded, physicsLoaded, textureLoaded, or modelLoaded.
    3. Once all components are processed, the model emits the ready event.

    Internally, the loading process uses middlewares to transform data:

    • urlToJSON() converts URLs to JSON.
    • jsonToSettings() converts JSON to settings.
    • setupOptionals() handles optional components like Pose or Physics.
    • setupLive2DModel() prepares the Live2D model.
    • createInternalModel() finalizes the InternalModel.
  3. Load Cubism Core runtimes

    master

    The plugin requires the official Live2D Cubism Core runtimes to function. You must load the appropriate runtime based on the model version you intend to support.

    Cubism 4 (Supports Cubism 3 and 4 models)

    Load live2dcubismcore.min.js.

    Cubism 2.1

    Load live2d.min.js.

    To optimize bundle size, choose the combination that matches your needs:

    • Cubism 2.1 support: Use cubism2.js + live2d.min.js.
    • Cubism 3/4 support: Use cubism4.js + live2dcubismcore.min.js.
    • Full support (All versions): Use index.js + live2d.min.js + live2dcubismcore.min.js.
  4. Enable automatic interaction with Pixi InteractionManager

    master

    By default, the model uses Pixi's InteractionManager to handle focusing (looking at the mouse) and tapping automatically.

    To use this, you can either import the full pixi.js package, which registers the manager automatically:

    import * as PIXI from 'pixi.js';

    Or, if you are using modular Pixi packages, you must manually register the InteractionManager as a plugin to the Renderer:

    import { Renderer } from '@pixi/core';
    import { InteractionManager } from '@pixi/interaction';
    
    Renderer.registerPlugin('interaction', InteractionManager);
  5. Register components for on-demand PixiJS imports

    master

    If you are using on-demand/modular PixiJS packages (e.g., @pixi/app, @pixi/ticker), you must manually register the necessary components to enable Live2D features like automatic updates and interaction.

    • For automatic updates: Register the Ticker with Live2DModel.registerTicker(Ticker).
    • For Application integration: Register the TickerPlugin with Application.registerPlugin(TickerPlugin).
    • For interaction/hit detection: Register the InteractionManager with the renderer using Renderer.registerPlugin('interaction', InteractionManager).
    import { Application } from '@pixi/app';
    import { Ticker } from '@pixi/ticker';
    import { InteractionManager } from '@pixi/interaction';
    import { Live2DModel } from 'pixi-live2d-display';
    
    // Register Ticker for Live2DModel
    Live2DModel.registerTicker(Ticker);
    
    // Register Ticker for Application
    Application.registerPlugin(TickerPlugin);
    
    // Register InteractionManager to support automatic interaction
    Renderer.registerPlugin('interaction', InteractionManager);
    
    (async function () {
        const app = new Application({
            view: document.getElementById('canvas'),
        });
    
        const model = await Live2DModel.from('shizuku.model.json');
    
        app.stage.addChild(model);
    })();
  6. Load a Live2D model from a zip file (experimental)

    master

    You can load models from .zip files by providing a URL or an uploaded File.

    Protocol: If the URL does not end in .zip, you must prepend it with the zip:// protocol (e.g., zip://http://example.com/model).

    Implementation Requirement: Because the library does not include a zipping library by default, you must implement the static methods of ZipLoader before attempting to load a zip file, otherwise a "Not implemented" error will be thrown. You can use libraries like jszip to provide these implementations.

    // Load via URL
    Live2DModel.from('path/to/shizuku.zip');
    
    // Load via fake protocol
    Live2DModel.from('zip://path/to/shizuku');
    
    // Load via uploaded file
    document.getElementById("zippicker").addEventListener("change", async function(event) {
        const files = event.target.files;
        if (files.length) {
            const model = await Live2DModel.from(files);
        }
    });
  7. Install pixi-live2d-display

    master

    You can install the package via npm or include it directly in your HTML using a CDN.

    npm Installation

    npm install pixi-live2d-display

    CDN Installation

    Include the script tag in your HTML. Note that when using the CDN, all members are exported to the PIXI.live2d namespace (e.g., PIXI.live2d.Live2DModel).

    • All versions: https://cdn.jsdelivr.net/npm/pixi-live2d-display/dist/index.min.js
    • Cubism 2.1 only: https://cdn.jsdelivr.net/npm/pixi-live2d-display/dist/cubism2.min.js
    • Cubism 4 only: https://cdn.jsdelivr.net/npm/pixi-live2d-display/dist/cubism4.min.js
  8. Update Live2D models

    master

    To animate a Live2D model, it must be updated every frame using model.update(deltaTime). You can choose between automatic or manual updates.

    Automatic Updates (Default)

    By default, models attempt to use PIXI.Ticker.shared. To enable this, you must ensure PIXI is available in the global scope:

    import * as PIXI from 'pixi.js';
    window.PIXI = PIXI;

    If you are using a modular setup, you must manually register the Ticker:

    import { Application } from '@pixi/app';
    import { Ticker, TickerPlugin } from '@pixi/ticker';
    
    Application.registerPlugin(TickerPlugin);
    Live2DModel.registerTicker(Ticker);

    Manual Updates

    To control the update loop yourself, set autoUpdate: false in the creation options and call .update() in your own loop.

    Using Ticker

    const model = await Live2DModel.from('shizuku.model.json', { autoUpdate: false });
    const ticker = new Ticker();
    
    ticker.add(() => model.update(ticker.elapsedMS));

    Using requestAnimationFrame

    const model = await Live2DModel.from('shizuku.model.json', { autoUpdate: false });
    let then = performance.now();
    
    function tick(now) {
        model.update(now - then);
        then = now;
        requestAnimationFrame(tick);
    }
    requestAnimationFrame(tick);
    const model = await Live2DModel.from('shizuku.model.json', { autoUpdate: false });
    
    const ticker = new Ticker();
    
    ticker.add(() => model.update(ticker.elapsedMS));
  9. Import Live2DModel via CDN

    master

    When using a CDN, all exported members are available under the PIXI.live2d namespace (e.g., PIXI.live2d.Live2DModel).

    <!-- Full support -->
    <script src="https://cdn.jsdelivr.net/npm/pixi-live2d-display/dist/index.min.js"></script>
    
    <!-- if only Cubism 2.1 -->
    <script src="https://cdn.jsdelivr.net/npm/pixi-live2d-display/dist/cubism2.min.js"></script>
    
    <!-- if only Cubism 4 -->
    <script src="https://cdn.jsdelivr.net/npm/pixi-live2d-display/dist/cubism4.min.js"></script>
  10. Clone the repository

    master

    To clone the repository with submodules included, use SSH. If you clone via HTTPS, submodules will not be included automatically and must be installed manually.

    git clone git@github.com:guansss/pixi-live2d-display.git --recursive

    Cloning via HTTPS

    git clone https://github.com/guansss/pixi-live2d-display.git

    If you used HTTPS, you must manually install the submodules using one of these methods:

    Method 1: Git Config Redirect

    git config --global url."https://github.com/guansss/CubismWebFramework.git".insteadOf "git@github.com:guansss/CubismWebFramework.git"
    
    git submodule sync
    git submodule update --init

    Method 2: Manual .gitmodules Edit

    1. Edit .gitmodules and replace git@github.com:guansss/CubismWebFramework.git with https://github.com/guansss/CubismWebFramework.git.
    2. Run:
    git submodule sync
    git submodule update --init

    Note: Do not commit changes to .gitmodules if you are contributing to the project.

    git clone git@github.com:guansss/pixi-live2d-display.git --recursive
  11. Manual installation of Cubism 2.1 core files

    master

    If you are manually managing Cubism 2.1 files, ensure the following are present in your project folder:

    • live2d.min.js: The Cubism 2.1 core library.
    • live2d.d.ts: The unofficial declaration file for Cubism 2.1.

    Note: live2d.min.js is no longer available on the official Live2D site but can be sourced from community repositories.