@hackbuild/live2d 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +130 -2
- package/dist/core.d.ts +102 -0
- package/dist/core.js +56 -0
- package/dist/files.d.ts +72 -0
- package/dist/files.js +186 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/live2d.iife.js +903 -0
- package/dist/model.d.ts +56 -0
- package/dist/model.js +269 -0
- package/dist/motion.d.ts +90 -0
- package/dist/motion.js +168 -0
- package/dist/render.d.ts +48 -0
- package/dist/render.js +204 -0
- package/dist/single.d.ts +1 -0
- package/dist/single.js +2 -0
- package/how.ts +25 -0
- package/llms.md +367 -0
- package/package.json +34 -4
package/dist/render.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A WebGL renderer for a Cubism model's drawables, written for this library:
|
|
3
|
+
* each drawable is a textured mesh drawn in the model's render order with
|
|
4
|
+
* its opacity, its multiply and screen colours, and one of the three Cubism
|
|
5
|
+
* blend modes (normal, additive, multiplicative) on premultiplied alpha. A
|
|
6
|
+
* drawable with clipping masks is drawn through the stencil buffer: its mask
|
|
7
|
+
* meshes write the stencil where their alpha passes half, and the drawable
|
|
8
|
+
* draws where the stencil is set, or where it is not for an inverted mask.
|
|
9
|
+
*/
|
|
10
|
+
const VS = `attribute vec2 a_pos;
|
|
11
|
+
attribute vec2 a_uv;
|
|
12
|
+
uniform mat3 u_m;
|
|
13
|
+
varying vec2 v_uv;
|
|
14
|
+
void main() {
|
|
15
|
+
vec3 p = u_m * vec3(a_pos, 1.0);
|
|
16
|
+
gl_Position = vec4(p.xy, 0.0, 1.0);
|
|
17
|
+
v_uv = vec2(a_uv.x, 1.0 - a_uv.y);
|
|
18
|
+
}`;
|
|
19
|
+
const FS = `precision mediump float;
|
|
20
|
+
varying vec2 v_uv;
|
|
21
|
+
uniform sampler2D u_tex;
|
|
22
|
+
uniform float u_opacity;
|
|
23
|
+
uniform vec4 u_mul;
|
|
24
|
+
uniform vec4 u_scr;
|
|
25
|
+
uniform float u_mask;
|
|
26
|
+
void main() {
|
|
27
|
+
vec4 c = texture2D(u_tex, v_uv);
|
|
28
|
+
if (u_mask > 0.5) { if (c.a < 0.5) discard; gl_FragColor = vec4(0.0); return; }
|
|
29
|
+
vec3 rgb = c.a > 0.0 ? c.rgb / c.a : vec3(0.0);
|
|
30
|
+
rgb *= u_mul.rgb;
|
|
31
|
+
rgb = rgb + u_scr.rgb - rgb * u_scr.rgb;
|
|
32
|
+
float a = c.a * u_opacity;
|
|
33
|
+
gl_FragColor = vec4(rgb * a, a);
|
|
34
|
+
}`;
|
|
35
|
+
/** Draws one Cubism model into a WebGL context that has a stencil buffer. */
|
|
36
|
+
export class Renderer {
|
|
37
|
+
gl;
|
|
38
|
+
core;
|
|
39
|
+
model;
|
|
40
|
+
prog;
|
|
41
|
+
pos;
|
|
42
|
+
uv;
|
|
43
|
+
idx;
|
|
44
|
+
tex = [];
|
|
45
|
+
loc = {};
|
|
46
|
+
aPos;
|
|
47
|
+
aUv;
|
|
48
|
+
order = [];
|
|
49
|
+
constructor(gl, core, model) {
|
|
50
|
+
this.gl = gl;
|
|
51
|
+
this.core = core;
|
|
52
|
+
this.model = model;
|
|
53
|
+
const sh = (type, src) => {
|
|
54
|
+
const s = gl.createShader(type);
|
|
55
|
+
gl.shaderSource(s, src);
|
|
56
|
+
gl.compileShader(s);
|
|
57
|
+
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS))
|
|
58
|
+
throw new Error(`live2d shader: ${gl.getShaderInfoLog(s)}`);
|
|
59
|
+
return s;
|
|
60
|
+
};
|
|
61
|
+
this.prog = gl.createProgram();
|
|
62
|
+
gl.attachShader(this.prog, sh(gl.VERTEX_SHADER, VS));
|
|
63
|
+
gl.attachShader(this.prog, sh(gl.FRAGMENT_SHADER, FS));
|
|
64
|
+
gl.linkProgram(this.prog);
|
|
65
|
+
if (!gl.getProgramParameter(this.prog, gl.LINK_STATUS))
|
|
66
|
+
throw new Error(`live2d program: ${gl.getProgramInfoLog(this.prog)}`);
|
|
67
|
+
for (const n of ["u_m", "u_tex", "u_opacity", "u_mul", "u_scr", "u_mask"])
|
|
68
|
+
this.loc[n] = gl.getUniformLocation(this.prog, n);
|
|
69
|
+
this.aPos = gl.getAttribLocation(this.prog, "a_pos");
|
|
70
|
+
this.aUv = gl.getAttribLocation(this.prog, "a_uv");
|
|
71
|
+
this.pos = gl.createBuffer();
|
|
72
|
+
this.uv = gl.createBuffer();
|
|
73
|
+
this.idx = gl.createBuffer();
|
|
74
|
+
}
|
|
75
|
+
/** Uploads the model's textures, premultiplied, with mipmaps. */
|
|
76
|
+
async textures(images) {
|
|
77
|
+
const gl = this.gl;
|
|
78
|
+
for (const b of images) {
|
|
79
|
+
// an ImageBitmap ignores the unpack flags: it is premultiplied as it is made
|
|
80
|
+
const bmp = await createImageBitmap(b, { premultiplyAlpha: "premultiply", colorSpaceConversion: "none" });
|
|
81
|
+
const t = gl.createTexture();
|
|
82
|
+
gl.bindTexture(gl.TEXTURE_2D, t);
|
|
83
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bmp);
|
|
84
|
+
gl.generateMipmap(gl.TEXTURE_2D);
|
|
85
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
|
|
86
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
87
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
88
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
89
|
+
bmp.close();
|
|
90
|
+
this.tex.push(t);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
mesh(i) {
|
|
94
|
+
const gl = this.gl, d = this.model.drawables;
|
|
95
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.pos);
|
|
96
|
+
gl.bufferData(gl.ARRAY_BUFFER, d.vertexPositions[i], gl.DYNAMIC_DRAW);
|
|
97
|
+
gl.enableVertexAttribArray(this.aPos);
|
|
98
|
+
gl.vertexAttribPointer(this.aPos, 2, gl.FLOAT, false, 0, 0);
|
|
99
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.uv);
|
|
100
|
+
gl.bufferData(gl.ARRAY_BUFFER, d.vertexUvs[i], gl.DYNAMIC_DRAW);
|
|
101
|
+
gl.enableVertexAttribArray(this.aUv);
|
|
102
|
+
gl.vertexAttribPointer(this.aUv, 2, gl.FLOAT, false, 0, 0);
|
|
103
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.idx);
|
|
104
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, d.indices[i], gl.DYNAMIC_DRAW);
|
|
105
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
106
|
+
gl.bindTexture(gl.TEXTURE_2D, this.tex[d.textureIndices[i]] ?? null);
|
|
107
|
+
gl.drawElements(gl.TRIANGLES, d.indexCounts[i], gl.UNSIGNED_SHORT, 0);
|
|
108
|
+
}
|
|
109
|
+
/** Draws the model with the matrix; the caller has run `model.update()`. */
|
|
110
|
+
draw(m) {
|
|
111
|
+
const gl = this.gl, d = this.model.drawables, U = this.core.Utils;
|
|
112
|
+
gl.useProgram(this.prog);
|
|
113
|
+
gl.uniformMatrix3fv(this.loc.u_m, false, m);
|
|
114
|
+
gl.uniform1i(this.loc.u_tex, 0);
|
|
115
|
+
gl.disable(gl.CULL_FACE);
|
|
116
|
+
gl.disable(gl.DEPTH_TEST);
|
|
117
|
+
gl.enable(gl.BLEND);
|
|
118
|
+
if (this.order.length !== d.count)
|
|
119
|
+
this.order = Array.from({ length: d.count }, (_, i) => i);
|
|
120
|
+
this.order.sort((a, b) => d.renderOrders[a] - d.renderOrders[b]);
|
|
121
|
+
const visible = (i) => U.hasIsVisibleBit(d.dynamicFlags[i]) && d.opacities[i] > 0.001;
|
|
122
|
+
for (const i of this.order) {
|
|
123
|
+
if (!visible(i) || d.indexCounts[i] === 0)
|
|
124
|
+
continue;
|
|
125
|
+
const masks = d.maskCounts[i] > 0 ? d.masks[i] : null;
|
|
126
|
+
if (masks) {
|
|
127
|
+
gl.enable(gl.STENCIL_TEST);
|
|
128
|
+
gl.clear(gl.STENCIL_BUFFER_BIT);
|
|
129
|
+
gl.stencilFunc(gl.ALWAYS, 1, 0xff);
|
|
130
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.REPLACE);
|
|
131
|
+
gl.colorMask(false, false, false, false);
|
|
132
|
+
gl.uniform1f(this.loc.u_mask, 1);
|
|
133
|
+
for (let k = 0; k < d.maskCounts[i]; k++) {
|
|
134
|
+
const mi = masks[k];
|
|
135
|
+
if (mi >= 0 && d.indexCounts[mi] > 0)
|
|
136
|
+
this.mesh(mi);
|
|
137
|
+
}
|
|
138
|
+
gl.colorMask(true, true, true, true);
|
|
139
|
+
gl.uniform1f(this.loc.u_mask, 0);
|
|
140
|
+
gl.stencilFunc(U.hasIsInvertedMaskBit(d.constantFlags[i]) ? gl.NOTEQUAL : gl.EQUAL, 1, 0xff);
|
|
141
|
+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
|
|
142
|
+
}
|
|
143
|
+
else
|
|
144
|
+
gl.disable(gl.STENCIL_TEST);
|
|
145
|
+
const f = d.constantFlags[i];
|
|
146
|
+
if (U.hasBlendAdditiveBit(f))
|
|
147
|
+
gl.blendFunc(gl.ONE, gl.ONE);
|
|
148
|
+
else if (U.hasBlendMultiplicativeBit(f))
|
|
149
|
+
gl.blendFunc(gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA);
|
|
150
|
+
else
|
|
151
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
152
|
+
gl.uniform1f(this.loc.u_opacity, d.opacities[i]);
|
|
153
|
+
const mc = d.multiplyColors, sc = d.screenColors;
|
|
154
|
+
gl.uniform4f(this.loc.u_mul, mc ? mc[i * 4] : 1, mc ? mc[i * 4 + 1] : 1, mc ? mc[i * 4 + 2] : 1, 1);
|
|
155
|
+
gl.uniform4f(this.loc.u_scr, sc ? sc[i * 4] : 0, sc ? sc[i * 4 + 1] : 0, sc ? sc[i * 4 + 2] : 0, 1);
|
|
156
|
+
this.mesh(i);
|
|
157
|
+
}
|
|
158
|
+
gl.disable(gl.STENCIL_TEST);
|
|
159
|
+
}
|
|
160
|
+
dispose() {
|
|
161
|
+
const gl = this.gl;
|
|
162
|
+
for (const t of this.tex)
|
|
163
|
+
gl.deleteTexture(t);
|
|
164
|
+
gl.deleteBuffer(this.pos);
|
|
165
|
+
gl.deleteBuffer(this.uv);
|
|
166
|
+
gl.deleteBuffer(this.idx);
|
|
167
|
+
gl.deleteProgram(this.prog);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/** The bounds of the model's visible vertices, in model units. */
|
|
171
|
+
export function modelBounds(model, core) {
|
|
172
|
+
const d = model.drawables;
|
|
173
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
174
|
+
for (let i = 0; i < d.count; i++) {
|
|
175
|
+
if (!core.Utils.hasIsVisibleBit(d.dynamicFlags[i]) || d.opacities[i] <= 0.001)
|
|
176
|
+
continue;
|
|
177
|
+
const v = d.vertexPositions[i];
|
|
178
|
+
for (let k = 0; k < v.length; k += 2) {
|
|
179
|
+
const x = v[k], y = v[k + 1];
|
|
180
|
+
if (x < minX)
|
|
181
|
+
minX = x;
|
|
182
|
+
if (x > maxX)
|
|
183
|
+
maxX = x;
|
|
184
|
+
if (y < minY)
|
|
185
|
+
minY = y;
|
|
186
|
+
if (y > maxY)
|
|
187
|
+
maxY = y;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (!Number.isFinite(minX)) {
|
|
191
|
+
const c = model.canvasinfo, w = c.CanvasWidth / c.PixelsPerUnit, h = c.CanvasHeight / c.PixelsPerUnit;
|
|
192
|
+
return { minX: -w / 2, minY: -h / 2, maxX: w / 2, maxY: h / 2 };
|
|
193
|
+
}
|
|
194
|
+
return { minX, minY, maxX, maxY };
|
|
195
|
+
}
|
|
196
|
+
/** A matrix fitting a box of model units into a canvas of w by h pixels, keeping the aspect, with a margin. */
|
|
197
|
+
export function fitMatrix(box, w, h, margin = 0.04) {
|
|
198
|
+
const bw = box.maxX - box.minX, bh = box.maxY - box.minY;
|
|
199
|
+
// one scale that fits the box both ways in clip units, then x corrected for the canvas aspect
|
|
200
|
+
const sy = Math.min((2 * (1 - margin)) / bh, ((2 * (1 - margin)) / bw) * (w / h));
|
|
201
|
+
const sx = sy * (h / w);
|
|
202
|
+
const cx = (box.minX + box.maxX) / 2, cy = (box.minY + box.maxY) / 2;
|
|
203
|
+
return new Float32Array([sx, 0, 0, 0, sy, 0, -cx * sx, -cy * sy, 1]);
|
|
204
|
+
}
|
package/dist/single.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./index.js";
|
package/dist/single.js
ADDED
package/how.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// how this works: the fold of the package page and of the suite tool. Plain data, no imports;
|
|
2
|
+
// shape in tools/how.d.ts, checked by tsconfig.how.json, shipped in the tarball, never imported
|
|
3
|
+
// by the runtime entry. Text from the README.
|
|
4
|
+
const how: HowDoc = {
|
|
5
|
+
what: "live2d cubism models in a canvas: open a zip, folder or url, then motions, expressions, blink, breath, look and lip sync, drawn by a webgl renderer written here.",
|
|
6
|
+
how: [
|
|
7
|
+
"the files are read from a zip, a dropped folder or a url: the .model3.json names the .moc3, the textures, motions and expressions; files it names that are not there are listed, and the model still opens.",
|
|
8
|
+
"live2d's own cubism core reads the .moc3. it is never bundled: the first open adds it to the page from cubism.live2d.com, under live2d's licence, which the page is told.",
|
|
9
|
+
"each frame starts from the parameters' defaults, then motions (crossfaded), the expression, the blink, breath, where it looks and the mouth level; the renderer draws the meshes in order with their blend modes and clipping masks.",
|
|
10
|
+
],
|
|
11
|
+
packages: ["@hackbuild/live2d"],
|
|
12
|
+
install: "npm install @hackbuild/live2d",
|
|
13
|
+
today: "git clone https://github.com/hackbuild/hackbuild-motion && cd hackbuild-motion && npm ci && npm run build && npm pack -w @hackbuild/live2d",
|
|
14
|
+
code: `import { createLive2D, filesFrom, modelFromFiles } from "@hackbuild/live2d";
|
|
15
|
+
|
|
16
|
+
const source = await modelFromFiles(await filesFrom(input.files));
|
|
17
|
+
const model = await createLive2D(canvas, source, { frame: "bust" });
|
|
18
|
+
model.expression("smile");
|
|
19
|
+
model.look(0.3, 0.1);
|
|
20
|
+
model.mouth(level); // 0 to 1, each frame, from a lip sync level`,
|
|
21
|
+
lang: "ts",
|
|
22
|
+
links: { page: "https://hackbuild.github.io/hackbuild-motion/live2d/", source: "https://github.com/hackbuild/hackbuild-motion/tree/main/packages/live2d" },
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default how;
|
package/llms.md
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
# @hackbuild/live2d
|
|
2
|
+
|
|
3
|
+
> Show a Live2D Cubism model in a canvas and drive its expressions, motions, gaze and mouth.
|
|
4
|
+
|
|
5
|
+
Generated by tools/llms.mjs from the TypeDoc JSON, the README and the examples of hackbuild-motion. Human docs: https://hackbuild.github.io/hackbuild-motion/docs/live2d/
|
|
6
|
+
|
|
7
|
+
## Purpose
|
|
8
|
+
|
|
9
|
+
- Opens a Cubism 3 or later model from a zip, a dropped folder or the URL of its `.model3.json`, and lists the files it names that are missing instead of failing.
|
|
10
|
+
- Draws it with a WebGL renderer written for this package: render order, opacity, multiply and screen colours, the normal, additive and multiplicative blend modes, and clipping masks through the stencil buffer.
|
|
11
|
+
- Plays `.motion3.json` motions (linear, bezier, stepped and inverse stepped segments, fades, loops, part opacity and pose groups) and `.exp3.json` expressions (add, multiply, overwrite).
|
|
12
|
+
- Blinks, breathes and plays the idle group on its own; turns toward a point; opens the mouth from a level you pass, such as a lip sync output.
|
|
13
|
+
- Leaves Live2D's own runtime, the Cubism Core, outside the package: it loads from cubism.live2d.com when a model opens, under Live2D's licence.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @hackbuild/live2d
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
It has no dependencies. The Cubism Core is Live2D's and ships outside npm: `loadCubismCore` adds Live2D's script to the page from `https://cubism.live2d.com/sdk-web/cubismcore/live2dcubismcore.min.js` the first time a model opens, and `onLicence` receives the line to show. Pass `url` to load it from your own copy, or `core` when it is already on the page.
|
|
22
|
+
|
|
23
|
+
## Exports
|
|
24
|
+
|
|
25
|
+
### @hackbuild/live2d
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
function blendExpression(current: number, p: { blend: ExpressionBlend; value: number }, w: number): number
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
One parameter value with an expression applied at weight w.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
class Blinker {
|
|
35
|
+
constructor(rng: () => number, now?: number);
|
|
36
|
+
open(now: number): number;
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
An eye blink in the rhythm people blink: open, then about every 2 to 6 s a close of 0.1 s, a hold of 0.05 s and an open of 0.15 s (inferred from blink studies).
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
interface CoreModel {
|
|
44
|
+
canvasinfo: { CanvasHeight: number; CanvasOriginX: number; CanvasOriginY: number; CanvasWidth: number; PixelsPerUnit: number };
|
|
45
|
+
drawables: CoreDrawables;
|
|
46
|
+
parameters: CoreParameters;
|
|
47
|
+
parts: CoreParts;
|
|
48
|
+
release(): void;
|
|
49
|
+
update(): void;
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
One model instance from a moc.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
interface CoreOptions {
|
|
57
|
+
onLicence?: (text: string) => void;
|
|
58
|
+
url?: string;
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Options for loadCubismCore.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
function coreVersion(core: CubismCore): string
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The core's version as major.minor.patch, from its packed number.
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
function createLive2D(canvas: HTMLCanvasElement, source: ModelSource, o?: Live2DOptions): Promise<Live2DModel>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Opens a model source on a canvas and starts it.
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const CUBISM_CORE_LICENCE: "live2d cubism core, under the live2d proprietary software license agreement (https://www.live2d.com/eula/live2d-proprietary-software-license-agreement_en.html). it loads from cubism.live2d.com and is not part of this library."
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The line a page shows the first time the core loads.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
const CUBISM_CORE_URL: "https://cubism.live2d.com/sdk-web/cubismcore/live2dcubismcore.min.js"
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Where Live2D serves the Cubism Core for the web.
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
interface CubismCore {
|
|
90
|
+
Moc: { prototype: unknown; fromArrayBuffer(buffer: ArrayBuffer): CoreMoc | null } & { hasMocConsistency?(buffer: ArrayBuffer): boolean };
|
|
91
|
+
Model: { fromMoc(moc: CoreMoc): CoreModel | null };
|
|
92
|
+
Utils: { hasBlendAdditiveBit(flags: number): boolean; hasBlendMultiplicativeBit(flags: number): boolean; hasIsInvertedMaskBit(flags: number): boolean; hasIsVisibleBit(flags: number): boolean };
|
|
93
|
+
Version: { csmGetVersion(): number };
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The `Live2DCubismCore` global the core script defines.
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
interface Curve {
|
|
101
|
+
fadeIn: number | null;
|
|
102
|
+
fadeOut: number | null;
|
|
103
|
+
id: string;
|
|
104
|
+
segs: Seg[];
|
|
105
|
+
t0: number;
|
|
106
|
+
target: CurveTarget;
|
|
107
|
+
v0: number;
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
One curve: its target, id, optional own fades and its segments.
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
function curveValue(c: Curve, t: number, restricted?: boolean): number
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
A curve's value at time t (seconds); before its first point the first value, after its last the last.
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
function ease(x: number): number
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Cubism's fade curve: a half cosine from 0 to 1.
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
interface Expression {
|
|
127
|
+
fadeIn: number;
|
|
128
|
+
fadeOut: number;
|
|
129
|
+
params: { blend: ExpressionBlend; id: string; value: number }[];
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
A parsed expression.
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
type ExpressionBlend = "Add" | "Multiply" | "Overwrite"
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
How an expression parameter blends.
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
function filesFrom(list: Iterable<File>): Promise<ModelFiles>
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Files from a drop or a picker: a zip is unpacked, a folder keeps its relative paths.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
function findModel(files: ModelFiles): string
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
The model settings file among the files: the .model3.json nearest the root.
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
function fitMatrix(box: { maxX: number; maxY: number; minX: number; minY: number }, w: number, h: number, margin?: number): Mat3
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
A matrix fitting a box of model units into a canvas of w by h pixels, keeping the aspect, with a margin.
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
function joinPath(base: string, ref: string): string
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Joins a reference onto the folder of the file that names it, resolving `..` and `.`.
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
class Live2DError extends Error {
|
|
167
|
+
constructor(message: string);
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
A Live2D model the library cannot open, with the reason in words a page can show.
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
type Live2DFrame = "full" | "bust"
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
How much of the model the canvas shows: all of it, or the head and shoulders.
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
interface Live2DModel {
|
|
181
|
+
readonly coreVersion: string;
|
|
182
|
+
readonly expressions: string[];
|
|
183
|
+
readonly missing: string[];
|
|
184
|
+
readonly motions: Record<string, number>;
|
|
185
|
+
readonly name: string;
|
|
186
|
+
readonly parameters: string[];
|
|
187
|
+
dispose(): void;
|
|
188
|
+
expression(name: string | null): boolean;
|
|
189
|
+
frame(mode: Live2DFrame): void;
|
|
190
|
+
look(x: number, y: number): void;
|
|
191
|
+
motion(group: string, index?: number): boolean;
|
|
192
|
+
mouth(open: number): void;
|
|
193
|
+
resize(): void;
|
|
194
|
+
set(id: string, value: number | null): void;
|
|
195
|
+
update(dt: number): void;
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
A model on a canvas, with its controls.
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
interface Live2DOptions extends CoreOptions {
|
|
203
|
+
autoplay?: boolean;
|
|
204
|
+
core?: CubismCore;
|
|
205
|
+
frame?: Live2DFrame;
|
|
206
|
+
loop?: boolean;
|
|
207
|
+
onLicence?: (text: string) => void;
|
|
208
|
+
rng?: () => number;
|
|
209
|
+
url?: string;
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Options for createLive2D.
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
function loadCubismCore(o?: CoreOptions): Promise<CubismCore>
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Adds Live2D's Cubism Core to the page once and resolves to it; a core already on the page is used as is.
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
interface Model3 {
|
|
223
|
+
FileReferences: { Expressions?: { File: string; Name: string }[]; Moc: string; Motions?: Record<string, { FadeInTime?: number; FadeOutTime?: number; File: string; Sound?: string }[]>; Physics?: string; Pose?: string; Textures: string[] };
|
|
224
|
+
Groups?: { Ids: string[]; Name: string; Target: string }[];
|
|
225
|
+
Version?: number;
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
The parts of a .model3.json this library reads.
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
function modelBounds(model: CoreModel, core: CubismCore): { maxX: number; maxY: number; minX: number; minY: number }
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
The bounds of the model's visible vertices, in model units.
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
type ModelFiles = Map<string, Blob>
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
A model's files by path, relative to the archive or folder root, with forward slashes.
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
function modelFromFiles(files: ModelFiles, path?: string): Promise<ModelSource>
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
A model from dropped files: the settings, every file it names that is present, and the rest in `missing`.
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
function modelFromUrl(url: string, f?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>): Promise<ModelSource>
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
A model by the URL of its .model3.json; every file it names is fetched beside it.
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
interface ModelSource {
|
|
257
|
+
expressions: { json: unknown; name: string }[];
|
|
258
|
+
missing: string[];
|
|
259
|
+
moc: ArrayBuffer;
|
|
260
|
+
motions: MotionRef[];
|
|
261
|
+
name: string;
|
|
262
|
+
physics: unknown;
|
|
263
|
+
pose: unknown;
|
|
264
|
+
settings: Model3;
|
|
265
|
+
textures: Blob[];
|
|
266
|
+
}
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
A model ready to open: the settings, the moc bytes, textures, motions, expressions and what was missing.
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
interface Motion {
|
|
273
|
+
curves: Curve[];
|
|
274
|
+
duration: number;
|
|
275
|
+
fadeIn: number;
|
|
276
|
+
fadeOut: number;
|
|
277
|
+
loop: boolean;
|
|
278
|
+
restricted: boolean;
|
|
279
|
+
}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
A parsed motion.
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
interface MotionFrame {
|
|
286
|
+
groups: Map<string, number>;
|
|
287
|
+
params: Map<string, { value: number; weight: number }>;
|
|
288
|
+
parts: Map<string, number>;
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
What a motion asks for at one moment: parameter values, part opacities and group factors, each with its weight.
|
|
293
|
+
|
|
294
|
+
```ts
|
|
295
|
+
interface MotionRef {
|
|
296
|
+
fadeIn?: number;
|
|
297
|
+
fadeOut?: number;
|
|
298
|
+
file: string;
|
|
299
|
+
group: string;
|
|
300
|
+
index: number;
|
|
301
|
+
json: unknown;
|
|
302
|
+
}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
One motion as the model names it, with its parsed file or null when it is missing.
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
function parseExpression(json: unknown): Expression
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
Parses a .exp3.json; fades default to 1 s.
|
|
312
|
+
|
|
313
|
+
```ts
|
|
314
|
+
function parseMotion(json: unknown, o?: { fadeIn?: number; fadeOut?: number }): Motion
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
Parses a .motion3.json; fades default to the motion's own, then the model's entry, then 1 s and 1 s.
|
|
318
|
+
|
|
319
|
+
```ts
|
|
320
|
+
function readZip(buffer: ArrayBuffer): Promise<ModelFiles>
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
Reads a zip into files: stored and deflated entries, by its central directory.
|
|
324
|
+
|
|
325
|
+
```ts
|
|
326
|
+
function sampleMotion(m: Motion, elapsed: number, weight?: number, outAt?: number | null): MotionFrame
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
Samples a motion `elapsed` seconds after it started, with an overall weight; `outAt` is when its fade out began (seconds after start), or null.
|
|
330
|
+
|
|
331
|
+
## Examples
|
|
332
|
+
|
|
333
|
+
### quick start
|
|
334
|
+
|
|
335
|
+
```ts
|
|
336
|
+
import { createLive2D, filesFrom, modelFromFiles } from "@hackbuild/live2d";
|
|
337
|
+
|
|
338
|
+
const canvas = document.querySelector<HTMLCanvasElement>("canvas")!;
|
|
339
|
+
const input = document.querySelector<HTMLInputElement>("input[type=file]")!;
|
|
340
|
+
|
|
341
|
+
// a .zip of a cubism 3 or later model, or its folder (input webkitdirectory)
|
|
342
|
+
input.addEventListener("change", async () => {
|
|
343
|
+
const source = await modelFromFiles(await filesFrom(input.files ?? []));
|
|
344
|
+
const model = await createLive2D(canvas, source, { frame: "bust", onLicence: (line) => console.info(line) });
|
|
345
|
+
model.expression(model.expressions[0] ?? null);
|
|
346
|
+
model.look(0.3, 0.1);
|
|
347
|
+
let t = 0;
|
|
348
|
+
setInterval(() => model.mouth(0.5 + 0.5 * Math.sin((t += 0.4))), 60);
|
|
349
|
+
});
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
## Mistakes to avoid
|
|
353
|
+
|
|
354
|
+
- Do not bundle or copy live2dcubismcore.min.js into an app's repository; it is Live2D's, under its own licence. Let `loadCubismCore` add it from cubism.live2d.com, or pass `url` to a copy you are licensed to serve.
|
|
355
|
+
- Do not pass Cubism 2 models (`.moc`, `model.json`); this package reads Cubism 3 and later (`.moc3`, `.model3.json`).
|
|
356
|
+
- Do not flatten a model's folder when zipping it; references in the `.model3.json` are relative paths and must still resolve.
|
|
357
|
+
- Call `resize()` after the canvas changes size on screen, or the model draws stretched.
|
|
358
|
+
- Call `dispose()` before opening another model on the same canvas; each model holds its textures and buffers.
|
|
359
|
+
- Pass the mouth level every frame while speaking and 0 after; `mouth()` holds its last value.
|
|
360
|
+
|
|
361
|
+
## Errors this package throws
|
|
362
|
+
|
|
363
|
+
- `this is a cubism 2 model (.moc)` (the model is from Cubism 2): export it from a Cubism 3 or later editor, or use a .moc3 model
|
|
364
|
+
- `no live2d model here` (the drop holds no `.model3.json`): drop the zip or the whole folder
|
|
365
|
+
- `the moc ... that the settings name is missing` (the `.moc3` is not where the settings say): keep the folder structure the model came in
|
|
366
|
+
- `could not read ...moc3: it may be newer than the core` (the model needs a newer Cubism Core): load a newer core with `url`
|
|
367
|
+
- `the cubism core did not load from cubism.live2d.com` (offline, or the site is blocked): serve the core yourself and pass `url`
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackbuild/live2d",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Live2D Cubism models in a canvas: open a zip, folder or URL, then motions, expressions, blink, breath, look and lip sync on a WebGL renderer written here. Live2D's own Cubism Core loads from cubism.live2d.com at run time under its licence and is never bundled.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Moheeb Zara <hackbuildvideo@gmail.com>",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/hackbuild/hackbuild-motion.git",
|
|
11
|
+
"directory": "packages/live2d"
|
|
12
|
+
},
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": "^22.18.0 || >=24.11.0"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./iife": "./dist/live2d.iife.js"
|
|
23
|
+
},
|
|
24
|
+
"unpkg": "dist/live2d.iife.js",
|
|
25
|
+
"jsdelivr": "dist/live2d.iife.js",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"README.md",
|
|
29
|
+
"llms.md",
|
|
30
|
+
"LICENSE",
|
|
31
|
+
"how.ts"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "node ../../tools/build-package.mjs live2d"
|
|
35
|
+
}
|
|
36
|
+
}
|