1.1.1 • Published 1 year ago

live2d-library v1.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Live2d + TypeScript + Vite

基于 Live2d SDK 4.x 实现, 在官方给的 demo 上修改

视线跟踪设定

在 Model3.json 增加参数映射 Mapper 字段

{
    "Version": 3,
    "FileReferences": {
        "Moc": "FileReferences_Moc_0.moc3",
        "Textures": [
            "FileReferences_Textures_0_0.png",
        ],
        "Physics": "FileReferences_Physics_0.json",
        "Mapper": "Param_Mapper.json",
        "Motions": {
            "Idle": [
                {
                    "File": "FileReferences_Motions_Idle_0_File_0.json"
                }
            ],
            ...
        }
    },
    ...
}

Param_Mapper.json 参数映射文件实现, 文件中 Id 不用修改, 改 Value 为对应的参数即可, 如果没有实现 Param_Mapper.json 那么 id 值就是默认参数

{
  "Meta": {
    "MapperCount": 6
  },
  "Mapper": [
    {
      "Value": "PARAM_ANGLE_X",
      "Id": "ParamAngleX"
    },
    {
      "Value": "PARAM_ANGLE_Y",
      "Id": "ParamAngleY"
    },
    {
      "Value": "PARAM_ANGLE_Z",
      "Id": "ParamAngleZ"
    },
    {
      "Value": "PARAM_BODY_ANGLE_X",
      "Id": "ParamBodyAngleX"
    },
    {
      "Value": "PARAM_EYE_BALL_X",
      "Id": "ParamEyeBallX"
    },
    {
      "Value": "PARAM_EYE_BALL_Y",
      "Id": "ParamEyeBallY"
    }
  ]
}

模型命中检测

在 Model3.json 中 HitAreas 里面添加 Motion 字段, 点击 HitAreas Name 区域, 将自动获取 Motions 字段下对应的 Motion, 并执行

{
    "Version": 3,
    "FileReferences": {
        "Moc": "FileReferences_Moc_0.moc3",
        "Textures": [
            "FileReferences_Textures_0_0.png",
        ],
        "Physics": "FileReferences_Physics_0.json",
        "Mapper": "Param_Mapper.json",
        "Motions": {
            "Idle": [
                {
                    "File": "FileReferences_Motions_Idle_0_File_0.json"
                }
            ],
            "1": [
                {
                    "File": "FileReferences_Motions_Id_favor1_1_0_File_0.json",
                    "Sound": "FileReferences_Motions_1_0_Sound_0.mp3"
                },
                {
                    "File": "FileReferences_Motions_Id_favor1_2_0_File_0.json",
                    "Sound": "FileReferences_Motions_1_1_Sound_0.mp3"
                },
                {
                    "File": "FileReferences_Motions_Id_favor1_3_0_File_0.json",
                    "Sound": "FileReferences_Motions_1_2_Sound_0.mp3"
                }
            ],
            "2": [
                {
                    "File": "FileReferences_Motions_Id_favor2_1_0_File_0.json",
                    "Sound": "FileReferences_Motions_2_0_Sound_0.mp3"
                },
                {
                    "File": "FileReferences_Motions_Id_favor3_1_0_File_0.json",
                    "Sound": "FileReferences_Motions_2_1_Sound_0.mp3"
                }
            ],
            "3": [
                {
                    "File": "FileReferences_Motions_Id_favor4_1_0_File_0.json",
                    "Sound": "FileReferences_Motions_3_0_Sound_0.mp3"
                },
                {
                    "File": "FileReferences_Motions_Id_favor5_1_0_File_0.json",
                    "Sound": "FileReferences_Motions_3_1_Sound_0.mp3"
                },
                {
                    "File": "FileReferences_Motions_Id_favor6_1_0_File_0.json",
                    "Sound": "FileReferences_Motions_3_2_Sound_0.mp3"
                }
            ]
        }
    },
    "HitAreas": [
        {
            "Name": "head",
            "Id": "HitArea",
            "Motion": "1"
        },
        {
            "Name": "body",
            "Id": "HitArea2",
            "Motion": "2"
        },
        {
            "Name": "tui",
            "Id": "HitArea3",
            "Motion": "3"
        }
    ]
}

