forked from jwagner/fluidwebgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl-matrix.js
3455 lines (3099 loc) · 103 KB
/
gl-matrix.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
/**
* @fileoverview gl-matrix - High performance matrix and vector operations for WebGL
* @author Brandon Jones
* @author Colin MacKenzie IV
* @version 1.3.7
*/
/*
* Copyright (c) 2012 Brandon Jones, Colin MacKenzie IV
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
// Updated to use a modification of the "returnExportsGlobal" pattern from https://github.com/umdjs/umd
(function (root, factory) {
if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory(global);
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], function () {
return factory(root);
});
} else {
// Browser globals
factory(root);
}
}(this, function (root) {
// Tweak to your liking
var FLOAT_EPSILON = 0.000001;
var glMath = {};
(function() {
if (typeof(Float32Array) != 'undefined') {
var y = new Float32Array(1);
var i = new Int32Array(y.buffer);
/**
* Fast way to calculate the inverse square root,
* see http://jsperf.com/inverse-square-root/5
*
* If typed arrays are not available, a slower
* implementation will be used.
*
* @param {Number} number the number
* @returns {Number} Inverse square root
*/
glMath.invsqrt = function(number) {
var x2 = number * 0.5;
y[0] = number;
var threehalfs = 1.5;
i[0] = 0x5f3759df - (i[0] >> 1);
var number2 = y[0];
return number2 * (threehalfs - (x2 * number2 * number2));
};
} else {
glMath.invsqrt = function(number) { return 1.0 / Math.sqrt(number); };
}
})();
/**
* @class System-specific optimal array type
* @name MatrixArray
*/
var MatrixArray = null;
// explicitly sets and returns the type of array to use within glMatrix
function setMatrixArrayType(type) {
MatrixArray = type;
return MatrixArray;
}
// auto-detects and returns the best type of array to use within glMatrix, falling
// back to Array if typed arrays are unsupported
function determineMatrixArrayType() {
MatrixArray = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
return MatrixArray;
}
determineMatrixArrayType();
/**
* @class 3 Dimensional Vector
* @name vec3
*/
var vec3 = {};
/**
* Creates a new instance of a vec3 using the default array type
* Any javascript array-like objects containing at least 3 numeric elements can serve as a vec3
*
* @param {vec3} [vec] vec3 containing values to initialize with
*
* @returns {vec3} New vec3
*/
vec3.create = function (vec) {
var dest = new MatrixArray(3);
if (vec) {
dest[0] = vec[0];
dest[1] = vec[1];
dest[2] = vec[2];
} else {
dest[0] = dest[1] = dest[2] = 0;
}
return dest;
};
/**
* Creates a new instance of a vec3, initializing it with the given arguments
*
* @param {number} x X value
* @param {number} y Y value
* @param {number} z Z value
* @returns {vec3} New vec3
*/
vec3.createFrom = function (x, y, z) {
var dest = new MatrixArray(3);
dest[0] = x;
dest[1] = y;
dest[2] = z;
return dest;
};
/**
* Copies the values of one vec3 to another
*
* @param {vec3} vec vec3 containing values to copy
* @param {vec3} dest vec3 receiving copied values
*
* @returns {vec3} dest
*/
vec3.set = function (vec, dest) {
dest[0] = vec[0];
dest[1] = vec[1];
dest[2] = vec[2];
return dest;
};
/**
* Compares two vectors for equality within a certain margin of error
*
* @param {vec3} a First vector
* @param {vec3} b Second vector
*
* @returns {Boolean} True if a is equivalent to b
*/
vec3.equal = function (a, b) {
return a === b || (
Math.abs(a[0] - b[0]) < FLOAT_EPSILON &&
Math.abs(a[1] - b[1]) < FLOAT_EPSILON &&
Math.abs(a[2] - b[2]) < FLOAT_EPSILON
);
};
/**
* Performs a vector addition
*
* @param {vec3} vec First operand
* @param {vec3} vec2 Second operand
* @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
*
* @returns {vec3} dest if specified, vec otherwise
*/
vec3.add = function (vec, vec2, dest) {
if (!dest || vec === dest) {
vec[0] += vec2[0];
vec[1] += vec2[1];
vec[2] += vec2[2];
return vec;
}
dest[0] = vec[0] + vec2[0];
dest[1] = vec[1] + vec2[1];
dest[2] = vec[2] + vec2[2];
return dest;
};
/**
* Performs a vector subtraction
*
* @param {vec3} vec First operand
* @param {vec3} vec2 Second operand
* @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
*
* @returns {vec3} dest if specified, vec otherwise
*/
vec3.subtract = function (vec, vec2, dest) {
if (!dest || vec === dest) {
vec[0] -= vec2[0];
vec[1] -= vec2[1];
vec[2] -= vec2[2];
return vec;
}
dest[0] = vec[0] - vec2[0];
dest[1] = vec[1] - vec2[1];
dest[2] = vec[2] - vec2[2];
return dest;
};
/**
* Performs a vector multiplication
*
* @param {vec3} vec First operand
* @param {vec3} vec2 Second operand
* @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
*
* @returns {vec3} dest if specified, vec otherwise
*/
vec3.multiply = function (vec, vec2, dest) {
if (!dest || vec === dest) {
vec[0] *= vec2[0];
vec[1] *= vec2[1];
vec[2] *= vec2[2];
return vec;
}
dest[0] = vec[0] * vec2[0];
dest[1] = vec[1] * vec2[1];
dest[2] = vec[2] * vec2[2];
return dest;
};
/**
* Negates the components of a vec3
*
* @param {vec3} vec vec3 to negate
* @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
*
* @returns {vec3} dest if specified, vec otherwise
*/
vec3.negate = function (vec, dest) {
if (!dest) { dest = vec; }
dest[0] = -vec[0];
dest[1] = -vec[1];
dest[2] = -vec[2];
return dest;
};
/**
* Multiplies the components of a vec3 by a scalar value
*
* @param {vec3} vec vec3 to scale
* @param {number} val Value to scale by
* @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
*
* @returns {vec3} dest if specified, vec otherwise
*/
vec3.scale = function (vec, val, dest) {
if (!dest || vec === dest) {
vec[0] *= val;
vec[1] *= val;
vec[2] *= val;
return vec;
}
dest[0] = vec[0] * val;
dest[1] = vec[1] * val;
dest[2] = vec[2] * val;
return dest;
};
/**
* Generates a unit vector of the same direction as the provided vec3
* If vector length is 0, returns [0, 0, 0]
*
* @param {vec3} vec vec3 to normalize
* @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
*
* @returns {vec3} dest if specified, vec otherwise
*/
vec3.normalize = function (vec, dest) {
if (!dest) { dest = vec; }
var x = vec[0], y = vec[1], z = vec[2],
len = Math.sqrt(x * x + y * y + z * z);
if (!len) {
dest[0] = 0;
dest[1] = 0;
dest[2] = 0;
return dest;
} else if (len === 1) {
dest[0] = x;
dest[1] = y;
dest[2] = z;
return dest;
}
len = 1 / len;
dest[0] = x * len;
dest[1] = y * len;
dest[2] = z * len;
return dest;
};
/**
* Generates the cross product of two vec3s
*
* @param {vec3} vec First operand
* @param {vec3} vec2 Second operand
* @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
*
* @returns {vec3} dest if specified, vec otherwise
*/
vec3.cross = function (vec, vec2, dest) {
if (!dest) { dest = vec; }
var x = vec[0], y = vec[1], z = vec[2],
x2 = vec2[0], y2 = vec2[1], z2 = vec2[2];
dest[0] = y * z2 - z * y2;
dest[1] = z * x2 - x * z2;
dest[2] = x * y2 - y * x2;
return dest;
};
/**
* Caclulates the length of a vec3
*
* @param {vec3} vec vec3 to calculate length of
*
* @returns {number} Length of vec
*/
vec3.length = function (vec) {
var x = vec[0], y = vec[1], z = vec[2];
return Math.sqrt(x * x + y * y + z * z);
};
/**
* Caclulates the squared length of a vec3
*
* @param {vec3} vec vec3 to calculate squared length of
*
* @returns {number} Squared Length of vec
*/
vec3.squaredLength = function (vec) {
var x = vec[0], y = vec[1], z = vec[2];
return x * x + y * y + z * z;
};
/**
* Caclulates the dot product of two vec3s
*
* @param {vec3} vec First operand
* @param {vec3} vec2 Second operand
*
* @returns {number} Dot product of vec and vec2
*/
vec3.dot = function (vec, vec2) {
return vec[0] * vec2[0] + vec[1] * vec2[1] + vec[2] * vec2[2];
};
/**
* Generates a unit vector pointing from one vector to another
*
* @param {vec3} vec Origin vec3
* @param {vec3} vec2 vec3 to point to
* @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
*
* @returns {vec3} dest if specified, vec otherwise
*/
vec3.direction = function (vec, vec2, dest) {
if (!dest) { dest = vec; }
var x = vec[0] - vec2[0],
y = vec[1] - vec2[1],
z = vec[2] - vec2[2],
len = Math.sqrt(x * x + y * y + z * z);
if (!len) {
dest[0] = 0;
dest[1] = 0;
dest[2] = 0;
return dest;
}
len = 1 / len;
dest[0] = x * len;
dest[1] = y * len;
dest[2] = z * len;
return dest;
};
/**
* Performs a linear interpolation between two vec3
*
* @param {vec3} vec First vector
* @param {vec3} vec2 Second vector
* @param {number} lerp Interpolation amount between the two inputs
* @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
*
* @returns {vec3} dest if specified, vec otherwise
*/
vec3.lerp = function (vec, vec2, lerp, dest) {
if (!dest) { dest = vec; }
dest[0] = vec[0] + lerp * (vec2[0] - vec[0]);
dest[1] = vec[1] + lerp * (vec2[1] - vec[1]);
dest[2] = vec[2] + lerp * (vec2[2] - vec[2]);
return dest;
};
/**
* Calculates the euclidian distance between two vec3
*
* Params:
* @param {vec3} vec First vector
* @param {vec3} vec2 Second vector
*
* @returns {number} Distance between vec and vec2
*/
vec3.dist = function (vec, vec2) {
var x = vec2[0] - vec[0],
y = vec2[1] - vec[1],
z = vec2[2] - vec[2];
return Math.sqrt(x*x + y*y + z*z);
};
// Pre-allocated to prevent unecessary garbage collection
var unprojectMat = null;
var unprojectVec = new MatrixArray(4);
/**
* Projects the specified vec3 from screen space into object space
* Based on the <a href="http://webcvs.freedesktop.org/mesa/Mesa/src/glu/mesa/project.c?revision=1.4&view=markup">Mesa gluUnProject implementation</a>
*
* @param {vec3} vec Screen-space vector to project
* @param {mat4} view View matrix
* @param {mat4} proj Projection matrix
* @param {vec4} viewport Viewport as given to gl.viewport [x, y, width, height]
* @param {vec3} [dest] vec3 receiving unprojected result. If not specified result is written to vec
*
* @returns {vec3} dest if specified, vec otherwise
*/
vec3.unproject = function (vec, view, proj, viewport, dest) {
if (!dest) { dest = vec; }
if(!unprojectMat) {
unprojectMat = mat4.create();
}
var m = unprojectMat;
var v = unprojectVec;
v[0] = (vec[0] - viewport[0]) * 2.0 / viewport[2] - 1.0;
v[1] = (vec[1] - viewport[1]) * 2.0 / viewport[3] - 1.0;
v[2] = 2.0 * vec[2] - 1.0;
v[3] = 1.0;
mat4.multiply(proj, view, m);
if(!mat4.inverse(m)) { return null; }
mat4.multiplyVec4(m, v);
if(v[3] === 0.0) { return null; }
dest[0] = v[0] / v[3];
dest[1] = v[1] / v[3];
dest[2] = v[2] / v[3];
return dest;
};
var xUnitVec3 = vec3.createFrom(1,0,0);
var yUnitVec3 = vec3.createFrom(0,1,0);
var zUnitVec3 = vec3.createFrom(0,0,1);
var tmpvec3 = vec3.create();
/**
* Generates a quaternion of rotation between two given normalized vectors
*
* @param {vec3} a Normalized source vector
* @param {vec3} b Normalized target vector
* @param {quat4} [dest] quat4 receiving operation result.
*
* @returns {quat4} dest if specified, a new quat4 otherwise
*/
vec3.rotationTo = function (a, b, dest) {
if (!dest) { dest = quat4.create(); }
var d = vec3.dot(a, b);
var axis = tmpvec3;
if (d >= 1.0) {
quat4.set(identityQuat4, dest);
} else if (d < (0.000001 - 1.0)) {
vec3.cross(xUnitVec3, a, axis);
if (vec3.length(axis) < 0.000001)
vec3.cross(yUnitVec3, a, axis);
if (vec3.length(axis) < 0.000001)
vec3.cross(zUnitVec3, a, axis);
vec3.normalize(axis);
quat4.fromAngleAxis(Math.PI, axis, dest);
} else {
var s = Math.sqrt((1.0 + d) * 2.0);
var sInv = 1.0 / s;
vec3.cross(a, b, axis);
dest[0] = axis[0] * sInv;
dest[1] = axis[1] * sInv;
dest[2] = axis[2] * sInv;
dest[3] = s * 0.5;
quat4.normalize(dest);
}
if (dest[3] > 1.0) dest[3] = 1.0;
else if (dest[3] < -1.0) dest[3] = -1.0;
return dest;
};
/**
* Returns a string representation of a vector
*
* @param {vec3} vec Vector to represent as a string
*
* @returns {string} String representation of vec
*/
vec3.str = function (vec) {
return '[' + vec[0] + ', ' + vec[1] + ', ' + vec[2] + ']';
};
/**
* @class 3x3 Matrix
* @name mat3
*/
var mat3 = {};
/**
* Creates a new instance of a mat3 using the default array type
* Any javascript array-like object containing at least 9 numeric elements can serve as a mat3
*
* @param {mat3} [mat] mat3 containing values to initialize with
*
* @returns {mat3} New mat3
*/
mat3.create = function (mat) {
var dest = new MatrixArray(9);
if (mat) {
dest[0] = mat[0];
dest[1] = mat[1];
dest[2] = mat[2];
dest[3] = mat[3];
dest[4] = mat[4];
dest[5] = mat[5];
dest[6] = mat[6];
dest[7] = mat[7];
dest[8] = mat[8];
} else {
dest[0] = dest[1] =
dest[2] = dest[3] =
dest[4] = dest[5] =
dest[6] = dest[7] =
dest[8] = 0;
}
return dest;
};
/**
* Creates a new instance of a mat3, initializing it with the given arguments
*
* @param {number} m00
* @param {number} m01
* @param {number} m02
* @param {number} m10
* @param {number} m11
* @param {number} m12
* @param {number} m20
* @param {number} m21
* @param {number} m22
* @returns {mat3} New mat3
*/
mat3.createFrom = function (m00, m01, m02, m10, m11, m12, m20, m21, m22) {
var dest = new MatrixArray(9);
dest[0] = m00;
dest[1] = m01;
dest[2] = m02;
dest[3] = m10;
dest[4] = m11;
dest[5] = m12;
dest[6] = m20;
dest[7] = m21;
dest[8] = m22;
return dest;
};
/**
* Calculates the determinant of a mat3
*
* @param {mat3} mat mat3 to calculate determinant of
*
* @returns {Number} determinant of mat
*/
mat3.determinant = function (mat) {
var a00 = mat[0], a01 = mat[1], a02 = mat[2],
a10 = mat[3], a11 = mat[4], a12 = mat[5],
a20 = mat[6], a21 = mat[7], a22 = mat[8];
return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
};
/**
* Calculates the inverse matrix of a mat3
*
* @param {mat3} mat mat3 to calculate inverse of
* @param {mat3} [dest] mat3 receiving inverse matrix. If not specified result is written to mat
*
* @param {mat3} dest is specified, mat otherwise, null if matrix cannot be inverted
*/
mat3.inverse = function (mat, dest) {
var a00 = mat[0], a01 = mat[1], a02 = mat[2],
a10 = mat[3], a11 = mat[4], a12 = mat[5],
a20 = mat[6], a21 = mat[7], a22 = mat[8],
b01 = a22 * a11 - a12 * a21,
b11 = -a22 * a10 + a12 * a20,
b21 = a21 * a10 - a11 * a20,
d = a00 * b01 + a01 * b11 + a02 * b21,
id;
if (!d) { return null; }
id = 1 / d;
if (!dest) { dest = mat3.create(); }
dest[0] = b01 * id;
dest[1] = (-a22 * a01 + a02 * a21) * id;
dest[2] = (a12 * a01 - a02 * a11) * id;
dest[3] = b11 * id;
dest[4] = (a22 * a00 - a02 * a20) * id;
dest[5] = (-a12 * a00 + a02 * a10) * id;
dest[6] = b21 * id;
dest[7] = (-a21 * a00 + a01 * a20) * id;
dest[8] = (a11 * a00 - a01 * a10) * id;
return dest;
};
/**
* Performs a matrix multiplication
*
* @param {mat3} mat First operand
* @param {mat3} mat2 Second operand
* @param {mat3} [dest] mat3 receiving operation result. If not specified result is written to mat
*
* @returns {mat3} dest if specified, mat otherwise
*/
mat3.multiply = function (mat, mat2, dest) {
if (!dest) { dest = mat; }
// Cache the matrix values (makes for huge speed increases!)
var a00 = mat[0], a01 = mat[1], a02 = mat[2],
a10 = mat[3], a11 = mat[4], a12 = mat[5],
a20 = mat[6], a21 = mat[7], a22 = mat[8],
b00 = mat2[0], b01 = mat2[1], b02 = mat2[2],
b10 = mat2[3], b11 = mat2[4], b12 = mat2[5],
b20 = mat2[6], b21 = mat2[7], b22 = mat2[8];
dest[0] = b00 * a00 + b01 * a10 + b02 * a20;
dest[1] = b00 * a01 + b01 * a11 + b02 * a21;
dest[2] = b00 * a02 + b01 * a12 + b02 * a22;
dest[3] = b10 * a00 + b11 * a10 + b12 * a20;
dest[4] = b10 * a01 + b11 * a11 + b12 * a21;
dest[5] = b10 * a02 + b11 * a12 + b12 * a22;
dest[6] = b20 * a00 + b21 * a10 + b22 * a20;
dest[7] = b20 * a01 + b21 * a11 + b22 * a21;
dest[8] = b20 * a02 + b21 * a12 + b22 * a22;
return dest;
};
/**
* Transforms the vec2 according to the given mat3.
*
* @param {mat3} matrix mat3 to multiply against
* @param {vec2} vec the vector to multiply
* @param {vec2} [dest] an optional receiving vector. If not given, vec is used.
*
* @returns {vec2} The multiplication result
**/
mat3.multiplyVec2 = function(matrix, vec, dest) {
if (!dest) dest = vec;
var x = vec[0], y = vec[1];
dest[0] = x * matrix[0] + y * matrix[3] + matrix[6];
dest[1] = x * matrix[1] + y * matrix[4] + matrix[7];
return dest;
};
/**
* Transforms the vec3 according to the given mat3
*
* @param {mat3} matrix mat3 to multiply against
* @param {vec3} vec the vector to multiply
* @param {vec3} [dest] an optional receiving vector. If not given, vec is used.
*
* @returns {vec3} The multiplication result
**/
mat3.multiplyVec3 = function(matrix, vec, dest) {
if (!dest) dest = vec;
var x = vec[0], y = vec[1], z = vec[2];
dest[0] = x * matrix[0] + y * matrix[3] + z * matrix[6];
dest[1] = x * matrix[1] + y * matrix[4] + z * matrix[7];
dest[2] = x * matrix[2] + y * matrix[5] + z * matrix[8];
return dest;
};
/**
* Copies the values of one mat3 to another
*
* @param {mat3} mat mat3 containing values to copy
* @param {mat3} dest mat3 receiving copied values
*
* @returns {mat3} dest
*/
mat3.set = function (mat, dest) {
dest[0] = mat[0];
dest[1] = mat[1];
dest[2] = mat[2];
dest[3] = mat[3];
dest[4] = mat[4];
dest[5] = mat[5];
dest[6] = mat[6];
dest[7] = mat[7];
dest[8] = mat[8];
return dest;
};
/**
* Compares two matrices for equality within a certain margin of error
*
* @param {mat3} a First matrix
* @param {mat3} b Second matrix
*
* @returns {Boolean} True if a is equivalent to b
*/
mat3.equal = function (a, b) {
return a === b || (
Math.abs(a[0] - b[0]) < FLOAT_EPSILON &&
Math.abs(a[1] - b[1]) < FLOAT_EPSILON &&
Math.abs(a[2] - b[2]) < FLOAT_EPSILON &&
Math.abs(a[3] - b[3]) < FLOAT_EPSILON &&
Math.abs(a[4] - b[4]) < FLOAT_EPSILON &&
Math.abs(a[5] - b[5]) < FLOAT_EPSILON &&
Math.abs(a[6] - b[6]) < FLOAT_EPSILON &&
Math.abs(a[7] - b[7]) < FLOAT_EPSILON &&
Math.abs(a[8] - b[8]) < FLOAT_EPSILON
);
};
/**
* Sets a mat3 to an identity matrix
*
* @param {mat3} dest mat3 to set
*
* @returns dest if specified, otherwise a new mat3
*/
mat3.identity = function (dest) {
if (!dest) { dest = mat3.create(); }
dest[0] = 1;
dest[1] = 0;
dest[2] = 0;
dest[3] = 0;
dest[4] = 1;
dest[5] = 0;
dest[6] = 0;
dest[7] = 0;
dest[8] = 1;
return dest;
};
/**
* Transposes a mat3 (flips the values over the diagonal)
*
* Params:
* @param {mat3} mat mat3 to transpose
* @param {mat3} [dest] mat3 receiving transposed values. If not specified result is written to mat
*
* @returns {mat3} dest is specified, mat otherwise
*/
mat3.transpose = function (mat, dest) {
// If we are transposing ourselves we can skip a few steps but have to cache some values
if (!dest || mat === dest) {
var a01 = mat[1], a02 = mat[2],
a12 = mat[5];
mat[1] = mat[3];
mat[2] = mat[6];
mat[3] = a01;
mat[5] = mat[7];
mat[6] = a02;
mat[7] = a12;
return mat;
}
dest[0] = mat[0];
dest[1] = mat[3];
dest[2] = mat[6];
dest[3] = mat[1];
dest[4] = mat[4];
dest[5] = mat[7];
dest[6] = mat[2];
dest[7] = mat[5];
dest[8] = mat[8];
return dest;
};
/**
* Copies the elements of a mat3 into the upper 3x3 elements of a mat4
*
* @param {mat3} mat mat3 containing values to copy
* @param {mat4} [dest] mat4 receiving copied values
*
* @returns {mat4} dest if specified, a new mat4 otherwise
*/
mat3.toMat4 = function (mat, dest) {
if (!dest) { dest = mat4.create(); }
dest[15] = 1;
dest[14] = 0;
dest[13] = 0;
dest[12] = 0;
dest[11] = 0;
dest[10] = mat[8];
dest[9] = mat[7];
dest[8] = mat[6];
dest[7] = 0;
dest[6] = mat[5];
dest[5] = mat[4];
dest[4] = mat[3];
dest[3] = 0;
dest[2] = mat[2];
dest[1] = mat[1];
dest[0] = mat[0];
return dest;
};
/**
* Returns a string representation of a mat3
*
* @param {mat3} mat mat3 to represent as a string
*
* @param {string} String representation of mat
*/
mat3.str = function (mat) {
return '[' + mat[0] + ', ' + mat[1] + ', ' + mat[2] +
', ' + mat[3] + ', ' + mat[4] + ', ' + mat[5] +
', ' + mat[6] + ', ' + mat[7] + ', ' + mat[8] + ']';
};
/**
* @class 4x4 Matrix
* @name mat4
*/
var mat4 = {};
/**
* Creates a new instance of a mat4 using the default array type
* Any javascript array-like object containing at least 16 numeric elements can serve as a mat4
*
* @param {mat4} [mat] mat4 containing values to initialize with
*
* @returns {mat4} New mat4
*/
mat4.create = function (mat) {
var dest = new MatrixArray(16);
if (mat) {
dest[0] = mat[0];
dest[1] = mat[1];
dest[2] = mat[2];
dest[3] = mat[3];
dest[4] = mat[4];
dest[5] = mat[5];
dest[6] = mat[6];
dest[7] = mat[7];
dest[8] = mat[8];
dest[9] = mat[9];
dest[10] = mat[10];
dest[11] = mat[11];
dest[12] = mat[12];
dest[13] = mat[13];
dest[14] = mat[14];
dest[15] = mat[15];
}
return dest;
};
/**
* Creates a new instance of a mat4, initializing it with the given arguments
*
* @param {number} m00
* @param {number} m01
* @param {number} m02
* @param {number} m03
* @param {number} m10
* @param {number} m11
* @param {number} m12
* @param {number} m13
* @param {number} m20
* @param {number} m21
* @param {number} m22
* @param {number} m23
* @param {number} m30
* @param {number} m31
* @param {number} m32
* @param {number} m33
* @returns {mat4} New mat4
*/
mat4.createFrom = function (m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
var dest = new MatrixArray(16);
dest[0] = m00;
dest[1] = m01;
dest[2] = m02;
dest[3] = m03;
dest[4] = m10;
dest[5] = m11;
dest[6] = m12;
dest[7] = m13;
dest[8] = m20;
dest[9] = m21;
dest[10] = m22;
dest[11] = m23;
dest[12] = m30;
dest[13] = m31;
dest[14] = m32;
dest[15] = m33;
return dest;
};
/**
* Copies the values of one mat4 to another
*
* @param {mat4} mat mat4 containing values to copy
* @param {mat4} dest mat4 receiving copied values
*
* @returns {mat4} dest
*/
mat4.set = function (mat, dest) {
dest[0] = mat[0];
dest[1] = mat[1];
dest[2] = mat[2];
dest[3] = mat[3];
dest[4] = mat[4];
dest[5] = mat[5];
dest[6] = mat[6];
dest[7] = mat[7];
dest[8] = mat[8];
dest[9] = mat[9];
dest[10] = mat[10];
dest[11] = mat[11];
dest[12] = mat[12];
dest[13] = mat[13];
dest[14] = mat[14];
dest[15] = mat[15];