-
Notifications
You must be signed in to change notification settings - Fork 0
/
m4.js
1467 lines (1327 loc) · 42.2 KB
/
m4.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
/*
* Copyright 2021 GFXFundamentals.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of GFXFundamentals. nor the names of his
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Various 3d math functions.
*
* @module webgl-3d-math
*/
(function(root, factory) { // eslint-disable-line
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else {
// Browser globals
root.m4 = factory();
}
}(this, function() {
"use strict";
/**
* An array or typed array with 3 values.
* @typedef {number[]|TypedArray} Vector3
* @memberOf module:webgl-3d-math
*/
/**
* An array or typed array with 4 values.
* @typedef {number[]|TypedArray} Vector4
* @memberOf module:webgl-3d-math
*/
/**
* An array or typed array with 16 values.
* @typedef {number[]|TypedArray} Matrix4
* @memberOf module:webgl-3d-math
*/
let MatType = Float32Array;
/**
* Sets the type this library creates for a Mat4
* @param {constructor} Ctor the constructor for the type. Either `Float32Array` or `Array`
* @return {constructor} previous constructor for Mat4
*/
function setDefaultType(Ctor) {
const OldType = MatType;
MatType = Ctor;
return OldType;
}
/**
* Takes two 4-by-4 matrices, a and b, and computes the product in the order
* that pre-composes b with a. In other words, the matrix returned will
* transform by b first and then a. Note this is subtly different from just
* multiplying the matrices together. For given a and b, this function returns
* the same object in both row-major and column-major mode.
* @param {Matrix4} a A matrix.
* @param {Matrix4} b A matrix.
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
*/
function multiply(a, b, dst) {
dst = dst || new MatType(16);
var b00 = b[0 * 4 + 0];
var b01 = b[0 * 4 + 1];
var b02 = b[0 * 4 + 2];
var b03 = b[0 * 4 + 3];
var b10 = b[1 * 4 + 0];
var b11 = b[1 * 4 + 1];
var b12 = b[1 * 4 + 2];
var b13 = b[1 * 4 + 3];
var b20 = b[2 * 4 + 0];
var b21 = b[2 * 4 + 1];
var b22 = b[2 * 4 + 2];
var b23 = b[2 * 4 + 3];
var b30 = b[3 * 4 + 0];
var b31 = b[3 * 4 + 1];
var b32 = b[3 * 4 + 2];
var b33 = b[3 * 4 + 3];
var a00 = a[0 * 4 + 0];
var a01 = a[0 * 4 + 1];
var a02 = a[0 * 4 + 2];
var a03 = a[0 * 4 + 3];
var a10 = a[1 * 4 + 0];
var a11 = a[1 * 4 + 1];
var a12 = a[1 * 4 + 2];
var a13 = a[1 * 4 + 3];
var a20 = a[2 * 4 + 0];
var a21 = a[2 * 4 + 1];
var a22 = a[2 * 4 + 2];
var a23 = a[2 * 4 + 3];
var a30 = a[3 * 4 + 0];
var a31 = a[3 * 4 + 1];
var a32 = a[3 * 4 + 2];
var a33 = a[3 * 4 + 3];
dst[ 0] = b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30;
dst[ 1] = b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31;
dst[ 2] = b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32;
dst[ 3] = b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33;
dst[ 4] = b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30;
dst[ 5] = b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31;
dst[ 6] = b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32;
dst[ 7] = b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33;
dst[ 8] = b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30;
dst[ 9] = b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31;
dst[10] = b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32;
dst[11] = b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33;
dst[12] = b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30;
dst[13] = b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31;
dst[14] = b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32;
dst[15] = b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33;
return dst;
}
/**
* adds 2 vectors3s
* @param {Vector3} a a
* @param {Vector3} b b
* @param {Vector3} dst optional vector3 to store result
* @return {Vector3} dst or new Vector3 if not provided
* @memberOf module:webgl-3d-math
*/
function addVectors(a, b, dst) {
dst = dst || new MatType(3);
dst[0] = a[0] + b[0];
dst[1] = a[1] + b[1];
dst[2] = a[2] + b[2];
return dst;
}
/**
* subtracts 2 vectors3s
* @param {Vector3} a a
* @param {Vector3} b b
* @param {Vector3} dst optional vector3 to store result
* @return {Vector3} dst or new Vector3 if not provided
* @memberOf module:webgl-3d-math
*/
function subtractVectors(a, b, dst) {
dst = dst || new MatType(3);
dst[0] = a[0] - b[0];
dst[1] = a[1] - b[1];
dst[2] = a[2] - b[2];
return dst;
}
/**
* scale vectors3
* @param {Vector3} v vector
* @param {Number} s scale
* @param {Vector3} dst optional vector3 to store result
* @return {Vector3} dst or new Vector3 if not provided
* @memberOf module:webgl-3d-math
*/
function scaleVector(v, s, dst) {
dst = dst || new MatType(3);
dst[0] = v[0] * s;
dst[1] = v[1] * s;
dst[2] = v[2] * s;
return dst;
}
/**
* normalizes a vector.
* @param {Vector3} v vector to normalize
* @param {Vector3} dst optional vector3 to store result
* @return {Vector3} dst or new Vector3 if not provided
* @memberOf module:webgl-3d-math
*/
function normalize(v, dst) {
dst = dst || new MatType(3);
var length = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
// make sure we don't divide by 0.
if (length > 0.00001) {
dst[0] = v[0] / length;
dst[1] = v[1] / length;
dst[2] = v[2] / length;
}
return dst;
}
/**
* Computes the length of a vector
* @param {Vector3} v vector to take length of
* @return {number} length of vector
*/
function length(v) {
return Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}
/**
* Computes the length squared of a vector
* @param {Vector3} v vector to take length of
* @return {number} length sqaured of vector
*/
function lengthSq(v) {
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
}
/**
* Computes the cross product of 2 vectors3s
* @param {Vector3} a a
* @param {Vector3} b b
* @param {Vector3} dst optional vector3 to store result
* @return {Vector3} dst or new Vector3 if not provided
* @memberOf module:webgl-3d-math
*/
function cross(a, b, dst) {
dst = dst || new MatType(3);
dst[0] = a[1] * b[2] - a[2] * b[1];
dst[1] = a[2] * b[0] - a[0] * b[2];
dst[2] = a[0] * b[1] - a[1] * b[0];
return dst;
}
/**
* Computes the dot product of two vectors; assumes both vectors have
* three entries.
* @param {Vector3} a Operand vector.
* @param {Vector3} b Operand vector.
* @return {number} dot product
* @memberOf module:webgl-3d-math
*/
function dot(a, b) {
return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]);
}
/**
* Computes the distance squared between 2 points
* @param {Vector3} a
* @param {Vector3} b
* @return {number} distance squared between a and b
*/
function distanceSq(a, b) {
const dx = a[0] - b[0];
const dy = a[1] - b[1];
const dz = a[2] - b[2];
return dx * dx + dy * dy + dz * dz;
}
/**
* Computes the distance between 2 points
* @param {Vector3} a
* @param {Vector3} b
* @return {number} distance between a and b
*/
function distance(a, b) {
return Math.sqrt(distanceSq(a, b));
}
/**
* Makes an identity matrix.
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function identity(dst) {
dst = dst || new MatType(16);
dst[ 0] = 1;
dst[ 1] = 0;
dst[ 2] = 0;
dst[ 3] = 0;
dst[ 4] = 0;
dst[ 5] = 1;
dst[ 6] = 0;
dst[ 7] = 0;
dst[ 8] = 0;
dst[ 9] = 0;
dst[10] = 1;
dst[11] = 0;
dst[12] = 0;
dst[13] = 0;
dst[14] = 0;
dst[15] = 1;
return dst;
}
/**
* Transposes a matrix.
* @param {Matrix4} m matrix to transpose.
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function transpose(m, dst) {
dst = dst || new MatType(16);
dst[ 0] = m[0];
dst[ 1] = m[4];
dst[ 2] = m[8];
dst[ 3] = m[12];
dst[ 4] = m[1];
dst[ 5] = m[5];
dst[ 6] = m[9];
dst[ 7] = m[13];
dst[ 8] = m[2];
dst[ 9] = m[6];
dst[10] = m[10];
dst[11] = m[14];
dst[12] = m[3];
dst[13] = m[7];
dst[14] = m[11];
dst[15] = m[15];
return dst;
}
/**
* Creates a lookAt matrix.
* This is a world matrix for a camera. In other words it will transform
* from the origin to a place and orientation in the world. For a view
* matrix take the inverse of this.
* @param {Vector3} cameraPosition position of the camera
* @param {Vector3} target position of the target
* @param {Vector3} up direction
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function lookAt(cameraPosition, target, up, dst) {
dst = dst || new MatType(16);
var zAxis = normalize(
subtractVectors(cameraPosition, target));
var xAxis = normalize(cross(up, zAxis));
var yAxis = normalize(cross(zAxis, xAxis));
dst[ 0] = xAxis[0];
dst[ 1] = xAxis[1];
dst[ 2] = xAxis[2];
dst[ 3] = 0;
dst[ 4] = yAxis[0];
dst[ 5] = yAxis[1];
dst[ 6] = yAxis[2];
dst[ 7] = 0;
dst[ 8] = zAxis[0];
dst[ 9] = zAxis[1];
dst[10] = zAxis[2];
dst[11] = 0;
dst[12] = cameraPosition[0];
dst[13] = cameraPosition[1];
dst[14] = cameraPosition[2];
dst[15] = 1;
return dst;
}
/**
* Computes a 4-by-4 perspective transformation matrix given the angular height
* of the frustum, the aspect ratio, and the near and far clipping planes. The
* arguments define a frustum extending in the negative z direction. The given
* angle is the vertical angle of the frustum, and the horizontal angle is
* determined to produce the given aspect ratio. The arguments near and far are
* the distances to the near and far clipping planes. Note that near and far
* are not z coordinates, but rather they are distances along the negative
* z-axis. The matrix generated sends the viewing frustum to the unit box.
* We assume a unit box extending from -1 to 1 in the x and y dimensions and
* from -1 to 1 in the z dimension.
* @param {number} fieldOfViewInRadians field of view in y axis.
* @param {number} aspect aspect of viewport (width / height)
* @param {number} near near Z clipping plane
* @param {number} far far Z clipping plane
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function perspective(fieldOfViewInRadians, aspect, near, far, dst) {
dst = dst || new MatType(16);
var f = Math.tan(Math.PI * 0.5 - 0.5 * fieldOfViewInRadians);
var rangeInv = 1.0 / (near - far);
dst[ 0] = f / aspect;
dst[ 1] = 0;
dst[ 2] = 0;
dst[ 3] = 0;
dst[ 4] = 0;
dst[ 5] = f;
dst[ 6] = 0;
dst[ 7] = 0;
dst[ 8] = 0;
dst[ 9] = 0;
dst[10] = (near + far) * rangeInv;
dst[11] = -1;
dst[12] = 0;
dst[13] = 0;
dst[14] = near * far * rangeInv * 2;
dst[15] = 0;
return dst;
}
/**
* Computes a 4-by-4 orthographic projection matrix given the coordinates of the
* planes defining the axis-aligned, box-shaped viewing volume. The matrix
* generated sends that box to the unit box. Note that although left and right
* are x coordinates and bottom and top are y coordinates, near and far
* are not z coordinates, but rather they are distances along the negative
* z-axis. We assume a unit box extending from -1 to 1 in the x and y
* dimensions and from -1 to 1 in the z dimension.
* @param {number} left The x coordinate of the left plane of the box.
* @param {number} right The x coordinate of the right plane of the box.
* @param {number} bottom The y coordinate of the bottom plane of the box.
* @param {number} top The y coordinate of the right plane of the box.
* @param {number} near The negative z coordinate of the near plane of the box.
* @param {number} far The negative z coordinate of the far plane of the box.
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function orthographic(left, right, bottom, top, near, far, dst) {
dst = dst || new MatType(16);
dst[ 0] = 2 / (right - left);
dst[ 1] = 0;
dst[ 2] = 0;
dst[ 3] = 0;
dst[ 4] = 0;
dst[ 5] = 2 / (top - bottom);
dst[ 6] = 0;
dst[ 7] = 0;
dst[ 8] = 0;
dst[ 9] = 0;
dst[10] = 2 / (near - far);
dst[11] = 0;
dst[12] = (left + right) / (left - right);
dst[13] = (bottom + top) / (bottom - top);
dst[14] = (near + far) / (near - far);
dst[15] = 1;
return dst;
}
/**
* Computes a 4-by-4 perspective transformation matrix given the left, right,
* top, bottom, near and far clipping planes. The arguments define a frustum
* extending in the negative z direction. The arguments near and far are the
* distances to the near and far clipping planes. Note that near and far are not
* z coordinates, but rather they are distances along the negative z-axis. The
* matrix generated sends the viewing frustum to the unit box. We assume a unit
* box extending from -1 to 1 in the x and y dimensions and from -1 to 1 in the z
* dimension.
* @param {number} left The x coordinate of the left plane of the box.
* @param {number} right The x coordinate of the right plane of the box.
* @param {number} bottom The y coordinate of the bottom plane of the box.
* @param {number} top The y coordinate of the right plane of the box.
* @param {number} near The negative z coordinate of the near plane of the box.
* @param {number} far The negative z coordinate of the far plane of the box.
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function frustum(left, right, bottom, top, near, far, dst) {
dst = dst || new MatType(16);
var dx = right - left;
var dy = top - bottom;
var dz = far - near;
dst[ 0] = 2 * near / dx;
dst[ 1] = 0;
dst[ 2] = 0;
dst[ 3] = 0;
dst[ 4] = 0;
dst[ 5] = 2 * near / dy;
dst[ 6] = 0;
dst[ 7] = 0;
dst[ 8] = (left + right) / dx;
dst[ 9] = (top + bottom) / dy;
dst[10] = -(far + near) / dz;
dst[11] = -1;
dst[12] = 0;
dst[13] = 0;
dst[14] = -2 * near * far / dz;
dst[15] = 0;
return dst;
}
/**
* Makes a translation matrix
* @param {number} tx x translation.
* @param {number} ty y translation.
* @param {number} tz z translation.
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function translation(tx, ty, tz, dst) {
dst = dst || new MatType(16);
dst[ 0] = 1;
dst[ 1] = 0;
dst[ 2] = 0;
dst[ 3] = 0;
dst[ 4] = 0;
dst[ 5] = 1;
dst[ 6] = 0;
dst[ 7] = 0;
dst[ 8] = 0;
dst[ 9] = 0;
dst[10] = 1;
dst[11] = 0;
dst[12] = tx;
dst[13] = ty;
dst[14] = tz;
dst[15] = 1;
return dst;
}
/**
* Multiply by translation matrix.
* @param {Matrix4} m matrix to multiply
* @param {number} tx x translation.
* @param {number} ty y translation.
* @param {number} tz z translation.
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function translate(m, tx, ty, tz, dst) {
// This is the optimized version of
// return multiply(m, translation(tx, ty, tz), dst);
dst = dst || new MatType(16);
var m00 = m[0];
var m01 = m[1];
var m02 = m[2];
var m03 = m[3];
var m10 = m[1 * 4 + 0];
var m11 = m[1 * 4 + 1];
var m12 = m[1 * 4 + 2];
var m13 = m[1 * 4 + 3];
var m20 = m[2 * 4 + 0];
var m21 = m[2 * 4 + 1];
var m22 = m[2 * 4 + 2];
var m23 = m[2 * 4 + 3];
var m30 = m[3 * 4 + 0];
var m31 = m[3 * 4 + 1];
var m32 = m[3 * 4 + 2];
var m33 = m[3 * 4 + 3];
if (m !== dst) {
dst[ 0] = m00;
dst[ 1] = m01;
dst[ 2] = m02;
dst[ 3] = m03;
dst[ 4] = m10;
dst[ 5] = m11;
dst[ 6] = m12;
dst[ 7] = m13;
dst[ 8] = m20;
dst[ 9] = m21;
dst[10] = m22;
dst[11] = m23;
}
dst[12] = m00 * tx + m10 * ty + m20 * tz + m30;
dst[13] = m01 * tx + m11 * ty + m21 * tz + m31;
dst[14] = m02 * tx + m12 * ty + m22 * tz + m32;
dst[15] = m03 * tx + m13 * ty + m23 * tz + m33;
return dst;
}
/**
* Makes an x rotation matrix
* @param {number} angleInRadians amount to rotate
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function xRotation(angleInRadians, dst) {
dst = dst || new MatType(16);
var c = Math.cos(angleInRadians);
var s = Math.sin(angleInRadians);
dst[ 0] = 1;
dst[ 1] = 0;
dst[ 2] = 0;
dst[ 3] = 0;
dst[ 4] = 0;
dst[ 5] = c;
dst[ 6] = s;
dst[ 7] = 0;
dst[ 8] = 0;
dst[ 9] = -s;
dst[10] = c;
dst[11] = 0;
dst[12] = 0;
dst[13] = 0;
dst[14] = 0;
dst[15] = 1;
return dst;
}
/**
* Multiply by an x rotation matrix
* @param {Matrix4} m matrix to multiply
* @param {number} angleInRadians amount to rotate
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function xRotate(m, angleInRadians, dst) {
// this is the optimized version of
// return multiply(m, xRotation(angleInRadians), dst);
dst = dst || new MatType(16);
var m10 = m[4];
var m11 = m[5];
var m12 = m[6];
var m13 = m[7];
var m20 = m[8];
var m21 = m[9];
var m22 = m[10];
var m23 = m[11];
var c = Math.cos(angleInRadians);
var s = Math.sin(angleInRadians);
dst[4] = c * m10 + s * m20;
dst[5] = c * m11 + s * m21;
dst[6] = c * m12 + s * m22;
dst[7] = c * m13 + s * m23;
dst[8] = c * m20 - s * m10;
dst[9] = c * m21 - s * m11;
dst[10] = c * m22 - s * m12;
dst[11] = c * m23 - s * m13;
if (m !== dst) {
dst[ 0] = m[ 0];
dst[ 1] = m[ 1];
dst[ 2] = m[ 2];
dst[ 3] = m[ 3];
dst[12] = m[12];
dst[13] = m[13];
dst[14] = m[14];
dst[15] = m[15];
}
return dst;
}
/**
* Makes an y rotation matrix
* @param {number} angleInRadians amount to rotate
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function yRotation(angleInRadians, dst) {
dst = dst || new MatType(16);
var c = Math.cos(angleInRadians);
var s = Math.sin(angleInRadians);
dst[ 0] = c;
dst[ 1] = 0;
dst[ 2] = -s;
dst[ 3] = 0;
dst[ 4] = 0;
dst[ 5] = 1;
dst[ 6] = 0;
dst[ 7] = 0;
dst[ 8] = s;
dst[ 9] = 0;
dst[10] = c;
dst[11] = 0;
dst[12] = 0;
dst[13] = 0;
dst[14] = 0;
dst[15] = 1;
return dst;
}
/**
* Multiply by an y rotation matrix
* @param {Matrix4} m matrix to multiply
* @param {number} angleInRadians amount to rotate
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function yRotate(m, angleInRadians, dst) {
// this is the optimized version of
// return multiply(m, yRotation(angleInRadians), dst);
dst = dst || new MatType(16);
var m00 = m[0 * 4 + 0];
var m01 = m[0 * 4 + 1];
var m02 = m[0 * 4 + 2];
var m03 = m[0 * 4 + 3];
var m20 = m[2 * 4 + 0];
var m21 = m[2 * 4 + 1];
var m22 = m[2 * 4 + 2];
var m23 = m[2 * 4 + 3];
var c = Math.cos(angleInRadians);
var s = Math.sin(angleInRadians);
dst[ 0] = c * m00 - s * m20;
dst[ 1] = c * m01 - s * m21;
dst[ 2] = c * m02 - s * m22;
dst[ 3] = c * m03 - s * m23;
dst[ 8] = c * m20 + s * m00;
dst[ 9] = c * m21 + s * m01;
dst[10] = c * m22 + s * m02;
dst[11] = c * m23 + s * m03;
if (m !== dst) {
dst[ 4] = m[ 4];
dst[ 5] = m[ 5];
dst[ 6] = m[ 6];
dst[ 7] = m[ 7];
dst[12] = m[12];
dst[13] = m[13];
dst[14] = m[14];
dst[15] = m[15];
}
return dst;
}
/**
* Makes an z rotation matrix
* @param {number} angleInRadians amount to rotate
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function zRotation(angleInRadians, dst) {
dst = dst || new MatType(16);
var c = Math.cos(angleInRadians);
var s = Math.sin(angleInRadians);
dst[ 0] = c;
dst[ 1] = s;
dst[ 2] = 0;
dst[ 3] = 0;
dst[ 4] = -s;
dst[ 5] = c;
dst[ 6] = 0;
dst[ 7] = 0;
dst[ 8] = 0;
dst[ 9] = 0;
dst[10] = 1;
dst[11] = 0;
dst[12] = 0;
dst[13] = 0;
dst[14] = 0;
dst[15] = 1;
return dst;
}
/**
* Multiply by an z rotation matrix
* @param {Matrix4} m matrix to multiply
* @param {number} angleInRadians amount to rotate
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function zRotate(m, angleInRadians, dst) {
// This is the optimized version of
// return multiply(m, zRotation(angleInRadians), dst);
dst = dst || new MatType(16);
var m00 = m[0 * 4 + 0];
var m01 = m[0 * 4 + 1];
var m02 = m[0 * 4 + 2];
var m03 = m[0 * 4 + 3];
var m10 = m[1 * 4 + 0];
var m11 = m[1 * 4 + 1];
var m12 = m[1 * 4 + 2];
var m13 = m[1 * 4 + 3];
var c = Math.cos(angleInRadians);
var s = Math.sin(angleInRadians);
dst[ 0] = c * m00 + s * m10;
dst[ 1] = c * m01 + s * m11;
dst[ 2] = c * m02 + s * m12;
dst[ 3] = c * m03 + s * m13;
dst[ 4] = c * m10 - s * m00;
dst[ 5] = c * m11 - s * m01;
dst[ 6] = c * m12 - s * m02;
dst[ 7] = c * m13 - s * m03;
if (m !== dst) {
dst[ 8] = m[ 8];
dst[ 9] = m[ 9];
dst[10] = m[10];
dst[11] = m[11];
dst[12] = m[12];
dst[13] = m[13];
dst[14] = m[14];
dst[15] = m[15];
}
return dst;
}
/**
* Makes an rotation matrix around an arbitrary axis
* @param {Vector3} axis axis to rotate around
* @param {number} angleInRadians amount to rotate
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function axisRotation(axis, angleInRadians, dst) {
dst = dst || new MatType(16);
var x = axis[0];
var y = axis[1];
var z = axis[2];
var n = Math.sqrt(x * x + y * y + z * z);
x /= n;
y /= n;
z /= n;
var xx = x * x;
var yy = y * y;
var zz = z * z;
var c = Math.cos(angleInRadians);
var s = Math.sin(angleInRadians);
var oneMinusCosine = 1 - c;
dst[ 0] = xx + (1 - xx) * c;
dst[ 1] = x * y * oneMinusCosine + z * s;
dst[ 2] = x * z * oneMinusCosine - y * s;
dst[ 3] = 0;
dst[ 4] = x * y * oneMinusCosine - z * s;
dst[ 5] = yy + (1 - yy) * c;
dst[ 6] = y * z * oneMinusCosine + x * s;
dst[ 7] = 0;
dst[ 8] = x * z * oneMinusCosine + y * s;
dst[ 9] = y * z * oneMinusCosine - x * s;
dst[10] = zz + (1 - zz) * c;
dst[11] = 0;
dst[12] = 0;
dst[13] = 0;
dst[14] = 0;
dst[15] = 1;
return dst;
}
/**
* Multiply by an axis rotation matrix
* @param {Matrix4} m matrix to multiply
* @param {Vector3} axis axis to rotate around
* @param {number} angleInRadians amount to rotate
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function axisRotate(m, axis, angleInRadians, dst) {
// This is the optimized version of
// return multiply(m, axisRotation(axis, angleInRadians), dst);
dst = dst || new MatType(16);
var x = axis[0];
var y = axis[1];
var z = axis[2];
var n = Math.sqrt(x * x + y * y + z * z);
x /= n;
y /= n;
z /= n;
var xx = x * x;
var yy = y * y;
var zz = z * z;
var c = Math.cos(angleInRadians);
var s = Math.sin(angleInRadians);
var oneMinusCosine = 1 - c;
var r00 = xx + (1 - xx) * c;
var r01 = x * y * oneMinusCosine + z * s;
var r02 = x * z * oneMinusCosine - y * s;
var r10 = x * y * oneMinusCosine - z * s;
var r11 = yy + (1 - yy) * c;
var r12 = y * z * oneMinusCosine + x * s;
var r20 = x * z * oneMinusCosine + y * s;
var r21 = y * z * oneMinusCosine - x * s;
var r22 = zz + (1 - zz) * c;
var m00 = m[0];
var m01 = m[1];
var m02 = m[2];
var m03 = m[3];
var m10 = m[4];
var m11 = m[5];
var m12 = m[6];
var m13 = m[7];
var m20 = m[8];
var m21 = m[9];
var m22 = m[10];
var m23 = m[11];
dst[ 0] = r00 * m00 + r01 * m10 + r02 * m20;
dst[ 1] = r00 * m01 + r01 * m11 + r02 * m21;
dst[ 2] = r00 * m02 + r01 * m12 + r02 * m22;
dst[ 3] = r00 * m03 + r01 * m13 + r02 * m23;
dst[ 4] = r10 * m00 + r11 * m10 + r12 * m20;
dst[ 5] = r10 * m01 + r11 * m11 + r12 * m21;
dst[ 6] = r10 * m02 + r11 * m12 + r12 * m22;
dst[ 7] = r10 * m03 + r11 * m13 + r12 * m23;
dst[ 8] = r20 * m00 + r21 * m10 + r22 * m20;
dst[ 9] = r20 * m01 + r21 * m11 + r22 * m21;
dst[10] = r20 * m02 + r21 * m12 + r22 * m22;
dst[11] = r20 * m03 + r21 * m13 + r22 * m23;
if (m !== dst) {
dst[12] = m[12];
dst[13] = m[13];
dst[14] = m[14];
dst[15] = m[15];
}
return dst;
}
/**
* Makes a scale matrix
* @param {number} sx x scale.
* @param {number} sy y scale.
* @param {number} sz z scale.
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function scaling(sx, sy, sz, dst) {
dst = dst || new MatType(16);
dst[ 0] = sx;
dst[ 1] = 0;
dst[ 2] = 0;
dst[ 3] = 0;
dst[ 4] = 0;
dst[ 5] = sy;
dst[ 6] = 0;
dst[ 7] = 0;
dst[ 8] = 0;
dst[ 9] = 0;
dst[10] = sz;
dst[11] = 0;
dst[12] = 0;
dst[13] = 0;
dst[14] = 0;
dst[15] = 1;
return dst;
}
/**
* Multiply by a scaling matrix
* @param {Matrix4} m matrix to multiply
* @param {number} sx x scale.
* @param {number} sy y scale.
* @param {number} sz z scale.
* @param {Matrix4} [dst] optional matrix to store result
* @return {Matrix4} dst or a new matrix if none provided
* @memberOf module:webgl-3d-math
*/
function scale(m, sx, sy, sz, dst) {
// This is the optimized version of
// return multiply(m, scaling(sx, sy, sz), dst);
dst = dst || new MatType(16);
dst[ 0] = sx * m[0 * 4 + 0];