-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathc2runtime.js
22207 lines (22207 loc) · 503 KB
/
c2runtime.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
// Generated by Construct 2, the HTML5 game and app creator :: http://www.scirra.com
var cr = {};
cr.plugins_ = {};
cr.behaviors = {};
if (typeof Object.getPrototypeOf !== "function")
{
if (typeof "test".__proto__ === "object")
{
Object.getPrototypeOf = function(object) {
return object.__proto__;
};
}
else
{
Object.getPrototypeOf = function(object) {
return object.constructor.prototype;
};
}
}
(function(){
cr.logexport = function (msg)
{
if (window.console && window.console.log)
window.console.log(msg);
};
cr.seal = function(x)
{
return x;
};
cr.freeze = function(x)
{
return x;
};
cr.is_undefined = function (x)
{
return typeof x === "undefined";
};
cr.is_number = function (x)
{
return typeof x === "number";
};
cr.is_string = function (x)
{
return typeof x === "string";
};
cr.isPOT = function (x)
{
return x > 0 && ((x - 1) & x) === 0;
};
cr.abs = function (x)
{
return (x < 0 ? -x : x);
};
cr.max = function (a, b)
{
return (a > b ? a : b);
};
cr.min = function (a, b)
{
return (a < b ? a : b);
};
cr.PI = Math.PI;
cr.round = function (x)
{
return (x + 0.5) | 0;
};
cr.floor = function (x)
{
return x | 0;
};
function Vector2(x, y)
{
this.x = x;
this.y = y;
cr.seal(this);
};
Vector2.prototype.offset = function (px, py)
{
this.x += px;
this.y += py;
return this;
};
Vector2.prototype.mul = function (px, py)
{
this.x *= px;
this.y *= py;
return this;
};
cr.vector2 = Vector2;
cr.segments_intersect = function(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y)
{
if (cr.max(a1x, a2x) < cr.min(b1x, b2x)
|| cr.min(a1x, a2x) > cr.max(b1x, b2x)
|| cr.max(a1y, a2y) < cr.min(b1y, b2y)
|| cr.min(a1y, a2y) > cr.max(b1y, b2y))
{
return false;
}
var dpx = b1x - a1x + b2x - a2x;
var dpy = b1y - a1y + b2y - a2y;
var qax = a2x - a1x;
var qay = a2y - a1y;
var qbx = b2x - b1x;
var qby = b2y - b1y;
var d = cr.abs(qay * qbx - qby * qax);
var la = qbx * dpy - qby * dpx;
var lb = qax * dpy - qay * dpx;
return cr.abs(la) <= d && cr.abs(lb) <= d;
};
function Rect(left, top, right, bottom)
{
this.set(left, top, right, bottom);
cr.seal(this);
};
Rect.prototype.set = function (left, top, right, bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
};
Rect.prototype.width = function ()
{
return this.right - this.left;
};
Rect.prototype.height = function ()
{
return this.bottom - this.top;
};
Rect.prototype.offset = function (px, py)
{
this.left += px;
this.top += py;
this.right += px;
this.bottom += py;
return this;
};
Rect.prototype.intersects_rect = function (rc)
{
return !(rc.right < this.left || rc.bottom < this.top || rc.left > this.right || rc.top > this.bottom);
};
Rect.prototype.contains_pt = function (x, y)
{
return (x >= this.left && x <= this.right) && (y >= this.top && y <= this.bottom);
};
cr.rect = Rect;
function Quad()
{
this.tlx = 0;
this.tly = 0;
this.trx = 0;
this.try_ = 0; // is a keyword otherwise!
this.brx = 0;
this.bry = 0;
this.blx = 0;
this.bly = 0;
cr.seal(this);
};
Quad.prototype.set_from_rect = function (rc)
{
this.tlx = rc.left;
this.tly = rc.top;
this.trx = rc.right;
this.try_ = rc.top;
this.brx = rc.right;
this.bry = rc.bottom;
this.blx = rc.left;
this.bly = rc.bottom;
};
Quad.prototype.set_from_rotated_rect = function (rc, a)
{
if (a === 0)
{
this.set_from_rect(rc);
}
else
{
var sin_a = Math.sin(a);
var cos_a = Math.cos(a);
var left_sin_a = rc.left * sin_a;
var top_sin_a = rc.top * sin_a;
var right_sin_a = rc.right * sin_a;
var bottom_sin_a = rc.bottom * sin_a;
var left_cos_a = rc.left * cos_a;
var top_cos_a = rc.top * cos_a;
var right_cos_a = rc.right * cos_a;
var bottom_cos_a = rc.bottom * cos_a;
this.tlx = left_cos_a - top_sin_a;
this.tly = top_cos_a + left_sin_a;
this.trx = right_cos_a - top_sin_a;
this.try_ = top_cos_a + right_sin_a;
this.brx = right_cos_a - bottom_sin_a;
this.bry = bottom_cos_a + right_sin_a;
this.blx = left_cos_a - bottom_sin_a;
this.bly = bottom_cos_a + left_sin_a;
}
};
Quad.prototype.offset = function (px, py)
{
this.tlx += px;
this.tly += py;
this.trx += px;
this.try_ += py;
this.brx += px;
this.bry += py;
this.blx += px;
this.bly += py;
return this;
};
Quad.prototype.bounding_box = function (rc)
{
rc.left = cr.min(cr.min(this.tlx, this.trx), cr.min(this.brx, this.blx));
rc.top = cr.min(cr.min(this.tly, this.try_), cr.min(this.bry, this.bly));
rc.right = cr.max(cr.max(this.tlx, this.trx), cr.max(this.brx, this.blx));
rc.bottom = cr.max(cr.max(this.tly, this.try_), cr.max(this.bry, this.bly));
};
Quad.prototype.contains_pt = function (x, y)
{
var v0x = this.trx - this.tlx;
var v0y = this.try_ - this.tly;
var v1x = this.brx - this.tlx;
var v1y = this.bry - this.tly;
var v2x = x - this.tlx;
var v2y = y - this.tly;
var dot00 = v0x * v0x + v0y * v0y
var dot01 = v0x * v1x + v0y * v1y
var dot02 = v0x * v2x + v0y * v2y
var dot11 = v1x * v1x + v1y * v1y
var dot12 = v1x * v2x + v1y * v2y
var invDenom = 1.0 / (dot00 * dot11 - dot01 * dot01);
var u = (dot11 * dot02 - dot01 * dot12) * invDenom;
var v = (dot00 * dot12 - dot01 * dot02) * invDenom;
if ((u >= 0.0) && (v > 0.0) && (u + v < 1))
return true;
v0x = this.blx - this.tlx;
v0y = this.bly - this.tly;
var dot00 = v0x * v0x + v0y * v0y
var dot01 = v0x * v1x + v0y * v1y
var dot02 = v0x * v2x + v0y * v2y
invDenom = 1.0 / (dot00 * dot11 - dot01 * dot01);
u = (dot11 * dot02 - dot01 * dot12) * invDenom;
v = (dot00 * dot12 - dot01 * dot02) * invDenom;
return (u >= 0.0) && (v > 0.0) && (u + v < 1);
};
Quad.prototype.at = function (i, xory)
{
switch (i)
{
case 0: return xory ? this.tlx : this.tly;
case 1: return xory ? this.trx : this.try_;
case 2: return xory ? this.brx : this.bry;
case 3: return xory ? this.blx : this.bly;
case 4: return xory ? this.tlx : this.tly; // same as 0, repeated for perf
default: return xory ? this.tlx : this.tly;
}
};
Quad.prototype.midX = function ()
{
return (this.tlx + this.trx + this.brx + this.blx) / 4;
};
Quad.prototype.midY = function ()
{
return (this.tly + this.try_ + this.bry + this.bly) / 4;
};
Quad.prototype.intersects_quad = function (rhs)
{
var midx = rhs.midX();
var midy = rhs.midY();
if (this.contains_pt(midx, midy))
return true;
midx = this.midX();
midy = this.midY();
if (rhs.contains_pt(midx, midy))
return true;
var a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y;
var i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
a1x = this.at(i, true);
a1y = this.at(i, false);
a2x = this.at(i + 1, true);
a2y = this.at(i + 1, false);
b1x = rhs.at(j, true);
b1y = rhs.at(j, false);
b2x = rhs.at(j + 1, true);
b2y = rhs.at(j + 1, false);
if (cr.segments_intersect(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y))
return true;
}
}
return false;
};
cr.quad = Quad;
cr.RGB = function (red, green, blue)
{
return Math.max(Math.min(red, 255), 0)
| (Math.max(Math.min(green, 255), 0) << 8)
| (Math.max(Math.min(blue, 255), 0) << 16);
};
cr.GetRValue = function (rgb)
{
return rgb & 0xFF;
};
cr.GetGValue = function (rgb)
{
return (rgb & 0xFF00) >> 8;
};
cr.GetBValue = function (rgb)
{
return (rgb & 0xFF0000) >> 16;
};
cr.shallowCopy = function (a, b, allowOverwrite)
{
var attr;
for (attr in b)
{
if (b.hasOwnProperty(attr))
{
;
a[attr] = b[attr];
}
}
return a;
};
cr.arrayRemove = function (arr, index)
{
var i, len;
index = cr.floor(index);
if (index < 0 || index >= arr.length)
return; // index out of bounds
if (index === 0) // removing first item
arr.shift();
else if (index === arr.length - 1) // removing last item
arr.pop();
else
{
for (i = index, len = arr.length - 1; i < len; i++)
arr[i] = arr[i + 1];
arr.length = len;
}
};
cr.shallowAssignArray = function(dest, src)
{
dest.length = src.length;
var i, len;
for (i = 0, len = src.length; i < len; i++)
dest[i] = src[i];
};
cr.arrayFindRemove = function (arr, item)
{
var index = arr.indexOf(item);
if (index !== -1)
cr.arrayRemove(arr, index);
};
cr.clamp = function(x, a, b)
{
if (x < a)
return a;
else if (x > b)
return b;
else
return x;
};
cr.to_radians = function(x)
{
return x / (180.0 / cr.PI);
};
cr.to_degrees = function(x)
{
return x * (180.0 / cr.PI);
};
cr.clamp_angle_degrees = function (a)
{
a %= 360; // now in (-360, 360) range
if (a < 0)
a += 360; // now in [0, 360) range
return a;
};
cr.clamp_angle = function (a)
{
a %= 2 * cr.PI; // now in (-2pi, 2pi) range
if (a < 0)
a += 2 * cr.PI; // now in [0, 2pi) range
return a;
};
cr.to_clamped_degrees = function (x)
{
return cr.clamp_angle_degrees(cr.to_degrees(x));
};
cr.to_clamped_radians = function (x)
{
return cr.clamp_angle(cr.to_radians(x));
};
cr.angleTo = function(x1, y1, x2, y2)
{
var dx = x2 - x1;
var dy = y2 - y1;
return Math.atan2(dy, dx);
};
cr.angleDiff = function (a1, a2)
{
if (a1 === a2)
return 0;
var s1 = Math.sin(a1);
var c1 = Math.cos(a1);
var s2 = Math.sin(a2);
var c2 = Math.cos(a2);
var n = s1 * s2 + c1 * c2;
if (n >= 1)
return 0;
if (n <= -1)
return cr.PI;
return Math.acos(n);
};
cr.angleRotate = function (start, end, step)
{
var ss = Math.sin(start);
var cs = Math.cos(start);
var se = Math.sin(end);
var ce = Math.cos(end);
if (Math.acos(ss * se + cs * ce) > step)
{
if (cs * se - ss * ce > 0)
return cr.clamp_angle(start + step);
else
return cr.clamp_angle(start - step);
}
else
return cr.clamp_angle(end);
};
cr.angleClockwise = function (a1, a2)
{
var s1 = Math.sin(a1);
var c1 = Math.cos(a1);
var s2 = Math.sin(a2);
var c2 = Math.cos(a2);
return c1 * s2 - s1 * c2 <= 0;
};
cr.distanceTo = function(x1, y1, x2, y2)
{
var dx = x2 - x1;
var dy = y2 - y1;
return Math.sqrt(dx*dx + dy*dy);
};
cr.xor = function (x, y)
{
return !x !== !y;
};
cr.lerp = function (a, b, x)
{
return a + (b - a) * x;
};
cr.hasAnyOwnProperty = function (o)
{
var p;
for (p in o)
{
if (o.hasOwnProperty(p))
return true;
}
return false;
};
cr.wipe = function (obj)
{
var p;
for (p in obj)
{
if (obj.hasOwnProperty(p))
delete obj[p];
}
};
cr.performance_now = function()
{
if (typeof window["performance"] !== "undefined")
{
var winperf = window["performance"];
if (typeof winperf.now !== "undefined")
return winperf.now();
else if (typeof winperf["webkitNow"] !== "undefined")
return winperf["webkitNow"]();
else if (typeof winperf["msNow"] !== "undefined")
return winperf["msNow"]();
}
return Date.now();
};
function ObjectSet_()
{
this.items = {};
this.item_count = 0;
this.values_cache = [];
this.cache_valid = true;
cr.seal(this);
};
ObjectSet_.prototype.contains = function (x)
{
return this.items.hasOwnProperty(x.toString());
};
ObjectSet_.prototype.add = function (x)
{
var str = x.toString();
if (!this.items.hasOwnProperty(str))
{
this.items[str] = x;
this.item_count++;
this.cache_valid = false;
}
return this;
};
ObjectSet_.prototype.remove = function (x)
{
var str = x.toString();
if (this.items.hasOwnProperty(str))
{
delete this.items[str];
this.item_count--;
this.cache_valid = false;
}
return this;
};
ObjectSet_.prototype.clear = function ()
{
cr.wipe(this.items);
this.item_count = 0;
this.values_cache.length = 0;
this.cache_valid = true;
return this;
};
ObjectSet_.prototype.isEmpty = function ()
{
return this.item_count === 0;
};
ObjectSet_.prototype.count = function ()
{
return this.item_count;
};
ObjectSet_.prototype.update_cache = function ()
{
if (this.cache_valid)
return;
this.values_cache.length = this.item_count;
var p, n = 0;
for (p in this.items)
{
if (this.items.hasOwnProperty(p))
this.values_cache[n++] = this.items[p];
}
;
this.cache_valid = true;
};
ObjectSet_.prototype.values = function ()
{
this.update_cache();
return this.values_cache.slice(0);
};
ObjectSet_.prototype.valuesRef = function ()
{
this.update_cache();
return this.values_cache;
};
cr.ObjectSet = ObjectSet_;
function KahanAdder_()
{
this.c = 0;
this.y = 0;
this.t = 0;
this.sum = 0;
cr.seal(this);
};
KahanAdder_.prototype.add = function (v)
{
this.y = v - this.c;
this.t = this.sum + this.y;
this.c = (this.t - this.sum) - this.y;
this.sum = this.t;
};
KahanAdder_.prototype.reset = function ()
{
this.c = 0;
this.y = 0;
this.t = 0;
this.sum = 0;
};
cr.KahanAdder = KahanAdder_;
cr.regexp_escape = function(text)
{
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};
function CollisionPoly_(pts_array_)
{
this.pts_cache = [];
this.set_pts(pts_array_);
cr.seal(this);
};
CollisionPoly_.prototype.set_pts = function(pts_array_)
{
this.pts_array = pts_array_;
this.pts_count = pts_array_.length / 2; // x, y, x, y... in array
this.pts_cache.length = pts_array_.length;
this.cache_width = -1;
this.cache_height = -1;
this.cache_angle = 0;
};
CollisionPoly_.prototype.is_empty = function()
{
return !this.pts_array.length;
};
CollisionPoly_.prototype.set_from_quad = function(q, offx, offy, w, h)
{
this.pts_cache.length = 8;
this.pts_count = 4;
var myptscache = this.pts_cache;
myptscache[0] = q.tlx - offx;
myptscache[1] = q.tly - offy;
myptscache[2] = q.trx - offx;
myptscache[3] = q.try_ - offy;
myptscache[4] = q.brx - offx;
myptscache[5] = q.bry - offy;
myptscache[6] = q.blx - offx;
myptscache[7] = q.bly - offy;
this.cache_width = w;
this.cache_height = h;
};
CollisionPoly_.prototype.set_from_poly = function (r)
{
this.pts_count = r.pts_count;
cr.shallowAssignArray(this.pts_cache, r.pts_cache);
};
CollisionPoly_.prototype.cache_poly = function(w, h, a)
{
if (this.cache_width === w && this.cache_height === h && this.cache_angle === a)
return; // cache up-to-date
this.cache_width = w;
this.cache_height = h;
this.cache_angle = a;
var i, len, x, y;
var sina = 0;
var cosa = 1;
var myptsarray = this.pts_array;
var myptscache = this.pts_cache;
if (a !== 0)
{
sina = Math.sin(a);
cosa = Math.cos(a);
}
for (i = 0, len = this.pts_count; i < len; i++)
{
x = myptsarray[i*2] * w;
y = myptsarray[i*2+1] * h;
myptscache[i*2] = (x * cosa) - (y * sina);
myptscache[i*2+1] = (y * cosa) + (x * sina);
}
};
CollisionPoly_.prototype.contains_pt = function (a2x, a2y)
{
var myptscache = this.pts_cache;
if (a2x === myptscache[0] && a2y === myptscache[1])
return true;
var a1x = -this.cache_width * 5 - 1;
var a1y = -this.cache_height * 5 - 1;
var a3x = this.cache_width * 5 + 1;
var a3y = -1;
var b1x, b1y, b2x, b2y;
var i, len;
var count1 = 0, count2 = 0;
for (i = 0, len = this.pts_count; i < len; i++)
{
b1x = myptscache[i*2];
b1y = myptscache[i*2+1];
b2x = myptscache[((i+1)%len)*2];
b2y = myptscache[((i+1)%len)*2+1];
if (cr.segments_intersect(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y))
count1++;
if (cr.segments_intersect(a3x, a3y, a2x, a2y, b1x, b1y, b2x, b2y))
count2++;
}
return (count1 % 2 === 1) || (count2 % 2 === 1);
};
CollisionPoly_.prototype.intersects_poly = function (rhs, offx, offy)
{
var rhspts = rhs.pts_cache;
var mypts = this.pts_cache;
if (this.contains_pt(rhspts[0] + offx, rhspts[1] + offy))
return true;
if (rhs.contains_pt(mypts[0] - offx, mypts[1] - offy))
return true;
var i, leni, j, lenj;
var a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y;
for (i = 0, leni = this.pts_count; i < leni; i++)
{
a1x = mypts[i*2];
a1y = mypts[i*2+1];
a2x = mypts[((i+1)%leni)*2];
a2y = mypts[((i+1)%leni)*2+1];
for (j = 0, lenj = rhs.pts_count; j < lenj; j++)
{
b1x = rhspts[j*2] + offx;
b1y = rhspts[j*2+1] + offy;
b2x = rhspts[((j+1)%lenj)*2] + offx;
b2y = rhspts[((j+1)%lenj)*2+1] + offy;
if (cr.segments_intersect(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y))
return true;
}
}
return false;
};
cr.CollisionPoly = CollisionPoly_;
var fxNames = [ "lighter",
"xor",
"copy",
"destination-over",
"source-in",
"destination-in",
"source-out",
"destination-out",
"source-atop",
"destination-atop"];
cr.effectToCompositeOp = function(effect)
{
if (effect <= 0 || effect >= 11)
return "source-over";
return fxNames[effect - 1]; // not including "none" so offset by 1
};
cr.setGLBlend = function(this_, effect, gl)
{
if (!gl)
return;
this_.srcBlend = gl.ONE;
this_.destBlend = gl.ONE_MINUS_SRC_ALPHA;
switch (effect) {
case 1: // lighter (additive)
this_.srcBlend = gl.ONE;
this_.destBlend = gl.ONE;
break;
case 2: // xor
break; // todo
case 3: // copy
this_.srcBlend = gl.ONE;
this_.destBlend = gl.ZERO;
break;
case 4: // destination-over
this_.srcBlend = gl.ONE_MINUS_DST_ALPHA;
this_.destBlend = gl.ONE;
break;
case 5: // source-in
this_.srcBlend = gl.DST_ALPHA;
this_.destBlend = gl.ZERO;
break;
case 6: // destination-in
this_.srcBlend = gl.ZERO;
this_.destBlend = gl.SRC_ALPHA;
break;
case 7: // source-out
this_.srcBlend = gl.ONE_MINUS_DST_ALPHA;
this_.destBlend = gl.ZERO;
break;
case 8: // destination-out
this_.srcBlend = gl.ZERO;
this_.destBlend = gl.ONE_MINUS_SRC_ALPHA;
break;
case 9: // source-atop
this_.srcBlend = gl.DST_ALPHA;
this_.destBlend = gl.ONE_MINUS_SRC_ALPHA;
break;
case 10: // destination-atop
this_.srcBlend = gl.ONE_MINUS_DST_ALPHA;
this_.destBlend = gl.SRC_ALPHA;
break;
}
};
cr.round6dp = function (x)
{
return Math.round(x * 1000000) / 1000000;
};
}());
var MatrixArray=typeof Float32Array!=="undefined"?Float32Array:Array,glMatrixArrayType=MatrixArray,vec3={},mat3={},mat4={},quat4={};vec3.create=function(a){var b=new MatrixArray(3);a&&(b[0]=a[0],b[1]=a[1],b[2]=a[2]);return b};vec3.set=function(a,b){b[0]=a[0];b[1]=a[1];b[2]=a[2];return b};vec3.add=function(a,b,c){if(!c||a===c)return a[0]+=b[0],a[1]+=b[1],a[2]+=b[2],a;c[0]=a[0]+b[0];c[1]=a[1]+b[1];c[2]=a[2]+b[2];return c};
vec3.subtract=function(a,b,c){if(!c||a===c)return a[0]-=b[0],a[1]-=b[1],a[2]-=b[2],a;c[0]=a[0]-b[0];c[1]=a[1]-b[1];c[2]=a[2]-b[2];return c};vec3.negate=function(a,b){b||(b=a);b[0]=-a[0];b[1]=-a[1];b[2]=-a[2];return b};vec3.scale=function(a,b,c){if(!c||a===c)return a[0]*=b,a[1]*=b,a[2]*=b,a;c[0]=a[0]*b;c[1]=a[1]*b;c[2]=a[2]*b;return c};
vec3.normalize=function(a,b){b||(b=a);var c=a[0],d=a[1],e=a[2],g=Math.sqrt(c*c+d*d+e*e);if(g){if(g===1)return b[0]=c,b[1]=d,b[2]=e,b}else return b[0]=0,b[1]=0,b[2]=0,b;g=1/g;b[0]=c*g;b[1]=d*g;b[2]=e*g;return b};vec3.cross=function(a,b,c){c||(c=a);var d=a[0],e=a[1],a=a[2],g=b[0],f=b[1],b=b[2];c[0]=e*b-a*f;c[1]=a*g-d*b;c[2]=d*f-e*g;return c};vec3.length=function(a){var b=a[0],c=a[1],a=a[2];return Math.sqrt(b*b+c*c+a*a)};vec3.dot=function(a,b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]};
vec3.direction=function(a,b,c){c||(c=a);var d=a[0]-b[0],e=a[1]-b[1],a=a[2]-b[2],b=Math.sqrt(d*d+e*e+a*a);if(!b)return c[0]=0,c[1]=0,c[2]=0,c;b=1/b;c[0]=d*b;c[1]=e*b;c[2]=a*b;return c};vec3.lerp=function(a,b,c,d){d||(d=a);d[0]=a[0]+c*(b[0]-a[0]);d[1]=a[1]+c*(b[1]-a[1]);d[2]=a[2]+c*(b[2]-a[2]);return d};vec3.str=function(a){return"["+a[0]+", "+a[1]+", "+a[2]+"]"};
mat3.create=function(a){var b=new MatrixArray(9);a&&(b[0]=a[0],b[1]=a[1],b[2]=a[2],b[3]=a[3],b[4]=a[4],b[5]=a[5],b[6]=a[6],b[7]=a[7],b[8]=a[8]);return b};mat3.set=function(a,b){b[0]=a[0];b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];b[5]=a[5];b[6]=a[6];b[7]=a[7];b[8]=a[8];return b};mat3.identity=function(a){a[0]=1;a[1]=0;a[2]=0;a[3]=0;a[4]=1;a[5]=0;a[6]=0;a[7]=0;a[8]=1;return a};
mat3.transpose=function(a,b){if(!b||a===b){var c=a[1],d=a[2],e=a[5];a[1]=a[3];a[2]=a[6];a[3]=c;a[5]=a[7];a[6]=d;a[7]=e;return a}b[0]=a[0];b[1]=a[3];b[2]=a[6];b[3]=a[1];b[4]=a[4];b[5]=a[7];b[6]=a[2];b[7]=a[5];b[8]=a[8];return b};mat3.toMat4=function(a,b){b||(b=mat4.create());b[15]=1;b[14]=0;b[13]=0;b[12]=0;b[11]=0;b[10]=a[8];b[9]=a[7];b[8]=a[6];b[7]=0;b[6]=a[5];b[5]=a[4];b[4]=a[3];b[3]=0;b[2]=a[2];b[1]=a[1];b[0]=a[0];return b};
mat3.str=function(a){return"["+a[0]+", "+a[1]+", "+a[2]+", "+a[3]+", "+a[4]+", "+a[5]+", "+a[6]+", "+a[7]+", "+a[8]+"]"};mat4.create=function(a){var b=new MatrixArray(16);a&&(b[0]=a[0],b[1]=a[1],b[2]=a[2],b[3]=a[3],b[4]=a[4],b[5]=a[5],b[6]=a[6],b[7]=a[7],b[8]=a[8],b[9]=a[9],b[10]=a[10],b[11]=a[11],b[12]=a[12],b[13]=a[13],b[14]=a[14],b[15]=a[15]);return b};
mat4.set=function(a,b){b[0]=a[0];b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];b[5]=a[5];b[6]=a[6];b[7]=a[7];b[8]=a[8];b[9]=a[9];b[10]=a[10];b[11]=a[11];b[12]=a[12];b[13]=a[13];b[14]=a[14];b[15]=a[15];return b};mat4.identity=function(a){a[0]=1;a[1]=0;a[2]=0;a[3]=0;a[4]=0;a[5]=1;a[6]=0;a[7]=0;a[8]=0;a[9]=0;a[10]=1;a[11]=0;a[12]=0;a[13]=0;a[14]=0;a[15]=1;return a};
mat4.transpose=function(a,b){if(!b||a===b){var c=a[1],d=a[2],e=a[3],g=a[6],f=a[7],h=a[11];a[1]=a[4];a[2]=a[8];a[3]=a[12];a[4]=c;a[6]=a[9];a[7]=a[13];a[8]=d;a[9]=g;a[11]=a[14];a[12]=e;a[13]=f;a[14]=h;return a}b[0]=a[0];b[1]=a[4];b[2]=a[8];b[3]=a[12];b[4]=a[1];b[5]=a[5];b[6]=a[9];b[7]=a[13];b[8]=a[2];b[9]=a[6];b[10]=a[10];b[11]=a[14];b[12]=a[3];b[13]=a[7];b[14]=a[11];b[15]=a[15];return b};
mat4.determinant=function(a){var b=a[0],c=a[1],d=a[2],e=a[3],g=a[4],f=a[5],h=a[6],i=a[7],j=a[8],k=a[9],l=a[10],n=a[11],o=a[12],m=a[13],p=a[14],a=a[15];return o*k*h*e-j*m*h*e-o*f*l*e+g*m*l*e+j*f*p*e-g*k*p*e-o*k*d*i+j*m*d*i+o*c*l*i-b*m*l*i-j*c*p*i+b*k*p*i+o*f*d*n-g*m*d*n-o*c*h*n+b*m*h*n+g*c*p*n-b*f*p*n-j*f*d*a+g*k*d*a+j*c*h*a-b*k*h*a-g*c*l*a+b*f*l*a};
mat4.inverse=function(a,b){b||(b=a);var c=a[0],d=a[1],e=a[2],g=a[3],f=a[4],h=a[5],i=a[6],j=a[7],k=a[8],l=a[9],n=a[10],o=a[11],m=a[12],p=a[13],r=a[14],s=a[15],A=c*h-d*f,B=c*i-e*f,t=c*j-g*f,u=d*i-e*h,v=d*j-g*h,w=e*j-g*i,x=k*p-l*m,y=k*r-n*m,z=k*s-o*m,C=l*r-n*p,D=l*s-o*p,E=n*s-o*r,q=1/(A*E-B*D+t*C+u*z-v*y+w*x);b[0]=(h*E-i*D+j*C)*q;b[1]=(-d*E+e*D-g*C)*q;b[2]=(p*w-r*v+s*u)*q;b[3]=(-l*w+n*v-o*u)*q;b[4]=(-f*E+i*z-j*y)*q;b[5]=(c*E-e*z+g*y)*q;b[6]=(-m*w+r*t-s*B)*q;b[7]=(k*w-n*t+o*B)*q;b[8]=(f*D-h*z+j*x)*q;
b[9]=(-c*D+d*z-g*x)*q;b[10]=(m*v-p*t+s*A)*q;b[11]=(-k*v+l*t-o*A)*q;b[12]=(-f*C+h*y-i*x)*q;b[13]=(c*C-d*y+e*x)*q;b[14]=(-m*u+p*B-r*A)*q;b[15]=(k*u-l*B+n*A)*q;return b};mat4.toRotationMat=function(a,b){b||(b=mat4.create());b[0]=a[0];b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];b[5]=a[5];b[6]=a[6];b[7]=a[7];b[8]=a[8];b[9]=a[9];b[10]=a[10];b[11]=a[11];b[12]=0;b[13]=0;b[14]=0;b[15]=1;return b};
mat4.toMat3=function(a,b){b||(b=mat3.create());b[0]=a[0];b[1]=a[1];b[2]=a[2];b[3]=a[4];b[4]=a[5];b[5]=a[6];b[6]=a[8];b[7]=a[9];b[8]=a[10];return b};mat4.toInverseMat3=function(a,b){var c=a[0],d=a[1],e=a[2],g=a[4],f=a[5],h=a[6],i=a[8],j=a[9],k=a[10],l=k*f-h*j,n=-k*g+h*i,o=j*g-f*i,m=c*l+d*n+e*o;if(!m)return null;m=1/m;b||(b=mat3.create());b[0]=l*m;b[1]=(-k*d+e*j)*m;b[2]=(h*d-e*f)*m;b[3]=n*m;b[4]=(k*c-e*i)*m;b[5]=(-h*c+e*g)*m;b[6]=o*m;b[7]=(-j*c+d*i)*m;b[8]=(f*c-d*g)*m;return b};
mat4.multiply=function(a,b,c){c||(c=a);var d=a[0],e=a[1],g=a[2],f=a[3],h=a[4],i=a[5],j=a[6],k=a[7],l=a[8],n=a[9],o=a[10],m=a[11],p=a[12],r=a[13],s=a[14],a=a[15],A=b[0],B=b[1],t=b[2],u=b[3],v=b[4],w=b[5],x=b[6],y=b[7],z=b[8],C=b[9],D=b[10],E=b[11],q=b[12],F=b[13],G=b[14],b=b[15];c[0]=A*d+B*h+t*l+u*p;c[1]=A*e+B*i+t*n+u*r;c[2]=A*g+B*j+t*o+u*s;c[3]=A*f+B*k+t*m+u*a;c[4]=v*d+w*h+x*l+y*p;c[5]=v*e+w*i+x*n+y*r;c[6]=v*g+w*j+x*o+y*s;c[7]=v*f+w*k+x*m+y*a;c[8]=z*d+C*h+D*l+E*p;c[9]=z*e+C*i+D*n+E*r;c[10]=z*g+C*
j+D*o+E*s;c[11]=z*f+C*k+D*m+E*a;c[12]=q*d+F*h+G*l+b*p;c[13]=q*e+F*i+G*n+b*r;c[14]=q*g+F*j+G*o+b*s;c[15]=q*f+F*k+G*m+b*a;return c};mat4.multiplyVec3=function(a,b,c){c||(c=b);var d=b[0],e=b[1],b=b[2];c[0]=a[0]*d+a[4]*e+a[8]*b+a[12];c[1]=a[1]*d+a[5]*e+a[9]*b+a[13];c[2]=a[2]*d+a[6]*e+a[10]*b+a[14];return c};
mat4.multiplyVec4=function(a,b,c){c||(c=b);var d=b[0],e=b[1],g=b[2],b=b[3];c[0]=a[0]*d+a[4]*e+a[8]*g+a[12]*b;c[1]=a[1]*d+a[5]*e+a[9]*g+a[13]*b;c[2]=a[2]*d+a[6]*e+a[10]*g+a[14]*b;c[3]=a[3]*d+a[7]*e+a[11]*g+a[15]*b;return c};
mat4.translate=function(a,b,c){var d=b[0],e=b[1],b=b[2],g,f,h,i,j,k,l,n,o,m,p,r;if(!c||a===c)return a[12]=a[0]*d+a[4]*e+a[8]*b+a[12],a[13]=a[1]*d+a[5]*e+a[9]*b+a[13],a[14]=a[2]*d+a[6]*e+a[10]*b+a[14],a[15]=a[3]*d+a[7]*e+a[11]*b+a[15],a;g=a[0];f=a[1];h=a[2];i=a[3];j=a[4];k=a[5];l=a[6];n=a[7];o=a[8];m=a[9];p=a[10];r=a[11];c[0]=g;c[1]=f;c[2]=h;c[3]=i;c[4]=j;c[5]=k;c[6]=l;c[7]=n;c[8]=o;c[9]=m;c[10]=p;c[11]=r;c[12]=g*d+j*e+o*b+a[12];c[13]=f*d+k*e+m*b+a[13];c[14]=h*d+l*e+p*b+a[14];c[15]=i*d+n*e+r*b+a[15];
return c};mat4.scale=function(a,b,c){var d=b[0],e=b[1],b=b[2];if(!c||a===c)return a[0]*=d,a[1]*=d,a[2]*=d,a[3]*=d,a[4]*=e,a[5]*=e,a[6]*=e,a[7]*=e,a[8]*=b,a[9]*=b,a[10]*=b,a[11]*=b,a;c[0]=a[0]*d;c[1]=a[1]*d;c[2]=a[2]*d;c[3]=a[3]*d;c[4]=a[4]*e;c[5]=a[5]*e;c[6]=a[6]*e;c[7]=a[7]*e;c[8]=a[8]*b;c[9]=a[9]*b;c[10]=a[10]*b;c[11]=a[11]*b;c[12]=a[12];c[13]=a[13];c[14]=a[14];c[15]=a[15];return c};
mat4.rotate=function(a,b,c,d){var e=c[0],g=c[1],c=c[2],f=Math.sqrt(e*e+g*g+c*c),h,i,j,k,l,n,o,m,p,r,s,A,B,t,u,v,w,x,y,z;if(!f)return null;f!==1&&(f=1/f,e*=f,g*=f,c*=f);h=Math.sin(b);i=Math.cos(b);j=1-i;b=a[0];f=a[1];k=a[2];l=a[3];n=a[4];o=a[5];m=a[6];p=a[7];r=a[8];s=a[9];A=a[10];B=a[11];t=e*e*j+i;u=g*e*j+c*h;v=c*e*j-g*h;w=e*g*j-c*h;x=g*g*j+i;y=c*g*j+e*h;z=e*c*j+g*h;e=g*c*j-e*h;g=c*c*j+i;d?a!==d&&(d[12]=a[12],d[13]=a[13],d[14]=a[14],d[15]=a[15]):d=a;d[0]=b*t+n*u+r*v;d[1]=f*t+o*u+s*v;d[2]=k*t+m*u+A*
v;d[3]=l*t+p*u+B*v;d[4]=b*w+n*x+r*y;d[5]=f*w+o*x+s*y;d[6]=k*w+m*x+A*y;d[7]=l*w+p*x+B*y;d[8]=b*z+n*e+r*g;d[9]=f*z+o*e+s*g;d[10]=k*z+m*e+A*g;d[11]=l*z+p*e+B*g;return d};mat4.rotateX=function(a,b,c){var d=Math.sin(b),b=Math.cos(b),e=a[4],g=a[5],f=a[6],h=a[7],i=a[8],j=a[9],k=a[10],l=a[11];c?a!==c&&(c[0]=a[0],c[1]=a[1],c[2]=a[2],c[3]=a[3],c[12]=a[12],c[13]=a[13],c[14]=a[14],c[15]=a[15]):c=a;c[4]=e*b+i*d;c[5]=g*b+j*d;c[6]=f*b+k*d;c[7]=h*b+l*d;c[8]=e*-d+i*b;c[9]=g*-d+j*b;c[10]=f*-d+k*b;c[11]=h*-d+l*b;return c};
mat4.rotateY=function(a,b,c){var d=Math.sin(b),b=Math.cos(b),e=a[0],g=a[1],f=a[2],h=a[3],i=a[8],j=a[9],k=a[10],l=a[11];c?a!==c&&(c[4]=a[4],c[5]=a[5],c[6]=a[6],c[7]=a[7],c[12]=a[12],c[13]=a[13],c[14]=a[14],c[15]=a[15]):c=a;c[0]=e*b+i*-d;c[1]=g*b+j*-d;c[2]=f*b+k*-d;c[3]=h*b+l*-d;c[8]=e*d+i*b;c[9]=g*d+j*b;c[10]=f*d+k*b;c[11]=h*d+l*b;return c};
mat4.rotateZ=function(a,b,c){var d=Math.sin(b),b=Math.cos(b),e=a[0],g=a[1],f=a[2],h=a[3],i=a[4],j=a[5],k=a[6],l=a[7];c?a!==c&&(c[8]=a[8],c[9]=a[9],c[10]=a[10],c[11]=a[11],c[12]=a[12],c[13]=a[13],c[14]=a[14],c[15]=a[15]):c=a;c[0]=e*b+i*d;c[1]=g*b+j*d;c[2]=f*b+k*d;c[3]=h*b+l*d;c[4]=e*-d+i*b;c[5]=g*-d+j*b;c[6]=f*-d+k*b;c[7]=h*-d+l*b;return c};
mat4.frustum=function(a,b,c,d,e,g,f){f||(f=mat4.create());var h=b-a,i=d-c,j=g-e;f[0]=e*2/h;f[1]=0;f[2]=0;f[3]=0;f[4]=0;f[5]=e*2/i;f[6]=0;f[7]=0;f[8]=(b+a)/h;f[9]=(d+c)/i;f[10]=-(g+e)/j;f[11]=-1;f[12]=0;f[13]=0;f[14]=-(g*e*2)/j;f[15]=0;return f};mat4.perspective=function(a,b,c,d,e){a=c*Math.tan(a*Math.PI/360);b*=a;return mat4.frustum(-b,b,-a,a,c,d,e)};
mat4.ortho=function(a,b,c,d,e,g,f){f||(f=mat4.create());var h=b-a,i=d-c,j=g-e;f[0]=2/h;f[1]=0;f[2]=0;f[3]=0;f[4]=0;f[5]=2/i;f[6]=0;f[7]=0;f[8]=0;f[9]=0;f[10]=-2/j;f[11]=0;f[12]=-(a+b)/h;f[13]=-(d+c)/i;f[14]=-(g+e)/j;f[15]=1;return f};
mat4.lookAt=function(a,b,c,d){d||(d=mat4.create());var e,g,f,h,i,j,k,l,n=a[0],o=a[1],a=a[2];g=c[0];f=c[1];e=c[2];c=b[1];j=b[2];if(n===b[0]&&o===c&&a===j)return mat4.identity(d);c=n-b[0];j=o-b[1];k=a-b[2];l=1/Math.sqrt(c*c+j*j+k*k);c*=l;j*=l;k*=l;b=f*k-e*j;e=e*c-g*k;g=g*j-f*c;(l=Math.sqrt(b*b+e*e+g*g))?(l=1/l,b*=l,e*=l,g*=l):g=e=b=0;f=j*g-k*e;h=k*b-c*g;i=c*e-j*b;(l=Math.sqrt(f*f+h*h+i*i))?(l=1/l,f*=l,h*=l,i*=l):i=h=f=0;d[0]=b;d[1]=f;d[2]=c;d[3]=0;d[4]=e;d[5]=h;d[6]=j;d[7]=0;d[8]=g;d[9]=i;d[10]=k;d[11]=
0;d[12]=-(b*n+e*o+g*a);d[13]=-(f*n+h*o+i*a);d[14]=-(c*n+j*o+k*a);d[15]=1;return d};mat4.fromRotationTranslation=function(a,b,c){c||(c=mat4.create());var d=a[0],e=a[1],g=a[2],f=a[3],h=d+d,i=e+e,j=g+g,a=d*h,k=d*i;d*=j;var l=e*i;e*=j;g*=j;h*=f;i*=f;f*=j;c[0]=1-(l+g);c[1]=k+f;c[2]=d-i;c[3]=0;c[4]=k-f;c[5]=1-(a+g);c[6]=e+h;c[7]=0;c[8]=d+i;c[9]=e-h;c[10]=1-(a+l);c[11]=0;c[12]=b[0];c[13]=b[1];c[14]=b[2];c[15]=1;return c};
mat4.str=function(a){return"["+a[0]+", "+a[1]+", "+a[2]+", "+a[3]+", "+a[4]+", "+a[5]+", "+a[6]+", "+a[7]+", "+a[8]+", "+a[9]+", "+a[10]+", "+a[11]+", "+a[12]+", "+a[13]+", "+a[14]+", "+a[15]+"]"};quat4.create=function(a){var b=new MatrixArray(4);a&&(b[0]=a[0],b[1]=a[1],b[2]=a[2],b[3]=a[3]);return b};quat4.set=function(a,b){b[0]=a[0];b[1]=a[1];b[2]=a[2];b[3]=a[3];return b};
quat4.calculateW=function(a,b){var c=a[0],d=a[1],e=a[2];if(!b||a===b)return a[3]=-Math.sqrt(Math.abs(1-c*c-d*d-e*e)),a;b[0]=c;b[1]=d;b[2]=e;b[3]=-Math.sqrt(Math.abs(1-c*c-d*d-e*e));return b};quat4.inverse=function(a,b){if(!b||a===b)return a[0]*=-1,a[1]*=-1,a[2]*=-1,a;b[0]=-a[0];b[1]=-a[1];b[2]=-a[2];b[3]=a[3];return b};quat4.length=function(a){var b=a[0],c=a[1],d=a[2],a=a[3];return Math.sqrt(b*b+c*c+d*d+a*a)};
quat4.normalize=function(a,b){b||(b=a);var c=a[0],d=a[1],e=a[2],g=a[3],f=Math.sqrt(c*c+d*d+e*e+g*g);if(f===0)return b[0]=0,b[1]=0,b[2]=0,b[3]=0,b;f=1/f;b[0]=c*f;b[1]=d*f;b[2]=e*f;b[3]=g*f;return b};quat4.multiply=function(a,b,c){c||(c=a);var d=a[0],e=a[1],g=a[2],a=a[3],f=b[0],h=b[1],i=b[2],b=b[3];c[0]=d*b+a*f+e*i-g*h;c[1]=e*b+a*h+g*f-d*i;c[2]=g*b+a*i+d*h-e*f;c[3]=a*b-d*f-e*h-g*i;return c};
quat4.multiplyVec3=function(a,b,c){c||(c=b);var d=b[0],e=b[1],g=b[2],b=a[0],f=a[1],h=a[2],a=a[3],i=a*d+f*g-h*e,j=a*e+h*d-b*g,k=a*g+b*e-f*d,d=-b*d-f*e-h*g;c[0]=i*a+d*-b+j*-h-k*-f;c[1]=j*a+d*-f+k*-b-i*-h;c[2]=k*a+d*-h+i*-f-j*-b;return c};quat4.toMat3=function(a,b){b||(b=mat3.create());var c=a[0],d=a[1],e=a[2],g=a[3],f=c+c,h=d+d,i=e+e,j=c*f,k=c*h;c*=i;var l=d*h;d*=i;e*=i;f*=g;h*=g;g*=i;b[0]=1-(l+e);b[1]=k+g;b[2]=c-h;b[3]=k-g;b[4]=1-(j+e);b[5]=d+f;b[6]=c+h;b[7]=d-f;b[8]=1-(j+l);return b};
quat4.toMat4=function(a,b){b||(b=mat4.create());var c=a[0],d=a[1],e=a[2],g=a[3],f=c+c,h=d+d,i=e+e,j=c*f,k=c*h;c*=i;var l=d*h;d*=i;e*=i;f*=g;h*=g;g*=i;b[0]=1-(l+e);b[1]=k+g;b[2]=c-h;b[3]=0;b[4]=k-g;b[5]=1-(j+e);b[6]=d+f;b[7]=0;b[8]=c+h;b[9]=d-f;b[10]=1-(j+l);b[11]=0;b[12]=0;b[13]=0;b[14]=0;b[15]=1;return b};
quat4.slerp=function(a,b,c,d){d||(d=a);var e=a[0]*b[0]+a[1]*b[1]+a[2]*b[2]+a[3]*b[3],g,f;if(Math.abs(e)>=1)return d!==a&&(d[0]=a[0],d[1]=a[1],d[2]=a[2],d[3]=a[3]),d;g=Math.acos(e);f=Math.sqrt(1-e*e);if(Math.abs(f)<0.001)return d[0]=a[0]*0.5+b[0]*0.5,d[1]=a[1]*0.5+b[1]*0.5,d[2]=a[2]*0.5+b[2]*0.5,d[3]=a[3]*0.5+b[3]*0.5,d;e=Math.sin((1-c)*g)/f;c=Math.sin(c*g)/f;d[0]=a[0]*e+b[0]*c;d[1]=a[1]*e+b[1]*c;d[2]=a[2]*e+b[2]*c;d[3]=a[3]*e+b[3]*c;return d};
quat4.str=function(a){return"["+a[0]+", "+a[1]+", "+a[2]+", "+a[3]+"]"};
(function()
{
var MAX_VERTICES = 8000; // equates to 2500 objects being drawn
var MAX_INDICES = (MAX_VERTICES / 2) * 3; // 6 indices for every 4 vertices
var MAX_POINTS = 8000;
var MULTI_BUFFERS = 4; // cycle 4 buffers to try and avoid blocking
var BATCH_NULL = 0;
var BATCH_QUAD = 1;
var BATCH_SETTEXTURE = 2;
var BATCH_SETOPACITY = 3;
var BATCH_SETBLEND = 4;
var BATCH_UPDATEMODELVIEW = 5;
var BATCH_RENDERTOTEXTURE = 6;
var BATCH_CLEAR = 7;
var BATCH_POINTS = 8;
var BATCH_SETPROGRAM = 9;
var BATCH_SETPROGRAMPARAMETERS = 10;
function GLWrap_(gl, isMobile)
{
this.width = 0; // not yet known, wait for call to setSize()
this.height = 0;
this.cam = vec3.create([0, 0, 100]); // camera position
this.look = vec3.create([0, 0, 0]); // lookat position
this.up = vec3.create([0, 1, 0]); // up vector
this.worldScale = vec3.create([1, 1, 1]); // world scaling factor
this.matP = mat4.create(); // perspective matrix
this.matMV = mat4.create(); // model view matrix
this.lastMV = mat4.create();
this.currentMV = mat4.create();
this.gl = gl;
this.initState();
};
GLWrap_.prototype.initState = function ()
{
var gl = this.gl;
var i, len;
this.lastOpacity = 1;
this.lastTexture = null;
this.currentOpacity = 1;
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.enable(gl.BLEND);
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
gl.disable(gl.CULL_FACE);
gl.disable(gl.DEPTH_TEST);
this.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
this.lastSrcBlend = gl.ONE;
this.lastDestBlend = gl.ONE_MINUS_SRC_ALPHA;
this.pointBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.pointBuffer);
this.vertexBuffers = new Array(MULTI_BUFFERS);
this.texcoordBuffers = new Array(MULTI_BUFFERS);
for (i = 0; i < MULTI_BUFFERS; i++)
{
this.vertexBuffers[i] = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffers[i]);
this.texcoordBuffers[i] = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.texcoordBuffers[i]);
}
this.curBuffer = 0;
this.indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
this.vertexData = new Float32Array(MAX_VERTICES * 2);
this.texcoordData = new Float32Array(MAX_VERTICES * 2);
this.pointData = new Float32Array(MAX_POINTS * 4);
var indexData = new Uint16Array(MAX_INDICES);
i = 0, len = MAX_INDICES;
var fv = 0;
while (i < len)
{
indexData[i++] = fv; // top left
indexData[i++] = fv + 1; // top right
indexData[i++] = fv + 2; // bottom right (first tri)
indexData[i++] = fv; // top left
indexData[i++] = fv + 2; // bottom right
indexData[i++] = fv + 3; // bottom left
fv += 4;
}
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indexData, gl.STATIC_DRAW);
this.vertexPtr = 0;
this.pointPtr = 0;
var fsSource, vsSource;
this.shaderPrograms = [];
fsSource = [
"varying mediump vec2 vTex;",
"uniform lowp float opacity;",
"uniform lowp sampler2D samplerFront;",
"void main(void) {",
" gl_FragColor = texture2D(samplerFront, vTex);",
" gl_FragColor *= opacity;",
"}"
].join("\n");
vsSource = [
"attribute highp vec2 aPos;",
"attribute mediump vec2 aTex;",
"varying mediump vec2 vTex;",
"uniform highp mat4 matP;",
"uniform highp mat4 matMV;",
"void main(void) {",
" gl_Position = matP * matMV * vec4(aPos.x, aPos.y, 0.0, 1.0);",
" vTex = aTex;",
"}"
].join("\n");
var shaderProg = this.createShaderProgram({src: fsSource}, vsSource, "<default>");
;
this.shaderPrograms.push(shaderProg); // Default shader is always shader 0
fsSource = [
"uniform mediump sampler2D samplerFront;",
"varying lowp float opacity;",
"void main(void) {",
" gl_FragColor = texture2D(samplerFront, gl_PointCoord);",
" gl_FragColor *= opacity;",
"}"
].join("\n");
var pointVsSource = [
"attribute vec4 aPos;",
"varying float opacity;",
"uniform mat4 matP;",
"uniform mat4 matMV;",
"void main(void) {",
" gl_Position = matP * matMV * vec4(aPos.x, aPos.y, 0.0, 1.0);",
" gl_PointSize = aPos.z;",
" opacity = aPos.w;",
"}"
].join("\n");
shaderProg = this.createShaderProgram({src: fsSource}, pointVsSource, "<point>");
;
this.shaderPrograms.push(shaderProg); // Point shader is always shader 1
for (var shader_name in cr.shaders)
{
if (cr.shaders.hasOwnProperty(shader_name))
this.shaderPrograms.push(this.createShaderProgram(cr.shaders[shader_name], vsSource, shader_name));
}
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, null);
this.batch = [];
this.batchPtr = 0;
this.hasQuadBatchTop = false;
this.hasPointBatchTop = false;
this.lastProgram = -1; // start -1 so first switchProgram can do work
this.currentProgram = -1; // current program during batch execution
this.currentShader = null;
this.fbo = gl.createFramebuffer();
this.renderToTex = null;
this.tmpVec3 = vec3.create([0, 0, 0]);
;
;
var pointsizes = gl.getParameter(gl.ALIASED_POINT_SIZE_RANGE);
this.minPointSize = pointsizes[0];
this.maxPointSize = pointsizes[1];
;
this.switchProgram(0);
cr.seal(this);
};
function GLShaderProgram(gl, shaderProgram, name)
{
this.gl = gl;
this.shaderProgram = shaderProgram;
this.name = name;
this.locAPos = gl.getAttribLocation(shaderProgram, "aPos");
this.locATex = gl.getAttribLocation(shaderProgram, "aTex");
this.locMatP = gl.getUniformLocation(shaderProgram, "matP");
this.locMatMV = gl.getUniformLocation(shaderProgram, "matMV");
this.locOpacity = gl.getUniformLocation(shaderProgram, "opacity");
this.locSamplerFront = gl.getUniformLocation(shaderProgram, "samplerFront");
this.locSamplerBack = gl.getUniformLocation(shaderProgram, "samplerBack");
this.locDestStart = gl.getUniformLocation(shaderProgram, "destStart");
this.locDestEnd = gl.getUniformLocation(shaderProgram, "destEnd");
this.locSeconds = gl.getUniformLocation(shaderProgram, "seconds");
this.locPixelWidth = gl.getUniformLocation(shaderProgram, "pixelWidth");
this.locPixelHeight = gl.getUniformLocation(shaderProgram, "pixelHeight");
this.locLayerScale = gl.getUniformLocation(shaderProgram, "layerScale");
if (this.locOpacity)
gl.uniform1f(this.locOpacity, 1);
if (this.locSamplerFront)
gl.uniform1i(this.locSamplerFront, 0);
if (this.locSamplerBack)
gl.uniform1i(this.locSamplerBack, 1);
if (this.locDestStart)
gl.uniform2f(this.locDestStart, 0.0, 0.0);
if (this.locDestEnd)
gl.uniform2f(this.locDestEnd, 1.0, 1.0);
this.hasCurrentMatMV = false; // matMV needs updating
};
GLWrap_.prototype.createShaderProgram = function(shaderEntry, vsSource, name)
{
var gl = this.gl;
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, shaderEntry.src);
gl.compileShader(fragmentShader);
if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS))