/
lappmodel.ts
927 lines (773 loc) · 28.5 KB
/
lappmodel.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
/**
* Copyright(c) Live2D Inc. All rights reserved.
*
* Use of this source code is governed by the Live2D Open Software license
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
*/
import 'whatwg-fetch';
import { CubismDefaultParameterId } from '@framework/cubismdefaultparameterid';
import { CubismModelSettingJson } from '@framework/cubismmodelsettingjson';
import {
BreathParameterData,
CubismBreath
} from '@framework/effect/cubismbreath';
import { CubismEyeBlink } from '@framework/effect/cubismeyeblink';
import { ICubismModelSetting } from '@framework/icubismmodelsetting';
import { CubismIdHandle } from '@framework/id/cubismid';
import { CubismFramework } from '@framework/live2dcubismframework';
import { CubismMatrix44 } from '@framework/math/cubismmatrix44';
import { CubismUserModel } from '@framework/model/cubismusermodel';
import {
ACubismMotion,
FinishedMotionCallback
} from '@framework/motion/acubismmotion';
import { CubismMotion } from '@framework/motion/cubismmotion';
import {
CubismMotionQueueEntryHandle,
InvalidMotionQueueEntryHandleValue
} from '@framework/motion/cubismmotionqueuemanager';
import { csmMap } from '@framework/type/csmmap';
import { csmRect } from '@framework/type/csmrectf';
import { csmString } from '@framework/type/csmstring';
import { csmVector } from '@framework/type/csmvector';
import {
CSM_ASSERT,
CubismLogError,
CubismLogInfo
} from '@framework/utils/cubismdebug';
import * as LAppDefine from './lappdefine';
import { canvas, frameBuffer, gl, LAppDelegate } from './lappdelegate';
import { LAppPal } from './lapppal';
import { TextureInfo } from './lapptexturemanager';
import { LAppWavFileHandler } from './lappwavfilehandler';
import { CubismMoc } from '@framework/model/cubismmoc';
enum LoadStep {
LoadAssets,
LoadModel,
WaitLoadModel,
LoadExpression,
WaitLoadExpression,
LoadPhysics,
WaitLoadPhysics,
LoadPose,
WaitLoadPose,
SetupEyeBlink,
SetupBreath,
LoadUserData,
WaitLoadUserData,
SetupEyeBlinkIds,
SetupLipSyncIds,
SetupLayout,
LoadMotion,
WaitLoadMotion,
CompleteInitialize,
CompleteSetupModel,
LoadTexture,
WaitLoadTexture,
CompleteSetup
}
/**
* ユーザーが実際に使用するモデルの実装クラス<br>
* モデル生成、機能コンポーネント生成、更新処理とレンダリングの呼び出しを行う。
*/
export class LAppModel extends CubismUserModel {
/**
* model3.jsonが置かれたディレクトリとファイルパスからモデルを生成する
* @param dir
* @param fileName
*/
public loadAssets(dir: string, fileName: string): void {
this._modelHomeDir = dir;
fetch(`${this._modelHomeDir}${fileName}`)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
const setting: ICubismModelSetting = new CubismModelSettingJson(
arrayBuffer,
arrayBuffer.byteLength
);
// ステートを更新
this._state = LoadStep.LoadModel;
// 結果を保存
this.setupModel(setting);
});
}
/**
* model3.jsonからモデルを生成する。
* model3.jsonの記述に従ってモデル生成、モーション、物理演算などのコンポーネント生成を行う。
*
* @param setting ICubismModelSettingのインスタンス
*/
private setupModel(setting: ICubismModelSetting): void {
this._updating = true;
this._initialized = false;
this._modelSetting = setting;
// CubismModel
if (this._modelSetting.getModelFileName() != '') {
const modelFileName = this._modelSetting.getModelFileName();
fetch(`${this._modelHomeDir}${modelFileName}`)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
this.loadModel(arrayBuffer, this._mocConsistency);
this._state = LoadStep.LoadExpression;
// callback
loadCubismExpression();
});
this._state = LoadStep.WaitLoadModel;
} else {
LAppPal.printMessage('Model data does not exist.');
}
// Expression
const loadCubismExpression = (): void => {
if (this._modelSetting.getExpressionCount() > 0) {
const count: number = this._modelSetting.getExpressionCount();
for (let i = 0; i < count; i++) {
const expressionName = this._modelSetting.getExpressionName(i);
const expressionFileName =
this._modelSetting.getExpressionFileName(i);
fetch(`${this._modelHomeDir}${expressionFileName}`)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
const motion: ACubismMotion = this.loadExpression(
arrayBuffer,
arrayBuffer.byteLength,
expressionName
);
if (this._expressions.getValue(expressionName) != null) {
ACubismMotion.delete(
this._expressions.getValue(expressionName)
);
this._expressions.setValue(expressionName, null);
}
this._expressions.setValue(expressionName, motion);
this._expressionCount++;
if (this._expressionCount >= count) {
this._state = LoadStep.LoadPhysics;
// callback
loadCubismPhysics();
}
});
}
this._state = LoadStep.WaitLoadExpression;
} else {
this._state = LoadStep.LoadPhysics;
// callback
loadCubismPhysics();
}
};
// Physics
const loadCubismPhysics = (): void => {
if (this._modelSetting.getPhysicsFileName() != '') {
const physicsFileName = this._modelSetting.getPhysicsFileName();
fetch(`${this._modelHomeDir}${physicsFileName}`)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
this.loadPhysics(arrayBuffer, arrayBuffer.byteLength);
this._state = LoadStep.LoadPose;
// callback
loadCubismPose();
});
this._state = LoadStep.WaitLoadPhysics;
} else {
this._state = LoadStep.LoadPose;
// callback
loadCubismPose();
}
};
// Pose
const loadCubismPose = (): void => {
if (this._modelSetting.getPoseFileName() != '') {
const poseFileName = this._modelSetting.getPoseFileName();
fetch(`${this._modelHomeDir}${poseFileName}`)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
this.loadPose(arrayBuffer, arrayBuffer.byteLength);
this._state = LoadStep.SetupEyeBlink;
// callback
setupEyeBlink();
});
this._state = LoadStep.WaitLoadPose;
} else {
this._state = LoadStep.SetupEyeBlink;
// callback
setupEyeBlink();
}
};
// EyeBlink
const setupEyeBlink = (): void => {
if (this._modelSetting.getEyeBlinkParameterCount() > 0) {
this._eyeBlink = CubismEyeBlink.create(this._modelSetting);
this._state = LoadStep.SetupBreath;
}
// callback
setupBreath();
};
// Breath
const setupBreath = (): void => {
this._breath = CubismBreath.create();
const breathParameters: csmVector<BreathParameterData> = new csmVector();
breathParameters.pushBack(
new BreathParameterData(this._idParamAngleX, 0.0, 15.0, 6.5345, 0.5)
);
breathParameters.pushBack(
new BreathParameterData(this._idParamAngleY, 0.0, 8.0, 3.5345, 0.5)
);
breathParameters.pushBack(
new BreathParameterData(this._idParamAngleZ, 0.0, 10.0, 5.5345, 0.5)
);
breathParameters.pushBack(
new BreathParameterData(this._idParamBodyAngleX, 0.0, 4.0, 15.5345, 0.5)
);
breathParameters.pushBack(
new BreathParameterData(
CubismFramework.getIdManager().getId(
CubismDefaultParameterId.ParamBreath
),
0.5,
0.5,
3.2345,
1
)
);
this._breath.setParameters(breathParameters);
this._state = LoadStep.LoadUserData;
// callback
loadUserData();
};
// UserData
const loadUserData = (): void => {
if (this._modelSetting.getUserDataFile() != '') {
const userDataFile = this._modelSetting.getUserDataFile();
fetch(`${this._modelHomeDir}${userDataFile}`)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
this.loadUserData(arrayBuffer, arrayBuffer.byteLength);
this._state = LoadStep.SetupEyeBlinkIds;
// callback
setupEyeBlinkIds();
});
this._state = LoadStep.WaitLoadUserData;
} else {
this._state = LoadStep.SetupEyeBlinkIds;
// callback
setupEyeBlinkIds();
}
};
// EyeBlinkIds
const setupEyeBlinkIds = (): void => {
const eyeBlinkIdCount: number =
this._modelSetting.getEyeBlinkParameterCount();
for (let i = 0; i < eyeBlinkIdCount; ++i) {
this._eyeBlinkIds.pushBack(
this._modelSetting.getEyeBlinkParameterId(i)
);
}
this._state = LoadStep.SetupLipSyncIds;
// callback
setupLipSyncIds();
};
// LipSyncIds
const setupLipSyncIds = (): void => {
const lipSyncIdCount = this._modelSetting.getLipSyncParameterCount();
for (let i = 0; i < lipSyncIdCount; ++i) {
this._lipSyncIds.pushBack(this._modelSetting.getLipSyncParameterId(i));
}
this._state = LoadStep.SetupLayout;
// callback
setupLayout();
};
// Layout
const setupLayout = (): void => {
const layout: csmMap<string, number> = new csmMap<string, number>();
if (this._modelSetting == null || this._modelMatrix == null) {
CubismLogError('Failed to setupLayout().');
return;
}
this._modelSetting.getLayoutMap(layout);
this._modelMatrix.setupFromLayout(layout);
this._state = LoadStep.LoadMotion;
// callback
loadCubismMotion();
};
// Motion
const loadCubismMotion = (): void => {
this._state = LoadStep.WaitLoadMotion;
this._model.saveParameters();
this._allMotionCount = 0;
this._motionCount = 0;
const group: string[] = [];
const motionGroupCount: number = this._modelSetting.getMotionGroupCount();
// モーションの総数を求める
for (let i = 0; i < motionGroupCount; i++) {
group[i] = this._modelSetting.getMotionGroupName(i);
this._allMotionCount += this._modelSetting.getMotionCount(group[i]);
}
// モーションの読み込み
for (let i = 0; i < motionGroupCount; i++) {
this.preLoadMotionGroup(group[i]);
}
// モーションがない場合
if (motionGroupCount == 0) {
this._state = LoadStep.LoadTexture;
// 全てのモーションを停止する
this._motionManager.stopAllMotions();
this._updating = false;
this._initialized = true;
this.createRenderer();
this.setupTextures();
this.getRenderer().startUp(gl);
}
};
}
/**
* テクスチャユニットにテクスチャをロードする
*/
private setupTextures(): void {
// iPhoneでのアルファ品質向上のためTypescriptではpremultipliedAlphaを採用
const usePremultiply = true;
if (this._state == LoadStep.LoadTexture) {
// テクスチャ読み込み用
const textureCount: number = this._modelSetting.getTextureCount();
for (
let modelTextureNumber = 0;
modelTextureNumber < textureCount;
modelTextureNumber++
) {
// テクスチャ名が空文字だった場合はロード・バインド処理をスキップ
if (this._modelSetting.getTextureFileName(modelTextureNumber) == '') {
console.log('getTextureFileName null');
continue;
}
// WebGLのテクスチャユニットにテクスチャをロードする
let texturePath =
this._modelSetting.getTextureFileName(modelTextureNumber);
texturePath = this._modelHomeDir + texturePath;
// ロード完了時に呼び出すコールバック関数
const onLoad = (textureInfo: TextureInfo): void => {
this.getRenderer().bindTexture(modelTextureNumber, textureInfo.id);
this._textureCount++;
if (this._textureCount >= textureCount) {
// ロード完了
this._state = LoadStep.CompleteSetup;
}
};
// 読み込み
LAppDelegate.getInstance()
.getTextureManager()
.createTextureFromPngFile(texturePath, usePremultiply, onLoad);
this.getRenderer().setIsPremultipliedAlpha(usePremultiply);
}
this._state = LoadStep.WaitLoadTexture;
}
}
/**
* レンダラを再構築する
*/
public reloadRenderer(): void {
this.deleteRenderer();
this.createRenderer();
this.setupTextures();
}
/**
* 更新
*/
public update(): void {
if (this._state != LoadStep.CompleteSetup) return;
const deltaTimeSeconds: number = LAppPal.getDeltaTime();
this._userTimeSeconds += deltaTimeSeconds;
this._dragManager.update(deltaTimeSeconds);
this._dragX = this._dragManager.getX();
this._dragY = this._dragManager.getY();
// モーションによるパラメータ更新の有無
let motionUpdated = false;
//--------------------------------------------------------------------------
this._model.loadParameters(); // 前回セーブされた状態をロード
if (this._motionManager.isFinished()) {
// モーションの再生がない場合、待機モーションの中からランダムで再生する
this.startRandomMotion(
LAppDefine.MotionGroupIdle,
LAppDefine.PriorityIdle
);
} else {
motionUpdated = this._motionManager.updateMotion(
this._model,
deltaTimeSeconds
); // モーションを更新
}
this._model.saveParameters(); // 状態を保存
//--------------------------------------------------------------------------
// まばたき
if (!motionUpdated) {
if (this._eyeBlink != null) {
// メインモーションの更新がないとき
this._eyeBlink.updateParameters(this._model, deltaTimeSeconds); // 目パチ
}
}
if (this._expressionManager != null) {
this._expressionManager.updateMotion(this._model, deltaTimeSeconds); // 表情でパラメータ更新(相対変化)
}
// ドラッグによる変化
// ドラッグによる顔の向きの調整
this._model.addParameterValueById(this._idParamAngleX, this._dragX * 30); // -30から30の値を加える
this._model.addParameterValueById(this._idParamAngleY, this._dragY * 30);
this._model.addParameterValueById(
this._idParamAngleZ,
this._dragX * this._dragY * -30
);
// ドラッグによる体の向きの調整
this._model.addParameterValueById(
this._idParamBodyAngleX,
this._dragX * 10
); // -10から10の値を加える
// ドラッグによる目の向きの調整
this._model.addParameterValueById(this._idParamEyeBallX, this._dragX); // -1から1の値を加える
this._model.addParameterValueById(this._idParamEyeBallY, this._dragY);
// 呼吸など
if (this._breath != null) {
this._breath.updateParameters(this._model, deltaTimeSeconds);
}
// 物理演算の設定
if (this._physics != null) {
this._physics.evaluate(this._model, deltaTimeSeconds);
}
// リップシンクの設定
if (this._lipsync) {
let value = 0.0; // リアルタイムでリップシンクを行う場合、システムから音量を取得して、0~1の範囲で値を入力します。
this._wavFileHandler.update(deltaTimeSeconds);
value = this._wavFileHandler.getRms();
for (let i = 0; i < this._lipSyncIds.getSize(); ++i) {
this._model.addParameterValueById(this._lipSyncIds.at(i), value, 0.8);
}
}
// ポーズの設定
if (this._pose != null) {
this._pose.updateParameters(this._model, deltaTimeSeconds);
}
this._model.update();
}
/**
* 引数で指定したモーションの再生を開始する
* @param group モーショングループ名
* @param no グループ内の番号
* @param priority 優先度
* @param onFinishedMotionHandler モーション再生終了時に呼び出されるコールバック関数
* @return 開始したモーションの識別番号を返す。個別のモーションが終了したか否かを判定するisFinished()の引数で使用する。開始できない時は[-1]
*/
public startMotion(
group: string,
no: number,
priority: number,
onFinishedMotionHandler?: FinishedMotionCallback
): CubismMotionQueueEntryHandle {
if (priority == LAppDefine.PriorityForce) {
this._motionManager.setReservePriority(priority);
} else if (!this._motionManager.reserveMotion(priority)) {
if (this._debugMode) {
LAppPal.printMessage("[APP]can't start motion.");
}
return InvalidMotionQueueEntryHandleValue;
}
const motionFileName = this._modelSetting.getMotionFileName(group, no);
// ex) idle_0
const name = `${group}_${no}`;
let motion: CubismMotion = this._motions.getValue(name) as CubismMotion;
let autoDelete = false;
if (motion == null) {
fetch(`${this._modelHomeDir}${motionFileName}`)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
motion = this.loadMotion(
arrayBuffer,
arrayBuffer.byteLength,
null,
onFinishedMotionHandler
);
let fadeTime: number = this._modelSetting.getMotionFadeInTimeValue(
group,
no
);
if (fadeTime >= 0.0) {
motion.setFadeInTime(fadeTime);
}
fadeTime = this._modelSetting.getMotionFadeOutTimeValue(group, no);
if (fadeTime >= 0.0) {
motion.setFadeOutTime(fadeTime);
}
motion.setEffectIds(this._eyeBlinkIds, this._lipSyncIds);
autoDelete = true; // 終了時にメモリから削除
});
} else {
motion.setFinishedMotionHandler(onFinishedMotionHandler);
}
//voice
const voice = this._modelSetting.getMotionSoundFileName(group, no);
if (voice.localeCompare('') != 0) {
let path = voice;
path = this._modelHomeDir + path;
this._wavFileHandler.start(path);
}
if (this._debugMode) {
LAppPal.printMessage(`[APP]start motion: [${group}_${no}`);
}
return this._motionManager.startMotionPriority(
motion,
autoDelete,
priority
);
}
/**
* ランダムに選ばれたモーションの再生を開始する。
* @param group モーショングループ名
* @param priority 優先度
* @param onFinishedMotionHandler モーション再生終了時に呼び出されるコールバック関数
* @return 開始したモーションの識別番号を返す。個別のモーションが終了したか否かを判定するisFinished()の引数で使用する。開始できない時は[-1]
*/
public startRandomMotion(
group: string,
priority: number,
onFinishedMotionHandler?: FinishedMotionCallback
): CubismMotionQueueEntryHandle {
if (this._modelSetting.getMotionCount(group) == 0) {
return InvalidMotionQueueEntryHandleValue;
}
const no: number = Math.floor(
Math.random() * this._modelSetting.getMotionCount(group)
);
return this.startMotion(group, no, priority, onFinishedMotionHandler);
}
/**
* 引数で指定した表情モーションをセットする
*
* @param expressionId 表情モーションのID
*/
public setExpression(expressionId: string): void {
const motion: ACubismMotion = this._expressions.getValue(expressionId);
if (this._debugMode) {
LAppPal.printMessage(`[APP]expression: [${expressionId}]`);
}
if (motion != null) {
this._expressionManager.startMotionPriority(
motion,
false,
LAppDefine.PriorityForce
);
} else {
if (this._debugMode) {
LAppPal.printMessage(`[APP]expression[${expressionId}] is null`);
}
}
}
/**
* ランダムに選ばれた表情モーションをセットする
*/
public setRandomExpression(): void {
if (this._expressions.getSize() == 0) {
return;
}
const no: number = Math.floor(Math.random() * this._expressions.getSize());
for (let i = 0; i < this._expressions.getSize(); i++) {
if (i == no) {
const name: string = this._expressions._keyValues[i].first;
this.setExpression(name);
return;
}
}
}
/**
* イベントの発火を受け取る
*/
public motionEventFired(eventValue: csmString): void {
CubismLogInfo('{0} is fired on LAppModel!!', eventValue.s);
}
/**
* 当たり判定テスト
* 指定IDの頂点リストから矩形を計算し、座標をが矩形範囲内か判定する。
*
* @param hitArenaName 当たり判定をテストする対象のID
* @param x 判定を行うX座標
* @param y 判定を行うY座標
*/
public hitTest(hitArenaName: string, x: number, y: number): boolean {
// 透明時は当たり判定無し。
if (this._opacity < 1) {
return false;
}
const count: number = this._modelSetting.getHitAreasCount();
for (let i = 0; i < count; i++) {
if (this._modelSetting.getHitAreaName(i) == hitArenaName) {
const drawId: CubismIdHandle = this._modelSetting.getHitAreaId(i);
return this.isHit(drawId, x, y);
}
}
return false;
}
/**
* モーションデータをグループ名から一括でロードする。
* モーションデータの名前は内部でModelSettingから取得する。
*
* @param group モーションデータのグループ名
*/
public preLoadMotionGroup(group: string): void {
for (let i = 0; i < this._modelSetting.getMotionCount(group); i++) {
const motionFileName = this._modelSetting.getMotionFileName(group, i);
// ex) idle_0
const name = `${group}_${i}`;
if (this._debugMode) {
LAppPal.printMessage(
`[APP]load motion: ${motionFileName} => [${name}]`
);
}
fetch(`${this._modelHomeDir}${motionFileName}`)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
const tmpMotion: CubismMotion = this.loadMotion(
arrayBuffer,
arrayBuffer.byteLength,
name
);
let fadeTime = this._modelSetting.getMotionFadeInTimeValue(group, i);
if (fadeTime >= 0.0) {
tmpMotion.setFadeInTime(fadeTime);
}
fadeTime = this._modelSetting.getMotionFadeOutTimeValue(group, i);
if (fadeTime >= 0.0) {
tmpMotion.setFadeOutTime(fadeTime);
}
tmpMotion.setEffectIds(this._eyeBlinkIds, this._lipSyncIds);
if (this._motions.getValue(name) != null) {
ACubismMotion.delete(this._motions.getValue(name));
}
this._motions.setValue(name, tmpMotion);
this._motionCount++;
if (this._motionCount >= this._allMotionCount) {
this._state = LoadStep.LoadTexture;
// 全てのモーションを停止する
this._motionManager.stopAllMotions();
this._updating = false;
this._initialized = true;
this.createRenderer();
this.setupTextures();
this.getRenderer().startUp(gl);
}
});
}
}
/**
* すべてのモーションデータを解放する。
*/
public releaseMotions(): void {
this._motions.clear();
}
/**
* 全ての表情データを解放する。
*/
public releaseExpressions(): void {
this._expressions.clear();
}
/**
* モデルを描画する処理。モデルを描画する空間のView-Projection行列を渡す。
*/
public doDraw(): void {
if (this._model == null) return;
// キャンバスサイズを渡す
const viewport: number[] = [0, 0, canvas.width, canvas.height];
this.getRenderer().setRenderState(frameBuffer, viewport);
this.getRenderer().drawModel();
}
/**
* モデルを描画する処理。モデルを描画する空間のView-Projection行列を渡す。
*/
public draw(matrix: CubismMatrix44): void {
if (this._model == null) {
return;
}
// 各読み込み終了後
if (this._state == LoadStep.CompleteSetup) {
matrix.multiplyByMatrix(this._modelMatrix);
this.getRenderer().setMvpMatrix(matrix);
this.doDraw();
}
}
public async hasMocConsistencyFromFile() {
CSM_ASSERT(this._modelSetting.getModelFileName().localeCompare(``));
// CubismModel
if (this._modelSetting.getModelFileName() != '') {
const modelFileName = this._modelSetting.getModelFileName();
const response = await fetch(`${this._modelHomeDir}${modelFileName}`);
const arrayBuffer = await response.arrayBuffer();
this._consistency = CubismMoc.hasMocConsistency(arrayBuffer);
if (!this._consistency) {
CubismLogInfo('Inconsistent MOC3.');
} else {
CubismLogInfo('Consistent MOC3.');
}
return this._consistency;
} else {
LAppPal.printMessage('Model data does not exist.');
}
}
/**
* コンストラクタ
*/
public constructor() {
super();
this._modelSetting = null;
this._modelHomeDir = null;
this._userTimeSeconds = 0.0;
this._eyeBlinkIds = new csmVector<CubismIdHandle>();
this._lipSyncIds = new csmVector<CubismIdHandle>();
this._motions = new csmMap<string, ACubismMotion>();
this._expressions = new csmMap<string, ACubismMotion>();
this._hitArea = new csmVector<csmRect>();
this._userArea = new csmVector<csmRect>();
this._idParamAngleX = CubismFramework.getIdManager().getId(
CubismDefaultParameterId.ParamAngleX
);
this._idParamAngleY = CubismFramework.getIdManager().getId(
CubismDefaultParameterId.ParamAngleY
);
this._idParamAngleZ = CubismFramework.getIdManager().getId(
CubismDefaultParameterId.ParamAngleZ
);
this._idParamEyeBallX = CubismFramework.getIdManager().getId(
CubismDefaultParameterId.ParamEyeBallX
);
this._idParamEyeBallY = CubismFramework.getIdManager().getId(
CubismDefaultParameterId.ParamEyeBallY
);
this._idParamBodyAngleX = CubismFramework.getIdManager().getId(
CubismDefaultParameterId.ParamBodyAngleX
);
if (LAppDefine.MOCConsistencyValidationEnable) {
this._mocConsistency = true;
}
this._state = LoadStep.LoadAssets;
this._expressionCount = 0;
this._textureCount = 0;
this._motionCount = 0;
this._allMotionCount = 0;
this._wavFileHandler = new LAppWavFileHandler();
this._consistency = false;
}
_modelSetting: ICubismModelSetting; // モデルセッティング情報
_modelHomeDir: string; // モデルセッティングが置かれたディレクトリ
_userTimeSeconds: number; // デルタ時間の積算値[秒]
_eyeBlinkIds: csmVector<CubismIdHandle>; // モデルに設定された瞬き機能用パラメータID
_lipSyncIds: csmVector<CubismIdHandle>; // モデルに設定されたリップシンク機能用パラメータID
_motions: csmMap<string, ACubismMotion>; // 読み込まれているモーションのリスト
_expressions: csmMap<string, ACubismMotion>; // 読み込まれている表情のリスト
_hitArea: csmVector<csmRect>;
_userArea: csmVector<csmRect>;
_idParamAngleX: CubismIdHandle; // パラメータID: ParamAngleX
_idParamAngleY: CubismIdHandle; // パラメータID: ParamAngleY
_idParamAngleZ: CubismIdHandle; // パラメータID: ParamAngleZ
_idParamEyeBallX: CubismIdHandle; // パラメータID: ParamEyeBallX
_idParamEyeBallY: CubismIdHandle; // パラメータID: ParamEyeBAllY
_idParamBodyAngleX: CubismIdHandle; // パラメータID: ParamBodyAngleX
_state: LoadStep; // 現在のステータス管理用
_expressionCount: number; // 表情データカウント
_textureCount: number; // テクスチャカウント
_motionCount: number; // モーションデータカウント
_allMotionCount: number; // モーション総数
_wavFileHandler: LAppWavFileHandler; //wavファイルハンドラ
_consistency: boolean; // MOC3一貫性チェック管理用
}