-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCubismRenderer_CC.cpp
2306 lines (2059 loc) · 77.9 KB
/
CubismRenderer_CC.cpp
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "CubismRenderer_CC.h"
#include "Type/CubismBasicType.hpp"
#include "Model/CubismModel.hpp"
#include "renderer/backend/Device.h"
#include <cstring>
#include <vector>
#ifdef CC_USE_GFX
#include "renderer/backend/gfx/DeviceGFX.h"
using Type = cocos2d::backend::DeviceGFX::Type;
using Uniform = cocos2d::backend::DeviceGFX::Uniform;
using UniformList = cocos2d::backend::DeviceGFX::UniformList;
using UniformBlock = cocos2d::backend::DeviceGFX::UniformBlock;
using UniformBlockList = cocos2d::backend::DeviceGFX::UniformBlockList;
using UniformSamplerTexture = cocos2d::backend::DeviceGFX::UniformSamplerTexture;
using UniformSamplerTextureList = cocos2d::backend::DeviceGFX::UniformSamplerTextureList;
using Attribute = cocos2d::backend::DeviceGFX::Attribute;
using AttributeList = cocos2d::backend::DeviceGFX::AttributeList;
#endif
#define CSM_SAFE_DELETE_NULL(p) do { if(p) {CSM_DELETE(p); p = nullptr;} } while (false)
using namespace cocos2d;
using namespace cocos2d::backend;
using namespace Live2D::Cubism::Framework;
using namespace Live2D::Cubism::Framework::Rendering;
/*********************************************************************************************************************
* CubismClippingManager_CC
********************************************************************************************************************/
///< ファイルスコープの変数宣言
namespace {
const csmInt32 ColorChannelCount = 4; ///< 実験時に1チャンネルの場合は1、RGBだけの場合は3、アルファも含める場合は4
}
CubismDrawCommand_CC::CubismDrawCommand_CC()
{
_command.init(0.0);
_command.setDrawType(cocos2d::CustomCommand::DrawType::ELEMENT);
_command.setPrimitiveType(cocos2d::CustomCommand::PrimitiveType::TRIANGLE);
}
CubismDrawCommand_CC::~CubismDrawCommand_CC()
{
if(_drawBuffer)
CSM_FREE(_drawBuffer);
}
void CubismDrawCommand_CC::CreateVertexBuffer(csmSizeInt stride, csmSizeInt count)
{
_vbSize = stride * count;
if (_drawBuffer)
CSM_FREE(_drawBuffer);
_drawBuffer = static_cast<csmUint8*>(CSM_MALLOC(_vbSize));
_command.createVertexBuffer(stride, count, cocos2d::CustomCommand::BufferUsage::DYNAMIC);
}
void CubismDrawCommand_CC::CreateIndexBuffer(csmSizeInt count)
{
_command.createIndexBuffer(cocos2d::backend::IndexFormat::U_SHORT, count,
cocos2d::CustomCommand::BufferUsage::DYNAMIC);
}
void CubismDrawCommand_CC::UpdateVertexBuffer(void* data, void* uvData, csmSizeInt count)
{
const auto vbuf = reinterpret_cast<Core::csmVector2*>(_drawBuffer);
const auto pos = static_cast<Core::csmVector2*>(data);
const auto uv = static_cast<Core::csmVector2*>(uvData);
for (size_t i = 0; i < count; ++i)
{
vbuf[i * 2] = pos[i];
vbuf[i * 2 + 1] = uv[i];
}
_command.updateVertexBuffer(_drawBuffer, _vbSize);
}
void CubismDrawCommand_CC::UpdateIndexBuffer(void* data, csmSizeInt count)
{
_command.updateIndexBuffer(data, count * sizeof(csmInt16));
}
CubismClippingManager_CC::CubismClippingManager_CC() :
_currentFrameNo(0)
, _clippingMaskBufferSize(256, 256)
{
CubismRenderer::CubismTextureColor* tmp;
tmp = CSM_NEW CubismRenderer::CubismTextureColor();
tmp->R = 1.0f;
tmp->G = 0.0f;
tmp->B = 0.0f;
tmp->A = 0.0f;
_channelColors.PushBack(tmp);
tmp = CSM_NEW CubismRenderer::CubismTextureColor();
tmp->R = 0.0f;
tmp->G = 1.0f;
tmp->B = 0.0f;
tmp->A = 0.0f;
_channelColors.PushBack(tmp);
tmp = CSM_NEW CubismRenderer::CubismTextureColor();
tmp->R = 0.0f;
tmp->G = 0.0f;
tmp->B = 1.0f;
tmp->A = 0.0f;
_channelColors.PushBack(tmp);
tmp = CSM_NEW CubismRenderer::CubismTextureColor();
tmp->R = 0.0f;
tmp->G = 0.0f;
tmp->B = 0.0f;
tmp->A = 1.0f;
_channelColors.PushBack(tmp);
}
CubismClippingManager_CC::~CubismClippingManager_CC()
{
for (csmUint32 i = 0; i < _clippingContextListForMask.GetSize(); i++)
{
if (_clippingContextListForMask[i]) CSM_DELETE_SELF(CubismClippingContext_CC, _clippingContextListForMask[i]);
_clippingContextListForMask[i] = nullptr;
}
// _clippingContextListForDrawは_clippingContextListForMaskにあるインスタンスを指している。上記の処理により要素ごとのDELETEは不要。
for (csmUint32 i = 0; i < _clippingContextListForDraw.GetSize(); i++)
{
_clippingContextListForDraw[i] = nullptr;
}
for (csmUint32 i = 0; i < _channelColors.GetSize(); i++)
{
if (_channelColors[i]) CSM_DELETE(_channelColors[i]);
_channelColors[i] = nullptr;
}
}
void CubismClippingManager_CC::Initialize(CubismModel& model, csmInt32 drawableCount, const csmInt32** drawableMasks, const csmInt32* drawableMaskCounts)
{
//クリッピングマスクを使う描画オブジェクトを全て登録する
//クリッピングマスクは、通常数個程度に限定して使うものとする
for (csmInt32 i = 0; i < drawableCount; i++)
{
if (drawableMaskCounts[i] <= 0)
{
//クリッピングマスクが使用されていないアートメッシュ(多くの場合使用しない)
_clippingContextListForDraw.PushBack(nullptr);
continue;
}
// 既にあるClipContextと同じかチェックする
CubismClippingContext_CC* cc = FindSameClip(drawableMasks[i], drawableMaskCounts[i]);
if (cc == nullptr)
{
// 同一のマスクが存在していない場合は生成する
cc = CSM_NEW CubismClippingContext_CC(this, model, drawableMasks[i], drawableMaskCounts[i]);
_clippingContextListForMask.PushBack(cc);
}
cc->AddClippedDrawable(i);
_clippingContextListForDraw.PushBack(cc);
}
}
CubismClippingContext_CC* CubismClippingManager_CC::FindSameClip(const csmInt32* drawableMasks, csmInt32 drawableMaskCounts) const
{
// 作成済みClippingContextと一致するか確認
for (csmUint32 i = 0; i < _clippingContextListForMask.GetSize(); i++)
{
CubismClippingContext_CC* cc = _clippingContextListForMask[i];
const csmInt32 count = cc->_clippingIdCount;
if (count != drawableMaskCounts) continue; //個数が違う場合は別物
csmInt32 samecount = 0;
// 同じIDを持つか確認。配列の数が同じなので、一致した個数が同じなら同じ物を持つとする。
for (csmInt32 j = 0; j < count; j++)
{
const csmInt32 clipId = cc->_clippingIdList[j];
for (csmInt32 k = 0; k < count; k++)
{
if (drawableMasks[k] == clipId)
{
samecount++;
break;
}
}
}
if (samecount == count)
{
return cc;
}
}
return nullptr; //見つからなかった
}
void CubismClippingManager_CC::SetupClippingContext(CubismModel& model, CubismRenderer_CC* renderer,
const Viewport& lastViewport)
{
_currentFrameNo++;
// 全てのクリッピングを用意する
// 同じクリップ(複数の場合はまとめて1つのクリップ)を使う場合は1度だけ設定する
csmInt32 usingClipCount = 0;
for (csmUint32 clipIndex = 0; clipIndex < _clippingContextListForMask.GetSize(); clipIndex++)
{
// 1つのクリッピングマスクに関して
CubismClippingContext_CC* cc = _clippingContextListForMask[clipIndex];
// このクリップを利用する描画オブジェクト群全体を囲む矩形を計算
CalcClippedDrawTotalBounds(model, cc);
if (cc->_isUsing)
{
usingClipCount++; //使用中としてカウント
}
}
// マスク作成処理
if (usingClipCount > 0)
{
if (!renderer->IsUsingHighPrecisionMask())
{
// 生成したFrameBufferと同じサイズでビューポートを設定
renderer->SetViewPort(0, 0, _clippingMaskBufferSize.X, _clippingMaskBufferSize.Y);
// モデル描画時にDrawMeshNowに渡される変換(モデルtoワールド座標変換)
CubismMatrix44 modelToWorldF = renderer->GetMvpMatrix();
renderer->PreDraw(); // バッファをクリアする
// _offscreenFrameBufferへ切り替え
renderer->_offscreenFrameBuffer.BeginDraw();
// マスクをクリアする
// 1が無効(描かれない)領域、0が有効(描かれる)領域。(シェーダで Cd*Csで0に近い値をかけてマスクを作る。1をかけると何も起こらない)
renderer->_offscreenFrameBuffer.Clear(1.0f, 1.0f, 1.0f, 1.0f);
}
// 各マスクのレイアウトを決定していく
SetupLayoutBounds(renderer->IsUsingHighPrecisionMask() ? 0 : usingClipCount);
// 実際にマスクを生成する
// 全てのマスクをどの様にレイアウトして描くかを決定し、ClipContext , ClippedDrawContext に記憶する
for (csmUint32 clipIndex = 0; clipIndex < _clippingContextListForMask.GetSize(); clipIndex++)
{
// --- 実際に1つのマスクを描く ---
CubismClippingContext_CC* clipContext = _clippingContextListForMask[clipIndex];
csmRectF* allClippedDrawRect = clipContext->_allClippedDrawRect; //このマスクを使う、全ての描画オブジェクトの論理座標上の囲み矩形
csmRectF* layoutBoundsOnTex01 = clipContext->_layoutBounds; //この中にマスクを収める
const csmFloat32 MARGIN = 0.05f;
csmFloat32 scaleX = 0.0f;
csmFloat32 scaleY = 0.0f;
if (renderer->IsUsingHighPrecisionMask())
{
const csmFloat32 ppu = model.GetPixelsPerUnit();
const csmFloat32 maskPixelWidth = clipContext->_owner->_clippingMaskBufferSize.X;
const csmFloat32 maskPixelHeight = clipContext->_owner->_clippingMaskBufferSize.Y;
const csmFloat32 physicalMaskWidth = layoutBoundsOnTex01->Width * maskPixelWidth;
const csmFloat32 physicalMaskHeight = layoutBoundsOnTex01->Height * maskPixelHeight;
_tmpBoundsOnModel.SetRect(allClippedDrawRect);
if (_tmpBoundsOnModel.Width * ppu > physicalMaskWidth)
{
_tmpBoundsOnModel.Expand(allClippedDrawRect->Width * MARGIN, 0.0f);
scaleX = layoutBoundsOnTex01->Width / _tmpBoundsOnModel.Width;
}
else
{
scaleX = ppu / physicalMaskWidth;
}
if (_tmpBoundsOnModel.Height * ppu > physicalMaskHeight)
{
_tmpBoundsOnModel.Expand(0.0f, allClippedDrawRect->Height * MARGIN);
scaleY = layoutBoundsOnTex01->Height / _tmpBoundsOnModel.Height;
}
else
{
scaleY = ppu / physicalMaskHeight;
}
}
else
{
// モデル座標上の矩形を、適宜マージンを付けて使う
_tmpBoundsOnModel.SetRect(allClippedDrawRect);
_tmpBoundsOnModel.Expand(allClippedDrawRect->Width * MARGIN, allClippedDrawRect->Height * MARGIN);
//########## 本来は割り当てられた領域の全体を使わず必要最低限のサイズがよい
// シェーダ用の計算式を求める。回転を考慮しない場合は以下のとおり
// movePeriod' = movePeriod * scaleX + offX [[ movePeriod' = (movePeriod - tmpBoundsOnModel.movePeriod)*scale + layoutBoundsOnTex01.movePeriod ]]
scaleX = layoutBoundsOnTex01->Width / _tmpBoundsOnModel.Width;
scaleY = layoutBoundsOnTex01->Height / _tmpBoundsOnModel.Height;
}
// マスク生成時に使う行列を求める
{
// シェーダに渡す行列を求める <<<<<<<<<<<<<<<<<<<<<<<< 要最適化(逆順に計算すればシンプルにできる)
_tmpMatrix.LoadIdentity();
{
// Layout0..1 を -1..1に変換
_tmpMatrix.TranslateRelative(-1.0f, -1.0f);
_tmpMatrix.ScaleRelative(2.0f, 2.0f);
}
{
// view to Layout0..1
_tmpMatrix.TranslateRelative(layoutBoundsOnTex01->X, layoutBoundsOnTex01->Y); //new = [translate]
_tmpMatrix.ScaleRelative(scaleX, scaleY); //new = [translate][scale]
_tmpMatrix.TranslateRelative(-_tmpBoundsOnModel.X, -_tmpBoundsOnModel.Y);
//new = [translate][scale][translate]
}
// tmpMatrixForMask が計算結果
_tmpMatrixForMask.SetMatrix(_tmpMatrix.GetArray());
}
//--------- draw時の mask 参照用行列を計算
{
// シェーダに渡す行列を求める <<<<<<<<<<<<<<<<<<<<<<<< 要最適化(逆順に計算すればシンプルにできる)
_tmpMatrix.LoadIdentity();
{
_tmpMatrix.TranslateRelative(layoutBoundsOnTex01->X, layoutBoundsOnTex01->Y); //new = [translate]
_tmpMatrix.ScaleRelative(scaleX, scaleY); //new = [translate][scale]
_tmpMatrix.TranslateRelative(-_tmpBoundsOnModel.X, -_tmpBoundsOnModel.Y);
//new = [translate][scale][translate]
}
_tmpMatrixForDraw.SetMatrix(_tmpMatrix.GetArray());
}
clipContext->_matrixForMask.SetMatrix(_tmpMatrixForMask.GetArray());
clipContext->_matrixForDraw.SetMatrix(_tmpMatrixForDraw.GetArray());
if (!renderer->IsUsingHighPrecisionMask())
{
const csmInt32 clipDrawCount = clipContext->_clippingIdCount;
for (csmInt32 i = 0; i < clipDrawCount; i++)
{
const csmInt32 clipDrawIndex = clipContext->_clippingIdList[i];
// 頂点情報が更新されておらず、信頼性がない場合は描画をパスする
if (!model.GetDrawableDynamicFlagVertexPositionsDidChange(clipDrawIndex))
{
continue;
}
renderer->IsCulling(model.GetDrawableCulling(clipDrawIndex) != 0);
if (model.GetDrawableVertexCount(clipDrawIndex) <= 0)
{
continue;
}
// 今回専用の変換を適用して描く
// チャンネルも切り替える必要がある(A,R,G,B)
renderer->SetClippingContextBufferForMask(clipContext);
renderer->DrawMeshCC(clipContext->_clippingCommandBufferList->At(i),
model.GetDrawableTextureIndex(clipDrawIndex),
model.GetDrawableVertexIndexCount(clipDrawIndex),
model.GetDrawableVertexCount(clipDrawIndex),
const_cast<csmUint16*>(model.GetDrawableVertexIndices(clipDrawIndex)),
const_cast<csmFloat32*>(model.GetDrawableVertices(clipDrawIndex)),
reinterpret_cast<csmFloat32*>(const_cast<Core::csmVector2*>(model.GetDrawableVertexUvs(clipDrawIndex))),
model.GetMultiplyColor(clipDrawIndex),
model.GetScreenColor(clipDrawIndex),
model.GetDrawableOpacity(clipDrawIndex),
CubismRenderer::CubismBlendMode_Normal, //クリッピングは通常描画を強制
false // マスク生成時はクリッピングの反転使用は全く関係がない
);
}
}
}
if (!renderer->IsUsingHighPrecisionMask())
{
// --- 後処理 ---
renderer->_offscreenFrameBuffer.EndDraw(); // 描画対象を戻す
renderer->SetClippingContextBufferForMask(nullptr);
renderer->SetViewPort(lastViewport.x, lastViewport.y, lastViewport.w, lastViewport.h);
}
}
}
void CubismClippingManager_CC::CalcClippedDrawTotalBounds(CubismModel& model, CubismClippingContext_CC* clippingContext)
{
// 被クリッピングマスク(マスクされる描画オブジェクト)の全体の矩形
csmFloat32 clippedDrawTotalMinX = FLT_MAX, clippedDrawTotalMinY = FLT_MAX;
csmFloat32 clippedDrawTotalMaxX = -FLT_MAX, clippedDrawTotalMaxY = -FLT_MAX;
// このマスクが実際に必要か判定する
// このクリッピングを利用する「描画オブジェクト」がひとつでも使用可能であればマスクを生成する必要がある
const csmInt32 clippedDrawCount = clippingContext->_clippedDrawableIndexList->GetSize();
for (csmInt32 clippedDrawableIndex = 0; clippedDrawableIndex < clippedDrawCount; clippedDrawableIndex++)
{
// マスクを使用する描画オブジェクトの描画される矩形を求める
const csmInt32 drawableIndex = (*clippingContext->_clippedDrawableIndexList)[clippedDrawableIndex];
const csmInt32 drawableVertexCount = model.GetDrawableVertexCount(drawableIndex);
csmFloat32* drawableVertexes = const_cast<csmFloat32*>(model.GetDrawableVertices(drawableIndex));
csmFloat32 minX = FLT_MAX, minY = FLT_MAX;
csmFloat32 maxX = -FLT_MAX, maxY = -FLT_MAX;
csmInt32 loop = drawableVertexCount * Constant::VertexStep;
for (csmInt32 pi = Constant::VertexOffset; pi < loop; pi += Constant::VertexStep)
{
csmFloat32 x = drawableVertexes[pi];
csmFloat32 y = drawableVertexes[pi + 1];
if (x < minX) minX = x;
if (x > maxX) maxX = x;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
}
//
if (minX == FLT_MAX) continue; //有効な点がひとつも取れなかったのでスキップする
// 全体の矩形に反映
if (minX < clippedDrawTotalMinX) clippedDrawTotalMinX = minX;
if (minY < clippedDrawTotalMinY) clippedDrawTotalMinY = minY;
if (maxX > clippedDrawTotalMaxX) clippedDrawTotalMaxX = maxX;
if (maxY > clippedDrawTotalMaxY) clippedDrawTotalMaxY = maxY;
}
if (clippedDrawTotalMinX == FLT_MAX)
{
clippingContext->_allClippedDrawRect->X = 0.0f;
clippingContext->_allClippedDrawRect->Y = 0.0f;
clippingContext->_allClippedDrawRect->Width = 0.0f;
clippingContext->_allClippedDrawRect->Height = 0.0f;
clippingContext->_isUsing = false;
}
else
{
clippingContext->_isUsing = true;
csmFloat32 w = clippedDrawTotalMaxX - clippedDrawTotalMinX;
csmFloat32 h = clippedDrawTotalMaxY - clippedDrawTotalMinY;
clippingContext->_allClippedDrawRect->X = clippedDrawTotalMinX;
clippingContext->_allClippedDrawRect->Y = clippedDrawTotalMinY;
clippingContext->_allClippedDrawRect->Width = w;
clippingContext->_allClippedDrawRect->Height = h;
}
}
void CubismClippingManager_CC::SetupLayoutBounds(csmInt32 usingClipCount) const
{
if (usingClipCount <= 0)
{// この場合は一つのマスクターゲットを毎回クリアして使用する
for (csmUint32 index = 0; index < _clippingContextListForMask.GetSize(); index++)
{
CubismClippingContext_CC* cc = _clippingContextListForMask[index];
cc->_layoutChannelNo = 0; // どうせ毎回消すので固定で良い
cc->_layoutBounds->X = 0.0f;
cc->_layoutBounds->Y = 0.0f;
cc->_layoutBounds->Width = 1.0f;
cc->_layoutBounds->Height = 1.0f;
}
return;
}
// ひとつのRenderTextureを極力いっぱいに使ってマスクをレイアウトする
// マスクグループの数が4以下ならRGBA各チャンネルに1つずつマスクを配置し、5以上6以下ならRGBAを2,2,1,1と配置する
// RGBAを順番に使っていく。
const csmInt32 div = usingClipCount / ColorChannelCount; //1チャンネルに配置する基本のマスク個数
const csmInt32 mod = usingClipCount % ColorChannelCount; //余り、この番号のチャンネルまでに1つずつ配分する
// RGBAそれぞれのチャンネルを用意していく(0:R , 1:G , 2:B, 3:A, )
csmInt32 curClipIndex = 0; //順番に設定していく
for (csmInt32 channelNo = 0; channelNo < ColorChannelCount; channelNo++)
{
// このチャンネルにレイアウトする数
const csmInt32 layoutCount = div + (channelNo < mod ? 1 : 0);
// 分割方法を決定する
if (layoutCount == 0)
{
// 何もしない
}
else if (layoutCount == 1)
{
//全てをそのまま使う
CubismClippingContext_CC* cc = _clippingContextListForMask[curClipIndex++];
cc->_layoutChannelNo = channelNo;
cc->_layoutBounds->X = 0.0f;
cc->_layoutBounds->Y = 0.0f;
cc->_layoutBounds->Width = 1.0f;
cc->_layoutBounds->Height = 1.0f;
}
else if (layoutCount == 2)
{
for (csmInt32 i = 0; i < layoutCount; i++)
{
const csmInt32 xpos = i % 2;
CubismClippingContext_CC* cc = _clippingContextListForMask[curClipIndex++];
cc->_layoutChannelNo = channelNo;
cc->_layoutBounds->X = xpos * 0.5f;
cc->_layoutBounds->Y = 0.0f;
cc->_layoutBounds->Width = 0.5f;
cc->_layoutBounds->Height = 1.0f;
//UVを2つに分解して使う
}
}
else if (layoutCount <= 4)
{
//4分割して使う
for (csmInt32 i = 0; i < layoutCount; i++)
{
const csmInt32 xpos = i % 2;
const csmInt32 ypos = i / 2;
CubismClippingContext_CC* cc = _clippingContextListForMask[curClipIndex++];
cc->_layoutChannelNo = channelNo;
cc->_layoutBounds->X = xpos * 0.5f;
cc->_layoutBounds->Y = ypos * 0.5f;
cc->_layoutBounds->Width = 0.5f;
cc->_layoutBounds->Height = 0.5f;
}
}
else if (layoutCount <= 9)
{
//9分割して使う
for (csmInt32 i = 0; i < layoutCount; i++)
{
const csmInt32 xpos = i % 3;
const csmInt32 ypos = i / 3;
CubismClippingContext_CC* cc = _clippingContextListForMask[curClipIndex++];
cc->_layoutChannelNo = channelNo;
cc->_layoutBounds->X = xpos / 3.0f;
cc->_layoutBounds->Y = ypos / 3.0f;
cc->_layoutBounds->Width = 1.0f / 3.0f;
cc->_layoutBounds->Height = 1.0f / 3.0f;
}
}
else
{
CubismLogError("not supported mask count : %d", layoutCount);
// 開発モードの場合は停止させる
CSM_ASSERT(0);
// 引き続き実行する場合、 SetupShaderProgramでオーバーアクセスが発生するので仕方なく適当に入れておく
// もちろん描画結果はろくなことにならない
for (csmInt32 i = 0; i < layoutCount; i++)
{
CubismClippingContext_CC* cc = _clippingContextListForMask[curClipIndex++];
cc->_layoutChannelNo = 0;
cc->_layoutBounds->X = 0.0f;
cc->_layoutBounds->Y = 0.0f;
cc->_layoutBounds->Width = 1.0f;
cc->_layoutBounds->Height = 1.0f;
}
}
}
}
CubismRenderer::CubismTextureColor* CubismClippingManager_CC::GetChannelFlagAsColor(csmInt32 channelNo)
{
return _channelColors[channelNo];
}
csmVector<CubismClippingContext_CC*>* CubismClippingManager_CC::GetClippingContextListForDraw()
{
return &_clippingContextListForDraw;
}
void CubismClippingManager_CC::SetClippingMaskBufferSize(csmFloat32 width, csmFloat32 height)
{
_clippingMaskBufferSize = CubismVector2(width, height);
}
CubismVector2 CubismClippingManager_CC::GetClippingMaskBufferSize() const
{
return _clippingMaskBufferSize;
}
/*********************************************************************************************************************
* CubismClippingContext
********************************************************************************************************************/
CubismClippingContext_CC::CubismClippingContext_CC(
CubismClippingManager_CC* manager, CubismModel& model, const csmInt32* clippingDrawableIndices, csmInt32 clipCount)
:_isUsing(false)
{
_owner = manager;
// クリップしている(=マスク用の)Drawableのインデックスリスト
_clippingIdList = clippingDrawableIndices;
// マスクの数
_clippingIdCount = clipCount;
_layoutChannelNo = 0;
_allClippedDrawRect = CSM_NEW csmRectF();
_layoutBounds = CSM_NEW csmRectF();
_clippedDrawableIndexList = CSM_NEW csmVector<csmInt32>();
_clippingCommandBufferList = CSM_NEW csmVector<CubismDrawCommand_CC*>();
for (csmUint32 i = 0; i < _clippingIdCount; ++i)
{
const csmInt32 clippingId = _clippingIdList[i];
const csmInt32 drawableVertexCount = model.GetDrawableVertexCount(clippingId);
const csmInt32 drawableVertexIndexCount = model.GetDrawableVertexIndexCount(clippingId);
auto drawCommandBuffer = CSM_NEW CubismDrawCommand_CC();
drawCommandBuffer->CreateVertexBuffer(sizeof(csmFloat32) * 4, drawableVertexCount); // Vertices + UVs
drawCommandBuffer->CreateIndexBuffer(drawableVertexIndexCount);
_clippingCommandBufferList->PushBack(drawCommandBuffer);
}
}
CubismClippingContext_CC::~CubismClippingContext_CC()
{
CSM_SAFE_DELETE_NULL(_layoutBounds);
CSM_SAFE_DELETE_NULL(_allClippedDrawRect);
CSM_SAFE_DELETE_NULL(_clippedDrawableIndexList);
if (_clippingCommandBufferList)
{
for (csmUint32 i = 0; i < _clippingCommandBufferList->GetSize(); ++i)
{
CSM_DELETE(_clippingCommandBufferList->At(i));
_clippingCommandBufferList->At(i) = nullptr;
}
CSM_DELETE(_clippingCommandBufferList);
_clippingCommandBufferList = nullptr;
}
}
void CubismClippingContext_CC::AddClippedDrawable(csmInt32 drawableIndex)
{
_clippedDrawableIndexList->PushBack(drawableIndex);
}
CubismClippingManager_CC* CubismClippingContext_CC::GetClippingManager()
{
return _owner;
}
/*********************************************************************************************************************
* CubismDrawProfile_OpenGL
********************************************************************************************************************/
CubismRendererProfile_CC::CubismRendererProfile_CC()
{
ccr = cocos2d::Director::getInstance()->getRenderer();
}
void CubismRendererProfile_CC::Save()
{
_lastScissorTest = ccr->getScissorTest();
_lastStencilTest = ccr->getStencilTest();
_lastDepthTest = ccr->getDepthTest();
_lastCullFace = ccr->getCullMode();
_lastWinding = ccr->getWinding();
_lastViewport = ccr->getViewport();
}
void CubismRendererProfile_CC::Restore()
{
ccr->setScissorTest(_lastScissorTest);
ccr->setStencilTest(_lastStencilTest);
ccr->setDepthTest(_lastDepthTest);
ccr->setCullMode(_lastCullFace);
ccr->setWinding(_lastWinding);
ccr->setViewPort(_lastViewport.x, _lastViewport.y, _lastViewport.w, _lastViewport.h);
}
/*********************************************************************************************************************
* CubismShader_CC
********************************************************************************************************************/
namespace {
const csmInt32 ShaderCount = 19; ///< シェーダの数 = マスク生成用 + (通常 + 加算 + 乗算) * (マスク無 + マスク有 + マスク有反転 + マスク無の乗算済アルファ対応版 + マスク有の乗算済アルファ対応版 + マスク有反転の乗算済アルファ対応版)
CubismShader_CC* s_instance;
}
enum ShaderNames
{
// SetupMask
ShaderNames_SetupMask,
//Normal
ShaderNames_Normal,
ShaderNames_NormalMasked,
ShaderNames_NormalMaskedInverted,
ShaderNames_NormalPremultipliedAlpha,
ShaderNames_NormalMaskedPremultipliedAlpha,
ShaderNames_NormalMaskedInvertedPremultipliedAlpha,
//Add
ShaderNames_Add,
ShaderNames_AddMasked,
ShaderNames_AddMaskedInverted,
ShaderNames_AddPremultipliedAlpha,
ShaderNames_AddMaskedPremultipliedAlpha,
ShaderNames_AddMaskedPremultipliedAlphaInverted,
//Mult
ShaderNames_Mult,
ShaderNames_MultMasked,
ShaderNames_MultMaskedInverted,
ShaderNames_MultPremultipliedAlpha,
ShaderNames_MultMaskedPremultipliedAlpha,
ShaderNames_MultMaskedPremultipliedAlphaInverted,
};
void CubismShader_CC::ReleaseShaderProgram()
{
std::set<backend::Program*> programs;
for (csmUint32 i = 0; i < _shaderSets.GetSize(); i++)
{
if (_shaderSets[i]->ShaderProgram)
{
programs.insert(_shaderSets[i]->ShaderProgram);
_shaderSets[i]->ShaderProgram = nullptr;
CSM_DELETE(_shaderSets[i]);
}
}
for (auto& p : programs)
{
CC_SAFE_RELEASE(p);
}
}
#if defined(CC_PLATFORM_MOBILE)
#define FRAG_SHADER_HEADER "#version 100\nprecision mediump float;\n"
#define VERT_SHADER_HEADER "#version 100\n"
#else
#define FRAG_SHADER_HEADER "#version 120\n"
#define VERT_SHADER_HEADER "#version 120\n"
#endif
// SetupMask
static const csmChar* VertShaderSrcSetupMask =
#ifndef CC_USE_GFX
FRAG_SHADER_HEADER
"attribute vec2 a_position;"
"attribute vec2 a_texCoord;"
"varying vec2 v_texCoord;"
"varying vec4 v_myPos;"
"uniform mat4 u_clipMatrix;"
"void main()"
"{"
"vec4 pos = vec4(a_position.x, a_position.y, 0.0, 1.0);"
"gl_Position = u_clipMatrix * pos;"
"v_myPos = u_clipMatrix * pos;"
"v_texCoord = a_texCoord;"
"v_texCoord.y = 1.0 - v_texCoord.y;"
"}";
#else
R"(
layout(location=0) in vec2 a_position;
layout(location=1) in vec2 a_texCoord;
layout(std140, binding=0) uniform VSBlock
{
mat4 u_clipMatrix;
};
layout(location=0) out vec4 v_myPos;
layout(location=1) out vec2 v_texCoord;
void main()
{
vec4 pos = vec4(a_position.x, a_position.y, 0.0, 1.0);
gl_Position = u_clipMatrix * pos;
v_myPos = u_clipMatrix * pos;
v_texCoord = a_texCoord;
v_texCoord.y = 1.0 - v_texCoord.y;
}
)";
static const UniformBlock VertShaderSrcSetupMaskBlock = {
0,0,"VSBlock",
UniformList({
{"u_clipMatrix", Type::MAT4, 1},
}),
1
};
#endif
static const csmChar* FragShaderSrcSetupMask =
#ifndef CC_USE_GFX
FRAG_SHADER_HEADER
"varying vec2 v_texCoord;"
"varying vec4 v_myPos;"
"uniform sampler2D s_texture0;"
"uniform vec4 u_channelFlag;"
"uniform vec4 u_baseColor;"
"void main()"
"{"
"float isInside = "
" step(u_baseColor.x, v_myPos.x/v_myPos.w)"
"* step(u_baseColor.y, v_myPos.y/v_myPos.w)"
"* step(v_myPos.x/v_myPos.w, u_baseColor.z)"
"* step(v_myPos.y/v_myPos.w, u_baseColor.w);"
"gl_FragColor = u_channelFlag * texture2D(s_texture0 , v_texCoord).a * isInside;"
"}";
#else
R"(
layout(location=0) in vec4 v_myPos;
layout(location=1) in vec2 v_texCoord;
layout(std140, binding=1) uniform FSBlock
{
vec4 u_channelFlag;
vec4 u_baseColor;
};
layout(binding=2) uniform sampler2D s_texture0;
layout(location=0) out vec4 cc_FragColor;
void main()
{
float isInside =
step(u_baseColor.x, v_myPos.x/v_myPos.w)
* step(u_baseColor.y, v_myPos.y/v_myPos.w)
* step(v_myPos.x/v_myPos.w, u_baseColor.z)
* step(v_myPos.y/v_myPos.w, u_baseColor.w);
cc_FragColor = u_channelFlag * texture(s_texture0 , v_texCoord).a * isInside;
}
)";
static const UniformBlock FragShaderSrcSetupMaskBlock = {
0,1,"FSBlock",
UniformList({
{"u_channelFlag", Type::FLOAT4, 1},
{"u_baseColor", Type::FLOAT4, 1},
}),
1
};
static const UniformSamplerTextureList FragShaderSrcSetupMaskTextures = {
UniformSamplerTexture {
0,2,"s_texture0",Type::SAMPLER2D,1}
};
#endif
#if CSM_RENDERER_EXT
static const csmChar* FragShaderSrcSetupMaskTegra =
"#version 100\n"
"#extension GL_NV_shader_framebuffer_fetch : enable\n"
"precision mediump float;"
"varying vec2 v_texCoord;"
"varying vec4 v_myPos;"
"uniform sampler2D s_texture0;"
"uniform vec4 u_channelFlag;"
"uniform vec4 u_baseColor;"
"void main()"
"{"
"float isInside = "
" step(u_baseColor.x, v_myPos.x/v_myPos.w)"
"* step(u_baseColor.y, v_myPos.y/v_myPos.w)"
"* step(v_myPos.x/v_myPos.w, u_baseColor.z)"
"* step(v_myPos.y/v_myPos.w, u_baseColor.w);"
"gl_FragColor = u_channelFlag * texture2D(s_texture0 , v_texCoord).a * isInside;"
"}";
#endif
//----- バーテックスシェーダプログラム -----
// Normal & Add & Mult 共通
static const csmChar* VertShaderSrc =
#ifndef CC_USE_GFX
VERT_SHADER_HEADER
"attribute vec2 a_position;" //v.vertex
"attribute vec2 a_texCoord;" //v.texcoord
"varying vec2 v_texCoord;" //v2f.texcoord
"uniform mat4 u_matrix;"
"void main()"
"{"
"vec4 pos = vec4(a_position.x, a_position.y, 0.0, 1.0);"
"gl_Position = u_matrix * pos;"
"v_texCoord = a_texCoord;"
"v_texCoord.y = 1.0 - v_texCoord.y;"
"}";
#else
R"(
layout(location=0) in vec2 a_position;
layout(location=1) in vec2 a_texCoord;
layout(std140, binding=0) uniform VSBlock
{
mat4 u_matrix;
};
layout(location=0) out vec2 v_texCoord;
void main()
{
vec4 pos = vec4(a_position.x, a_position.y, 0.0, 1.0);
gl_Position = u_matrix * pos;
v_texCoord = a_texCoord;
v_texCoord.y = 1.0 - v_texCoord.y;
}
)";
static const UniformBlock VertShaderSrcBlock = {
0,0,"VSBlock",
UniformList({
{"u_matrix", Type::MAT4, 1},
}),
1
};
#endif
// Normal & Add & Mult 共通(クリッピングされたものの描画用)
static const csmChar* VertShaderSrcMasked =
#ifndef CC_USE_GFX
VERT_SHADER_HEADER
"attribute vec2 a_position;"
"attribute vec2 a_texCoord;"
"varying vec2 v_texCoord;"
"varying vec4 v_clipPos;"
"uniform mat4 u_matrix;"
"uniform mat4 u_clipMatrix;"
"void main()"
"{"
"vec4 pos = vec4(a_position.x, a_position.y, 0.0, 1.0);"
"gl_Position = u_matrix * pos;"
"v_clipPos = u_clipMatrix * pos;"
"v_texCoord = a_texCoord;"
"v_texCoord.y = 1.0 - v_texCoord.y;"
"}";
#else
R"(
layout(location=0) in vec2 a_position;
layout(location=1) in vec2 a_texCoord;
layout(std140, binding=0) uniform VSBlock
{
mat4 u_matrix;
mat4 u_clipMatrix;
};
layout(location=0) out vec2 v_texCoord;
layout(location=1) out vec4 v_clipPos;
void main()
{
vec4 pos = vec4(a_position.x, a_position.y, 0.0, 1.0);
gl_Position = u_matrix * pos;
v_clipPos = u_clipMatrix * pos;
v_texCoord = a_texCoord;
v_texCoord.y = 1.0 - v_texCoord.y;
}
)";
static const UniformBlock VertShaderSrcMaskedBlock = {
0,0,"VSBlock",
UniformList({
{"u_matrix", Type::MAT4, 1},
{"u_clipMatrix", Type::MAT4, 1},
}),
1
};
#endif
//----- フラグメントシェーダプログラム -----
// Normal & Add & Mult 共通
static const csmChar* FragShaderSrc =
#ifndef CC_USE_GFX
FRAG_SHADER_HEADER
"varying vec2 v_texCoord;" //v2f.texcoord
"uniform sampler2D s_texture0;" //_MainTex
"uniform vec4 u_baseColor;" //v2f.color
"uniform vec4 u_multiplyColor;"
"uniform vec4 u_screenColor;"
"void main()"
"{"
"vec4 texColor = texture2D(s_texture0 , v_texCoord);"
"texColor.rgb = texColor.rgb * u_multiplyColor.rgb;"
"texColor.rgb = texColor.rgb + u_screenColor.rgb - (texColor.rgb * u_screenColor.rgb);"
"vec4 color = texColor * u_baseColor;"
"gl_FragColor = vec4(color.rgb * color.a, color.a);"
"}";
#else
R"(
layout(location=0) in vec2 v_texCoord;
layout(std140, binding=1) uniform FSBlock
{
vec4 u_baseColor;
vec4 u_multiplyColor;
vec4 u_screenColor;
};
layout(binding=2) uniform sampler2D s_texture0;
layout(location=0) out vec4 cc_FragColor;
void main()
{
vec4 texColor = texture(s_texture0 , v_texCoord);
texColor.rgb = texColor.rgb * u_multiplyColor.rgb;
texColor.rgb = texColor.rgb + u_screenColor.rgb - (texColor.rgb * u_screenColor.rgb);
vec4 color = texColor * u_baseColor;
cc_FragColor = vec4(color.rgb * color.a, color.a);
}
)";
static const UniformBlock FragShaderSrcBlock = {
0,1,"FSBlock",
UniformList({
{"u_baseColor", Type::FLOAT4, 1},
{"u_multiplyColor", Type::FLOAT4, 1},
{"u_screenColor", Type::FLOAT4, 1},
}),
1
};
static const UniformSamplerTextureList FragShaderSrcTextures = {
UniformSamplerTexture {
0,2,"s_texture0",Type::SAMPLER2D,1}
};
#endif
#if CSM_RENDERER_EXT