forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec4.js
More file actions
662 lines (616 loc) · 19.1 KB
/
vec4.js
File metadata and controls
662 lines (616 loc) · 19.1 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
/**
* @class
* @name Vec4
* @classdesc A 4-dimensional vector.
* @description Creates a new Vec4 object.
* @param {number|number[]} [x] - The x value. If x is an array of length 4, the array will be used to populate all components.
* @param {number} [y] - The y value.
* @param {number} [z] - The z value.
* @param {number} [w] - The w value.
* @example
* var v = new pc.Vec4(1, 2, 3, 4);
*/
/**
* @field
* @name Vec4#x
* @type {number}
* @description The first component of the vector.
* @example
* var vec = new pc.Vec4(10, 20, 30, 40);
*
* // Get x
* var x = vec.x;
*
* // Set x
* vec.x = 0;
*/
/**
* @field
* @name Vec4#y
* @type {number}
* @description The second component of the vector.
* @example
* var vec = new pc.Vec4(10, 20, 30, 40);
*
* // Get y
* var y = vec.y;
*
* // Set y
* vec.y = 0;
*/
/**
* @field
* @name Vec4#z
* @type {number}
* @description The third component of the vector.
* @example
* var vec = new pc.Vec4(10, 20, 30, 40);
*
* // Get z
* var z = vec.z;
*
* // Set z
* vec.z = 0;
*/
/**
* @field
* @name Vec4#w
* @type {number}
* @description The fourth component of the vector.
* @example
* var vec = new pc.Vec4(10, 20, 30, 40);
*
* // Get w
* var w = vec.w;
*
* // Set w
* vec.w = 0;
*/
class Vec4 {
constructor(x = 0, y = 0, z = 0, w = 0) {
if (x.length === 4) {
this.x = x[0];
this.y = x[1];
this.z = x[2];
this.w = x[3];
} else {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
}
/**
* @function
* @name Vec4#add
* @description Adds a 4-dimensional vector to another in place.
* @param {Vec4} rhs - The vector to add to the specified vector.
* @returns {Vec4} Self for chaining.
* @example
* var a = new pc.Vec4(10, 10, 10, 10);
* var b = new pc.Vec4(20, 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;
this.w += rhs.w;
return this;
}
/**
* @function
* @name Vec4#add2
* @description Adds two 4-dimensional vectors together and returns the result.
* @param {Vec4} lhs - The first vector operand for the addition.
* @param {Vec4} rhs - The second vector operand for the addition.
* @returns {Vec4} Self for chaining.
* @example
* var a = new pc.Vec4(10, 10, 10, 10);
* var b = new pc.Vec4(20, 20, 20, 20);
* var r = new pc.Vec4();
*
* 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;
this.w = lhs.w + rhs.w;
return this;
}
/**
* @function
* @name Vec4#addScalar
* @description Adds a number to each element of a vector.
* @param {number} scalar - The number to add.
* @returns {Vec4} Self for chaining.
* @example
* var vec = new pc.Vec4(3, 4, 5, 6);
*
* vec.addScalar(2);
*
* // Outputs [5, 6, 7, 8]
* console.log("The result of the addition is: " + vec.toString());
*/
addScalar(scalar) {
this.x += scalar;
this.y += scalar;
this.z += scalar;
this.w += scalar;
return this;
}
/**
* @function
* @name Vec4#clone
* @description Returns an identical copy of the specified 4-dimensional vector.
* @returns {Vec4} A 4-dimensional vector containing the result of the cloning.
* @example
* var v = new pc.Vec4(10, 20, 30, 40);
* var vclone = v.clone();
* console.log("The result of the cloning is: " + vclone.toString());
*/
clone() {
return new Vec4(this.x, this.y, this.z, this.w);
}
/**
* @function
* @name Vec4#copy
* @description Copied the contents of a source 4-dimensional vector to a destination 4-dimensional vector.
* @param {Vec4} rhs - A vector to copy to the specified vector.
* @returns {Vec4} Self for chaining.
* @example
* var src = new pc.Vec4(10, 20, 30, 40);
* var dst = new pc.Vec4();
*
* 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;
this.w = rhs.w;
return this;
}
/**
* @function
* @name Vec4#div
* @description Divides a 4-dimensional vector by another in place.
* @param {Vec4} rhs - The vector to divide the specified vector by.
* @returns {Vec4} Self for chaining.
* @example
* var a = new pc.Vec4(4, 9, 16, 25);
* var b = new pc.Vec4(2, 3, 4, 5);
*
* a.div(b);
*
* // Outputs [2, 3, 4, 5]
* console.log("The result of the division is: " + a.toString());
*/
div(rhs) {
this.x /= rhs.x;
this.y /= rhs.y;
this.z /= rhs.z;
this.w /= rhs.w;
return this;
}
/**
* @function
* @name Vec4#div2
* @description Divides one 4-dimensional vector by another and writes the result to
* the specified vector.
* @param {Vec4} lhs - The dividend vector (the vector being divided).
* @param {Vec4} rhs - The divisor vector (the vector dividing the dividend).
* @returns {Vec4} Self for chaining.
* @example
* var a = new pc.Vec4(4, 9, 16, 25);
* var b = new pc.Vec4(2, 3, 4, 5);
* var r = new pc.Vec4();
*
* r.div2(a, b);
* // Outputs [2, 3, 4, 5]
*
* 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;
this.w = lhs.w / rhs.w;
return this;
}
/**
* @function
* @name Vec4#divScalar
* @description Divides each element of a vector by a number.
* @param {number} scalar - The number to divide by.
* @returns {Vec4} Self for chaining.
* @example
* var vec = new pc.Vec4(3, 6, 9, 12);
*
* vec.divScalar(3);
*
* // Outputs [1, 2, 3, 4]
* console.log("The result of the division is: " + vec.toString());
*/
divScalar(scalar) {
this.x /= scalar;
this.y /= scalar;
this.z /= scalar;
this.w /= scalar;
return this;
}
/**
* @function
* @name Vec4#dot
* @description Returns the result of a dot product operation performed on the two specified 4-dimensional vectors.
* @param {Vec4} rhs - The second 4-dimensional vector operand of the dot product.
* @returns {number} The result of the dot product operation.
* @example
* var v1 = new pc.Vec4(5, 10, 20, 40);
* var v2 = new pc.Vec4(10, 20, 40, 80);
* 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 + this.w * rhs.w;
}
/**
* @function
* @name Vec4#equals
* @description Reports whether two vectors are equal.
* @param {Vec4} 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.Vec4(1, 2, 3, 4);
* var b = new pc.Vec4(5, 6, 7, 8);
* 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 && this.w === rhs.w;
}
/**
* @function
* @name Vec4#length
* @description Returns the magnitude of the specified 4-dimensional vector.
* @returns {number} The magnitude of the specified 4-dimensional vector.
* @example
* var vec = new pc.Vec4(3, 4, 0, 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 + this.w * this.w);
}
/**
* @function
* @name Vec4#lengthSq
* @description Returns the magnitude squared of the specified 4-dimensional vector.
* @returns {number} The magnitude of the specified 4-dimensional vector.
* @example
* var vec = new pc.Vec4(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 + this.w * this.w;
}
/**
* @function
* @name Vec4#lerp
* @description Returns the result of a linear interpolation between two specified 4-dimensional vectors.
* @param {Vec4} lhs - The 4-dimensional to interpolate from.
* @param {Vec4} rhs - The 4-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 {Vec4} Self for chaining.
* @example
* var a = new pc.Vec4(0, 0, 0, 0);
* var b = new pc.Vec4(10, 10, 10, 10);
* var r = new pc.Vec4();
*
* r.lerp(a, b, 0); // r is equal to a
* r.lerp(a, b, 0.5); // r is 5, 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);
this.w = lhs.w + alpha * (rhs.w - lhs.w);
return this;
}
/**
* @function
* @name Vec4#mul
* @description Multiplies a 4-dimensional vector to another in place.
* @param {Vec4} rhs - The 4-dimensional vector used as the second multiplicand of the operation.
* @returns {Vec4} Self for chaining.
* @example
* var a = new pc.Vec4(2, 3, 4, 5);
* var b = new pc.Vec4(4, 5, 6, 7);
*
* a.mul(b);
*
* // Outputs 8, 15, 24, 35
* console.log("The result of the multiplication is: " + a.toString());
*/
mul(rhs) {
this.x *= rhs.x;
this.y *= rhs.y;
this.z *= rhs.z;
this.w *= rhs.w;
return this;
}
/**
* @function
* @name Vec4#mul2
* @description Returns the result of multiplying the specified 4-dimensional vectors together.
* @param {Vec4} lhs - The 4-dimensional vector used as the first multiplicand of the operation.
* @param {Vec4} rhs - The 4-dimensional vector used as the second multiplicand of the operation.
* @returns {Vec4} Self for chaining.
* @example
* var a = new pc.Vec4(2, 3, 4, 5);
* var b = new pc.Vec4(4, 5, 6, 7);
* var r = new pc.Vec4();
*
* r.mul2(a, b);
*
* // Outputs 8, 15, 24, 35
* 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;
this.w = lhs.w * rhs.w;
return this;
}
/**
* @function
* @name Vec4#mulScalar
* @description Multiplies each element of a vector by a number.
* @param {number} scalar - The number to multiply by.
* @returns {Vec4} Self for chaining.
* @example
* var vec = new pc.Vec4(3, 6, 9, 12);
*
* vec.mulScalar(3);
*
* // Outputs [9, 18, 27, 36]
* console.log("The result of the multiplication is: " + vec.toString());
*/
mulScalar(scalar) {
this.x *= scalar;
this.y *= scalar;
this.z *= scalar;
this.w *= scalar;
return this;
}
/**
* @function
* @name Vec4#normalize
* @description Returns this 4-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 {Vec4} Self for chaining.
* @example
* var v = new pc.Vec4(25, 0, 0, 0);
*
* v.normalize();
*
* // Outputs 1, 0, 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 + this.w * this.w;
if (lengthSq > 0) {
const invLength = 1 / Math.sqrt(lengthSq);
this.x *= invLength;
this.y *= invLength;
this.z *= invLength;
this.w *= invLength;
}
return this;
}
/**
* @function
* @name Vec4#floor
* @description Each element is set to the largest integer less than or equal to its value.
* @returns {Vec4} Self for chaining.
*/
floor() {
this.x = Math.floor(this.x);
this.y = Math.floor(this.y);
this.z = Math.floor(this.z);
this.w = Math.floor(this.w);
return this;
}
/**
* @function
* @name Vec4#ceil
* @description Each element is rounded up to the next largest integer.
* @returns {Vec4} Self for chaining.
*/
ceil() {
this.x = Math.ceil(this.x);
this.y = Math.ceil(this.y);
this.z = Math.ceil(this.z);
this.w = Math.ceil(this.w);
return this;
}
/**
* @function
* @name Vec4#round
* @description Each element is rounded up or down to the nearest integer.
* @returns {Vec4} Self for chaining.
*/
round() {
this.x = Math.round(this.x);
this.y = Math.round(this.y);
this.z = Math.round(this.z);
this.w = Math.round(this.w);
return this;
}
/**
* @function
* @name Vec4#min
* @description Each element is assigned a value from rhs parameter if it is smaller.
* @param {Vec4} rhs - The 4-dimensional vector used as the source of elements to compare to.
* @returns {Vec4} 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;
if (rhs.w < this.w) this.w = rhs.w;
return this;
}
/**
* @function
* @name Vec4#max
* @description Each element is assigned a value from rhs parameter if it is larger.
* @param {Vec4} rhs - The 4-dimensional vector used as the source of elements to compare to.
* @returns {Vec4} 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;
if (rhs.w > this.w) this.w = rhs.w;
return this;
}
/**
* @function
* @name Vec4#set
* @description Sets the specified 4-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.
* @param {number} w - The value to set on the fourth component of the vector.
* @returns {Vec4} Self for chaining.
* @example
* var v = new pc.Vec4();
* v.set(5, 10, 20, 40);
*
* // Outputs 5, 10, 20, 40
* console.log("The result of the vector set is: " + v.toString());
*/
set(x, y, z, w) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
return this;
}
/**
* @function
* @name Vec4#sub
* @description Subtracts a 4-dimensional vector from another in place.
* @param {Vec4} rhs - The vector to add to the specified vector.
* @returns {Vec4} Self for chaining.
* @example
* var a = new pc.Vec4(10, 10, 10, 10);
* var b = new pc.Vec4(20, 20, 20, 20);
*
* a.sub(b);
*
* // Outputs [-10, -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;
this.w -= rhs.w;
return this;
}
/**
* @function
* @name Vec4#sub2
* @description Subtracts two 4-dimensional vectors from one another and returns the result.
* @param {Vec4} lhs - The first vector operand for the subtraction.
* @param {Vec4} rhs - The second vector operand for the subtraction.
* @returns {Vec4} Self for chaining.
* @example
* var a = new pc.Vec4(10, 10, 10, 10);
* var b = new pc.Vec4(20, 20, 20, 20);
* var r = new pc.Vec4();
*
* r.sub2(a, b);
*
* // Outputs [-10, -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;
this.w = lhs.w - rhs.w;
return this;
}
/**
* @function
* @name Vec4#subScalar
* @description Subtracts a number from each element of a vector.
* @param {number} scalar - The number to subtract.
* @returns {Vec4} Self for chaining.
* @example
* var vec = new pc.Vec4(3, 4, 5, 6);
*
* vec.subScalar(2);
*
* // Outputs [1, 2, 3, 4]
* console.log("The result of the subtraction is: " + vec.toString());
*/
subScalar(scalar) {
this.x -= scalar;
this.y -= scalar;
this.z -= scalar;
this.w -= scalar;
return this;
}
/**
* @function
* @name Vec4#toString
* @description Converts the vector to string form.
* @returns {string} The vector in string form.
* @example
* var v = new pc.Vec4(20, 10, 5, 0);
* // Outputs [20, 10, 5, 0]
* console.log(v.toString());
*/
toString() {
return '[' + this.x + ', ' + this.y + ', ' + this.z + ', ' + this.w + ']';
}
/**
* @field
* @static
* @readonly
* @name Vec4.ZERO
* @type {Vec4}
* @description A constant vector set to [0, 0, 0, 0].
*/
static ZERO = Object.freeze(new Vec4(0, 0, 0, 0));
/**
* @field
* @static
* @readonly
* @name Vec4.ONE
* @type {Vec4}
* @description A constant vector set to [1, 1, 1, 1].
*/
static ONE = Object.freeze(new Vec4(1, 1, 1, 1));
}
export { Vec4 };