@doki-land/live2d 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/l2d.esm.js +38900 -19927
- package/dist/l2d.esm.js.map +1 -0
- package/dist/l2d.umd.js +688 -552
- package/dist/l2d.umd.js.map +1 -0
- package/lib/cubism5.d.ts +367 -0
- package/lib/cubism5.min.js +2 -1
- package/lib/index.d.ts +7 -0
- package/package.json +43 -37
- package/readme.md +16 -0
- package/src/index.ts +141 -149
- package/src/types/Live2dOptions.ts +5 -5
- package/src/types/index.ts +2 -2
package/lib/index.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,39 +1,45 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
2
|
+
"name": "@doki-land/live2d",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"live2d",
|
|
6
|
+
"pixi",
|
|
7
|
+
"typescript"
|
|
8
|
+
],
|
|
9
|
+
"description": "Live2D TypeScript runtime based on pixi-live2d-display",
|
|
10
|
+
"author": "Aster <192608617@qq.com>",
|
|
11
|
+
"license": "LGPL-3.0-or-later",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "dist/l2d.umd.js",
|
|
14
|
+
"module": "dist/l2d.esm.js",
|
|
15
|
+
"types": "src/index.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"require": "./dist/l2d.umd.js",
|
|
19
|
+
"import": "./dist/l2d.esm.js",
|
|
20
|
+
"types": "./src/index.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"lib",
|
|
26
|
+
"src"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"registry": "https://registry.npmjs.org",
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"dev": "vite",
|
|
34
|
+
"build": "vite build",
|
|
35
|
+
"preview": "vite preview"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@pixi/core": "^7.4.3",
|
|
39
|
+
"@pixi/display": "^7.4.3",
|
|
40
|
+
"@pixi/interaction": "^6.5.10",
|
|
41
|
+
"@pixi/ticker": "^7.4.3",
|
|
42
|
+
"pixi-live2d-display-lipsyncpatch": "0.5.0-ls-7",
|
|
43
|
+
"pixi.js": "^7.4.3"
|
|
21
44
|
}
|
|
22
|
-
|
|
23
|
-
"files": [
|
|
24
|
-
"dist",
|
|
25
|
-
"lib",
|
|
26
|
-
"src"
|
|
27
|
-
],
|
|
28
|
-
"dependencies": {
|
|
29
|
-
"@pixi/interaction": "6.*",
|
|
30
|
-
"@pixi/ticker": "6.*",
|
|
31
|
-
"pixi-live2d-display": "^0.4.0",
|
|
32
|
-
"pixi.js": "6.*"
|
|
33
|
-
},
|
|
34
|
-
"scripts": {
|
|
35
|
-
"dev": "vite",
|
|
36
|
-
"build": "vite build",
|
|
37
|
-
"preview": "vite preview"
|
|
38
|
-
}
|
|
39
|
-
}
|
|
45
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
## Using in html by module
|
|
3
|
+
|
|
4
|
+
```html
|
|
5
|
+
<script type="module">
|
|
6
|
+
const cdn = 'https://cdn.jsdelivr.net/npm/@doki-land/live2d@latest/dist/l2d.esm.js';
|
|
7
|
+
const { createLive2dModel, initializeLive2D } = await import(cdn);
|
|
8
|
+
initializeLive2D();
|
|
9
|
+
await createLive2dModel({
|
|
10
|
+
element_id: 'live2d-canvas',
|
|
11
|
+
models: [
|
|
12
|
+
{}
|
|
13
|
+
]
|
|
14
|
+
});
|
|
15
|
+
</script>
|
|
16
|
+
```
|
package/src/index.ts
CHANGED
|
@@ -1,149 +1,141 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
//
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
model.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
await model.motion('
|
|
105
|
-
} else
|
|
106
|
-
//
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
*
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
//
|
|
128
|
-
|
|
129
|
-
//
|
|
130
|
-
|
|
131
|
-
//
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
//
|
|
135
|
-
//
|
|
136
|
-
|
|
137
|
-
//
|
|
138
|
-
|
|
139
|
-
//
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// 导出默认对象
|
|
144
|
-
export default {
|
|
145
|
-
createLive2dModel,
|
|
146
|
-
initializeLive2D,
|
|
147
|
-
Live2DModel,
|
|
148
|
-
MotionManager
|
|
149
|
-
};
|
|
1
|
+
import '../lib/cubism2.min.js';
|
|
2
|
+
import '../lib/cubism5.min.js';
|
|
3
|
+
import * as PIXI from 'pixi.js';
|
|
4
|
+
import { Live2dOptions } from './types';
|
|
5
|
+
import { Ticker, TickerPlugin } from '@pixi/ticker';
|
|
6
|
+
import { InteractionManager } from '@pixi/interaction';
|
|
7
|
+
import { Live2DModel, MotionManager, SoundManager } from 'pixi-live2d-display-lipsyncpatch';
|
|
8
|
+
|
|
9
|
+
export { MotionManager, SoundManager, Live2DModel, PIXI, type Live2dOptions };
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 创建Live2D模型
|
|
14
|
+
* @param options 模型配置选项
|
|
15
|
+
* @returns Promise<Live2DModel> 加载完成的Live2D模型实例
|
|
16
|
+
*/
|
|
17
|
+
export async function createLive2dModel(options: Live2dOptions): Promise<Live2DModel> {
|
|
18
|
+
const {
|
|
19
|
+
models,
|
|
20
|
+
element_id,
|
|
21
|
+
auto_fit = true,
|
|
22
|
+
auto_motion = true,
|
|
23
|
+
mouse_tracking = true
|
|
24
|
+
} = options;
|
|
25
|
+
|
|
26
|
+
// 初始化PIXI应用
|
|
27
|
+
const app = new PIXI.Application({
|
|
28
|
+
view: document.getElementById(element_id) as HTMLCanvasElement,
|
|
29
|
+
width: 300,
|
|
30
|
+
height: 300,
|
|
31
|
+
backgroundAlpha: 0,
|
|
32
|
+
autoDensity: true,
|
|
33
|
+
antialias: true
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// 加载模型
|
|
37
|
+
const model = await Live2DModel.from(models[0]);
|
|
38
|
+
|
|
39
|
+
// 添加模型到舞台
|
|
40
|
+
app.stage.addChild(model);
|
|
41
|
+
|
|
42
|
+
// 自动适应大小
|
|
43
|
+
if (auto_fit) {
|
|
44
|
+
const scale = Math.min(300 / model.width, 300 / model.height);
|
|
45
|
+
model.scale.set(scale);
|
|
46
|
+
|
|
47
|
+
// 居中显示
|
|
48
|
+
model.x = (300 - model.width * scale) / 2;
|
|
49
|
+
model.y = (300 - model.height * scale) / 2;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 启用鼠标跟踪
|
|
53
|
+
if (mouse_tracking) {
|
|
54
|
+
enableMouseTracking(model, app.view);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 自动开始动画
|
|
58
|
+
if (auto_motion) {
|
|
59
|
+
await startIdleAnimation(model);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return model;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 启用鼠标跟踪功能
|
|
67
|
+
* @param model Live2D模型实例
|
|
68
|
+
* @param canvas 画布元素
|
|
69
|
+
*/
|
|
70
|
+
function enableMouseTracking(model: Live2DModel, canvas: HTMLCanvasElement) {
|
|
71
|
+
// 跟踪鼠标移动
|
|
72
|
+
document.addEventListener('mousemove', (event) => {
|
|
73
|
+
const rect = canvas.getBoundingClientRect();
|
|
74
|
+
const centerX = rect.left + rect.width / 2;
|
|
75
|
+
const centerY = rect.top + rect.height / 2;
|
|
76
|
+
|
|
77
|
+
// 计算鼠标位置相对于画布中心的偏移
|
|
78
|
+
const mouseX = (event.clientX - centerX) / rect.width;
|
|
79
|
+
const mouseY = (event.clientY - centerY) / rect.height;
|
|
80
|
+
|
|
81
|
+
// 更新模型的视线方向
|
|
82
|
+
if (model.internalModel) {
|
|
83
|
+
model.internalModel.focusController?.focus(mouseX, mouseY);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 开始空闲动画
|
|
90
|
+
* @param model Live2D模型实例
|
|
91
|
+
*/
|
|
92
|
+
async function startIdleAnimation(model: Live2DModel) {
|
|
93
|
+
// 尝试播放内置的动作组
|
|
94
|
+
try {
|
|
95
|
+
// 获取模型支持的动作组
|
|
96
|
+
const motions = model.internalModel?.motionManager.definitions;
|
|
97
|
+
|
|
98
|
+
if (motions) {
|
|
99
|
+
// 优先使用Idle动作组
|
|
100
|
+
if (motions.idle) {
|
|
101
|
+
await model.motion('idle');
|
|
102
|
+
} else if (motions.tap_body) {
|
|
103
|
+
// 如果没有idle动作,尝试使用tap_body动作
|
|
104
|
+
await model.motion('tap_body');
|
|
105
|
+
} else {
|
|
106
|
+
// 使用第一个可用的动作组
|
|
107
|
+
const firstMotionGroup = Object.keys(motions)[0];
|
|
108
|
+
if (firstMotionGroup) {
|
|
109
|
+
await model.motion(firstMotionGroup);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
} catch (error) {
|
|
114
|
+
console.warn('Failed to start idle animation:', error);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 加载Cubism SDK
|
|
120
|
+
* 在使用Live2D功能前必须调用此函数
|
|
121
|
+
* @param cubism2 可选的CubismCore对象,如果在非浏览器环境中使用,需要传入
|
|
122
|
+
* @param cubism5
|
|
123
|
+
*/
|
|
124
|
+
export async function initializeLive2D(cubism2?: any, cubism5?: any) {
|
|
125
|
+
// 首先初始化 Cubism2 SDK
|
|
126
|
+
// console.log('sdk2:', Cubism2);
|
|
127
|
+
// console.log('sdk5:', Cubism5);
|
|
128
|
+
// @ts-ignore
|
|
129
|
+
// window.Live2D = cubism2 || Cubism2;
|
|
130
|
+
// 然后初始化 Cubism5 SDK
|
|
131
|
+
// @ts-ignore
|
|
132
|
+
// window.Live2DCubismCore = cubism5 || Cubism5().Live2DCubismCore;
|
|
133
|
+
|
|
134
|
+
// 最后初始化 PIXI 相关功能
|
|
135
|
+
// 为 Live2DModel 注册 Ticker
|
|
136
|
+
Live2DModel.registerTicker(Ticker);
|
|
137
|
+
// 为 Application 注册 Ticker
|
|
138
|
+
PIXI.extensions.add(TickerPlugin);
|
|
139
|
+
// 注册 InteractionManager 以支持 Live2D 模型的自动交互
|
|
140
|
+
PIXI.extensions.add(InteractionManager);
|
|
141
|
+
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { ModelOptions } from './ModelOptions
|
|
1
|
+
import { ModelOptions } from './ModelOptions';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Live2D模型配置选项
|
|
5
5
|
*/
|
|
6
6
|
export interface Live2dOptions {
|
|
7
|
-
/**
|
|
8
|
-
* 要渲染到的HTML元素ID
|
|
9
|
-
*/
|
|
10
|
-
element_id: string;
|
|
11
7
|
/**
|
|
12
8
|
* 模型配置选项
|
|
13
9
|
*/
|
|
14
10
|
models: ModelOptions[];
|
|
11
|
+
/**
|
|
12
|
+
* 要渲染到的HTML元素ID
|
|
13
|
+
*/
|
|
14
|
+
element_id?: string;
|
|
15
15
|
/**
|
|
16
16
|
* 是否自动适应模型大小
|
|
17
17
|
* @default true
|
package/src/types/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export type { Live2dOptions } from './Live2dOptions
|
|
2
|
-
export type { ModelOptions, ModelPosition, ModelMobileAdaptation, ModelMainOptions } from './ModelOptions
|
|
1
|
+
export type { Live2dOptions } from './Live2dOptions';
|
|
2
|
+
export type { ModelOptions, ModelPosition, ModelMobileAdaptation, ModelMainOptions } from './ModelOptions';
|