-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Matrix4.js
3233 lines (2970 loc) · 94.8 KB
/
Matrix4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Cartesian3 from "./Cartesian3.js";
import Cartesian4 from "./Cartesian4.js";
import Check from "./Check.js";
import defaultValue from "./defaultValue.js";
import defined from "./defined.js";
import DeveloperError from "./DeveloperError.js";
import CesiumMath from "./Math.js";
import Matrix3 from "./Matrix3.js";
import RuntimeError from "./RuntimeError.js";
/**
* A 4x4 matrix, indexable as a column-major order array.
* Constructor parameters are in row-major order for code readability.
* @alias Matrix4
* @constructor
* @implements {ArrayLike<number>}
*
* @param {number} [column0Row0=0.0] The value for column 0, row 0.
* @param {number} [column1Row0=0.0] The value for column 1, row 0.
* @param {number} [column2Row0=0.0] The value for column 2, row 0.
* @param {number} [column3Row0=0.0] The value for column 3, row 0.
* @param {number} [column0Row1=0.0] The value for column 0, row 1.
* @param {number} [column1Row1=0.0] The value for column 1, row 1.
* @param {number} [column2Row1=0.0] The value for column 2, row 1.
* @param {number} [column3Row1=0.0] The value for column 3, row 1.
* @param {number} [column0Row2=0.0] The value for column 0, row 2.
* @param {number} [column1Row2=0.0] The value for column 1, row 2.
* @param {number} [column2Row2=0.0] The value for column 2, row 2.
* @param {number} [column3Row2=0.0] The value for column 3, row 2.
* @param {number} [column0Row3=0.0] The value for column 0, row 3.
* @param {number} [column1Row3=0.0] The value for column 1, row 3.
* @param {number} [column2Row3=0.0] The value for column 2, row 3.
* @param {number} [column3Row3=0.0] The value for column 3, row 3.
*
* @see Matrix4.fromArray
* @see Matrix4.fromColumnMajorArray
* @see Matrix4.fromRowMajorArray
* @see Matrix4.fromRotationTranslation
* @see Matrix4.fromTranslationQuaternionRotationScale
* @see Matrix4.fromTranslationRotationScale
* @see Matrix4.fromTranslation
* @see Matrix4.fromScale
* @see Matrix4.fromUniformScale
* @see Matrix4.fromRotation
* @see Matrix4.fromCamera
* @see Matrix4.computePerspectiveFieldOfView
* @see Matrix4.computeOrthographicOffCenter
* @see Matrix4.computePerspectiveOffCenter
* @see Matrix4.computeInfinitePerspectiveOffCenter
* @see Matrix4.computeViewportTransformation
* @see Matrix4.computeView
* @see Matrix2
* @see Matrix3
* @see Packable
*/
function Matrix4(
column0Row0,
column1Row0,
column2Row0,
column3Row0,
column0Row1,
column1Row1,
column2Row1,
column3Row1,
column0Row2,
column1Row2,
column2Row2,
column3Row2,
column0Row3,
column1Row3,
column2Row3,
column3Row3,
) {
this[0] = defaultValue(column0Row0, 0.0);
this[1] = defaultValue(column0Row1, 0.0);
this[2] = defaultValue(column0Row2, 0.0);
this[3] = defaultValue(column0Row3, 0.0);
this[4] = defaultValue(column1Row0, 0.0);
this[5] = defaultValue(column1Row1, 0.0);
this[6] = defaultValue(column1Row2, 0.0);
this[7] = defaultValue(column1Row3, 0.0);
this[8] = defaultValue(column2Row0, 0.0);
this[9] = defaultValue(column2Row1, 0.0);
this[10] = defaultValue(column2Row2, 0.0);
this[11] = defaultValue(column2Row3, 0.0);
this[12] = defaultValue(column3Row0, 0.0);
this[13] = defaultValue(column3Row1, 0.0);
this[14] = defaultValue(column3Row2, 0.0);
this[15] = defaultValue(column3Row3, 0.0);
}
/**
* The number of elements used to pack the object into an array.
* @type {number}
*/
Matrix4.packedLength = 16;
/**
* Stores the provided instance into the provided array.
*
* @param {Matrix4} value The value to pack.
* @param {number[]} array The array to pack into.
* @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
*
* @returns {number[]} The array that was packed into
*/
Matrix4.pack = function (value, array, startingIndex) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("value", value);
Check.defined("array", array);
//>>includeEnd('debug');
startingIndex = defaultValue(startingIndex, 0);
array[startingIndex++] = value[0];
array[startingIndex++] = value[1];
array[startingIndex++] = value[2];
array[startingIndex++] = value[3];
array[startingIndex++] = value[4];
array[startingIndex++] = value[5];
array[startingIndex++] = value[6];
array[startingIndex++] = value[7];
array[startingIndex++] = value[8];
array[startingIndex++] = value[9];
array[startingIndex++] = value[10];
array[startingIndex++] = value[11];
array[startingIndex++] = value[12];
array[startingIndex++] = value[13];
array[startingIndex++] = value[14];
array[startingIndex] = value[15];
return array;
};
/**
* Retrieves an instance from a packed array.
*
* @param {number[]} array The packed array.
* @param {number} [startingIndex=0] The starting index of the element to be unpacked.
* @param {Matrix4} [result] The object into which to store the result.
* @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
*/
Matrix4.unpack = function (array, startingIndex, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined("array", array);
//>>includeEnd('debug');
startingIndex = defaultValue(startingIndex, 0);
if (!defined(result)) {
result = new Matrix4();
}
result[0] = array[startingIndex++];
result[1] = array[startingIndex++];
result[2] = array[startingIndex++];
result[3] = array[startingIndex++];
result[4] = array[startingIndex++];
result[5] = array[startingIndex++];
result[6] = array[startingIndex++];
result[7] = array[startingIndex++];
result[8] = array[startingIndex++];
result[9] = array[startingIndex++];
result[10] = array[startingIndex++];
result[11] = array[startingIndex++];
result[12] = array[startingIndex++];
result[13] = array[startingIndex++];
result[14] = array[startingIndex++];
result[15] = array[startingIndex];
return result;
};
/**
* Flattens an array of Matrix4s into an array of components. The components
* are stored in column-major order.
*
* @param {Matrix4[]} array The array of matrices to pack.
* @param {number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 16 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 16) elements.
* @returns {number[]} The packed array.
*/
Matrix4.packArray = function (array, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined("array", array);
//>>includeEnd('debug');
const length = array.length;
const resultLength = length * 16;
if (!defined(result)) {
result = new Array(resultLength);
} else if (!Array.isArray(result) && result.length !== resultLength) {
//>>includeStart('debug', pragmas.debug);
throw new DeveloperError(
"If result is a typed array, it must have exactly array.length * 16 elements",
);
//>>includeEnd('debug');
} else if (result.length !== resultLength) {
result.length = resultLength;
}
for (let i = 0; i < length; ++i) {
Matrix4.pack(array[i], result, i * 16);
}
return result;
};
/**
* Unpacks an array of column-major matrix components into an array of Matrix4s.
*
* @param {number[]} array The array of components to unpack.
* @param {Matrix4[]} [result] The array onto which to store the result.
* @returns {Matrix4[]} The unpacked array.
*/
Matrix4.unpackArray = function (array, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined("array", array);
Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 16);
if (array.length % 16 !== 0) {
throw new DeveloperError("array length must be a multiple of 16.");
}
//>>includeEnd('debug');
const length = array.length;
if (!defined(result)) {
result = new Array(length / 16);
} else {
result.length = length / 16;
}
for (let i = 0; i < length; i += 16) {
const index = i / 16;
result[index] = Matrix4.unpack(array, i, result[index]);
}
return result;
};
/**
* Duplicates a Matrix4 instance.
*
* @param {Matrix4} matrix The matrix to duplicate.
* @param {Matrix4} [result] The object onto which to store the result.
* @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. (Returns undefined if matrix is undefined)
*/
Matrix4.clone = function (matrix, result) {
if (!defined(matrix)) {
return undefined;
}
if (!defined(result)) {
return new Matrix4(
matrix[0],
matrix[4],
matrix[8],
matrix[12],
matrix[1],
matrix[5],
matrix[9],
matrix[13],
matrix[2],
matrix[6],
matrix[10],
matrix[14],
matrix[3],
matrix[7],
matrix[11],
matrix[15],
);
}
result[0] = matrix[0];
result[1] = matrix[1];
result[2] = matrix[2];
result[3] = matrix[3];
result[4] = matrix[4];
result[5] = matrix[5];
result[6] = matrix[6];
result[7] = matrix[7];
result[8] = matrix[8];
result[9] = matrix[9];
result[10] = matrix[10];
result[11] = matrix[11];
result[12] = matrix[12];
result[13] = matrix[13];
result[14] = matrix[14];
result[15] = matrix[15];
return result;
};
/**
* Creates a Matrix4 from 16 consecutive elements in an array.
* @function
*
* @param {number[]} array The array whose 16 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
* @param {number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
* @param {Matrix4} [result] The object onto which to store the result.
* @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
*
* @example
* // Create the Matrix4:
* // [1.0, 2.0, 3.0, 4.0]
* // [1.0, 2.0, 3.0, 4.0]
* // [1.0, 2.0, 3.0, 4.0]
* // [1.0, 2.0, 3.0, 4.0]
*
* const v = [1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0];
* const m = Cesium.Matrix4.fromArray(v);
*
* // Create same Matrix4 with using an offset into an array
* const v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0];
* const m2 = Cesium.Matrix4.fromArray(v2, 2);
*/
Matrix4.fromArray = Matrix4.unpack;
/**
* Computes a Matrix4 instance from a column-major order array.
*
* @param {number[]} values The column-major order array.
* @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
*/
Matrix4.fromColumnMajorArray = function (values, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined("values", values);
//>>includeEnd('debug');
return Matrix4.clone(values, result);
};
/**
* Computes a Matrix4 instance from a row-major order array.
* The resulting matrix will be in column-major order.
*
* @param {number[]} values The row-major order array.
* @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
*/
Matrix4.fromRowMajorArray = function (values, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined("values", values);
//>>includeEnd('debug');
if (!defined(result)) {
return new Matrix4(
values[0],
values[1],
values[2],
values[3],
values[4],
values[5],
values[6],
values[7],
values[8],
values[9],
values[10],
values[11],
values[12],
values[13],
values[14],
values[15],
);
}
result[0] = values[0];
result[1] = values[4];
result[2] = values[8];
result[3] = values[12];
result[4] = values[1];
result[5] = values[5];
result[6] = values[9];
result[7] = values[13];
result[8] = values[2];
result[9] = values[6];
result[10] = values[10];
result[11] = values[14];
result[12] = values[3];
result[13] = values[7];
result[14] = values[11];
result[15] = values[15];
return result;
};
/**
* Computes a Matrix4 instance from a Matrix3 representing the rotation
* and a Cartesian3 representing the translation.
*
* @param {Matrix3} rotation The upper left portion of the matrix representing the rotation.
* @param {Cartesian3} [translation=Cartesian3.ZERO] The upper right portion of the matrix representing the translation.
* @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
*/
Matrix4.fromRotationTranslation = function (rotation, translation, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("rotation", rotation);
//>>includeEnd('debug');
translation = defaultValue(translation, Cartesian3.ZERO);
if (!defined(result)) {
return new Matrix4(
rotation[0],
rotation[3],
rotation[6],
translation.x,
rotation[1],
rotation[4],
rotation[7],
translation.y,
rotation[2],
rotation[5],
rotation[8],
translation.z,
0.0,
0.0,
0.0,
1.0,
);
}
result[0] = rotation[0];
result[1] = rotation[1];
result[2] = rotation[2];
result[3] = 0.0;
result[4] = rotation[3];
result[5] = rotation[4];
result[6] = rotation[5];
result[7] = 0.0;
result[8] = rotation[6];
result[9] = rotation[7];
result[10] = rotation[8];
result[11] = 0.0;
result[12] = translation.x;
result[13] = translation.y;
result[14] = translation.z;
result[15] = 1.0;
return result;
};
/**
* Computes a Matrix4 instance from a translation, rotation, and scale (TRS)
* representation with the rotation represented as a quaternion.
*
* @param {Cartesian3} translation The translation transformation.
* @param {Quaternion} rotation The rotation transformation.
* @param {Cartesian3} scale The non-uniform scale transformation.
* @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
*
* @example
* const result = Cesium.Matrix4.fromTranslationQuaternionRotationScale(
* new Cesium.Cartesian3(1.0, 2.0, 3.0), // translation
* Cesium.Quaternion.IDENTITY, // rotation
* new Cesium.Cartesian3(7.0, 8.0, 9.0), // scale
* result);
*/
Matrix4.fromTranslationQuaternionRotationScale = function (
translation,
rotation,
scale,
result,
) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("translation", translation);
Check.typeOf.object("rotation", rotation);
Check.typeOf.object("scale", scale);
//>>includeEnd('debug');
if (!defined(result)) {
result = new Matrix4();
}
const scaleX = scale.x;
const scaleY = scale.y;
const scaleZ = scale.z;
const x2 = rotation.x * rotation.x;
const xy = rotation.x * rotation.y;
const xz = rotation.x * rotation.z;
const xw = rotation.x * rotation.w;
const y2 = rotation.y * rotation.y;
const yz = rotation.y * rotation.z;
const yw = rotation.y * rotation.w;
const z2 = rotation.z * rotation.z;
const zw = rotation.z * rotation.w;
const w2 = rotation.w * rotation.w;
const m00 = x2 - y2 - z2 + w2;
const m01 = 2.0 * (xy - zw);
const m02 = 2.0 * (xz + yw);
const m10 = 2.0 * (xy + zw);
const m11 = -x2 + y2 - z2 + w2;
const m12 = 2.0 * (yz - xw);
const m20 = 2.0 * (xz - yw);
const m21 = 2.0 * (yz + xw);
const m22 = -x2 - y2 + z2 + w2;
result[0] = m00 * scaleX;
result[1] = m10 * scaleX;
result[2] = m20 * scaleX;
result[3] = 0.0;
result[4] = m01 * scaleY;
result[5] = m11 * scaleY;
result[6] = m21 * scaleY;
result[7] = 0.0;
result[8] = m02 * scaleZ;
result[9] = m12 * scaleZ;
result[10] = m22 * scaleZ;
result[11] = 0.0;
result[12] = translation.x;
result[13] = translation.y;
result[14] = translation.z;
result[15] = 1.0;
return result;
};
/**
* Creates a Matrix4 instance from a {@link TranslationRotationScale} instance.
*
* @param {TranslationRotationScale} translationRotationScale The instance.
* @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
*/
Matrix4.fromTranslationRotationScale = function (
translationRotationScale,
result,
) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("translationRotationScale", translationRotationScale);
//>>includeEnd('debug');
return Matrix4.fromTranslationQuaternionRotationScale(
translationRotationScale.translation,
translationRotationScale.rotation,
translationRotationScale.scale,
result,
);
};
/**
* Creates a Matrix4 instance from a Cartesian3 representing the translation.
*
* @param {Cartesian3} translation The upper right portion of the matrix representing the translation.
* @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
*
* @see Matrix4.multiplyByTranslation
*/
Matrix4.fromTranslation = function (translation, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("translation", translation);
//>>includeEnd('debug');
return Matrix4.fromRotationTranslation(Matrix3.IDENTITY, translation, result);
};
/**
* Computes a Matrix4 instance representing a non-uniform scale.
*
* @param {Cartesian3} scale The x, y, and z scale factors.
* @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
*
* @example
* // Creates
* // [7.0, 0.0, 0.0, 0.0]
* // [0.0, 8.0, 0.0, 0.0]
* // [0.0, 0.0, 9.0, 0.0]
* // [0.0, 0.0, 0.0, 1.0]
* const m = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
*/
Matrix4.fromScale = function (scale, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("scale", scale);
//>>includeEnd('debug');
if (!defined(result)) {
return new Matrix4(
scale.x,
0.0,
0.0,
0.0,
0.0,
scale.y,
0.0,
0.0,
0.0,
0.0,
scale.z,
0.0,
0.0,
0.0,
0.0,
1.0,
);
}
result[0] = scale.x;
result[1] = 0.0;
result[2] = 0.0;
result[3] = 0.0;
result[4] = 0.0;
result[5] = scale.y;
result[6] = 0.0;
result[7] = 0.0;
result[8] = 0.0;
result[9] = 0.0;
result[10] = scale.z;
result[11] = 0.0;
result[12] = 0.0;
result[13] = 0.0;
result[14] = 0.0;
result[15] = 1.0;
return result;
};
/**
* Computes a Matrix4 instance representing a uniform scale.
*
* @param {number} scale The uniform scale factor.
* @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
*
* @example
* // Creates
* // [2.0, 0.0, 0.0, 0.0]
* // [0.0, 2.0, 0.0, 0.0]
* // [0.0, 0.0, 2.0, 0.0]
* // [0.0, 0.0, 0.0, 1.0]
* const m = Cesium.Matrix4.fromUniformScale(2.0);
*/
Matrix4.fromUniformScale = function (scale, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.number("scale", scale);
//>>includeEnd('debug');
if (!defined(result)) {
return new Matrix4(
scale,
0.0,
0.0,
0.0,
0.0,
scale,
0.0,
0.0,
0.0,
0.0,
scale,
0.0,
0.0,
0.0,
0.0,
1.0,
);
}
result[0] = scale;
result[1] = 0.0;
result[2] = 0.0;
result[3] = 0.0;
result[4] = 0.0;
result[5] = scale;
result[6] = 0.0;
result[7] = 0.0;
result[8] = 0.0;
result[9] = 0.0;
result[10] = scale;
result[11] = 0.0;
result[12] = 0.0;
result[13] = 0.0;
result[14] = 0.0;
result[15] = 1.0;
return result;
};
/**
* Creates a rotation matrix.
*
* @param {Matrix3} rotation The rotation matrix.
* @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
*/
Matrix4.fromRotation = function (rotation, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("rotation", rotation);
//>>includeEnd('debug');
if (!defined(result)) {
result = new Matrix4();
}
result[0] = rotation[0];
result[1] = rotation[1];
result[2] = rotation[2];
result[3] = 0.0;
result[4] = rotation[3];
result[5] = rotation[4];
result[6] = rotation[5];
result[7] = 0.0;
result[8] = rotation[6];
result[9] = rotation[7];
result[10] = rotation[8];
result[11] = 0.0;
result[12] = 0.0;
result[13] = 0.0;
result[14] = 0.0;
result[15] = 1.0;
return result;
};
const fromCameraF = new Cartesian3();
const fromCameraR = new Cartesian3();
const fromCameraU = new Cartesian3();
/**
* Computes a Matrix4 instance from a Camera.
*
* @param {Camera} camera The camera to use.
* @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
*/
Matrix4.fromCamera = function (camera, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("camera", camera);
//>>includeEnd('debug');
const position = camera.position;
const direction = camera.direction;
const up = camera.up;
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("camera.position", position);
Check.typeOf.object("camera.direction", direction);
Check.typeOf.object("camera.up", up);
//>>includeEnd('debug');
Cartesian3.normalize(direction, fromCameraF);
Cartesian3.normalize(
Cartesian3.cross(fromCameraF, up, fromCameraR),
fromCameraR,
);
Cartesian3.normalize(
Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU),
fromCameraU,
);
const sX = fromCameraR.x;
const sY = fromCameraR.y;
const sZ = fromCameraR.z;
const fX = fromCameraF.x;
const fY = fromCameraF.y;
const fZ = fromCameraF.z;
const uX = fromCameraU.x;
const uY = fromCameraU.y;
const uZ = fromCameraU.z;
const positionX = position.x;
const positionY = position.y;
const positionZ = position.z;
const t0 = sX * -positionX + sY * -positionY + sZ * -positionZ;
const t1 = uX * -positionX + uY * -positionY + uZ * -positionZ;
const t2 = fX * positionX + fY * positionY + fZ * positionZ;
// The code below this comment is an optimized
// version of the commented lines.
// Rather that create two matrices and then multiply,
// we just bake in the multiplcation as part of creation.
// const rotation = new Matrix4(
// sX, sY, sZ, 0.0,
// uX, uY, uZ, 0.0,
// -fX, -fY, -fZ, 0.0,
// 0.0, 0.0, 0.0, 1.0);
// const translation = new Matrix4(
// 1.0, 0.0, 0.0, -position.x,
// 0.0, 1.0, 0.0, -position.y,
// 0.0, 0.0, 1.0, -position.z,
// 0.0, 0.0, 0.0, 1.0);
// return rotation.multiply(translation);
if (!defined(result)) {
return new Matrix4(
sX,
sY,
sZ,
t0,
uX,
uY,
uZ,
t1,
-fX,
-fY,
-fZ,
t2,
0.0,
0.0,
0.0,
1.0,
);
}
result[0] = sX;
result[1] = uX;
result[2] = -fX;
result[3] = 0.0;
result[4] = sY;
result[5] = uY;
result[6] = -fY;
result[7] = 0.0;
result[8] = sZ;
result[9] = uZ;
result[10] = -fZ;
result[11] = 0.0;
result[12] = t0;
result[13] = t1;
result[14] = t2;
result[15] = 1.0;
return result;
};
/**
* Computes a Matrix4 instance representing a perspective transformation matrix.
*
* @param {number} fovY The field of view along the Y axis in radians.
* @param {number} aspectRatio The aspect ratio.
* @param {number} near The distance to the near plane in meters.
* @param {number} far The distance to the far plane in meters.
* @param {Matrix4} result The object in which the result will be stored.
* @returns {Matrix4} The modified result parameter.
*
* @exception {DeveloperError} fovY must be in (0, PI].
* @exception {DeveloperError} aspectRatio must be greater than zero.
* @exception {DeveloperError} near must be greater than zero.
* @exception {DeveloperError} far must be greater than zero.
*/
Matrix4.computePerspectiveFieldOfView = function (
fovY,
aspectRatio,
near,
far,
result,
) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.number.greaterThan("fovY", fovY, 0.0);
Check.typeOf.number.lessThan("fovY", fovY, Math.PI);
Check.typeOf.number.greaterThan("near", near, 0.0);
Check.typeOf.number.greaterThan("far", far, 0.0);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const bottom = Math.tan(fovY * 0.5);
const column1Row1 = 1.0 / bottom;
const column0Row0 = column1Row1 / aspectRatio;
const column2Row2 = (far + near) / (near - far);
const column3Row2 = (2.0 * far * near) / (near - far);
result[0] = column0Row0;
result[1] = 0.0;
result[2] = 0.0;
result[3] = 0.0;
result[4] = 0.0;
result[5] = column1Row1;
result[6] = 0.0;
result[7] = 0.0;
result[8] = 0.0;
result[9] = 0.0;
result[10] = column2Row2;
result[11] = -1.0;
result[12] = 0.0;
result[13] = 0.0;
result[14] = column3Row2;
result[15] = 0.0;
return result;
};
/**
* Computes a Matrix4 instance representing an orthographic transformation matrix.
*
* @param {number} left The number of meters to the left of the camera that will be in view.
* @param {number} right The number of meters to the right of the camera that will be in view.
* @param {number} bottom The number of meters below of the camera that will be in view.
* @param {number} top The number of meters above of the camera that will be in view.
* @param {number} near The distance to the near plane in meters.
* @param {number} far The distance to the far plane in meters.
* @param {Matrix4} result The object in which the result will be stored.
* @returns {Matrix4} The modified result parameter.
*/
Matrix4.computeOrthographicOffCenter = function (
left,
right,
bottom,
top,
near,
far,
result,
) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.number("left", left);
Check.typeOf.number("right", right);
Check.typeOf.number("bottom", bottom);
Check.typeOf.number("top", top);
Check.typeOf.number("near", near);
Check.typeOf.number("far", far);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
let a = 1.0 / (right - left);
let b = 1.0 / (top - bottom);
let c = 1.0 / (far - near);
const tx = -(right + left) * a;
const ty = -(top + bottom) * b;
const tz = -(far + near) * c;
a *= 2.0;
b *= 2.0;
c *= -2.0;
result[0] = a;
result[1] = 0.0;
result[2] = 0.0;
result[3] = 0.0;
result[4] = 0.0;
result[5] = b;
result[6] = 0.0;
result[7] = 0.0;
result[8] = 0.0;
result[9] = 0.0;
result[10] = c;
result[11] = 0.0;
result[12] = tx;
result[13] = ty;
result[14] = tz;
result[15] = 1.0;
return result;
};
/**
* Computes a Matrix4 instance representing an off center perspective transformation.
*
* @param {number} left The number of meters to the left of the camera that will be in view.
* @param {number} right The number of meters to the right of the camera that will be in view.
* @param {number} bottom The number of meters below the camera that will be in view.
* @param {number} top The number of meters above the camera that will be in view.
* @param {number} near The distance to the near plane in meters.
* @param {number} far The distance to the far plane in meters.
* @param {Matrix4} result The object in which the result will be stored.
* @returns {Matrix4} The modified result parameter.
*/
Matrix4.computePerspectiveOffCenter = function (
left,
right,
bottom,
top,
near,
far,
result,
) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.number("left", left);
Check.typeOf.number("right", right);
Check.typeOf.number("bottom", bottom);
Check.typeOf.number("top", top);
Check.typeOf.number("near", near);
Check.typeOf.number("far", far);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const column0Row0 = (2.0 * near) / (right - left);
const column1Row1 = (2.0 * near) / (top - bottom);
const column2Row0 = (right + left) / (right - left);
const column2Row1 = (top + bottom) / (top - bottom);
const column2Row2 = -(far + near) / (far - near);
const column2Row3 = -1.0;
const column3Row2 = (-2.0 * far * near) / (far - near);
result[0] = column0Row0;
result[1] = 0.0;
result[2] = 0.0;
result[3] = 0.0;
result[4] = 0.0;
result[5] = column1Row1;
result[6] = 0.0;
result[7] = 0.0;
result[8] = column2Row0;
result[9] = column2Row1;
result[10] = column2Row2;
result[11] = column2Row3;
result[12] = 0.0;
result[13] = 0.0;
result[14] = column3Row2;
result[15] = 0.0;
return result;
};
/**
* Computes a Matrix4 instance representing an infinite off center perspective transformation.
*
* @param {number} left The number of meters to the left of the camera that will be in view.
* @param {number} right The number of meters to the right of the camera that will be in view.
* @param {number} bottom The number of meters below of the camera that will be in view.
* @param {number} top The number of meters above of the camera that will be in view.
* @param {number} near The distance to the near plane in meters.