-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Cubism2InternalModel.ts
332 lines (255 loc) · 10.7 KB
/
Cubism2InternalModel.ts
1
2
3
4
5
6
7
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
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
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
105
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import type { InternalModelOptions } from "@/cubism-common";
import type { CommonHitArea, CommonLayout } from "@/cubism-common/InternalModel";
import { InternalModel } from "@/cubism-common/InternalModel";
import { logger } from "../utils";
import type { Cubism2ModelSettings } from "./Cubism2ModelSettings";
import { Cubism2MotionManager } from "./Cubism2MotionManager";
import { Live2DEyeBlink } from "./Live2DEyeBlink";
import type { Live2DPhysics } from "./Live2DPhysics";
import type { Live2DPose } from "./Live2DPose";
import { clamp } from "@/utils";
// prettier-ignore
const tempMatrixArray = new Float32Array([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
]);
export class Cubism2InternalModel extends InternalModel {
settings: Cubism2ModelSettings;
coreModel: Live2DModelWebGL;
motionManager: Cubism2MotionManager;
eyeBlink?: Live2DEyeBlink;
declare physics?: Live2DPhysics;
declare pose?: Live2DPose;
// parameter indices, cached for better performance
eyeballXParamIndex: number;
eyeballYParamIndex: number;
angleXParamIndex: number;
angleYParamIndex: number;
angleZParamIndex: number;
bodyAngleXParamIndex: number;
breathParamIndex: number;
// mouthFormIndex: number;
textureFlipY = true;
lipSync = true;
/**
* Number of the drawables in this model.
*/
drawDataCount = 0;
/**
* If true, the face culling will always be disabled when drawing the model,
* regardless of the model's internal flags.
*/
disableCulling = false;
private hasDrawn = false;
constructor(
coreModel: Live2DModelWebGL,
settings: Cubism2ModelSettings,
options?: InternalModelOptions,
) {
super();
this.coreModel = coreModel;
this.settings = settings;
this.motionManager = new Cubism2MotionManager(settings, options);
this.eyeBlink = new Live2DEyeBlink(coreModel);
this.eyeballXParamIndex = coreModel.getParamIndex("PARAM_EYE_BALL_X");
this.eyeballYParamIndex = coreModel.getParamIndex("PARAM_EYE_BALL_Y");
this.angleXParamIndex = coreModel.getParamIndex("PARAM_ANGLE_X");
this.angleYParamIndex = coreModel.getParamIndex("PARAM_ANGLE_Y");
this.angleZParamIndex = coreModel.getParamIndex("PARAM_ANGLE_Z");
this.bodyAngleXParamIndex = coreModel.getParamIndex("PARAM_BODY_ANGLE_X");
this.breathParamIndex = coreModel.getParamIndex("PARAM_BREATH");
// this.mouthFormIndex = coreModel.getParamIndex("PARAM_MOUTH_FORM");
this.init();
}
protected init() {
super.init();
if (this.settings.initParams) {
this.settings.initParams.forEach(({ id, value }) =>
this.coreModel.setParamFloat(id, value),
);
}
if (this.settings.initOpacities) {
this.settings.initOpacities.forEach(({ id, value }) =>
this.coreModel.setPartsOpacity(id, value),
);
}
this.coreModel.saveParam();
const arr = this.coreModel.getModelContext()._$aS;
if ((arr as unknown[])?.length) {
this.drawDataCount = (arr as unknown[]).length;
}
let culling = this.coreModel.drawParamWebGL.culling;
Object.defineProperty(this.coreModel.drawParamWebGL, "culling", {
set: (v: boolean) => (culling = v),
// always return false when disabled
get: () => (this.disableCulling ? false : culling),
});
const clipManager = this.coreModel.getModelContext().clipManager;
const originalSetupClip = clipManager.setupClip;
// after setupClip(), the GL viewport will be set to [0, 0, canvas.width, canvas.height],
// so we have to set it back
clipManager.setupClip = (modelContext, drawParam) => {
originalSetupClip.call(clipManager, modelContext, drawParam);
drawParam.gl.viewport(...this.viewport);
};
}
protected getSize(): [number, number] {
return [this.coreModel.getCanvasWidth(), this.coreModel.getCanvasHeight()];
}
protected getLayout(): CommonLayout {
const layout: CommonLayout = {};
if (this.settings.layout) {
for (const [key, value] of Object.entries(this.settings.layout)) {
let commonKey = key;
if (key === "center_x") {
commonKey = "centerX";
} else if (key === "center_y") {
commonKey = "centerY";
}
layout[commonKey as keyof CommonLayout] = value;
}
}
return layout;
}
updateWebGLContext(gl: WebGLRenderingContext, glContextID: number): void {
const drawParamWebGL = this.coreModel.drawParamWebGL;
drawParamWebGL.firstDraw = true;
drawParamWebGL.setGL(gl);
drawParamWebGL.glno = glContextID;
// reset WebGL buffers
for (const [key, value] of Object.entries(drawParamWebGL)) {
if (value instanceof WebGLBuffer) {
(drawParamWebGL as unknown as Record<string, WebGLBuffer | null>)[key] = null;
}
}
const clipManager = this.coreModel.getModelContext().clipManager;
clipManager.curFrameNo = glContextID;
const framebuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING) as WebGLFramebuffer;
// force Live2D to re-create the framebuffer
clipManager.getMaskRenderTexture();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
}
bindTexture(index: number, texture: WebGLTexture): void {
this.coreModel.setTexture(index, texture);
}
protected getHitAreaDefs(): CommonHitArea[] {
return (
this.settings.hitAreas?.map((hitArea) => ({
id: hitArea.id,
name: hitArea.name,
index: this.coreModel.getDrawDataIndex(hitArea.id),
})) || []
);
}
getDrawableIDs(): string[] {
const modelContext = this.coreModel.getModelContext();
const ids = [];
for (let i = 0; i < this.drawDataCount; i++) {
const drawData = modelContext.getDrawData(i);
if (drawData) {
ids.push(drawData.getDrawDataID().id);
}
}
return ids;
}
getDrawableIndex(id: string): number {
return this.coreModel.getDrawDataIndex(id);
}
getDrawableVertices(drawIndex: number | string): Float32Array {
if (typeof drawIndex === "string") {
drawIndex = this.coreModel.getDrawDataIndex(drawIndex);
if (drawIndex === -1) throw new TypeError("Unable to find drawable ID: " + drawIndex);
}
return this.coreModel.getTransformedPoints(drawIndex).slice();
}
override hitTest(x: number, y: number): string[] {
if (!this.hasDrawn) {
logger.warn(
"Trying to hit-test a Cubism 2 model that has not been rendered yet. The result will always be empty since the draw data is not ready.",
);
}
return super.hitTest(x, y);
}
update(dt: DOMHighResTimeStamp, now: DOMHighResTimeStamp): void {
super.update(dt, now);
const model = this.coreModel;
this.emit("beforeMotionUpdate");
const motionUpdated = this.motionManager.update(this.coreModel, now);
this.emit("afterMotionUpdate");
model.saveParam();
this.motionManager.expressionManager?.update(model, now);
if (!motionUpdated) {
this.eyeBlink?.update(dt);
}
this.updateFocus();
this.updateNaturalMovements(dt, now);
if (this.lipSync && this.motionManager.currentAudio) {
let value = this.motionManager.mouthSync();
let min_ = 0;
const max_ = 1;
const bias_weight = 1.2;
const bias_power = 0.7;
if (value > 0.0) {
min_ = 0.4;
}
value = Math.pow(value, bias_power);
value = clamp(value * bias_weight, min_, max_);
for (let i = 0; i < this.motionManager.lipSyncIds.length; ++i) {
this.coreModel.setParamFloat(
this.coreModel.getParamIndex(this.motionManager.lipSyncIds[i]!),
value,
);
}
}
this.physics?.update(now);
this.pose?.update(dt);
this.emit("beforeModelUpdate");
model.update();
model.loadParam();
}
updateFocus() {
this.coreModel.addToParamFloat(this.eyeballXParamIndex, this.focusController.x);
this.coreModel.addToParamFloat(this.eyeballYParamIndex, this.focusController.y);
this.coreModel.addToParamFloat(this.angleXParamIndex, this.focusController.x * 30);
this.coreModel.addToParamFloat(this.angleYParamIndex, this.focusController.y * 30);
this.coreModel.addToParamFloat(
this.angleZParamIndex,
this.focusController.x * this.focusController.y * -30,
);
this.coreModel.addToParamFloat(this.bodyAngleXParamIndex, this.focusController.x * 10);
}
updateNaturalMovements(dt: DOMHighResTimeStamp, now: DOMHighResTimeStamp) {
const t = (now / 1000) * 2 * Math.PI;
this.coreModel.addToParamFloat(this.angleXParamIndex, 15 * Math.sin(t / 6.5345) * 0.5);
this.coreModel.addToParamFloat(this.angleYParamIndex, 8 * Math.sin(t / 3.5345) * 0.5);
this.coreModel.addToParamFloat(this.angleZParamIndex, 10 * Math.sin(t / 5.5345) * 0.5);
this.coreModel.addToParamFloat(this.bodyAngleXParamIndex, 4 * Math.sin(t / 15.5345) * 0.5);
this.coreModel.setParamFloat(this.breathParamIndex, 0.5 + 0.5 * Math.sin(t / 3.2345));
}
draw(gl: WebGLRenderingContext): void {
const disableCulling = this.disableCulling;
// culling must be disabled to get this cubism2 model drawn properly on a framebuffer
if (gl.getParameter(gl.FRAMEBUFFER_BINDING)) {
this.disableCulling = true;
}
const matrix = this.drawingMatrix;
// set given 3x3 matrix into a 4x4 matrix
tempMatrixArray[0] = matrix.a;
tempMatrixArray[1] = matrix.b;
tempMatrixArray[4] = matrix.c;
tempMatrixArray[5] = matrix.d;
tempMatrixArray[12] = matrix.tx;
tempMatrixArray[13] = matrix.ty;
this.coreModel.setMatrix(tempMatrixArray);
this.coreModel.draw();
this.hasDrawn = true;
this.disableCulling = disableCulling;
}
destroy() {
super.destroy();
// cubism2 core has a super dumb memory management so there's basically nothing much to do to release the model
(this as Partial<this>).coreModel = undefined;
}
}