@charivo/render-live2d 0.1.0 → 0.1.2
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/README.md
CHANGED
|
@@ -1,418 +1,49 @@
|
|
|
1
1
|
# @charivo/render-live2d
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Live2D Cubism renderer for Charivo.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
This package is the concrete browser renderer for Live2D models. In most apps
|
|
6
|
+
you use it together with `@charivo/render-core`.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
- ✨ **Simple API** - Just 3 lines to render a Live2D model
|
|
10
|
-
- 🎯 **TypeScript Support** - Full type safety
|
|
11
|
-
- 🔌 **Framework Agnostic** - Works with React, Vue, vanilla JS, etc.
|
|
12
|
-
- 💋 **Built-in Lip Sync** - Real-time mouth movement with audio
|
|
13
|
-
- 🎭 **Motion & Expression Control** - Easy animation control
|
|
14
|
-
- 📦 **Zero Configuration** - Works out of the box
|
|
15
|
-
|
|
16
|
-
## Installation
|
|
8
|
+
## Install
|
|
17
9
|
|
|
18
10
|
```bash
|
|
19
|
-
pnpm add @charivo/render-live2d @charivo/core
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
You also need to include the Live2D Cubism Core library in your HTML:
|
|
23
|
-
|
|
24
|
-
```html
|
|
25
|
-
<script src="/path/to/live2dcubismcore.min.js"></script>
|
|
11
|
+
pnpm add @charivo/render-live2d @charivo/render-core
|
|
26
12
|
```
|
|
27
13
|
|
|
28
|
-
##
|
|
14
|
+
## Usage
|
|
29
15
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
```typescript
|
|
33
|
-
import { createLive2DRenderer } from "@charivo/render-live2d";
|
|
34
|
-
|
|
35
|
-
const canvas = document.getElementById("canvas") as HTMLCanvasElement;
|
|
36
|
-
|
|
37
|
-
// 1. Create renderer
|
|
38
|
-
const renderer = createLive2DRenderer({ canvas });
|
|
39
|
-
|
|
40
|
-
// 2. Initialize
|
|
41
|
-
await renderer.initialize();
|
|
42
|
-
|
|
43
|
-
// 3. Load model
|
|
44
|
-
await renderer.loadModel("/live2d/hiyori/hiyori.model3.json");
|
|
45
|
-
|
|
46
|
-
// That's it! Your Live2D model is now rendering.
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
### With Motion Control
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
import { Live2DRenderer } from "@charivo/render-live2d";
|
|
53
|
-
|
|
54
|
-
const renderer = new Live2DRenderer({ canvas });
|
|
55
|
-
|
|
56
|
-
await renderer.initialize();
|
|
57
|
-
await renderer.loadModel("/live2d/model.model3.json");
|
|
58
|
-
|
|
59
|
-
// Play motions
|
|
60
|
-
renderer.playMotion("greeting"); // Wave hello
|
|
61
|
-
renderer.playMotion("happy"); // Happy animation
|
|
62
|
-
renderer.playMotion("thinking"); // Thinking pose
|
|
63
|
-
renderer.playMotion("talk"); // Idle talking
|
|
64
|
-
|
|
65
|
-
// Animate expressions
|
|
66
|
-
renderer.animateExpression("greeting"); // Smile
|
|
67
|
-
renderer.animateExpression("happy"); // Big smile
|
|
68
|
-
renderer.animateExpression("thinking"); // Surprised
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
### With Lip Sync
|
|
72
|
-
|
|
73
|
-
```typescript
|
|
74
|
-
const renderer = new Live2DRenderer({ canvas });
|
|
75
|
-
await renderer.initialize();
|
|
76
|
-
await renderer.loadModel("/live2d/model.model3.json");
|
|
77
|
-
|
|
78
|
-
// Enable lip-sync mode
|
|
79
|
-
renderer.setRealtimeLipSync(true);
|
|
80
|
-
|
|
81
|
-
// Update mouth opening based on audio RMS
|
|
82
|
-
// (usually done automatically by RenderManager)
|
|
83
|
-
renderer.updateRealtimeLipSyncRms(0.8); // 0.0 = closed, 1.0 = fully open
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
### With RenderManager (Recommended)
|
|
87
|
-
|
|
88
|
-
For state management, mouse tracking, and automatic lip-sync:
|
|
89
|
-
|
|
90
|
-
```typescript
|
|
16
|
+
```ts
|
|
91
17
|
import { createLive2DRenderer } from "@charivo/render-live2d";
|
|
92
18
|
import { createRenderManager } from "@charivo/render-core";
|
|
93
19
|
|
|
94
|
-
const canvas = document.getElementById("canvas") as HTMLCanvasElement;
|
|
95
|
-
|
|
96
20
|
const renderer = createLive2DRenderer({ canvas });
|
|
97
|
-
|
|
98
|
-
// Wrap with RenderManager for automatic motion/lip-sync/mouse-tracking
|
|
99
21
|
const renderManager = createRenderManager(renderer, {
|
|
100
22
|
canvas,
|
|
101
|
-
mouseTracking: "document"
|
|
23
|
+
mouseTracking: "document",
|
|
102
24
|
});
|
|
103
25
|
|
|
104
|
-
// Set character
|
|
105
|
-
renderManager.setCharacter({
|
|
106
|
-
id: "hiyori",
|
|
107
|
-
name: "Hiyori",
|
|
108
|
-
personality: "Cheerful AI assistant"
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
// Initialize (sets up mouse tracking automatically)
|
|
112
26
|
await renderManager.initialize();
|
|
113
|
-
await renderManager.loadModel("/live2d/
|
|
114
|
-
|
|
115
|
-
// Render messages (automatic motion inference!)
|
|
116
|
-
await renderManager.render({
|
|
117
|
-
id: "1",
|
|
118
|
-
content: "Hello! Nice to meet you!",
|
|
119
|
-
timestamp: new Date(),
|
|
120
|
-
type: "character"
|
|
121
|
-
});
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
## API Reference
|
|
125
|
-
|
|
126
|
-
### `Live2DRenderer`
|
|
127
|
-
|
|
128
|
-
Main renderer class.
|
|
129
|
-
|
|
130
|
-
#### Constructor
|
|
131
|
-
|
|
132
|
-
```typescript
|
|
133
|
-
new Live2DRenderer(options?: Live2DRendererOptions)
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
**Options:**
|
|
137
|
-
- `canvas?: HTMLCanvasElement` - Canvas element for rendering
|
|
138
|
-
|
|
139
|
-
#### Methods
|
|
140
|
-
|
|
141
|
-
##### `initialize()`
|
|
142
|
-
Initialize the renderer and start render loop.
|
|
143
|
-
|
|
144
|
-
```typescript
|
|
145
|
-
await renderer.initialize();
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
##### `loadModel(modelPath)`
|
|
149
|
-
Load a Live2D model.
|
|
150
|
-
|
|
151
|
-
```typescript
|
|
152
|
-
await renderer.loadModel("/live2d/hiyori/hiyori.model3.json");
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
**Note:** Path should point to the `.model3.json` file. All related assets (textures, motions, etc.) should be in the same directory.
|
|
156
|
-
|
|
157
|
-
##### `render(message, character?)`
|
|
158
|
-
Render a message (usually called by RenderManager).
|
|
159
|
-
|
|
160
|
-
```typescript
|
|
161
|
-
await renderer.render({
|
|
162
|
-
id: "1",
|
|
163
|
-
content: "Hello!",
|
|
164
|
-
timestamp: new Date(),
|
|
165
|
-
type: "character"
|
|
166
|
-
});
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
##### `playMotion(motionType)`
|
|
170
|
-
Play a motion animation.
|
|
171
|
-
|
|
172
|
-
```typescript
|
|
173
|
-
renderer.playMotion("greeting"); // "greeting" | "happy" | "thinking" | "talk"
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
##### `animateExpression(motionType)`
|
|
177
|
-
Animate facial expression.
|
|
178
|
-
|
|
179
|
-
```typescript
|
|
180
|
-
renderer.animateExpression("happy");
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
##### `setRealtimeLipSync(enabled)`
|
|
184
|
-
Enable/disable real-time lip-sync mode.
|
|
185
|
-
|
|
186
|
-
```typescript
|
|
187
|
-
renderer.setRealtimeLipSync(true);
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
##### `updateRealtimeLipSyncRms(rms)`
|
|
191
|
-
Update mouth opening based on audio RMS value (0.0 - 1.0).
|
|
192
|
-
|
|
193
|
-
```typescript
|
|
194
|
-
renderer.updateRealtimeLipSyncRms(0.5); // Half open
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
##### `destroy()`
|
|
198
|
-
Clean up and destroy the renderer.
|
|
199
|
-
|
|
200
|
-
```typescript
|
|
201
|
-
await renderer.destroy();
|
|
202
|
-
```
|
|
203
|
-
|
|
204
|
-
### Types
|
|
205
|
-
|
|
206
|
-
```typescript
|
|
207
|
-
interface Live2DRendererOptions {
|
|
208
|
-
canvas?: HTMLCanvasElement;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
type MotionType = "greeting" | "happy" | "thinking" | "talk";
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
## Mouse Tracking
|
|
215
|
-
|
|
216
|
-
Mouse tracking is now managed by `RenderManager` from `@charivo/render-core`. The Live2D renderer implements the `MouseTrackable` interface, which allows RenderManager to automatically set up mouse tracking:
|
|
217
|
-
|
|
218
|
-
```typescript
|
|
219
|
-
import { createLive2DRenderer } from "@charivo/render-live2d";
|
|
220
|
-
import { createRenderManager } from "@charivo/render-core";
|
|
221
|
-
|
|
222
|
-
const renderer = createLive2DRenderer({ canvas });
|
|
223
|
-
|
|
224
|
-
// RenderManager handles mouse tracking setup
|
|
225
|
-
const renderManager = createRenderManager(renderer, {
|
|
226
|
-
canvas,
|
|
227
|
-
mouseTracking: "document" // Track across entire page, or "canvas" for canvas only
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
await renderManager.initialize(); // Mouse tracking is set up here
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
The model's eyes will follow the cursor, and you can tap/click to trigger animations.
|
|
234
|
-
|
|
235
|
-
## Model Requirements
|
|
236
|
-
|
|
237
|
-
Your Live2D model should:
|
|
238
|
-
- Be in Cubism SDK 4 or 5 format (`.model3.json`)
|
|
239
|
-
- Include motion groups: `Idle`, `TapBody` (optional)
|
|
240
|
-
- Include expressions: `normal`, `smile`, `surprised` (optional)
|
|
241
|
-
- Include hit areas: `Body` (optional, for tap interaction)
|
|
242
|
-
|
|
243
|
-
## Example Project Structure
|
|
244
|
-
|
|
245
|
-
```
|
|
246
|
-
public/
|
|
247
|
-
live2d/
|
|
248
|
-
hiyori/
|
|
249
|
-
runtime/
|
|
250
|
-
hiyori.model3.json ← Point to this
|
|
251
|
-
hiyori.moc3
|
|
252
|
-
textures/
|
|
253
|
-
texture_00.png
|
|
254
|
-
motions/
|
|
255
|
-
idle_01.motion3.json
|
|
256
|
-
tap_body_01.motion3.json
|
|
257
|
-
expressions/
|
|
258
|
-
smile.exp3.json
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
## Framework Integration
|
|
262
|
-
|
|
263
|
-
### React
|
|
264
|
-
|
|
265
|
-
```tsx
|
|
266
|
-
import { useEffect, useRef } from "react";
|
|
267
|
-
import { createLive2DRenderer } from "@charivo/render-live2d";
|
|
268
|
-
|
|
269
|
-
function Live2DComponent() {
|
|
270
|
-
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
271
|
-
|
|
272
|
-
useEffect(() => {
|
|
273
|
-
if (!canvasRef.current) return;
|
|
274
|
-
|
|
275
|
-
const renderer = createLive2DRenderer({ canvas: canvasRef.current });
|
|
276
|
-
|
|
277
|
-
renderer.initialize().then(() => {
|
|
278
|
-
return renderer.loadModel("/live2d/model.model3.json");
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
return () => {
|
|
282
|
-
renderer.destroy();
|
|
283
|
-
};
|
|
284
|
-
}, []);
|
|
285
|
-
|
|
286
|
-
return <canvas ref={canvasRef} width={300} height={300} />;
|
|
287
|
-
}
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
### Vue
|
|
291
|
-
|
|
292
|
-
```vue
|
|
293
|
-
<template>
|
|
294
|
-
<canvas ref="canvas" width="300" height="300"></canvas>
|
|
295
|
-
</template>
|
|
296
|
-
|
|
297
|
-
<script setup>
|
|
298
|
-
import { ref, onMounted, onUnmounted } from "vue";
|
|
299
|
-
import { createLive2DRenderer } from "@charivo/render-live2d";
|
|
300
|
-
|
|
301
|
-
const canvas = ref(null);
|
|
302
|
-
let renderer = null;
|
|
303
|
-
|
|
304
|
-
onMounted(async () => {
|
|
305
|
-
renderer = createLive2DRenderer({ canvas: canvas.value });
|
|
306
|
-
await renderer.initialize();
|
|
307
|
-
await renderer.loadModel("/live2d/model.model3.json");
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
onUnmounted(() => {
|
|
311
|
-
renderer?.destroy();
|
|
312
|
-
});
|
|
313
|
-
</script>
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
### Vanilla JS
|
|
317
|
-
|
|
318
|
-
```html
|
|
319
|
-
<!DOCTYPE html>
|
|
320
|
-
<html>
|
|
321
|
-
<head>
|
|
322
|
-
<script src="/live2dcubismcore.min.js"></script>
|
|
323
|
-
</head>
|
|
324
|
-
<body>
|
|
325
|
-
<canvas id="canvas" width="300" height="300"></canvas>
|
|
326
|
-
|
|
327
|
-
<script type="module">
|
|
328
|
-
import { createLive2DRenderer } from "@charivo/render-live2d";
|
|
329
|
-
|
|
330
|
-
const canvas = document.getElementById("canvas");
|
|
331
|
-
const renderer = createLive2DRenderer({ canvas });
|
|
332
|
-
|
|
333
|
-
await renderer.initialize();
|
|
334
|
-
await renderer.loadModel("/live2d/hiyori/hiyori.model3.json");
|
|
335
|
-
</script>
|
|
336
|
-
</body>
|
|
337
|
-
</html>
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
## Comparison with Raw SDK
|
|
341
|
-
|
|
342
|
-
### Before (Raw Cubism SDK)
|
|
343
|
-
|
|
344
|
-
```typescript
|
|
345
|
-
// 100+ lines of boilerplate code
|
|
346
|
-
import { CubismFramework, Option } from "@framework/live2dcubismframework";
|
|
347
|
-
import { LAppModel } from "./lappmodel";
|
|
348
|
-
import { LAppDelegate } from "./lappdelegate";
|
|
349
|
-
// ... many more imports
|
|
350
|
-
|
|
351
|
-
// Complex initialization
|
|
352
|
-
const option = new Option();
|
|
353
|
-
option.logFunction = console.log;
|
|
354
|
-
option.loggingLevel = LogLevel.Verbose;
|
|
355
|
-
CubismFramework.startUp(option);
|
|
356
|
-
CubismFramework.initialize();
|
|
357
|
-
|
|
358
|
-
// Manual GL context setup
|
|
359
|
-
const gl = canvas.getContext("webgl");
|
|
360
|
-
// ... GL setup code
|
|
361
|
-
|
|
362
|
-
// Manual model loading
|
|
363
|
-
const model = new LAppModel();
|
|
364
|
-
await model.loadAssets(modelPath);
|
|
365
|
-
// ... animation loop setup
|
|
366
|
-
// ... matrix calculations
|
|
367
|
-
// ... resize handling
|
|
368
|
-
// ... etc.
|
|
27
|
+
await renderManager.loadModel?.("/live2d/hiyori/hiyori.model3.json");
|
|
369
28
|
```
|
|
370
29
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
```typescript
|
|
374
|
-
import { createLive2DRenderer } from "@charivo/render-live2d";
|
|
375
|
-
|
|
376
|
-
const renderer = createLive2DRenderer({ canvas });
|
|
377
|
-
await renderer.initialize();
|
|
378
|
-
await renderer.loadModel("/live2d/model.model3.json");
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
**90% less code!** 🎉
|
|
382
|
-
|
|
383
|
-
## Troubleshooting
|
|
384
|
-
|
|
385
|
-
### Model not showing
|
|
386
|
-
|
|
387
|
-
1. Make sure `live2dcubismcore.min.js` is loaded before your app
|
|
388
|
-
2. Check that the model path is correct
|
|
389
|
-
3. Verify all model assets are accessible (textures, motions, etc.)
|
|
390
|
-
|
|
391
|
-
### Mouse tracking not working
|
|
392
|
-
|
|
393
|
-
Make sure you're using `RenderManager` with the correct options:
|
|
394
|
-
|
|
395
|
-
```typescript
|
|
396
|
-
const renderManager = createRenderManager(renderer, {
|
|
397
|
-
canvas: yourCanvas,
|
|
398
|
-
mouseTracking: "document" // or "canvas"
|
|
399
|
-
});
|
|
400
|
-
await renderManager.initialize(); // This sets up mouse tracking
|
|
401
|
-
```
|
|
402
|
-
|
|
403
|
-
Note: Mouse tracking requires using `RenderManager` from `@charivo/render-core`.
|
|
404
|
-
|
|
405
|
-
### Performance issues
|
|
30
|
+
## Public Surface
|
|
406
31
|
|
|
407
|
-
|
|
408
|
-
- Ensure only one renderer instance per canvas
|
|
409
|
-
- Call `destroy()` when component unmounts
|
|
32
|
+
The renderer exposes:
|
|
410
33
|
|
|
411
|
-
|
|
34
|
+
- `initialize()`
|
|
35
|
+
- `loadModel(modelPath)`
|
|
36
|
+
- `render(message, character?)`
|
|
37
|
+
- `destroy()`
|
|
38
|
+
- `getAvailableExpressions()`
|
|
39
|
+
- `getAvailableMotionGroups()`
|
|
40
|
+
- `playExpression(expressionId)`
|
|
41
|
+
- `playMotionByGroup(group, index)`
|
|
412
42
|
|
|
413
|
-
|
|
43
|
+
When used through `@charivo/render-core`, realtime lip sync, mouse tracking,
|
|
44
|
+
and emotion-driven motion selection are handled for you.
|
|
414
45
|
|
|
415
|
-
##
|
|
46
|
+
## License Note
|
|
416
47
|
|
|
417
|
-
|
|
418
|
-
|
|
48
|
+
This package vendors parts of the Live2D Cubism SDK for Web. Review the Live2D
|
|
49
|
+
license terms before shipping or republishing it.
|
package/dist/renderer.js
CHANGED
|
@@ -13513,7 +13513,6 @@ var LAppPal = class {
|
|
|
13513
13513
|
this.lastFrame = this.currentFrame;
|
|
13514
13514
|
}
|
|
13515
13515
|
static printMessage(message) {
|
|
13516
|
-
console.log(message);
|
|
13517
13516
|
}
|
|
13518
13517
|
static lastUpdate = Date.now();
|
|
13519
13518
|
static currentFrame = 0;
|
|
@@ -13532,6 +13531,7 @@ var LAppWavFileHandler = class {
|
|
|
13532
13531
|
if (typeof window === "undefined") return;
|
|
13533
13532
|
this.stop();
|
|
13534
13533
|
this.ensureContext();
|
|
13534
|
+
if (!this.audioContext) return;
|
|
13535
13535
|
const context = this.audioContext;
|
|
13536
13536
|
fetch(filePath).then((response) => response.arrayBuffer()).then((arrayBuffer) => context.decodeAudioData(arrayBuffer)).then((buffer) => {
|
|
13537
13537
|
this.audioBufferSourceNode = context.createBufferSource();
|
|
@@ -13628,7 +13628,6 @@ var LAppModel = class extends CubismUserModel {
|
|
|
13628
13628
|
CubismDefaultParameterId.ParamBodyAngleX
|
|
13629
13629
|
);
|
|
13630
13630
|
async loadAssets(modelPath, host) {
|
|
13631
|
-
console.log(`\u{1F3AF} LAppModel: Loading model from ${modelPath}`);
|
|
13632
13631
|
this.host = host;
|
|
13633
13632
|
const separator = modelPath.lastIndexOf("/") + 1;
|
|
13634
13633
|
this.modelHomeDir = separator > 0 ? modelPath.substring(0, separator) : "";
|
|
@@ -13941,14 +13940,12 @@ var LAppModel = class extends CubismUserModel {
|
|
|
13941
13940
|
}
|
|
13942
13941
|
setupLipSyncIds(setting) {
|
|
13943
13942
|
const count = setting.getLipSyncParameterCount();
|
|
13944
|
-
console.log(`\u{1F3AF} LAppModel: Setting up ${count} lip sync parameters`);
|
|
13945
13943
|
for (let i = 0; i < count; i++) {
|
|
13946
13944
|
const paramId = setting.getLipSyncParameterId(i);
|
|
13947
13945
|
this.lipSyncIds.pushBack(paramId);
|
|
13948
|
-
console.log(`\u{1F3AF} LAppModel: Lip sync parameter ${i}:`, paramId);
|
|
13949
13946
|
}
|
|
13950
13947
|
if (count === 0) {
|
|
13951
|
-
console.warn(
|
|
13948
|
+
console.warn("Live2D model has no lip sync parameters.");
|
|
13952
13949
|
}
|
|
13953
13950
|
}
|
|
13954
13951
|
setupLayout(setting) {
|