-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Vector3.js
1040 lines (930 loc) · 26 KB
/
Vector3.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
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
// Adapted from [gl-matrix](https://github.com/toji/gl-matrix) by toji
// and [vecmath](https://github.com/mattdesl/vecmath) by mattdesl
var Class = require('../utils/Class');
/**
* @classdesc
* A representation of a vector in 3D space.
*
* A three-component vector.
*
* @class Vector3
* @memberof Phaser.Math
* @constructor
* @since 3.0.0
*
* @param {number} [x] - The x component.
* @param {number} [y] - The y component.
* @param {number} [z] - The z component.
*/
var Vector3 = new Class({
initialize:
function Vector3 (x, y, z)
{
/**
* The x component of this Vector.
*
* @name Phaser.Math.Vector3#x
* @type {number}
* @default 0
* @since 3.0.0
*/
this.x = 0;
/**
* The y component of this Vector.
*
* @name Phaser.Math.Vector3#y
* @type {number}
* @default 0
* @since 3.0.0
*/
this.y = 0;
/**
* The z component of this Vector.
*
* @name Phaser.Math.Vector3#z
* @type {number}
* @default 0
* @since 3.0.0
*/
this.z = 0;
if (typeof x === 'object')
{
this.x = x.x || 0;
this.y = x.y || 0;
this.z = x.z || 0;
}
else
{
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
},
/**
* Set this Vector to point up.
*
* Sets the y component of the vector to 1, and the others to 0.
*
* @method Phaser.Math.Vector3#up
* @since 3.0.0
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
up: function ()
{
this.x = 0;
this.y = 1;
this.z = 0;
return this;
},
/**
* Sets the components of this Vector to be the `Math.min` result from the given vector.
*
* @method Phaser.Math.Vector3#min
* @since 3.50.0
*
* @param {Phaser.Math.Vector3} v - The Vector3 to check the minimum values against.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
min: function (v)
{
this.x = Math.min(this.x, v.x);
this.y = Math.min(this.y, v.y);
this.z = Math.min(this.z, v.z);
return this;
},
/**
* Sets the components of this Vector to be the `Math.max` result from the given vector.
*
* @method Phaser.Math.Vector3#max
* @since 3.50.0
*
* @param {Phaser.Math.Vector3} v - The Vector3 to check the maximum values against.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
max: function (v)
{
this.x = Math.max(this.x, v.x);
this.y = Math.max(this.y, v.y);
this.z = Math.max(this.z, v.z);
return this;
},
/**
* Make a clone of this Vector3.
*
* @method Phaser.Math.Vector3#clone
* @since 3.0.0
*
* @return {Phaser.Math.Vector3} A new Vector3 object containing this Vectors values.
*/
clone: function ()
{
return new Vector3(this.x, this.y, this.z);
},
/**
* Adds the two given Vector3s and sets the results into this Vector3.
*
* @method Phaser.Math.Vector3#addVectors
* @since 3.50.0
*
* @param {Phaser.Math.Vector3} a - The first Vector to add.
* @param {Phaser.Math.Vector3} b - The second Vector to add.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
addVectors: function (a, b)
{
this.x = a.x + b.x;
this.y = a.y + b.y;
this.z = a.z + b.z;
return this;
},
/**
* Calculate the cross (vector) product of two given Vectors.
*
* @method Phaser.Math.Vector3#crossVectors
* @since 3.0.0
*
* @param {Phaser.Math.Vector3} a - The first Vector to multiply.
* @param {Phaser.Math.Vector3} b - The second Vector to multiply.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
crossVectors: function (a, b)
{
var ax = a.x;
var ay = a.y;
var az = a.z;
var bx = b.x;
var by = b.y;
var bz = b.z;
this.x = ay * bz - az * by;
this.y = az * bx - ax * bz;
this.z = ax * by - ay * bx;
return this;
},
/**
* Check whether this Vector is equal to a given Vector.
*
* Performs a strict equality check against each Vector's components.
*
* @method Phaser.Math.Vector3#equals
* @since 3.0.0
*
* @param {Phaser.Math.Vector3} v - The Vector3 to compare against.
*
* @return {boolean} True if the two vectors strictly match, otherwise false.
*/
equals: function (v)
{
return ((this.x === v.x) && (this.y === v.y) && (this.z === v.z));
},
/**
* Copy the components of a given Vector into this Vector.
*
* @method Phaser.Math.Vector3#copy
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3)} src - The Vector to copy the components from.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
copy: function (src)
{
this.x = src.x;
this.y = src.y;
this.z = src.z || 0;
return this;
},
/**
* Set the `x`, `y`, and `z` components of this Vector to the given `x`, `y`, and `z` values.
*
* @method Phaser.Math.Vector3#set
* @since 3.0.0
*
* @param {(number|object)} x - The x value to set for this Vector, or an object containing x, y and z components.
* @param {number} [y] - The y value to set for this Vector.
* @param {number} [z] - The z value to set for this Vector.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
set: function (x, y, z)
{
if (typeof x === 'object')
{
this.x = x.x || 0;
this.y = x.y || 0;
this.z = x.z || 0;
}
else
{
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
return this;
},
/**
* Sets the components of this Vector3 from the position of the given Matrix4.
*
* @method Phaser.Math.Vector3#setFromMatrixPosition
* @since 3.50.0
*
* @param {Phaser.Math.Matrix4} mat4 - The Matrix4 to get the position from.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
setFromMatrixPosition: function (m)
{
return this.fromArray(m.val, 12);
},
/**
* Sets the components of this Vector3 from the Matrix4 column specified.
*
* @method Phaser.Math.Vector3#setFromMatrixColumn
* @since 3.50.0
*
* @param {Phaser.Math.Matrix4} mat4 - The Matrix4 to get the column from.
* @param {number} index - The column index.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
setFromMatrixColumn: function (mat4, index)
{
return this.fromArray(mat4.val, index * 4);
},
/**
* Sets the components of this Vector3 from the given array, based on the offset.
*
* Vector3.x = array[offset]
* Vector3.y = array[offset + 1]
* Vector3.z = array[offset + 2]
*
* @method Phaser.Math.Vector3#fromArray
* @since 3.50.0
*
* @param {number[]} array - The array of values to get this Vector from.
* @param {number} [offset=0] - The offset index into the array.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
fromArray: function (array, offset)
{
if (offset === undefined) { offset = 0; }
this.x = array[offset];
this.y = array[offset + 1];
this.z = array[offset + 2];
return this;
},
/**
* Add a given Vector to this Vector. Addition is component-wise.
*
* @method Phaser.Math.Vector3#add
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3)} v - The Vector to add to this Vector.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
add: function (v)
{
this.x += v.x;
this.y += v.y;
this.z += v.z || 0;
return this;
},
/**
* Add the given value to each component of this Vector.
*
* @method Phaser.Math.Vector3#addScalar
* @since 3.50.0
*
* @param {number} s - The amount to add to this Vector.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
addScalar: function (s)
{
this.x += s;
this.y += s;
this.z += s;
return this;
},
/**
* Add and scale a given Vector to this Vector. Addition is component-wise.
*
* @method Phaser.Math.Vector3#addScale
* @since 3.50.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3)} v - The Vector to add to this Vector.
* @param {number} scale - The amount to scale `v` by.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
addScale: function (v, scale)
{
this.x += v.x * scale;
this.y += v.y * scale;
this.z += v.z * scale || 0;
return this;
},
/**
* Subtract the given Vector from this Vector. Subtraction is component-wise.
*
* @method Phaser.Math.Vector3#subtract
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3)} v - The Vector to subtract from this Vector.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
subtract: function (v)
{
this.x -= v.x;
this.y -= v.y;
this.z -= v.z || 0;
return this;
},
/**
* Perform a component-wise multiplication between this Vector and the given Vector.
*
* Multiplies this Vector by the given Vector.
*
* @method Phaser.Math.Vector3#multiply
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3)} v - The Vector to multiply this Vector by.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
multiply: function (v)
{
this.x *= v.x;
this.y *= v.y;
this.z *= v.z || 1;
return this;
},
/**
* Scale this Vector by the given value.
*
* @method Phaser.Math.Vector3#scale
* @since 3.0.0
*
* @param {number} scale - The value to scale this Vector by.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
scale: function (scale)
{
if (isFinite(scale))
{
this.x *= scale;
this.y *= scale;
this.z *= scale;
}
else
{
this.x = 0;
this.y = 0;
this.z = 0;
}
return this;
},
/**
* Perform a component-wise division between this Vector and the given Vector.
*
* Divides this Vector by the given Vector.
*
* @method Phaser.Math.Vector3#divide
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3)} v - The Vector to divide this Vector by.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
divide: function (v)
{
this.x /= v.x;
this.y /= v.y;
this.z /= v.z || 1;
return this;
},
/**
* Negate the `x`, `y` and `z` components of this Vector.
*
* @method Phaser.Math.Vector3#negate
* @since 3.0.0
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
negate: function ()
{
this.x = -this.x;
this.y = -this.y;
this.z = -this.z;
return this;
},
/**
* Calculate the distance between this Vector and the given Vector.
*
* @method Phaser.Math.Vector3#distance
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3)} v - The Vector to calculate the distance to.
*
* @return {number} The distance from this Vector to the given Vector.
*/
distance: function (v)
{
var dx = v.x - this.x;
var dy = v.y - this.y;
var dz = v.z - this.z || 0;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
},
/**
* Calculate the distance between this Vector and the given Vector, squared.
*
* @method Phaser.Math.Vector3#distanceSq
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3)} v - The Vector to calculate the distance to.
*
* @return {number} The distance from this Vector to the given Vector, squared.
*/
distanceSq: function (v)
{
var dx = v.x - this.x;
var dy = v.y - this.y;
var dz = v.z - this.z || 0;
return dx * dx + dy * dy + dz * dz;
},
/**
* Calculate the length (or magnitude) of this Vector.
*
* @method Phaser.Math.Vector3#length
* @since 3.0.0
*
* @return {number} The length of this Vector.
*/
length: function ()
{
var x = this.x;
var y = this.y;
var z = this.z;
return Math.sqrt(x * x + y * y + z * z);
},
/**
* Calculate the length of this Vector squared.
*
* @method Phaser.Math.Vector3#lengthSq
* @since 3.0.0
*
* @return {number} The length of this Vector, squared.
*/
lengthSq: function ()
{
var x = this.x;
var y = this.y;
var z = this.z;
return x * x + y * y + z * z;
},
/**
* Normalize this Vector.
*
* Makes the vector a unit length vector (magnitude of 1) in the same direction.
*
* @method Phaser.Math.Vector3#normalize
* @since 3.0.0
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
normalize: function ()
{
var x = this.x;
var y = this.y;
var z = this.z;
var len = x * x + y * y + z * z;
if (len > 0)
{
len = 1 / Math.sqrt(len);
this.x = x * len;
this.y = y * len;
this.z = z * len;
}
return this;
},
/**
* Calculate the dot product of this Vector and the given Vector.
*
* @method Phaser.Math.Vector3#dot
* @since 3.0.0
*
* @param {Phaser.Math.Vector3} v - The Vector3 to dot product with this Vector3.
*
* @return {number} The dot product of this Vector and `v`.
*/
dot: function (v)
{
return this.x * v.x + this.y * v.y + this.z * v.z;
},
/**
* Calculate the cross (vector) product of this Vector (which will be modified) and the given Vector.
*
* @method Phaser.Math.Vector3#cross
* @since 3.0.0
*
* @param {Phaser.Math.Vector3} v - The Vector to cross product with.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
cross: function (v)
{
var ax = this.x;
var ay = this.y;
var az = this.z;
var bx = v.x;
var by = v.y;
var bz = v.z;
this.x = ay * bz - az * by;
this.y = az * bx - ax * bz;
this.z = ax * by - ay * bx;
return this;
},
/**
* Linearly interpolate between this Vector and the given Vector.
*
* Interpolates this Vector towards the given Vector.
*
* @method Phaser.Math.Vector3#lerp
* @since 3.0.0
*
* @param {Phaser.Math.Vector3} v - The Vector3 to interpolate towards.
* @param {number} [t=0] - The interpolation percentage, between 0 and 1.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
lerp: function (v, t)
{
if (t === undefined) { t = 0; }
var ax = this.x;
var ay = this.y;
var az = this.z;
this.x = ax + t * (v.x - ax);
this.y = ay + t * (v.y - ay);
this.z = az + t * (v.z - az);
return this;
},
/**
* Takes a Matrix3 and applies it to this Vector3.
*
* @method Phaser.Math.Vector3#applyMatrix3
* @since 3.50.0
*
* @param {Phaser.Math.Matrix3} mat3 - The Matrix3 to apply to this Vector3.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
applyMatrix3: function (mat3)
{
var x = this.x;
var y = this.y;
var z = this.z;
var m = mat3.val;
this.x = m[0] * x + m[3] * y + m[6] * z;
this.y = m[1] * x + m[4] * y + m[7] * z;
this.z = m[2] * x + m[5] * y + m[8] * z;
return this;
},
/**
* Takes a Matrix4 and applies it to this Vector3.
*
* @method Phaser.Math.Vector3#applyMatrix4
* @since 3.50.0
*
* @param {Phaser.Math.Matrix4} mat4 - The Matrix4 to apply to this Vector3.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
applyMatrix4: function (mat4)
{
var x = this.x;
var y = this.y;
var z = this.z;
var m = mat4.val;
var w = 1 / (m[3] * x + m[7] * y + m[11] * z + m[15]);
this.x = (m[0] * x + m[4] * y + m[8] * z + m[12]) * w;
this.y = (m[1] * x + m[5] * y + m[9] * z + m[13]) * w;
this.z = (m[2] * x + m[6] * y + m[10] * z + m[14]) * w;
return this;
},
/**
* Transform this Vector with the given Matrix.
*
* @method Phaser.Math.Vector3#transformMat3
* @since 3.0.0
*
* @param {Phaser.Math.Matrix3} mat - The Matrix3 to transform this Vector3 with.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
transformMat3: function (mat)
{
var x = this.x;
var y = this.y;
var z = this.z;
var m = mat.val;
this.x = x * m[0] + y * m[3] + z * m[6];
this.y = x * m[1] + y * m[4] + z * m[7];
this.z = x * m[2] + y * m[5] + z * m[8];
return this;
},
/**
* Transform this Vector with the given Matrix4.
*
* @method Phaser.Math.Vector3#transformMat4
* @since 3.0.0
*
* @param {Phaser.Math.Matrix4} mat - The Matrix4 to transform this Vector3 with.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
transformMat4: function (mat)
{
var x = this.x;
var y = this.y;
var z = this.z;
var m = mat.val;
this.x = m[0] * x + m[4] * y + m[8] * z + m[12];
this.y = m[1] * x + m[5] * y + m[9] * z + m[13];
this.z = m[2] * x + m[6] * y + m[10] * z + m[14];
return this;
},
/**
* Transforms the coordinates of this Vector3 with the given Matrix4.
*
* @method Phaser.Math.Vector3#transformCoordinates
* @since 3.0.0
*
* @param {Phaser.Math.Matrix4} mat - The Matrix4 to transform this Vector3 with.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
transformCoordinates: function (mat)
{
var x = this.x;
var y = this.y;
var z = this.z;
var m = mat.val;
var tx = (x * m[0]) + (y * m[4]) + (z * m[8]) + m[12];
var ty = (x * m[1]) + (y * m[5]) + (z * m[9]) + m[13];
var tz = (x * m[2]) + (y * m[6]) + (z * m[10]) + m[14];
var tw = (x * m[3]) + (y * m[7]) + (z * m[11]) + m[15];
this.x = tx / tw;
this.y = ty / tw;
this.z = tz / tw;
return this;
},
/**
* Transform this Vector with the given Quaternion.
*
* @method Phaser.Math.Vector3#transformQuat
* @since 3.0.0
*
* @param {Phaser.Math.Quaternion} q - The Quaternion to transform this Vector with.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
transformQuat: function (q)
{
// benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
var x = this.x;
var y = this.y;
var z = this.z;
var qx = q.x;
var qy = q.y;
var qz = q.z;
var qw = q.w;
// calculate quat * vec
var ix = qw * x + qy * z - qz * y;
var iy = qw * y + qz * x - qx * z;
var iz = qw * z + qx * y - qy * x;
var iw = -qx * x - qy * y - qz * z;
// calculate result * inverse quat
this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
return this;
},
/**
* Multiplies this Vector3 by the specified matrix, applying a W divide. This is useful for projection,
* e.g. unprojecting a 2D point into 3D space.
*
* @method Phaser.Math.Vector3#project
* @since 3.0.0
*
* @param {Phaser.Math.Matrix4} mat - The Matrix4 to multiply this Vector3 with.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
project: function (mat)
{
var x = this.x;
var y = this.y;
var z = this.z;
var m = mat.val;
var a00 = m[0];
var a01 = m[1];
var a02 = m[2];
var a03 = m[3];
var a10 = m[4];
var a11 = m[5];
var a12 = m[6];
var a13 = m[7];
var a20 = m[8];
var a21 = m[9];
var a22 = m[10];
var a23 = m[11];
var a30 = m[12];
var a31 = m[13];
var a32 = m[14];
var a33 = m[15];
var lw = 1 / (x * a03 + y * a13 + z * a23 + a33);
this.x = (x * a00 + y * a10 + z * a20 + a30) * lw;
this.y = (x * a01 + y * a11 + z * a21 + a31) * lw;
this.z = (x * a02 + y * a12 + z * a22 + a32) * lw;
return this;
},
/**
* Multiplies this Vector3 by the given view and projection matrices.
*
* @method Phaser.Math.Vector3#projectViewMatrix
* @since 3.50.0
*
* @param {Phaser.Math.Matrix4} viewMatrix - A View Matrix.
* @param {Phaser.Math.Matrix4} projectionMatrix - A Projection Matrix.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
projectViewMatrix: function (viewMatrix, projectionMatrix)
{
return this.applyMatrix4(viewMatrix).applyMatrix4(projectionMatrix);
},
/**
* Multiplies this Vector3 by the given inversed projection matrix and world matrix.
*
* @method Phaser.Math.Vector3#unprojectViewMatrix
* @since 3.50.0
*
* @param {Phaser.Math.Matrix4} projectionMatrix - An inversed Projection Matrix.
* @param {Phaser.Math.Matrix4} worldMatrix - A World View Matrix.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
unprojectViewMatrix: function (projectionMatrix, worldMatrix)
{
return this.applyMatrix4(projectionMatrix).applyMatrix4(worldMatrix);
},
/**
* Unproject this point from 2D space to 3D space.
* The point should have its x and y properties set to
* 2D screen space, and the z either at 0 (near plane)
* or 1 (far plane). The provided matrix is assumed to already
* be combined, i.e. projection * view * model.
*
* After this operation, this vector's (x, y, z) components will
* represent the unprojected 3D coordinate.
*
* @method Phaser.Math.Vector3#unproject
* @since 3.0.0
*
* @param {Phaser.Math.Vector4} viewport - Screen x, y, width and height in pixels.
* @param {Phaser.Math.Matrix4} invProjectionView - Combined projection and view matrix.
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
unproject: function (viewport, invProjectionView)
{
var viewX = viewport.x;
var viewY = viewport.y;
var viewWidth = viewport.z;
var viewHeight = viewport.w;
var x = this.x - viewX;
var y = (viewHeight - this.y - 1) - viewY;
var z = this.z;
this.x = (2 * x) / viewWidth - 1;
this.y = (2 * y) / viewHeight - 1;
this.z = 2 * z - 1;
return this.project(invProjectionView);
},
/**
* Make this Vector the zero vector (0, 0, 0).
*
* @method Phaser.Math.Vector3#reset
* @since 3.0.0
*
* @return {Phaser.Math.Vector3} This Vector3.
*/
reset: function ()
{
this.x = 0;
this.y = 0;
this.z = 0;
return this;
}
});
/**
* A static zero Vector3 for use by reference.
*
* This constant is meant for comparison operations and should not be modified directly.
*
* @constant
* @name Phaser.Math.Vector3.ZERO
* @type {Phaser.Math.Vector3}
* @since 3.16.0
*/
Vector3.ZERO = new Vector3();
/**
* A static right Vector3 for use by reference.
*
* This constant is meant for comparison operations and should not be modified directly.
*
* @constant
* @name Phaser.Math.Vector3.RIGHT
* @type {Phaser.Math.Vector3}
* @since 3.16.0
*/
Vector3.RIGHT = new Vector3(1, 0, 0);
/**
* A static left Vector3 for use by reference.
*
* This constant is meant for comparison operations and should not be modified directly.
*
* @constant
* @name Phaser.Math.Vector3.LEFT
* @type {Phaser.Math.Vector3}
* @since 3.16.0
*/
Vector3.LEFT = new Vector3(-1, 0, 0);
/**
* A static up Vector3 for use by reference.
*
* This constant is meant for comparison operations and should not be modified directly.
*
* @constant
* @name Phaser.Math.Vector3.UP
* @type {Phaser.Math.Vector3}
* @since 3.16.0
*/
Vector3.UP = new Vector3(0, -1, 0);
/**
* A static down Vector3 for use by reference.
*
* This constant is meant for comparison operations and should not be modified directly.
*
* @constant
* @name Phaser.Math.Vector3.DOWN
* @type {Phaser.Math.Vector3}
* @since 3.16.0