口形同步

在 Model3.json 文件中 Motions 字段下对应动作, 添加 Sound 字段, 以及 Groups 字段 Motions 还可以添加字幕 Text 通过 emitter.on('message', (text: string) => {}) 监听

{
    "Version": 3,
    "FileReferences": {
        ...
        "Motions": {
            "Idle": [
                {
                    "File": "FileReferences_Motions_Idle_0_File_0.json"
                }
            ],
            "1": [
                {
                    "File": "FileReferences_Motions_Id_favor1_1_0_File_0.json",
                    "Sound": "FileReferences_Motions_1_0_Sound_0.mp3",
                    "Text": "这是一段字幕"
                },
                {
                    "File": "FileReferences_Motions_Id_favor1_2_0_File_0.json",
                    "Sound": "FileReferences_Motions_1_1_Sound_0.mp3"
                },
                {
                    "File": "FileReferences_Motions_Id_favor1_3_0_File_0.json",
                    "Sound": "FileReferences_Motions_1_2_Sound_0.mp3"
                }
            ],
            "2": [
                {
                    "File": "FileReferences_Motions_Id_favor2_1_0_File_0.json",
                    "Sound": "FileReferences_Motions_2_0_Sound_0.mp3"
                },
                {
                    "File": "FileReferences_Motions_Id_favor3_1_0_File_0.json",
                    "Sound": "FileReferences_Motions_2_1_Sound_0.mp3"
                }
            ],
            "3": [
                {
                    "File": "FileReferences_Motions_Id_favor4_1_0_File_0.json",
                    "Sound": "FileReferences_Motions_3_0_Sound_0.mp3"
                },
                {
                    "File": "FileReferences_Motions_Id_favor5_1_0_File_0.json",
                    "Sound": "FileReferences_Motions_3_1_Sound_0.mp3"
                },
                {
                    "File": "FileReferences_Motions_Id_favor6_1_0_File_0.json",
                    "Sound": "FileReferences_Motions_3_2_Sound_0.mp3"
                }
            ]
        }
    },
    "Groups": [
        {
            "Target": "Parameter",
            "Name": "LipSync",
            "Ids": [
                "PARAM_MOUTH_OPEN_Y"
            ]
        },
        {
            "Target": "Parameter",
            "Name": "EyeBlink",
            "Ids": [
                "PARAM_EYE_L_OPEN",
                "PARAM_EYE_R_OPEN"
            ]
        }
    ]
    ...
}

教程

在HTML文档中引用官方 Cubism Core for Web

<script src="./Core/live2dcubismcore.min.js"></script>

安装

npm install live2d-library

yarn add live2d-library

使用 demo 查看 demo 需要翻墙 Github demo

import live2d from 'live2d-library'

interface Live2dOptions {
    canvas: HTMLCanvasElement
    models: string[]
    scale?: number
    position?: [number, number]
}

interface Live2dReturn {
    emitter: Emitter<Events>
    nextModel: () => void
    nextRandomModel: () => void
    onResize: () => void
    setScale: (scale: number) => void
    release: () => void
}

live2d(options: Live2dOptions)

浏览器支持列表

平台浏览器版本号
AndroidGoogle Chrome90.0.4430.210
AndroidMicrosoft Edge46.04.4.5157
AndroidMozilla Firefox88.1.4
iOS / iPadOSGoogle Chrome90.0.4430.216
iOS / iPadOSMicrosoft Edge46.3.13
iOS / iPadOSMozilla Firefox33.1
iOS / iPadOSSafari14.1
macOSGoogle Chrome91.0.4472.77
macOSMicrosoft Edge90.0.818.66
macOSMozilla Firefox88.0.1
macOSSafari14.1
WindowsGoogle Chrome91.0.4472.77
WindowsInternet Explorer 1119041.928
WindowsMicrosoft Edge90.0.818.66
WindowsMozilla Firefox88.0.1
1.1.1

1 year ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago