-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLive2DRender.js
646 lines (591 loc) · 24 KB
/
Live2DRender.js
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
/*
* Live2D描画クラス
*/
THREE.Live2DRender = function(renderer, filepath, filenm, scale) {
// WebGL ContextのWebGLRenderer
if(renderer){
this.gl = renderer.getContext();
}else{
console.error("第1引数にrendererを渡して下さい");
return;
}
// モデルファイルパス
if(filepath){
this.filepath = filepath;
}else{
console.error("第2引数にFilePathを渡して下さい");
return;
}
// Jsonファイル名
if(filenm){
this.filenm = filenm;
}else{
console.error("第3引数にファイル名を渡して下さい");
return;
}
// Live2DモデルWebGL表示サイズ
this.modelscale = scale || 2.0;
// Live2Dモデルのインスタンス
this.live2DModel = null;
// モデルのロードが完了したら true
this.loadLive2DCompleted = false;
// モデルの初期化が完了したら true
this.initLive2DCompleted = false;
// WebGL Image型オブジェクトの配列
this.loadedImages = [];
// モーション
this.motions = [];
// モーション管理マネジャー
this.motionMgr = null;
// モーション番号
this.motionnm = 0;
// モーションフラグ
this.motionflg = false;
// サウンド
this.sounds = [];
// サウンド番号
this.soundnm = 0;
// 前に流したサウンド
this.beforesound = 0;
// 表情モーション
this.expressions = [];
// 表情モーション名
this.expressionsnm = [];
// 表情モーション管理マネジャー
this.expressionManager = null;
// 表情モーションフラグ
this.expressionflg = false;
// 表情モーション番号
this.expressionnm = 0;
// Live2Dモデル設定
this.modelDef = null;
// フェードイン
this.fadeines = [];
// フェードアウト
this.fadeoutes = [];
// ポーズ
this.pose = null;
// 物理演算
this.physics = null;
// ドラッグによるアニメーション管理
this.dragMgr = null; /*new L2DTargetPoint();*/
this.viewMatrix = null; /*new L2DViewMatrix();*/
this.projMatrix = null; /*new L2DMatrix44()*/
this.deviceToScreen = null; /*new L2DMatrix44();*/
this.drag = false; // ドラッグ中かどうか
this.lastMouseX = 0;
this.lastMouseY = 0;
this.dragX = 0;
this.dragY = 0;
// Live2Dの初期化
Live2D.init();
// OpenGLのコンテキストをセット
Live2D.setGL(this.gl);
// Live2Dモデル管理クラスのインスタンス化
this.live2DMgr = new LAppLive2DManager();
// Jsonをロード(modelDefをセット)
this.loadJson();
// マウスドラッグの座標設定
this.setMouseView(renderer);
// マウスドラッグのイベントリスナー
document.addEventListener("mousedown", this.mouseEvent.bind(this), false);
document.addEventListener("mousemove", this.mouseEvent.bind(this), false);
document.addEventListener("mouseup", this.mouseEvent.bind(this), false);
document.addEventListener("mouseout", this.mouseEvent.bind(this), false);
};
/*
* Live2D描画クラスのファンクション
*/
THREE.Live2DRender.prototype = {
/**
* WebGLコンテキストを取得・初期化。
* Live2Dの初期化、描画ループを開始。
*/
initLoop : function()
{
//------------ Live2Dの初期化 ------------
// コールバック対策用
var that = this;
// mocファイルからLive2Dモデルのインスタンスを生成
this.loadBytes(that.filepath + that.modelDef.model, function(buf){
that.live2DModel = Live2DModelWebGL.loadModel(buf);
});
/********** テクスチャの読み込み **********/
var loadCount = 0;
for(var i = 0; i < that.modelDef.textures.length; i++){
(function ( tno ){// 即時関数で i の値を tno に固定する(onerror用)
that.loadedImages[tno] = new Image();
that.loadedImages[tno].src = that.filepath + that.modelDef.textures[tno];
that.loadedImages[tno].onload = function(){
if((++loadCount) == that.modelDef.textures.length) {
that.loadLive2DCompleted = true;//全て読み終わった
}
}
that.loadedImages[tno].onerror = function() {
console.log("Failed to load image : " + that.modelDef.textures[tno]);
}
})( i );
}
/********** モーションの読み込み **********/
var motion_keys = []; // モーションキー配列
var mtn_tag = 0; // モーションタグ
var mtn_num = 0; // モーションカウント
// keyを取得
for(var key in that.modelDef.motions){
// moitons配下のキーを取得
motion_keys[mtn_tag] = key;
// 読み込むモーションファイル数を取得
mtn_num += that.modelDef.motions[motion_keys[mtn_tag]].length;
mtn_tag++;
}
// モーションタグ分ループ
for(var mtnkey in motion_keys){
// モーションとサウンドを読み込む(motions配下のタグを読み込む)
for(var j = 0; j < that.modelDef.motions[motion_keys[mtnkey]].length; j++){
// モーションの数だけロード
that.loadBytes(that.filepath + that.modelDef.motions[motion_keys[mtnkey]][j].file, function(buf){
that.motions.push(Live2DMotion.loadMotion(buf));
});
// サウンドの数だけロード
if(that.modelDef.motions[motion_keys[mtnkey]][j].sound == null){
that.sounds.push("");
}else{
that.sounds.push(new L2DSound(that.filepath + that.modelDef.motions[motion_keys[mtnkey]][j].sound));
}
// フェードイン
if(that.modelDef.motions[motion_keys[mtnkey]][j].fade_in == null){
that.fadeines.push("");
}else{
that.fadeines.push(that.modelDef.motions[motion_keys[mtnkey]][j].fade_in);
}
// フェードアウト
if(that.modelDef.motions[motion_keys[mtnkey]][j].fade_out == null){
that.fadeoutes.push("");
}else{
that.fadeoutes.push(that.modelDef.motions[motion_keys[mtnkey]][j].fade_out);
}
}
}
// モーションマネジャーのインスタンス化
that.motionMgr = new L2DMotionManager();
/********** 表情モーションの読み込み **********/
var expression_name = []; // 表情モーション名の配列
var expression_file = []; // 表情モーションファイル名の配列
// 表情のロード(json内にexpressionsがあるかチェック)
if(that.modelDef.expressions !== void 0){
for(var i = 0; i < that.modelDef.expressions.length; i++){
// 表情モーション名の配列を取得
expression_name[i] = that.modelDef.expressions[i].name;
expression_file[i] = that.filepath + that.modelDef.expressions[i].file;
// 表情ファイルをロード
that.loadExpression(expression_name[i], expression_file[i]);
}
}
// 表情モーションマネージャーのインスタンス化
that.expressionManager = new L2DMotionManager();
// ポーズのロード(json内のposeがあるかチェック)
if(that.modelDef.pose !== void 0){
that.loadBytes(that.filepath + that.modelDef.pose, function(buf){
// ポースクラスのロード
that.pose = L2DPose.load(buf);
});
}
// 物理演算のロード(json内のphysicsがあるかチェック)
if(that.modelDef.physics !== void 0){
that.loadBytes(that.filepath + that.modelDef.physics, function(buf){
// 物理演算クラスのロード
that.physics = L2DPhysics.load(buf);
});
}
},
/**
* Live2Dのドラッグ座標軸
*/
setMouseView : function(renderer){
// 3Dバッファの初期化
var width = renderer.getSize().width;
var height = renderer.getSize().height;
// ビュー行列
var ratio = height / width;
var left = -1.0;
var right = 1.0;
var bottom = -ratio;
var top = ratio;
// ドラッグ用のクラス
this.dragMgr = new L2DTargetPoint();
// Live2DのView座標クラス
this.viewMatrix = new L2DViewMatrix();
// デバイスに対応する画面の範囲。 Xの左端, Xの右端, Yの下端, Yの上端
this.viewMatrix.setScreenRect(left, right, bottom, top);
// デバイスに対応する画面の範囲。 Xの左端, Xの右端, Yの下端, Yの上端
this.viewMatrix.setMaxScreenRect(-2.0, 2.0, -2.0, 2.0);
this.viewMatrix.setMaxScale(2.0);
this.viewMatrix.setMinScale(0.8);
// Live2Dの座標系クラス
this.projMatrix = new L2DMatrix44();
this.projMatrix.multScale(1, (width / height));
// マウス用スクリーン変換行列
this.deviceToScreen = new L2DMatrix44();
this.deviceToScreen.multTranslate(-width / 2.0, -height / 2.0);
this.deviceToScreen.multScale(2 / width, -2 / width);
},
/**
* Live2Dの描画
*/
draw : function()
{
// Live2D初期化
if( ! this.live2DModel || ! this.loadLive2DCompleted )
return; //ロードが完了していないので何もしないで返る
// ロード完了後に初回のみ初期化する
if( ! this.initLive2DCompleted ){
this.initLive2DCompleted = true;
// 画像からWebGLテクスチャを生成し、モデルに登録
for( var i = 0; i < this.loadedImages.length; i++ ){
//Image型オブジェクトからテクスチャを生成
var texName = this.createTexture(this.gl, this.loadedImages[i]);
this.live2DModel.setTexture(i, texName); //モデルにテクスチャをセット
}
// テクスチャの元画像の参照をクリア
this.loadedImages = null;
// 表示位置を指定するための行列を定義する
var s = this.modelscale / this.live2DModel.getCanvasWidth(); //canvasの横幅を-1..1区間に収める
var matrix4x4 = [
s, 0, 0, 0,
0,-s, 0, 0,
0, 0, 1, 0,
-this.modelscale/2, this.modelscale/2, 0, 1
];
this.live2DModel.setMatrix(matrix4x4);
}
// アイドルモーション以外の場合(フラグと優先度で判定する)
if(this.motionflg == true && this.motionMgr.getCurrentPriority() == 0){
// フェードインの設定
this.motions[this.motionnm].setFadeIn(this.fadeines[this.motionnm]);
// フェードアウトの設定
this.motions[this.motionnm].setFadeOut(this.fadeoutes[this.motionnm]);
// アイドルモーションよりも優先度を高く再生する
this.motionMgr.startMotion(this.motions[this.motionnm], 1);
this.motionflg = false;
// 音声ファイルもあれば再生
if(this.sounds[this.motionnm]){
// 前回の音声があれば停止する
if(this.sounds[this.beforesound] != ""){
this.sounds[this.beforesound].stop();
}
// 音声を再生
this.sounds[this.motionnm].play();
// 途中で停止できるように格納する
this.beforesound = this.motionnm;
}
}
// モーションが終了していたらアイドルモーションの再生
if(this.motionMgr.isFinished() && this.motionnm != null){
// フェードインの設定
this.motions[this.motionnm].setFadeIn(this.fadeines[this.motionnm]);
// フェードアウトの設定
this.motions[this.motionnm].setFadeOut(this.fadeoutes[this.motionnm]);
// 優先度は低めでモーション再生
this.motionMgr.startMotion(this.motions[this.motionnm], 0);
// 音声ファイルもあれば再生
if(this.sounds[this.motionnm]){
// 前回の音声があれば停止する
if(this.sounds[this.beforesound] != ""){
this.sounds[this.beforesound].stop();
}
// 音声を再生
this.sounds[this.motionnm].play();
// 途中で停止できるように格納する
this.beforesound = this.motionnm;
}
}
// モーション指定されていない場合は何も再生しない
if(this.motionnm != null){
// モーションパラメータの更新
this.motionMgr.updateParam(this.live2DModel);
}
// 表情でパラメータ更新(相対変化)
if(this.expressionManager != null &&
this.expressions != null &&
!this.expressionManager.isFinished())
{
this.expressionManager.updateParam(this.live2DModel);
}
// ポーズパラメータの更新
if(this.pose != null)this.pose.updateParam(this.live2DModel);
// 物理演算パラメータの更新
if(this.physics != null)this.physics.updateParam(this.live2DModel);
// ドラッグ用パラメータの更新
this.dragMgr.update();
this.dragX = this.dragMgr.getX();
this.dragY = this.dragMgr.getY();
this.live2DModel.setParamFloat("PARAM_ANGLE_X", this.dragX * 30); // -30から30の値を加える
this.live2DModel.setParamFloat("PARAM_ANGLE_Y", this.dragY * 30);
// ドラッグによる体の向きの調整
this.live2DModel.setParamFloat("PARAM_BODY_ANGLE_X", this.dragX*10); // -10から10の値を加える
// ドラッグによる目の向きの調整
this.live2DModel.setParamFloat("PARAM_EYE_BALL_X", this.dragX); // -1から1の値を加える
this.live2DModel.setParamFloat("PARAM_EYE_BALL_Y", this.dragY);
// キャラクターのパラメータを適当に更新
var t = UtSystem.getTimeMSec() * 0.001 * 2 * Math.PI; //1秒ごとに2π(1周期)増える
var cycle = 3.0; //パラメータが一周する時間(秒)
// 呼吸する
this.live2DModel.setParamFloat("PARAM_BREATH", 0.5 + 0.5 * Math.sin(t/cycle));
// Live2Dモデルを更新して描画
this.live2DModel.update(); // 現在のパラメータに合わせて頂点等を計算
this.live2DModel.draw(); // 描画
},
/**
* マウスイベント
*/
mouseEvent : function(e)
{
// 右クリック制御
e.preventDefault();
// マウスダウン時
if (e.type == "mousedown") {
// 左クリック以外なら処理を抜ける
if("button" in e && e.button != 0) return;
this.modelTurnHead(e);
// マウス移動時
} else if (e.type == "mousemove") {
this.followPointer(e);
// マウスアップ時
} else if (e.type == "mouseup") {
// 左クリック以外なら処理を抜ける
if("button" in e && e.button != 0) return;
if (this.drag){
this.drag = false;
}
this.dragMgr.setPoint(0, 0);
// CANVAS外にマウスがいった時
} else if (e.type == "mouseout") {
if (this.drag)
{
this.drag = false;
}
this.dragMgr.setPoint(0, 0);
}
},
/**
* クリックされた方向を向く
* タップされた場所に応じてモーションを再生
*/
modelTurnHead : function(e)
{
this.drag = true;
var rect = e.target.getBoundingClientRect();
var sx = this.transformScreenX(e.clientX - rect.left);
var sy = this.transformScreenY(e.clientY - rect.top);
var vx = this.transformViewX(e.clientX - rect.left);
var vy = this.transformViewY(e.clientY - rect.top);
this.lastMouseX = sx;
this.lastMouseY = sy;
this.dragMgr.setPoint(vx, vy); // その方向を向く
},
/**
* マウスを動かした時のイベント
*/
followPointer : function(e)
{
var rect = e.target.getBoundingClientRect();
var sx = this.transformScreenX(e.clientX - rect.left);
var sy = this.transformScreenY(e.clientY - rect.top);
var vx = this.transformViewX(e.clientX - rect.left);
var vy = this.transformViewY(e.clientY - rect.top);
if (this.drag)
{
this.lastMouseX = sx;
this.lastMouseY = sy;
this.dragMgr.setPoint(vx, vy); // その方向を向く
}
},
/**
* 論理座標変換したView座標X
*/
transformViewX : function(deviceX)
{
var screenX = this.deviceToScreen.transformX(deviceX); // 論理座標変換した座標を取得。
return this.viewMatrix.invertTransformX(screenX); // 拡大、縮小、移動後の値。
},
/**
* 論理座標変換したView座標Y
*/
transformViewY : function(deviceY)
{
var screenY = this.deviceToScreen.transformY(deviceY); // 論理座標変換した座標を取得。
return this.viewMatrix.invertTransformY(screenY); // 拡大、縮小、移動後の値。
},
/**
* 論理座標変換したScreen座標X
*/
transformScreenX : function(deviceX)
{
return this.deviceToScreen.transformX(deviceX);
},
/**
* 論理座標変換したScreen座標Y
*/
transformScreenY : function(deviceY)
{
return this.deviceToScreen.transformY(deviceY);
},
/**
* Image型オブジェクトからテクスチャを生成
*/
createTexture : function(gl/*WebGLコンテキスト*/, image/*WebGL Image*/)
{
var texture = gl.createTexture(); //テクスチャオブジェクトを作成する
if ( !texture ){
console.log("Failed to generate gl texture name.");
return -1;
}
if(this.live2DModel.isPremultipliedAlpha() == false) {
// 乗算済アルファテクスチャ以外の場合
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1);
}
// imageを上下反転
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
// テクスチャのユニットを指定する
gl.activeTexture( gl.TEXTURE0 );
// テクスチャをバインドする
gl.bindTexture( gl.TEXTURE_2D , texture );
// テクスチャに画像データを紐付ける
gl.texImage2D( gl.TEXTURE_2D , 0 , gl.RGBA , gl.RGBA , gl.UNSIGNED_BYTE , image);
// テクスチャの品質を指定する(対象ピクセルの中心に最も近い点の値)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
// ミップマップの品質を指定する
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
// ミップマップの生成
gl.generateMipmap(gl.TEXTURE_2D);
// テクスチャのバインド開放
gl.bindTexture( gl.TEXTURE_2D , null );
return texture;
},
/**
* ファイルをバイト配列としてロードする
*/
loadBytes : function(path , callback)
{
var request = new XMLHttpRequest();
request.open("GET", path , true);
request.responseType = "arraybuffer";
request.onload = function(){
switch( request.status ){
case 200:
callback( request.response );
break;
default:
console.log( "Failed to load (" + request.status + ") : " + path );
break;
}
}
request.send(null);
},
/**
* Jsonファイルをロードする
*/
loadJson : function()
{
var thisRef = this;
var request = new XMLHttpRequest();
request.open("GET", this.filepath + this.filenm, true);
request.onreadystatechange = function(){
if(request.readyState == 4 && request.status == 200){
// model.jsonから取得
thisRef.modelDef = JSON.parse(request.responseText);
// 初期化処理
thisRef.initLoop();
}
}
request.send(null);
},
/**
* 表情をロードする
*/
loadExpression : function(name, path){
var thisRef = this;
this.loadBytes(path, function(buf) {
thisRef.expressionsnm[thisRef.expressionsnm.length] = name;
thisRef.expressions[thisRef.expressions.length] = L2DExpressionMotion.loadJson(buf);
});
},
/**
* 表情を設定する
*/
setExpression : function(name)
{
var cnt = 0;
for(var i = 0; i < this.expressionsnm.length; i++){
if(name == this.expressionsnm[i]){
break;
}
cnt++;
}
var expression = this.expressions[cnt];
this.expressionManager.startMotion(expression, false);
},
/**
* ランダム表情設定する
*/
setRandomExpression : function()
{
// ランダム再生する
var random = ~~(Math.random() * this.expressions.length);
var expression = this.expressions[random];
this.expressionManager.startMotion(expression, false);
},
/**
* モーションを設定する
*/
setMotion : function(name)
{
if(this.modelDef == null)return;
var cnt = 0;
// ファイル名からファイル番号を取り出す
for(var key in this.modelDef.motions){
for(var j = 0; j < this.modelDef.motions[key].length; j++){
// 余分なパスをカット
var strfilenm = this.modelDef.motions[key][j].file.split("/");
if(name == strfilenm[1]){
break;
}
cnt++;
}
}
this.motionnm = cnt;
this.motionflg = true;
},
/**
* ランダムモーション再生する
*/
setRandomMotion : function()
{
if(this.modelDef == null)return;
// ランダム再生する
this.motionnm = ~~(Math.random() * this.motions.length);
this.motionflg = true;
}
};
/****************************************
* サウンドクラス
****************************************/
var L2DSound = function(path /*音声ファイルパス*/) {
this.snd = document.createElement("audio");
this.snd.src = path;
};
L2DSound.prototype = {
/**
* 音声再生
*/
play : function() {
this.snd.play();
},
/**
* 音声停止
*/
stop : function() {
this.snd.pause();
this.snd.currentTime = 0;
}
};