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
693 lines (629 loc) · 20.7 KB
/
vec3.js
File metadata and controls
693 lines (629 loc) · 20.7 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
pc.extend(pc, (function () {
'use strict';
/**
* @name pc.Vec3
* @class A 3-dimensional vector.
* @description Creates a new Vec3 object
* @param {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);
*/
var Vec3 = function(x, y, z) {
if (x && x.length === 3) {
this.data = new Float32Array(x);
return;
}
this.data = new Float32Array(3);
this.data[0] = x || 0;
this.data[1] = y || 0;
this.data[2] = z || 0;
};
Vec3.prototype = {
/**
* @function
* @name pc.Vec3#add
* @description Adds a 3-dimensional vector to another in place.
* @param {pc.Vec3} rhs The vector to add to the specified vector.
* @returns {pc.Vec3} Self for chaining.
* @example
* var a = new pc.Vec3(10, 10, 10);
* var b = new pc.Vec3(20, 20, 20);
*
* a.add(b);
*
* // Should output [30, 30, 30]
* console.log("The result of the addition is: " + a.toString());
*/
add: function (rhs) {
var a = this.data,
b = rhs.data;
a[0] += b[0];
a[1] += b[1];
a[2] += b[2];
return this;
},
/**
* @function
* @name pc.Vec3#add2
* @description Adds two 3-dimensional vectors together and returns the result.
* @param {pc.Vec3} lhs The first vector operand for the addition.
* @param {pc.Vec3} rhs The second vector operand for the addition.
* @returns {pc.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);
* // Should output [30, 30, 30]
*
* console.log("The result of the addition is: " + r.toString());
*/
add2: function (lhs, rhs) {
var a = lhs.data,
b = rhs.data,
r = this.data;
r[0] = a[0] + b[0];
r[1] = a[1] + b[1];
r[2] = a[2] + b[2];
return this;
},
/**
* @function
* @name pc.Vec3#clone
* @description Returns an identical copy of the specified 3-dimensional vector.
* @returns {pc.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: function () {
return new Vec3().copy(this);
},
/**
* @function
* @name pc.Vec3#copy
* @description Copied the contents of a source 3-dimensional vector to a destination 3-dimensional vector.
* @param {pc.Vec3} rhs A vector to copy to the specified vector.
* @returns {pc.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: function (rhs) {
var a = this.data,
b = rhs.data;
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
return this;
},
/**
* @function
* @name pc.Vec3#cross
* @description Returns the result of a cross product operation performed on the two specified 3-dimensional vectors.
* @param {pc.Vec3} lhs The first 3-dimensional vector operand of the cross product.
* @param {pc.Vec3} rhs The second 3-dimensional vector operand of the cross product.
* @returns {pc.Vec3} Self for chaining.
* @example
* var back = new pc.Vec3().cross(pc.Vec3.RIGHT, pc.Vec3.UP);
*
* // Should print the Z axis (i.e. [0, 0, 1])
* console.log("The result of the cross product is: " + back.toString());
*/
cross: function (lhs, rhs) {
var a, b, r, ax, ay, az, bx, by, bz;
a = lhs.data;
b = rhs.data;
r = this.data;
ax = a[0];
ay = a[1];
az = a[2];
bx = b[0];
by = b[1];
bz = b[2];
r[0] = ay * bz - by * az;
r[1] = az * bx - bz * ax;
r[2] = ax * by - bx * ay;
return this;
},
/**
* @function
* @name pc.Vec3#dot
* @description Returns the result of a dot product operation performed on the two specified 3-dimensional vectors.
* @param {pc.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: function (rhs) {
var a = this.data,
b = rhs.data;
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
},
/**
* @function
* @name pc.Vec3#equals
* @description Reports whether two vectors are equal.
* @param {pc.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: function (rhs) {
var a = this.data,
b = rhs.data;
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
},
/**
* @function
* @name pc.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();
* // Should output 5
* console.log("The length of the vector is: " + len);
*/
length: function () {
var v = this.data;
return Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
},
/**
* @function
* @name pc.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();
* // Should output 25
* console.log("The length squared of the vector is: " + len);
*/
lengthSq: function () {
var v = this.data;
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
},
/**
* @function
* @name pc.Vec3#lerp
* @description Returns the result of a linear interpolation between two specified 3-dimensional vectors.
* @param {pc.Vec3} lhs The 3-dimensional to interpolate from.
* @param {pc.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 {pc.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: function (lhs, rhs, alpha) {
var a = lhs.data,
b = rhs.data,
r = this.data;
r[0] = a[0] + alpha * (b[0] - a[0]);
r[1] = a[1] + alpha * (b[1] - a[1]);
r[2] = a[2] + alpha * (b[2] - a[2]);
return this;
},
/**
* @function
* @name pc.Vec3#mul
* @description Multiplies a 3-dimensional vector to another in place.
* @param {pc.Vec3} rhs The 3-dimensional vector used as the second multiplicand of the operation.
* @returns {pc.Vec3} Self for chaining.
* @example
* var a = new pc.Vec3(2, 3, 4);
* var b = new pc.Vec3(4, 5, 6);
*
* a.mul(b);
*
* // Should output 8, 15, 24
* console.log("The result of the multiplication is: " + a.toString());
*/
mul: function (rhs) {
var a = this.data,
b = rhs.data;
a[0] *= b[0];
a[1] *= b[1];
a[2] *= b[2];
return this;
},
/**
* @function
* @name pc.Vec3#mul2
* @description Returns the result of multiplying the specified 3-dimensional vectors together.
* @param {pc.Vec3} lhs The 3-dimensional vector used as the first multiplicand of the operation.
* @param {pc.Vec3} rhs The 3-dimensional vector used as the second multiplicand of the operation.
* @returns {pc.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);
*
* // Should output 8, 15, 24
* console.log("The result of the multiplication is: " + r.toString());
*/
mul2: function (lhs, rhs) {
var a = lhs.data,
b = rhs.data,
r = this.data;
r[0] = a[0] * b[0];
r[1] = a[1] * b[1];
r[2] = a[2] * b[2];
return this;
},
/**
* @function
* @name pc.Vec3#normalize
* @description Returns the specified 3-dimensional vector copied and converted to a unit vector.
* If the vector has a length of zero, the vector's elements will be set to zero.
* @returns {pc.Vec3} The result of the normalization.
* @example
* var v = new pc.Vec3(25, 0, 0);
*
* v.normalize();
*
* // Should output 1, 0, 0, 0
* console.log("The result of the vector normalization is: " + v.toString());
*/
normalize: function () {
var v = this.data;
var lengthSq = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
if (lengthSq > 0) {
var invLength = 1 / Math.sqrt(lengthSq);
v[0] *= invLength;
v[1] *= invLength;
v[2] *= invLength;
}
return this;
},
/**
* @function
* @name pc.Vec3#project
* @description Projects this 3-dimensional vector onto the specified vector.
* @param {pc.Vec3} rhs The vector onto which the original vector will be projected on.
* @returns {pc.Vec3} Self for chaining.
* @example
* var v = new pc.Vec3(5, 5, 5);
* var normal = new pc.Vec3(1, 0, 0);
*
* v.project(normal);
*
* // Should output 5, 0, 0
* console.log("The result of the vector projection is: " + v.toString());
*/
project: function (rhs) {
var a = this.data;
var b = rhs.data;
var a_dot_b = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
var b_dot_b = b[0] * b[0] + b[1] * b[1] + b[2] * b[2];
var s = a_dot_b / b_dot_b;
a[0] = b[0] * s;
a[1] = b[1] * s;
a[2] = b[2] * s;
return this;
},
/**
* @function
* @name pc.Vec3#scale
* @description Scales each dimension of the specified 3-dimensional vector by the supplied
* scalar value.
* @param {Number} scalar The value by which each vector component is multiplied.
* @returns {pc.Vec3} Self for chaining.
* @example
* var v = new pc.Vec3(2, 4, 8);
*
* // Multiply by 2
* v.scale(2);
*
* // Negate
* v.scale(-1);
*
* // Divide by 2
* v.scale(0.5);
*/
scale: function (scalar) {
var v = this.data;
v[0] *= scalar;
v[1] *= scalar;
v[2] *= scalar;
return this;
},
/**
* @function
* @name pc.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.
* @example
* var v = new pc.Vec3();
* v.set(5, 10, 20);
*
* // Should output 5, 10, 20
* console.log("The result of the vector set is: " + v.toString());
*/
set: function (x, y, z) {
var v = this.data;
v[0] = x;
v[1] = y;
v[2] = z;
return this;
},
/**
* @function
* @name pc.Vec3#sub
* @description Subtracts a 3-dimensional vector from another in place.
* @param {pc.Vec3} rhs The vector to add to the specified vector.
* @returns {pc.Vec3} Self for chaining.
* @example
* var a = new pc.Vec3(10, 10, 10);
* var b = new pc.Vec3(20, 20, 20);
*
* a.sub(b);
*
* // Should output [-10, -10, -10]
* console.log("The result of the addition is: " + a.toString());
*/
sub: function (rhs) {
var a = this.data,
b = rhs.data;
a[0] -= b[0];
a[1] -= b[1];
a[2] -= b[2];
return this;
},
/**
* @function
* @name pc.Vec3#sub2
* @description Subtracts two 3-dimensional vectors from one another and returns the result.
* @param {pc.Vec3} lhs The first vector operand for the addition.
* @param {pc.Vec3} rhs The second vector operand for the addition.
* @returns {pc.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);
*
* // Should output [-10, -10, -10]
* console.log("The result of the addition is: " + r.toString());
*/
sub2: function (lhs, rhs) {
var a = lhs.data,
b = rhs.data,
r = this.data;
r[0] = a[0] - b[0];
r[1] = a[1] - b[1];
r[2] = a[2] - b[2];
return this;
},
/**
* @function
* @name pc.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);
* // Should output '[20, 10, 5]'
* console.log(v.toString());
*/
toString: function () {
return "[" + this.data[0] + ", " + this.data[1] + ", " + this.data[2] + "]";
}
};
/**
* @name pc.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;
*/
Object.defineProperty(Vec3.prototype, 'x', {
get: function () {
return this.data[0];
},
set: function (value) {
this.data[0] = value;
}
});
/**
* @name pc.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;
*/
Object.defineProperty(Vec3.prototype, 'y', {
get: function () {
return this.data[1];
},
set: function (value) {
this.data[1] = value;
}
});
/**
* @name pc.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;
*/
Object.defineProperty(Vec3.prototype, 'z', {
get: function () {
return this.data[2];
},
set: function (value) {
this.data[2] = value;
}
});
/**
* @static
* @readonly
* @type pc.Vec3
* @name pc.Vec3.BACK
* @description A constant vector set to [0, 0, 1].
*/
Object.defineProperty(Vec3, 'BACK', {
get: (function () {
var back = new Vec3(0, 0, 1);
return function () {
return back;
};
}())
});
/**
* @static
* @readonly
* @type pc.Vec3
* @name pc.Vec3.DOWN
* @description A constant vector set to [0, -1, 0].
*/
Object.defineProperty(Vec3, 'DOWN', {
get: (function () {
var down = new Vec3(0, -1, 0);
return function () {
return down;
};
}())
});
/**
* @static
* @readonly
* @type pc.Vec3
* @name pc.Vec3.FORWARD
* @description A constant vector set to [0, 0, -1].
*/
Object.defineProperty(Vec3, 'FORWARD', {
get: (function () {
var forward = new Vec3(0, 0, -1);
return function () {
return forward;
};
}())
});
/**
* @field
* @static
* @readonly
* @type pc.Vec3
* @name pc.Vec3.LEFT
* @description A constant vector set to [-1, 0, 0].
*/
Object.defineProperty(Vec3, 'LEFT', {
get: (function () {
var left = new Vec3(-1, 0, 0);
return function () {
return left;
};
}())
});
/**
* @field
* @static
* @readonly
* @type pc.Vec3
* @name pc.Vec3.ONE
* @description A constant vector set to [1, 1, 1].
*/
Object.defineProperty(Vec3, 'ONE', {
get: (function () {
var one = new Vec3(1, 1, 1);
return function () {
return one;
};
}())
});
/**
* @field
* @static
* @readonly
* @type pc.Vec3
* @name pc.Vec3.RIGHT
* @description A constant vector set to [1, 0, 0].
*/
Object.defineProperty(Vec3, 'RIGHT', {
get: (function () {
var right = new Vec3(1, 0, 0);
return function () {
return right;
};
}())
});
/**
* @field
* @static
* @readonly
* @type pc.Vec3
* @name pc.Vec3.UP
* @description A constant vector set to [0, 1, 0].
*/
Object.defineProperty(Vec3, 'UP', {
get: (function () {
var down = new Vec3(0, 1, 0);
return function () {
return down;
};
}())
});
/**
* @field
* @static
* @readonly
* @type pc.Vec3
* @name pc.Vec3.ZERO
* @description A constant vector set to [0, 0, 0].
*/
Object.defineProperty(Vec3, 'ZERO', {
get: (function () {
var zero = new Vec3(0, 0, 0);
return function () {
return zero;
};
}())
});
return {
Vec3: Vec3
};
}()));