-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.es.js
1727 lines (1454 loc) · 54 KB
/
index.es.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
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
import karas from 'karas';
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
Object.defineProperty(Constructor, "prototype", {
writable: false
});
return Constructor;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
Object.defineProperty(subClass, "prototype", {
writable: false
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
if (_i == null) return;
var _arr = [];
var _n = true;
var _d = false;
var _s, _e;
try {
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var inject = karas.inject,
math$2 = karas.math;
function parseAndLoadTex(tex, cb) {
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var src = props.imagePath || tex.imagePath;
var texHash = {};
inject.measureImg(src, function () {
tex.SubTexture.forEach(function (item) {
var name = item.name,
x = item.x,
y = item.y,
width = item.width,
height = item.height,
_item$frameX = item.frameX,
frameX = _item$frameX === void 0 ? 0 : _item$frameX,
_item$frameY = item.frameY,
frameY = _item$frameY === void 0 ? 0 : _item$frameY,
_item$frameWidth = item.frameWidth,
frameWidth = _item$frameWidth === void 0 ? width : _item$frameWidth,
_item$frameHeight = item.frameHeight,
frameHeight = _item$frameHeight === void 0 ? height : _item$frameHeight;
texHash[name] = {
name: name,
x: x,
y: y,
width: width,
height: height,
frameX: frameX,
frameY: frameY,
frameWidth: frameWidth,
frameHeight: frameHeight,
source: inject.IMG[src].source
};
});
cb(texHash);
});
}
function parseSke(ske, texHash) {
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var globalFrameRate = ske.frameRate,
armature = ske.armature;
var currentArmature = armature[0];
if (props.armature) {
for (var i = 0, len = armature.length; i < len; i++) {
var item = armature[i];
if (item.name === props.armature) {
currentArmature = item;
break;
}
}
if (!currentArmature) {
throw new Error('Can not find armature: ' + props.armature);
}
}
if (!currentArmature) {
console.warn('No armature data');
return;
}
var _currentArmature = currentArmature,
name = _currentArmature.name,
bone = _currentArmature.bone,
slot = _currentArmature.slot,
skin = _currentArmature.skin,
frameRate = _currentArmature.frameRate,
animation = _currentArmature.animation,
defaultActions = _currentArmature.defaultActions,
canvas = _currentArmature.canvas;
var boneHash = parseBone(bone);
var slotHash = parseSlot(slot);
var skinHash = parseSkin(skin, texHash, props);
var animationHash = parseAnimation(animation, frameRate || globalFrameRate || 60, boneHash);
return {
name: name,
bone: bone,
boneHash: boneHash,
slot: slot,
slotHash: slotHash,
skin: skin[0].slot,
skinHash: skinHash,
animationHash: animationHash,
defaultActions: defaultActions,
canvas: canvas
};
}
function parseSlot(data) {
var hash = {};
data.forEach(function (item) {
hash[item.name] = item;
});
return hash;
}
function parseBone(data) {
var hash = {}; // bone数据形成tree结构,符合dom格式,第一个一定是root
var root = data[0];
root.children = [];
hash[root.name] = root;
data.forEach(function (item, i) {
var name = item.name,
parent = item.parent,
_item$transform = item.transform,
transform = _item$transform === void 0 ? {} : _item$transform;
if (parent) {
hash[parent].children.push(item);
}
hash[name] = item;
item.children = [];
item.index = i; // 静态变换样式,可能某个骨骼没动画
var matrix = math$2.matrix.identity();
if (transform.x || transform.y) {
var m = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, transform.x || 0, transform.y || 0, 0, 1];
matrix = math$2.matrix.multiply(matrix, m);
}
if (transform.skX) {
var d = math$2.geom.d2r(transform.skX);
var sin = Math.sin(d);
var cos = Math.cos(d);
var _m = [cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
matrix = math$2.matrix.multiply(matrix, _m);
}
if (transform.scX !== undefined || transform.scY !== undefined) {
var _m2 = [transform.scX === undefined ? 1 : transform.scX, 0, 0, 0, 0, transform.scY === undefined ? 1 : transform.scY, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
matrix = karas.math.matrix.multiply(matrix, _m2);
}
item.matrix = matrix;
});
return hash;
}
function parseSkin(data, texHash, props) {
var hash = {};
data[0].slot.forEach(function (item) {
var slotName = item.name,
display = item.display;
hash[slotName] = item;
display.forEach(function (item) {
var type = item.type,
name = item.name,
path = item.path; // mesh网格分析三角形
if (type === 'mesh') {
(function () {
var vertices = item.vertices,
triangles = item.triangles,
uvs = item.uvs,
weights = item.weights,
bonePose = item.bonePose;
var weightHash;
var bonePoseHash; // 有权重则绑定骨骼,坐标系为世界;
// 没有权重时,完全属于父骨骼,类似普通贴图行为
if (weights) {
// 权重格式化hash
weightHash = {};
for (var i = 0, len = weights.length, verticesIndex = 0; i < len; i++) {
var num = weights[i];
var list = [];
for (var j = i + 1; j < i + 1 + num * 2; j += 2) {
var index = weights[j];
var value = weights[j + 1];
list.push({
index: index,
value: value
});
}
weightHash[verticesIndex++] = list;
i += num * 2;
} // 骨骼初始pose格式化和世界坐标
bonePoseHash = {};
for (var _i = 0, _len = bonePose.length; _i < _len; _i += 7) {
var _index = bonePose[_i];
var m = bonePose.slice(_i + 1, _i + 7);
var matrix = [m[0], m[1], 0, 0, m[2], m[3], 0, 0, 0, 0, 1, 0, m[4], m[5], 0, 1];
var coords = math$2.matrix.calPoint([0, 0], matrix);
bonePoseHash[_index] = {
coords: coords,
pose: matrix
};
}
} // 顶点格式化,相对于骨骼点的x/y位移差值
var verticesList = item.verticesList = [];
var _loop = function _loop(_i2, _len2) {
var index = _i2 >> 1;
var x = vertices[_i2];
var y = vertices[_i2 + 1];
var res = {
index: index,
x: x,
y: y
};
verticesList.push(res); // 有添加绑定骨骼才有权重
if (weightHash) {
res.weightList = [];
var weight = weightHash[index];
weight.forEach(function (item) {
var index = item.index,
value = item.value;
var _bonePoseHash$index = bonePoseHash[index],
coords = _bonePoseHash$index.coords,
pose = _bonePoseHash$index.pose; // 先求骨头的角度,逆向选择至水平后,平移x/y的差值
var _math$matrix$calPoint7 = math$2.matrix.calPoint([0, 0], pose),
_math$matrix$calPoint8 = _slicedToArray(_math$matrix$calPoint7, 2),
x0 = _math$matrix$calPoint8[0],
y0 = _math$matrix$calPoint8[1];
var _math$matrix$calPoint9 = math$2.matrix.calPoint([1, 0], pose),
_math$matrix$calPoint10 = _slicedToArray(_math$matrix$calPoint9, 2),
x1 = _math$matrix$calPoint10[0],
y1 = _math$matrix$calPoint10[1];
var dx = x1 - x0;
var dy = y1 - y0;
var theta; // 4个象限分开判断
if (dx >= 0 && dy >= 0) {
theta = -Math.atan(Math.abs(dy / dx));
} else if (dx < 0 && dy >= 0) {
theta = -Math.PI + Math.atan(Math.abs(dy / dx));
} else if (dx < 0 && dy < 0) {
theta = Math.PI - Math.atan(Math.abs(dy / dx));
} else {
theta = Math.atan(Math.abs(dy / dx));
}
var rotate = [Math.cos(theta), Math.sin(theta), 0, 0, -Math.sin(theta), Math.cos(theta), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
var translate = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x - coords[0], y - coords[1], 0, 1];
var matrix = math$2.matrix.multiply(rotate, translate);
res.weightList.push({
index: index,
value: value,
matrix: matrix
});
});
}
};
for (var _i2 = 0, _len2 = vertices.length; _i2 < _len2; _i2 += 2) {
_loop(_i2);
} // 三角形,切割图片坐标
var tex = texHash[path || name];
var width = tex.width,
height = tex.height;
var triangleList = item.triangleList = [];
for (var i = 0, len = triangles.length; i < len; i += 3) {
var i1 = triangles[i];
var i2 = triangles[i + 1];
var i3 = triangles[i + 2]; // uv坐标
var p1x = uvs[i1 * 2];
var p1y = uvs[i1 * 2 + 1];
var p2x = uvs[i2 * 2];
var p2y = uvs[i2 * 2 + 1];
var p3x = uvs[i3 * 2];
var p3y = uvs[i3 * 2 + 1]; // uv贴图坐标根据尺寸映射真实坐标
var x1 = p1x * width;
var y1 = p1y * height;
var x2 = p2x * width;
var y2 = p2y * height;
var x3 = p3x * width;
var y3 = p3y * height; // 从内心往外扩展约0.25px,可参数指定
var _math$geom$triangleIn = math$2.geom.triangleIncentre(x1, y1, x2, y2, x3, y3),
_math$geom$triangleIn2 = _slicedToArray(_math$geom$triangleIn, 2),
x0 = _math$geom$triangleIn2[0],
y0 = _math$geom$triangleIn2[1];
var px = parseFloat(props.enlarge);
if (isNaN(px)) {
px = 0.25;
} // 单独为slot配置的扩展参数
if (props.enlargeSlot && props.enlargeSlot.hasOwnProperty(slotName)) {
var n = parseFloat(props.enlargeSlot[slotName]);
if (isNaN(n)) {
n = 0.25;
}
px = n;
}
var scale = px ? triangleScale(x0, y0, x1, y1, x2, y2, x3, y3, px) : 1; // 以内心为transformOrigin
var m = math$2.matrix.identity();
m[12] = -x0;
m[13] = -y0; // 缩放
var t = math$2.matrix.identity();
t[0] = t[5] = scale;
m = math$2.matrix.multiply(t, m); // 移动回去
t[12] = x0;
t[13] = y0;
m = math$2.matrix.multiply(t, m); // 获取扩展后的三角形顶点坐标
var _math$matrix$calPoint = math$2.matrix.calPoint([x1, y1], m),
_math$matrix$calPoint2 = _slicedToArray(_math$matrix$calPoint, 2),
sx1 = _math$matrix$calPoint2[0],
sy1 = _math$matrix$calPoint2[1];
var _math$matrix$calPoint3 = math$2.matrix.calPoint([x2, y2], m),
_math$matrix$calPoint4 = _slicedToArray(_math$matrix$calPoint3, 2),
sx2 = _math$matrix$calPoint4[0],
sy2 = _math$matrix$calPoint4[1];
var _math$matrix$calPoint5 = math$2.matrix.calPoint([x3, y3], m),
_math$matrix$calPoint6 = _slicedToArray(_math$matrix$calPoint5, 2),
sx3 = _math$matrix$calPoint6[0],
sy3 = _math$matrix$calPoint6[1]; // 三角形所在矩形距离左上角原点的坐标,以此做img切割最小尺寸化,以及变换原点计算
// let [ox, oy, ow, oh] = triangleOriginCoords(sx1, sy1, sx2, sy2, sx3, sy3);
triangleList.push({
index: Math.round(i / 3),
indexList: [i1, i2, i3],
// ox,
// oy,
// ow,
// oh,
// points: [
// [p1x, p1y],
// [p2x, p2y],
// [p3x, p3y]
// ],
coords: [[x1, y1], [x2, y2], [x3, y3]],
scale: scale,
scaleCoords: [[sx1, sy1], [sx2, sy2], [sx3, sy3]],
width: width,
height: height
});
}
})();
}
});
});
return hash;
}
/**
* 将三角形从内心缩放指定像素
* @param x0
* @param y0
* @param x1
* @param y1
* @param x2
* @param y2
* @param x3
* @param y3
* @param px 缩放多少像素,可正负
* @returns {number} 缩放倍数
*/
function triangleScale(x0, y0, x1, y1, x2, y2, x3, y3) {
var px = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : 0;
// 内心到任意一边的距离
var a = y2 - y1;
var b = x1 - x2;
var c = x2 * y1 - x1 * y2;
var d = Math.abs(a * x0 + b * y0 + c) / Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
return (d + px) / d;
}
function parseAnimation(data, frameRate, boneHash) {
var hash = {};
data.forEach(function (item) {
var duration = item.duration,
playTimes = item.playTimes,
name = item.name,
_item$bone = item.bone,
bone = _item$bone === void 0 ? [] : _item$bone,
_item$slot = item.slot,
slot = _item$slot === void 0 ? [] : _item$slot,
_item$ffd = item.ffd,
ffd = _item$ffd === void 0 ? [] : _item$ffd;
hash[name] = item;
item.options = {
duration: 1000 * duration / frameRate,
iterations: playTimes === 0 ? Infinity : playTimes,
fill: 'forwards'
}; // 骨骼动画列表
item.boneAnimationList = bone.map(function (item) {
var name = item.name,
translateFrame = item.translateFrame,
rotateFrame = item.rotateFrame,
scaleFrame = item.scaleFrame;
var _boneHash$name$transf = boneHash[name].transform,
originTransform = _boneHash$name$transf === void 0 ? {} : _boneHash$name$transf;
var res = {
name: name,
list: []
};
if (translateFrame) {
var offsetSum = 0;
var last;
var value = translateFrame.map(function (frame) {
var _frame$duration = frame.duration,
d = _frame$duration === void 0 ? 1 : _frame$duration;
var easingFn = getEasing(frame);
var offset = offsetSum / duration;
offsetSum += d;
var _originTransform$x = originTransform.x,
x = _originTransform$x === void 0 ? 0 : _originTransform$x,
_originTransform$y = originTransform.y,
y = _originTransform$y === void 0 ? 0 : _originTransform$y;
var res = {
type: 0,
translateX: (frame.x || 0) + x,
translateY: (frame.y || 0) + y,
offset: offset,
easingFn: easingFn
};
if (last) {
last.dx = res.translateX - last.translateX;
last.dy = res.translateY - last.translateY;
}
last = res;
return res;
});
res.list.push(value);
}
if (rotateFrame) {
var _offsetSum = 0;
var _last;
var _value = rotateFrame.map(function (frame) {
var _frame$duration2 = frame.duration,
d = _frame$duration2 === void 0 ? 1 : _frame$duration2;
var easingFn = getEasing(frame);
var offset = _offsetSum / duration;
_offsetSum += d;
var _originTransform$skX = originTransform.skX,
skX = _originTransform$skX === void 0 ? 0 : _originTransform$skX;
var res = {
type: 1,
rotateZ: (frame.rotate || 0) + skX,
offset: offset,
easingFn: easingFn
};
if (_last) {
_last.dz = res.rotateZ - _last.rotateZ;
}
_last = res;
return res;
});
res.list.push(_value);
}
if (scaleFrame) {
var _offsetSum2 = 0;
var _last2;
var _value2 = scaleFrame.map(function (frame) {
var _frame$duration3 = frame.duration,
d = _frame$duration3 === void 0 ? 1 : _frame$duration3;
var easingFn = getEasing(frame);
var offset = _offsetSum2 / duration;
_offsetSum2 += d;
var _originTransform$scX = originTransform.scX,
scX = _originTransform$scX === void 0 ? 1 : _originTransform$scX,
_originTransform$scY = originTransform.scY,
scY = _originTransform$scY === void 0 ? 1 : _originTransform$scY;
var res = {
type: 2,
scaleX: (frame.x === undefined ? 1 : frame.x) * scX,
scaleY: (frame.y === undefined ? 1 : frame.y) * scY,
offset: offset,
easingFn: easingFn
};
if (_last2) {
_last2.dx = res.scaleX - _last2.scaleX;
_last2.dy = res.scaleY - _last2.scaleY;
}
_last2 = res;
return res;
});
res.list.push(_value2);
}
return res;
}); // 插槽动画列表
item.slotAnimationList = slot.map(function (item) {
var displayFrame = item.displayFrame,
colorFrame = item.colorFrame;
if (displayFrame) {
var offsetSum = 0;
displayFrame.forEach(function (frame) {
var _frame$duration4 = frame.duration,
d = _frame$duration4 === void 0 ? 1 : _frame$duration4;
var offset = offsetSum / duration;
offsetSum += d;
frame.offset = offset;
});
}
if (colorFrame) {
var _offsetSum3 = 0;
var last;
colorFrame.forEach(function (frame) {
var _frame$duration5 = frame.duration,
d = _frame$duration5 === void 0 ? 1 : _frame$duration5;
frame.easingFn = getEasing(frame);
var offset = _offsetSum3 / duration;
_offsetSum3 += d;
frame.offset = offset; // 没有value就用默认值
if (!frame.value) {
frame.value = {
aM: 100
};
}
if (frame.value.aM === undefined) {
frame.value.aM = 100;
}
if (last) {
last.da = frame.value.aM - last.value.aM;
}
last = frame;
});
}
return item;
}); // 自由变形列表
var ffdAnimationHash = item.ffdAnimationHash = {};
item.ffdAnimationList = ffd.map(function (item) {
var name = item.name,
slot = item.slot,
frame = item.frame; // db限制了不能出现在名字里
ffdAnimationHash[slot + '>' + name] = item;
if (frame) {
var offsetSum = 0;
var last;
frame.forEach(function (frame) {
var vertices = frame.vertices,
_frame$duration6 = frame.duration,
d = _frame$duration6 === void 0 ? 1 : _frame$duration6,
os = frame.offset;
frame.easingFn = getEasing(frame);
if (vertices) {
for (var i = 0; i < os; i++) {
vertices.unshift(0);
}
}
var offset = offsetSum / duration;
offsetSum += d;
frame.offset = offset; // 顶点变形数据vertices都是偏移量,无偏移为空
if (last) {
var verticesLast = last.vertices;
if (verticesLast && vertices) {
last.dv = [];
for (var _i3 = 0, len = Math.max(verticesLast.length, vertices.length); _i3 < len; _i3++) {
last.dv.push((vertices[_i3] || 0) - (verticesLast[_i3] || 0));
}
} else if (verticesLast) {
last.dv = last.vertices.map(function (n) {
return -n;
});
} else if (vertices) {
last.dv = vertices;
}
}
last = frame;
});
}
return item;
});
});
return hash;
}
function getEasing(frame) {
var curve = frame.curve;
if (curve && curve[0] !== 1 && curve[1] !== 1 && curve[2] !== 0 && curve[3] !== 0) {
return karas.animate.easing.cubicBezier(curve[0], curve[1], curve[2], curve[3]);
}
}
var parser = {
parseAndLoadTex: parseAndLoadTex,
parseSke: parseSke
};
var math$1 = karas.math;
/**
* 根据动画时间状态修改骨骼当前matrix
* @param animationList 动画列表
* @param offset 当前时间偏移量
* @param boneHash 骨骼hash
*/
function animateBoneMatrix(animationList, offset, boneHash) {
animationList.forEach(function (item) {
var name = item.name,
list = item.list;
var bone = boneHash[name]; // 先以静态变换样式为基础
var _bone$transform = bone.transform,
transform = _bone$transform === void 0 ? {} : _bone$transform;
var res = {};
res.translateX = transform.x || 0;
res.translateY = transform.y || 0;
res.rotateZ = transform.skX || 0;
res.scaleX = transform.scX === undefined ? 1 : transform.scX;
res.scaleY = transform.scY === undefined ? 1 : transform.scY; // 再assign动画中的变换样式
list.forEach(function (frames) {
var len = frames.length;
if (!len) {
return;
}
var type = frames[0].type;
var i = binarySearch(0, len - 1, offset, frames);
var current = frames[i];
var easingFn = current.easingFn; // 是否最后一帧
if (i === len - 1) {
if (type === 0) {
res.translateX = current.translateX;
res.translateY = current.translateY;
} else if (type === 1) {
res.rotateZ = current.rotateZ;
} else if (type === 2) {
res.scaleX = current.scaleX;
res.scaleY = current.scaleY;
}
} else {
var next = frames[i + 1];
var total = next.offset - current.offset;
var percent = (offset - current.offset) / total;
if (easingFn) {
percent = easingFn(percent);
}
if (type === 0) {
res.translateX = current.translateX + current.dx * percent;
res.translateY = current.translateY + current.dy * percent;
} else if (type === 1) {
res.rotateZ = current.rotateZ + current.dz * percent;
} else if (type === 2) {
res.scaleX = current.scaleX + current.dx * percent;
res.scaleY = current.scaleY + current.dy * percent;
}
}
});
var matrix = math$1.matrix.identity();
if (res.translateX || res.translateY) {
var m = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, res.translateX || 0, res.translateY || 0, 0, 1];
matrix = math$1.matrix.multiply(matrix, m);
}
if (res.rotateZ) {
var d = math$1.geom.d2r(res.rotateZ);
var sin = Math.sin(d);
var cos = Math.cos(d);
var _m = [cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
matrix = math$1.matrix.multiply(matrix, _m);
}
if (res.scaleX !== undefined || res.scaleY !== undefined) {
var _m2 = [res.scaleX === undefined ? 1 : res.scaleX, 0, 0, 0, 0, res.scaleY === undefined ? 1 : res.scaleY, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
matrix = math$1.matrix.multiply(matrix, _m2);
}
bone.matrixA = matrix;
});
}
/**
* 二分查找根据时间轴帧序列和当前百分比获得当前帧
* @param i
* @param j
* @param offset
* @param frames
* @returns {frame}
*/
function binarySearch(i, j, offset, frames) {
if (i === j) {
var frame = frames[i];
if (frame.offset > offset) {
return i - 1;
}
return i;
} else {
var middle = i + (j - i >> 1);
var _frame = frames[middle];
if (_frame.offset === offset) {
return middle;
} else if (_frame.offset > offset) {
return binarySearch(i, Math.max(middle - 1, i), offset, frames);
} else {
return binarySearch(Math.min(middle + 1, j), j, offset, frames);
}
}
}
/**
* 递归遍历骨骼,根据父子属性合并生成骨骼相对于舞台的最终currentMatrix
* @param root 根骨骼
*/
function mergeBoneMatrix(root) {
root.currentMatrix = root.matrixA || root.matrix;
root.children.forEach(function (item) {
mergeChildBoneMatrix(item, root.currentMatrix);
});
}
function mergeChildBoneMatrix(bone, parentMatrix) {
bone.currentMatrix = math$1.matrix.multiply(parentMatrix, bone.matrixA || bone.matrix);
bone.children.forEach(function (item) {
mergeChildBoneMatrix(item, bone.currentMatrix);
});
}
/**
* 根据当前动画时间执行slot的动画,确定显示slot下的皮肤索引和透明度
* @param slotAnimationList
* @param offset
* @param slotHash
*/
function animateSlot(slotAnimationList, offset, slotHash) {
slotAnimationList.forEach(function (item) {
var name = item.name,
displayFrame = item.displayFrame,
colorFrame = item.colorFrame;
var slot = slotHash[name];
if (displayFrame) {
if (displayFrame.length) {
var i = binarySearch(0, displayFrame.length - 1, offset, displayFrame);
var _displayFrame$i$value = displayFrame[i].value,
value = _displayFrame$i$value === void 0 ? 0 : _displayFrame$i$value;
slot.displayIndexA = value;
}
}
if (colorFrame) {
var len = colorFrame.length;
if (len) {
var _i = binarySearch(0, len - 1, offset, colorFrame);
var current = colorFrame[_i];
var easingFn = current.easing; // 是否最后一帧
if (_i === len - 1) {
slot.colorA = current.value;
} else {
var next = colorFrame[_i + 1];
var total = next.offset - current.offset;
var percent = (offset - current.offset) / total;
if (easingFn) {
percent = easingFn(percent);
}
slot.colorA = {
aM: current.value.aM + current.da * percent
};
}
}
}
});
}
/**
* 根据当前骨骼状态计算slot中显示对象变换matrix
* @param offset
* @param slot
* @param skinHash
* @param bone
* @param boneHash
* @param texHash
* @param ffdAnimationHash
*/
function calSlot(offset, slot, skinHash, bone, boneHash, texHash, ffdAnimationHash) {
slot.forEach(function (item) {
var name = item.name,
parent = item.parent,
_item$displayIndex = item.displayIndex,
displayIndex = _item$displayIndex === void 0 ? 0 : _item$displayIndex,
_item$displayIndexA = item.displayIndexA,
displayIndexA = _item$displayIndexA === void 0 ? displayIndex : _item$displayIndexA; // 插槽隐藏不显示
if (displayIndexA < 0) {
return;
}
var skin = skinHash[name];
var displayTarget = skin.display[displayIndexA]; // 网格类型
if (displayTarget.type === 'mesh') {
var verticesList = displayTarget.verticesList,
triangleList = displayTarget.triangleList; // 先进行顶点变换
verticesList.forEach(function (item) {
var weightList = item.weightList; // 有绑定骨骼的mesh,计算权重
if (weightList) {
var m = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
weightList.forEach(function (weight) {
var index = weight.index,
value = weight.value,
matrix = weight.matrix;
var boneMatrix = bone[index].currentMatrix;
var offset = karas.math.matrix.multiply(boneMatrix, matrix);
for (var i = 0; i < 16; i++) {