forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec3.js
More file actions
753 lines (696 loc) · 21.4 KB
/
vec3.js
File metadata and controls
753 lines (696 loc) · 21.4 KB
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
/**
* @class
* @name Vec3
* @classdesc A 3-dimensional vector.
* @description Creates a new Vec3 object.
* @param {number|number[]} [x] - The x value. If x is an array of length 3, the array will be used to populate all components.
* @param {number} [y] - The y value.
* @param {number} [z] - The z value.
* @example
* var v = new pc.Vec3(1, 2, 3);
*/
/**
* @name Vec3#x
* @type {number}
* @description The first component of the vector.
* @example
* var vec = new pc.Vec3(10, 20, 30);
*
* // Get x
* var x = vec.x;
*
* // Set x
* vec.x = 0;
*/
/**
* @name Vec3#y
* @type {number}
* @description The second component of the vector.
* @example
* var vec = new pc.Vec3(10, 20, 30);
*
* // Get y
* var y = vec.y;
*
* // Set y
* vec.y = 0;
*/
/**
* @name Vec3#z
* @type {number}
* @description The third component of the vector.
* @example
* var vec = new pc.Vec3(10, 20, 30);
*
* // Get z
* var z = vec.z;
*
* // Set z
* vec.z = 0;
*/
class Vec3 {
constructor(x = 0, y = 0, z = 0) {
if (x.length === 3) {
this.x = x[0];
this.y = x[1];
this.z = x[2];
} else {
this.x = x;
this.y = y;
this.z = z;
}
}
/**
* @function
* @name Vec3#add
* @description Adds a 3-dimensional vector to another in place.
* @param {Vec3} rhs - The vector to add to the specified vector.
* @returns {Vec3} Self for chaining.
* @example
* var a = new pc.Vec3(10, 10, 10);
* var b = new pc.Vec3(20, 20, 20);
*
* a.add(b);
*
* // Outputs [30, 30, 30]
* console.log("The result of the addition is: " + a.toString());
*/
add(rhs) {
this.x += rhs.x;
this.y += rhs.y;
this.z += rhs.z;
return this;
}
/**
* @function
* @name Vec3#add2
* @description Adds two 3-dimensional vectors together and returns the result.
* @param {Vec3} lhs - The first vector operand for the addition.
* @param {Vec3} rhs - The second vector operand for the addition.
* @returns {Vec3} Self for chaining.
* @example
* var a = new pc.Vec3(10, 10, 10);
* var b = new pc.Vec3(20, 20, 20);
* var r = new pc.Vec3();
*
* r.add2(a, b);
* // Outputs [30, 30, 30]
*
* console.log("The result of the addition is: " + r.toString());
*/
add2(lhs, rhs) {
this.x = lhs.x + rhs.x;
this.y = lhs.y + rhs.y;
this.z = lhs.z + rhs.z;
return this;
}
/**
* @function
* @name Vec3#addScalar
* @description Adds a number to each element of a vector.
* @param {number} scalar - The number to add.
* @returns {Vec3} Self for chaining.
* @example
* var vec = new pc.Vec3(3, 4, 5);
*
* vec.addScalar(2);
*
* // Outputs [5, 6, 7]
* console.log("The result of the addition is: " + vec.toString());
*/
addScalar(scalar) {
this.x += scalar;
this.y += scalar;
this.z += scalar;
return this;
}
/**
* @function
* @name Vec3#clone
* @description Returns an identical copy of the specified 3-dimensional vector.
* @returns {Vec3} A 3-dimensional vector containing the result of the cloning.
* @example
* var v = new pc.Vec3(10, 20, 30);
* var vclone = v.clone();
* console.log("The result of the cloning is: " + vclone.toString());
*/
clone() {
return new Vec3(this.x, this.y, this.z);
}
/**
* @function
* @name Vec3#copy
* @description Copied the contents of a source 3-dimensional vector to a destination 3-dimensional vector.
* @param {Vec3} rhs - A vector to copy to the specified vector.
* @returns {Vec3} Self for chaining.
* @example
* var src = new pc.Vec3(10, 20, 30);
* var dst = new pc.Vec3();
*
* dst.copy(src);
*
* console.log("The two vectors are " + (dst.equals(src) ? "equal" : "different"));
*/
copy(rhs) {
this.x = rhs.x;
this.y = rhs.y;
this.z = rhs.z;
return this;
}
/**
* @function
* @name Vec3#cross
* @description Returns the result of a cross product operation performed on the two specified 3-dimensional vectors.
* @param {Vec3} lhs - The first 3-dimensional vector operand of the cross product.
* @param {Vec3} rhs - The second 3-dimensional vector operand of the cross product.
* @returns {Vec3} Self for chaining.
* @example
* var back = new pc.Vec3().cross(pc.Vec3.RIGHT, pc.Vec3.UP);
*
* // Prints the Z axis (i.e. [0, 0, 1])
* console.log("The result of the cross product is: " + back.toString());
*/
cross(lhs, rhs) {
// Create temporary variables in case lhs or rhs are 'this'
const lx = lhs.x;
const ly = lhs.y;
const lz = lhs.z;
const rx = rhs.x;
const ry = rhs.y;
const rz = rhs.z;
this.x = ly * rz - ry * lz;
this.y = lz * rx - rz * lx;
this.z = lx * ry - rx * ly;
return this;
}
/**
* @function
* @name Vec3#distance
* @description Returns the distance between the two specified 3-dimensional vectors.
* @param {Vec3} rhs - The second 3-dimensional vector to test.
* @returns {number} The distance between the two vectors.
* @example
* var v1 = new pc.Vec3(5, 10, 20);
* var v2 = new pc.Vec3(10, 20, 40);
* var d = v1.distance(v2);
* console.log("The between v1 and v2 is: " + d);
*/
distance(rhs) {
const x = this.x - rhs.x;
const y = this.y - rhs.y;
const z = this.z - rhs.z;
return Math.sqrt(x * x + y * y + z * z);
}
/**
* @function
* @name Vec3#div
* @description Divides a 3-dimensional vector by another in place.
* @param {Vec3} rhs - The vector to divide the specified vector by.
* @returns {Vec3} Self for chaining.
* @example
* var a = new pc.Vec3(4, 9, 16);
* var b = new pc.Vec3(2, 3, 4);
*
* a.div(b);
*
* // Outputs [2, 3, 4]
* console.log("The result of the division is: " + a.toString());
*/
div(rhs) {
this.x /= rhs.x;
this.y /= rhs.y;
this.z /= rhs.z;
return this;
}
/**
* @function
* @name Vec3#div2
* @description Divides one 3-dimensional vector by another and writes the result to
* the specified vector.
* @param {Vec3} lhs - The dividend vector (the vector being divided).
* @param {Vec3} rhs - The divisor vector (the vector dividing the dividend).
* @returns {Vec3} Self for chaining.
* @example
* var a = new pc.Vec3(4, 9, 16);
* var b = new pc.Vec3(2, 3, 4);
* var r = new pc.Vec3();
*
* r.div2(a, b);
* // Outputs [2, 3, 4]
*
* console.log("The result of the division is: " + r.toString());
*/
div2(lhs, rhs) {
this.x = lhs.x / rhs.x;
this.y = lhs.y / rhs.y;
this.z = lhs.z / rhs.z;
return this;
}
/**
* @function
* @name Vec3#divScalar
* @description Divides each element of a vector by a number.
* @param {number} scalar - The number to divide by.
* @returns {Vec3} Self for chaining.
* @example
* var vec = new pc.Vec3(3, 6, 9);
*
* vec.divScalar(3);
*
* // Outputs [1, 2, 3]
* console.log("The result of the division is: " + vec.toString());
*/
divScalar(scalar) {
this.x /= scalar;
this.y /= scalar;
this.z /= scalar;
return this;
}
/**
* @function
* @name Vec3#dot
* @description Returns the result of a dot product operation performed on the two specified 3-dimensional vectors.
* @param {Vec3} rhs - The second 3-dimensional vector operand of the dot product.
* @returns {number} The result of the dot product operation.
* @example
* var v1 = new pc.Vec3(5, 10, 20);
* var v2 = new pc.Vec3(10, 20, 40);
* var v1dotv2 = v1.dot(v2);
* console.log("The result of the dot product is: " + v1dotv2);
*/
dot(rhs) {
return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;
}
/**
* @function
* @name Vec3#equals
* @description Reports whether two vectors are equal.
* @param {Vec3} rhs - The vector to compare to the specified vector.
* @returns {boolean} True if the vectors are equal and false otherwise.
* @example
* var a = new pc.Vec3(1, 2, 3);
* var b = new pc.Vec3(4, 5, 6);
* console.log("The two vectors are " + (a.equals(b) ? "equal" : "different"));
*/
equals(rhs) {
return this.x === rhs.x && this.y === rhs.y && this.z === rhs.z;
}
/**
* @function
* @name Vec3#length
* @description Returns the magnitude of the specified 3-dimensional vector.
* @returns {number} The magnitude of the specified 3-dimensional vector.
* @example
* var vec = new pc.Vec3(3, 4, 0);
* var len = vec.length();
* // Outputs 5
* console.log("The length of the vector is: " + len);
*/
length() {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
/**
* @function
* @name Vec3#lengthSq
* @description Returns the magnitude squared of the specified 3-dimensional vector.
* @returns {number} The magnitude of the specified 3-dimensional vector.
* @example
* var vec = new pc.Vec3(3, 4, 0);
* var len = vec.lengthSq();
* // Outputs 25
* console.log("The length squared of the vector is: " + len);
*/
lengthSq() {
return this.x * this.x + this.y * this.y + this.z * this.z;
}
/**
* @function
* @name Vec3#lerp
* @description Returns the result of a linear interpolation between two specified 3-dimensional vectors.
* @param {Vec3} lhs - The 3-dimensional to interpolate from.
* @param {Vec3} rhs - The 3-dimensional to interpolate to.
* @param {number} alpha - The value controlling the point of interpolation. Between 0 and 1, the linear interpolant
* will occur on a straight line between lhs and rhs. Outside of this range, the linear interpolant will occur on
* a ray extrapolated from this line.
* @returns {Vec3} Self for chaining.
* @example
* var a = new pc.Vec3(0, 0, 0);
* var b = new pc.Vec3(10, 10, 10);
* var r = new pc.Vec3();
*
* r.lerp(a, b, 0); // r is equal to a
* r.lerp(a, b, 0.5); // r is 5, 5, 5
* r.lerp(a, b, 1); // r is equal to b
*/
lerp(lhs, rhs, alpha) {
this.x = lhs.x + alpha * (rhs.x - lhs.x);
this.y = lhs.y + alpha * (rhs.y - lhs.y);
this.z = lhs.z + alpha * (rhs.z - lhs.z);
return this;
}
/**
* @function
* @name Vec3#mul
* @description Multiplies a 3-dimensional vector to another in place.
* @param {Vec3} rhs - The 3-dimensional vector used as the second multiplicand of the operation.
* @returns {Vec3} Self for chaining.
* @example
* var a = new pc.Vec3(2, 3, 4);
* var b = new pc.Vec3(4, 5, 6);
*
* a.mul(b);
*
* // Outputs 8, 15, 24
* console.log("The result of the multiplication is: " + a.toString());
*/
mul(rhs) {
this.x *= rhs.x;
this.y *= rhs.y;
this.z *= rhs.z;
return this;
}
/**
* @function
* @name Vec3#mul2
* @description Returns the result of multiplying the specified 3-dimensional vectors together.
* @param {Vec3} lhs - The 3-dimensional vector used as the first multiplicand of the operation.
* @param {Vec3} rhs - The 3-dimensional vector used as the second multiplicand of the operation.
* @returns {Vec3} Self for chaining.
* @example
* var a = new pc.Vec3(2, 3, 4);
* var b = new pc.Vec3(4, 5, 6);
* var r = new pc.Vec3();
*
* r.mul2(a, b);
*
* // Outputs 8, 15, 24
* console.log("The result of the multiplication is: " + r.toString());
*/
mul2(lhs, rhs) {
this.x = lhs.x * rhs.x;
this.y = lhs.y * rhs.y;
this.z = lhs.z * rhs.z;
return this;
}
/**
* @function
* @name Vec3#mulScalar
* @description Multiplies each element of a vector by a number.
* @param {number} scalar - The number to multiply by.
* @returns {Vec3} Self for chaining.
* @example
* var vec = new pc.Vec3(3, 6, 9);
*
* vec.mulScalar(3);
*
* // Outputs [9, 18, 27]
* console.log("The result of the multiplication is: " + vec.toString());
*/
mulScalar(scalar) {
this.x *= scalar;
this.y *= scalar;
this.z *= scalar;
return this;
}
/**
* @function
* @name Vec3#normalize
* @description Returns this 3-dimensional vector converted to a unit vector in place.
* If the vector has a length of zero, the vector's elements will be set to zero.
* @returns {Vec3} Self for chaining.
* @example
* var v = new pc.Vec3(25, 0, 0);
*
* v.normalize();
*
* // Outputs 1, 0, 0
* console.log("The result of the vector normalization is: " + v.toString());
*/
normalize() {
const lengthSq = this.x * this.x + this.y * this.y + this.z * this.z;
if (lengthSq > 0) {
const invLength = 1 / Math.sqrt(lengthSq);
this.x *= invLength;
this.y *= invLength;
this.z *= invLength;
}
return this;
}
/**
* @function
* @name Vec3#floor
* @description Each element is set to the largest integer less than or equal to its value.
* @returns {Vec3} Self for chaining.
*/
floor() {
this.x = Math.floor(this.x);
this.y = Math.floor(this.y);
this.z = Math.floor(this.z);
return this;
}
/**
* @function
* @name Vec3#ceil
* @description Each element is rounded up to the next largest integer.
* @returns {Vec3} Self for chaining.
*/
ceil() {
this.x = Math.ceil(this.x);
this.y = Math.ceil(this.y);
this.z = Math.ceil(this.z);
return this;
}
/**
* @function
* @name Vec3#round
* @description Each element is rounded up or down to the nearest integer.
* @returns {Vec3} Self for chaining.
*/
round() {
this.x = Math.round(this.x);
this.y = Math.round(this.y);
this.z = Math.round(this.z);
return this;
}
/**
* @function
* @name Vec3#min
* @description Each element is assigned a value from rhs parameter if it is smaller.
* @param {Vec3} rhs - The 3-dimensional vector used as the source of elements to compare to.
* @returns {Vec3} Self for chaining.
*/
min(rhs) {
if (rhs.x < this.x) this.x = rhs.x;
if (rhs.y < this.y) this.y = rhs.y;
if (rhs.z < this.z) this.z = rhs.z;
return this;
}
/**
* @function
* @name Vec3#max
* @description Each element is assigned a value from rhs parameter if it is larger.
* @param {Vec3} rhs - The 3-dimensional vector used as the source of elements to compare to.
* @returns {Vec3} Self for chaining.
*/
max(rhs) {
if (rhs.x > this.x) this.x = rhs.x;
if (rhs.y > this.y) this.y = rhs.y;
if (rhs.z > this.z) this.z = rhs.z;
return this;
}
/**
* @function
* @name Vec3#project
* @description Projects this 3-dimensional vector onto the specified vector.
* @param {Vec3} rhs - The vector onto which the original vector will be projected on.
* @returns {Vec3} Self for chaining.
* @example
* var v = new pc.Vec3(5, 5, 5);
* var normal = new pc.Vec3(1, 0, 0);
*
* v.project(normal);
*
* // Outputs 5, 0, 0
* console.log("The result of the vector projection is: " + v.toString());
*/
project(rhs) {
const a_dot_b = this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;
const b_dot_b = rhs.x * rhs.x + rhs.y * rhs.y + rhs.z * rhs.z;
const s = a_dot_b / b_dot_b;
this.x = rhs.x * s;
this.y = rhs.y * s;
this.z = rhs.z * s;
return this;
}
/**
* @function
* @name Vec3#set
* @description Sets the specified 3-dimensional vector to the supplied numerical values.
* @param {number} x - The value to set on the first component of the vector.
* @param {number} y - The value to set on the second component of the vector.
* @param {number} z - The value to set on the third component of the vector.
* @returns {Vec3} Self for chaining.
* @example
* var v = new pc.Vec3();
* v.set(5, 10, 20);
*
* // Outputs 5, 10, 20
* console.log("The result of the vector set is: " + v.toString());
*/
set(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
/**
* @function
* @name Vec3#sub
* @description Subtracts a 3-dimensional vector from another in place.
* @param {Vec3} rhs - The vector to add to the specified vector.
* @returns {Vec3} Self for chaining.
* @example
* var a = new pc.Vec3(10, 10, 10);
* var b = new pc.Vec3(20, 20, 20);
*
* a.sub(b);
*
* // Outputs [-10, -10, -10]
* console.log("The result of the subtraction is: " + a.toString());
*/
sub(rhs) {
this.x -= rhs.x;
this.y -= rhs.y;
this.z -= rhs.z;
return this;
}
/**
* @function
* @name Vec3#sub2
* @description Subtracts two 3-dimensional vectors from one another and returns the result.
* @param {Vec3} lhs - The first vector operand for the addition.
* @param {Vec3} rhs - The second vector operand for the addition.
* @returns {Vec3} Self for chaining.
* @example
* var a = new pc.Vec3(10, 10, 10);
* var b = new pc.Vec3(20, 20, 20);
* var r = new pc.Vec3();
*
* r.sub2(a, b);
*
* // Outputs [-10, -10, -10]
* console.log("The result of the subtraction is: " + r.toString());
*/
sub2(lhs, rhs) {
this.x = lhs.x - rhs.x;
this.y = lhs.y - rhs.y;
this.z = lhs.z - rhs.z;
return this;
}
/**
* @function
* @name Vec3#subScalar
* @description Subtracts a number from each element of a vector.
* @param {number} scalar - The number to subtract.
* @returns {Vec3} Self for chaining.
* @example
* var vec = new pc.Vec3(3, 4, 5);
*
* vec.subScalar(2);
*
* // Outputs [1, 2, 3]
* console.log("The result of the subtraction is: " + vec.toString());
*/
subScalar(scalar) {
this.x -= scalar;
this.y -= scalar;
this.z -= scalar;
return this;
}
/**
* @function
* @name Vec3#toString
* @description Converts the vector to string form.
* @returns {string} The vector in string form.
* @example
* var v = new pc.Vec3(20, 10, 5);
* // Outputs [20, 10, 5]
* console.log(v.toString());
*/
toString() {
return '[' + this.x + ', ' + this.y + ', ' + this.z + ']';
}
/**
* @field
* @static
* @readonly
* @name Vec3.ZERO
* @type {Vec3}
* @description A constant vector set to [0, 0, 0].
*/
static ZERO = Object.freeze(new Vec3(0, 0, 0));
/**
* @field
* @static
* @readonly
* @name Vec3.ONE
* @type {Vec3}
* @description A constant vector set to [1, 1, 1].
*/
static ONE = Object.freeze(new Vec3(1, 1, 1));
/**
* @field
* @static
* @readonly
* @name Vec3.UP
* @type {Vec3}
* @description A constant vector set to [0, 1, 0].
*/
static UP = Object.freeze(new Vec3(0, 1, 0));
/**
* @field
* @static
* @readonly
* @name Vec3.DOWN
* @type {Vec3}
* @description A constant vector set to [0, -1, 0].
*/
static DOWN = Object.freeze(new Vec3(0, -1, 0));
/**
* @field
* @static
* @readonly
* @name Vec3.RIGHT
* @type {Vec3}
* @description A constant vector set to [1, 0, 0].
*/
static RIGHT = Object.freeze(new Vec3(1, 0, 0));
/**
* @field
* @static
* @readonly
* @name Vec3.LEFT
* @type {Vec3}
* @description A constant vector set to [-1, 0, 0].
*/
static LEFT = Object.freeze(new Vec3(-1, 0, 0));
/**
* @field
* @static
* @readonly
* @name Vec3.FORWARD
* @type {Vec3}
* @description A constant vector set to [0, 0, -1].
*/
static FORWARD = Object.freeze(new Vec3(0, 0, -1));
/**
* @field
* @static
* @readonly
* @name Vec3.BACK
* @type {Vec3}
* @description A constant vector set to [0, 0, 1].
*/
static BACK = Object.freeze(new Vec3(0, 0, 1));
}
export { Vec3 };