jax 2.0.4 → 2.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +18 -0
- data/Rakefile +2 -2
- data/lib/assets/javascripts/jax/{application.js.erb → application.js} +0 -4
- data/lib/assets/javascripts/jax/core.js +1 -1
- data/lib/assets/javascripts/jax/core/base.js.erb +4 -0
- data/lib/assets/javascripts/jax/core/glMatrix_ext/vec3.js +22 -0
- data/lib/assets/javascripts/jax/core/matrix_stack.js +1 -1
- data/lib/assets/javascripts/jax/mvc/model.js +18 -0
- data/lib/assets/javascripts/jax/vendor/{glMatrix.js → gl-matrix.js} +1 -1
- data/lib/assets/javascripts/jax/webgl.js +1 -1
- data/lib/assets/javascripts/jax/webgl/camera.js +24 -39
- data/lib/assets/javascripts/jax/webgl/scene/light_source.js +58 -29
- data/lib/jax/version.rb +1 -1
- data/spec/javascripts/jax/core/utils_spec.js +9 -0
- data/spec/javascripts/jax/webgl/camera_spec.js +25 -0
- data/spec/javascripts/jax/webgl/lighting_spec.js +17 -0
- data/spec/javascripts/vendor/gl_matrix_spec.js +90 -0
- data/vendor/assets/javascripts/gl-matrix-pdoc.js +705 -0
- data/vendor/assets/javascripts/gl-matrix.js +1924 -0
- metadata +48 -45
- data/vendor/assets/javascripts/glMatrix.js +0 -1772
@@ -0,0 +1,1924 @@
|
|
1
|
+
/*
|
2
|
+
* gl-matrix.js - High performance matrix and vector operations for WebGL
|
3
|
+
* Version 1.1
|
4
|
+
*/
|
5
|
+
|
6
|
+
/*
|
7
|
+
* Copyright (c) 2011 Brandon Jones
|
8
|
+
*
|
9
|
+
* This software is provided 'as-is', without any express or implied
|
10
|
+
* warranty. In no event will the authors be held liable for any damages
|
11
|
+
* arising from the use of this software.
|
12
|
+
*
|
13
|
+
* Permission is granted to anyone to use this software for any purpose,
|
14
|
+
* including commercial applications, and to alter it and redistribute it
|
15
|
+
* freely, subject to the following restrictions:
|
16
|
+
*
|
17
|
+
* 1. The origin of this software must not be misrepresented; you must not
|
18
|
+
* claim that you wrote the original software. If you use this software
|
19
|
+
* in a product, an acknowledgment in the product documentation would be
|
20
|
+
* appreciated but is not required.
|
21
|
+
*
|
22
|
+
* 2. Altered source versions must be plainly marked as such, and must not
|
23
|
+
* be misrepresented as being the original software.
|
24
|
+
*
|
25
|
+
* 3. This notice may not be removed or altered from any source
|
26
|
+
* distribution.
|
27
|
+
*/
|
28
|
+
|
29
|
+
"use strict";
|
30
|
+
|
31
|
+
// Type declarations
|
32
|
+
(function() {
|
33
|
+
// account for CommonJS environments
|
34
|
+
var _global = (typeof(exports) != 'undefined') ? global : window;
|
35
|
+
_global.glMatrixArrayType = _global.MatrixArray = null;
|
36
|
+
_global.vec3 = {};
|
37
|
+
_global.mat3 = {};
|
38
|
+
_global.mat4 = {};
|
39
|
+
_global.quat4 = {};
|
40
|
+
|
41
|
+
// explicitly sets and returns the type of array to use within glMatrix
|
42
|
+
_global.setMatrixArrayType = function(type) {
|
43
|
+
return glMatrixArrayType = MatrixArray = type;
|
44
|
+
};
|
45
|
+
|
46
|
+
// auto-detects and returns the best type of array to use within glMatrix, falling
|
47
|
+
// back to Array if typed arrays are unsupported
|
48
|
+
_global.determineMatrixArrayType = function() {
|
49
|
+
return setMatrixArrayType((typeof Float32Array !== 'undefined') ? Float32Array : Array);
|
50
|
+
};
|
51
|
+
|
52
|
+
determineMatrixArrayType();
|
53
|
+
})();
|
54
|
+
|
55
|
+
/*
|
56
|
+
* vec3 - 3 Dimensional Vector
|
57
|
+
*/
|
58
|
+
|
59
|
+
/*
|
60
|
+
* vec3.create
|
61
|
+
* Creates a new instance of a vec3 using the default array type
|
62
|
+
* Any javascript array containing at least 3 numeric elements can serve as a vec3
|
63
|
+
*
|
64
|
+
* Params:
|
65
|
+
* vec - Optional, vec3 containing values to initialize with
|
66
|
+
*
|
67
|
+
* Returns:
|
68
|
+
* New vec3
|
69
|
+
*/
|
70
|
+
vec3.create = function (vec) {
|
71
|
+
var dest = new MatrixArray(3);
|
72
|
+
|
73
|
+
if (vec) {
|
74
|
+
dest[0] = vec[0];
|
75
|
+
dest[1] = vec[1];
|
76
|
+
dest[2] = vec[2];
|
77
|
+
} else {
|
78
|
+
dest[0] = dest[1] = dest[2] = 0;
|
79
|
+
}
|
80
|
+
|
81
|
+
return dest;
|
82
|
+
};
|
83
|
+
|
84
|
+
/*
|
85
|
+
* vec3.set
|
86
|
+
* Copies the values of one vec3 to another
|
87
|
+
*
|
88
|
+
* Params:
|
89
|
+
* vec - vec3 containing values to copy
|
90
|
+
* dest - vec3 receiving copied values
|
91
|
+
*
|
92
|
+
* Returns:
|
93
|
+
* dest
|
94
|
+
*/
|
95
|
+
vec3.set = function (vec, dest) {
|
96
|
+
dest[0] = vec[0];
|
97
|
+
dest[1] = vec[1];
|
98
|
+
dest[2] = vec[2];
|
99
|
+
|
100
|
+
return dest;
|
101
|
+
};
|
102
|
+
|
103
|
+
/*
|
104
|
+
* vec3.add
|
105
|
+
* Performs a vector addition
|
106
|
+
*
|
107
|
+
* Params:
|
108
|
+
* vec - vec3, first operand
|
109
|
+
* vec2 - vec3, second operand
|
110
|
+
* dest - Optional, vec3 receiving operation result. If not specified result is written to vec
|
111
|
+
*
|
112
|
+
* Returns:
|
113
|
+
* dest if specified, vec otherwise
|
114
|
+
*/
|
115
|
+
vec3.add = function (vec, vec2, dest) {
|
116
|
+
if (!dest || vec === dest) {
|
117
|
+
vec[0] += vec2[0];
|
118
|
+
vec[1] += vec2[1];
|
119
|
+
vec[2] += vec2[2];
|
120
|
+
return vec;
|
121
|
+
}
|
122
|
+
|
123
|
+
dest[0] = vec[0] + vec2[0];
|
124
|
+
dest[1] = vec[1] + vec2[1];
|
125
|
+
dest[2] = vec[2] + vec2[2];
|
126
|
+
return dest;
|
127
|
+
};
|
128
|
+
|
129
|
+
/*
|
130
|
+
* vec3.subtract
|
131
|
+
* Performs a vector subtraction
|
132
|
+
*
|
133
|
+
* Params:
|
134
|
+
* vec - vec3, first operand
|
135
|
+
* vec2 - vec3, second operand
|
136
|
+
* dest - Optional, vec3 receiving operation result. If not specified result is written to vec
|
137
|
+
*
|
138
|
+
* Returns:
|
139
|
+
* dest if specified, vec otherwise
|
140
|
+
*/
|
141
|
+
vec3.subtract = function (vec, vec2, dest) {
|
142
|
+
if (!dest || vec === dest) {
|
143
|
+
vec[0] -= vec2[0];
|
144
|
+
vec[1] -= vec2[1];
|
145
|
+
vec[2] -= vec2[2];
|
146
|
+
return vec;
|
147
|
+
}
|
148
|
+
|
149
|
+
dest[0] = vec[0] - vec2[0];
|
150
|
+
dest[1] = vec[1] - vec2[1];
|
151
|
+
dest[2] = vec[2] - vec2[2];
|
152
|
+
return dest;
|
153
|
+
};
|
154
|
+
|
155
|
+
/*
|
156
|
+
* vec3.negate
|
157
|
+
* Negates the components of a vec3
|
158
|
+
*
|
159
|
+
* Params:
|
160
|
+
* vec - vec3 to negate
|
161
|
+
* dest - Optional, vec3 receiving operation result. If not specified result is written to vec
|
162
|
+
*
|
163
|
+
* Returns:
|
164
|
+
* dest if specified, vec otherwise
|
165
|
+
*/
|
166
|
+
vec3.negate = function (vec, dest) {
|
167
|
+
if (!dest) { dest = vec; }
|
168
|
+
|
169
|
+
dest[0] = -vec[0];
|
170
|
+
dest[1] = -vec[1];
|
171
|
+
dest[2] = -vec[2];
|
172
|
+
return dest;
|
173
|
+
};
|
174
|
+
|
175
|
+
/*
|
176
|
+
* vec3.scale
|
177
|
+
* Multiplies the components of a vec3 by a scalar value
|
178
|
+
*
|
179
|
+
* Params:
|
180
|
+
* vec - vec3 to scale
|
181
|
+
* val - Numeric value to scale by
|
182
|
+
* dest - Optional, vec3 receiving operation result. If not specified result is written to vec
|
183
|
+
*
|
184
|
+
* Returns:
|
185
|
+
* dest if specified, vec otherwise
|
186
|
+
*/
|
187
|
+
vec3.scale = function (vec, val, dest) {
|
188
|
+
if (!dest || vec === dest) {
|
189
|
+
vec[0] *= val;
|
190
|
+
vec[1] *= val;
|
191
|
+
vec[2] *= val;
|
192
|
+
return vec;
|
193
|
+
}
|
194
|
+
|
195
|
+
dest[0] = vec[0] * val;
|
196
|
+
dest[1] = vec[1] * val;
|
197
|
+
dest[2] = vec[2] * val;
|
198
|
+
return dest;
|
199
|
+
};
|
200
|
+
|
201
|
+
/*
|
202
|
+
* vec3.normalize
|
203
|
+
* Generates a unit vector of the same direction as the provided vec3
|
204
|
+
* If vector length is 0, returns [0, 0, 0]
|
205
|
+
*
|
206
|
+
* Params:
|
207
|
+
* vec - vec3 to normalize
|
208
|
+
* dest - Optional, vec3 receiving operation result. If not specified result is written to vec
|
209
|
+
*
|
210
|
+
* Returns:
|
211
|
+
* dest if specified, vec otherwise
|
212
|
+
*/
|
213
|
+
vec3.normalize = function (vec, dest) {
|
214
|
+
if (!dest) { dest = vec; }
|
215
|
+
|
216
|
+
var x = vec[0], y = vec[1], z = vec[2],
|
217
|
+
len = Math.sqrt(x * x + y * y + z * z);
|
218
|
+
|
219
|
+
if (!len) {
|
220
|
+
dest[0] = 0;
|
221
|
+
dest[1] = 0;
|
222
|
+
dest[2] = 0;
|
223
|
+
return dest;
|
224
|
+
} else if (len === 1) {
|
225
|
+
dest[0] = x;
|
226
|
+
dest[1] = y;
|
227
|
+
dest[2] = z;
|
228
|
+
return dest;
|
229
|
+
}
|
230
|
+
|
231
|
+
len = 1 / len;
|
232
|
+
dest[0] = x * len;
|
233
|
+
dest[1] = y * len;
|
234
|
+
dest[2] = z * len;
|
235
|
+
return dest;
|
236
|
+
};
|
237
|
+
|
238
|
+
/*
|
239
|
+
* vec3.cross
|
240
|
+
* Generates the cross product of two vec3s
|
241
|
+
*
|
242
|
+
* Params:
|
243
|
+
* vec - vec3, first operand
|
244
|
+
* vec2 - vec3, second operand
|
245
|
+
* dest - Optional, vec3 receiving operation result. If not specified result is written to vec
|
246
|
+
*
|
247
|
+
* Returns:
|
248
|
+
* dest if specified, vec otherwise
|
249
|
+
*/
|
250
|
+
vec3.cross = function (vec, vec2, dest) {
|
251
|
+
if (!dest) { dest = vec; }
|
252
|
+
|
253
|
+
var x = vec[0], y = vec[1], z = vec[2],
|
254
|
+
x2 = vec2[0], y2 = vec2[1], z2 = vec2[2];
|
255
|
+
|
256
|
+
dest[0] = y * z2 - z * y2;
|
257
|
+
dest[1] = z * x2 - x * z2;
|
258
|
+
dest[2] = x * y2 - y * x2;
|
259
|
+
return dest;
|
260
|
+
};
|
261
|
+
|
262
|
+
/*
|
263
|
+
* vec3.length
|
264
|
+
* Caclulates the length of a vec3
|
265
|
+
*
|
266
|
+
* Params:
|
267
|
+
* vec - vec3 to calculate length of
|
268
|
+
*
|
269
|
+
* Returns:
|
270
|
+
* Length of vec
|
271
|
+
*/
|
272
|
+
vec3.length = function (vec) {
|
273
|
+
var x = vec[0], y = vec[1], z = vec[2];
|
274
|
+
return Math.sqrt(x * x + y * y + z * z);
|
275
|
+
};
|
276
|
+
|
277
|
+
/*
|
278
|
+
* vec3.dot
|
279
|
+
* Caclulates the dot product of two vec3s
|
280
|
+
*
|
281
|
+
* Params:
|
282
|
+
* vec - vec3, first operand
|
283
|
+
* vec2 - vec3, second operand
|
284
|
+
*
|
285
|
+
* Returns:
|
286
|
+
* Dot product of vec and vec2
|
287
|
+
*/
|
288
|
+
vec3.dot = function (vec, vec2) {
|
289
|
+
return vec[0] * vec2[0] + vec[1] * vec2[1] + vec[2] * vec2[2];
|
290
|
+
};
|
291
|
+
|
292
|
+
/*
|
293
|
+
* vec3.direction
|
294
|
+
* Generates a unit vector pointing from one vector to another
|
295
|
+
*
|
296
|
+
* Params:
|
297
|
+
* vec - origin vec3
|
298
|
+
* vec2 - vec3 to point to
|
299
|
+
* dest - Optional, vec3 receiving operation result. If not specified result is written to vec
|
300
|
+
*
|
301
|
+
* Returns:
|
302
|
+
* dest if specified, vec otherwise
|
303
|
+
*/
|
304
|
+
vec3.direction = function (vec, vec2, dest) {
|
305
|
+
if (!dest) { dest = vec; }
|
306
|
+
|
307
|
+
var x = vec[0] - vec2[0],
|
308
|
+
y = vec[1] - vec2[1],
|
309
|
+
z = vec[2] - vec2[2],
|
310
|
+
len = Math.sqrt(x * x + y * y + z * z);
|
311
|
+
|
312
|
+
if (!len) {
|
313
|
+
dest[0] = 0;
|
314
|
+
dest[1] = 0;
|
315
|
+
dest[2] = 0;
|
316
|
+
return dest;
|
317
|
+
}
|
318
|
+
|
319
|
+
len = 1 / len;
|
320
|
+
dest[0] = x * len;
|
321
|
+
dest[1] = y * len;
|
322
|
+
dest[2] = z * len;
|
323
|
+
return dest;
|
324
|
+
};
|
325
|
+
|
326
|
+
/*
|
327
|
+
* vec3.lerp
|
328
|
+
* Performs a linear interpolation between two vec3
|
329
|
+
*
|
330
|
+
* Params:
|
331
|
+
* vec - vec3, first vector
|
332
|
+
* vec2 - vec3, second vector
|
333
|
+
* lerp - interpolation amount between the two inputs
|
334
|
+
* dest - Optional, vec3 receiving operation result. If not specified result is written to vec
|
335
|
+
*
|
336
|
+
* Returns:
|
337
|
+
* dest if specified, vec otherwise
|
338
|
+
*/
|
339
|
+
vec3.lerp = function (vec, vec2, lerp, dest) {
|
340
|
+
if (!dest) { dest = vec; }
|
341
|
+
|
342
|
+
dest[0] = vec[0] + lerp * (vec2[0] - vec[0]);
|
343
|
+
dest[1] = vec[1] + lerp * (vec2[1] - vec[1]);
|
344
|
+
dest[2] = vec[2] + lerp * (vec2[2] - vec[2]);
|
345
|
+
|
346
|
+
return dest;
|
347
|
+
};
|
348
|
+
|
349
|
+
/*
|
350
|
+
* vec3.str
|
351
|
+
* Returns a string representation of a vector
|
352
|
+
*
|
353
|
+
* Params:
|
354
|
+
* vec - vec3 to represent as a string
|
355
|
+
*
|
356
|
+
* Returns:
|
357
|
+
* string representation of vec
|
358
|
+
*/
|
359
|
+
vec3.str = function (vec) {
|
360
|
+
return '[' + vec[0] + ', ' + vec[1] + ', ' + vec[2] + ']';
|
361
|
+
};
|
362
|
+
|
363
|
+
/*
|
364
|
+
* mat3 - 3x3 Matrix
|
365
|
+
*/
|
366
|
+
|
367
|
+
/*
|
368
|
+
* mat3.create
|
369
|
+
* Creates a new instance of a mat3 using the default array type
|
370
|
+
* Any javascript array containing at least 9 numeric elements can serve as a mat3
|
371
|
+
*
|
372
|
+
* Params:
|
373
|
+
* mat - Optional, mat3 containing values to initialize with
|
374
|
+
*
|
375
|
+
* Returns:
|
376
|
+
* New mat3
|
377
|
+
*/
|
378
|
+
mat3.create = function (mat) {
|
379
|
+
var dest = new MatrixArray(9);
|
380
|
+
|
381
|
+
if (mat) {
|
382
|
+
dest[0] = mat[0];
|
383
|
+
dest[1] = mat[1];
|
384
|
+
dest[2] = mat[2];
|
385
|
+
dest[3] = mat[3];
|
386
|
+
dest[4] = mat[4];
|
387
|
+
dest[5] = mat[5];
|
388
|
+
dest[6] = mat[6];
|
389
|
+
dest[7] = mat[7];
|
390
|
+
dest[8] = mat[8];
|
391
|
+
}
|
392
|
+
|
393
|
+
return dest;
|
394
|
+
};
|
395
|
+
|
396
|
+
/*
|
397
|
+
* mat3.set
|
398
|
+
* Copies the values of one mat3 to another
|
399
|
+
*
|
400
|
+
* Params:
|
401
|
+
* mat - mat3 containing values to copy
|
402
|
+
* dest - mat3 receiving copied values
|
403
|
+
*
|
404
|
+
* Returns:
|
405
|
+
* dest
|
406
|
+
*/
|
407
|
+
mat3.set = function (mat, dest) {
|
408
|
+
dest[0] = mat[0];
|
409
|
+
dest[1] = mat[1];
|
410
|
+
dest[2] = mat[2];
|
411
|
+
dest[3] = mat[3];
|
412
|
+
dest[4] = mat[4];
|
413
|
+
dest[5] = mat[5];
|
414
|
+
dest[6] = mat[6];
|
415
|
+
dest[7] = mat[7];
|
416
|
+
dest[8] = mat[8];
|
417
|
+
return dest;
|
418
|
+
};
|
419
|
+
|
420
|
+
/*
|
421
|
+
* mat3.identity
|
422
|
+
* Sets a mat3 to an identity matrix
|
423
|
+
*
|
424
|
+
* Params:
|
425
|
+
* dest - mat3 to set
|
426
|
+
*
|
427
|
+
* Returns:
|
428
|
+
* dest
|
429
|
+
*/
|
430
|
+
mat3.identity = function (dest) {
|
431
|
+
if (!dest) { dest = mat3.create(); }
|
432
|
+
dest[0] = 1;
|
433
|
+
dest[1] = 0;
|
434
|
+
dest[2] = 0;
|
435
|
+
dest[3] = 0;
|
436
|
+
dest[4] = 1;
|
437
|
+
dest[5] = 0;
|
438
|
+
dest[6] = 0;
|
439
|
+
dest[7] = 0;
|
440
|
+
dest[8] = 1;
|
441
|
+
return dest;
|
442
|
+
};
|
443
|
+
|
444
|
+
/*
|
445
|
+
* mat4.transpose
|
446
|
+
* Transposes a mat3 (flips the values over the diagonal)
|
447
|
+
*
|
448
|
+
* Params:
|
449
|
+
* mat - mat3 to transpose
|
450
|
+
* dest - Optional, mat3 receiving transposed values. If not specified result is written to mat
|
451
|
+
*
|
452
|
+
* Returns:
|
453
|
+
* dest is specified, mat otherwise
|
454
|
+
*/
|
455
|
+
mat3.transpose = function (mat, dest) {
|
456
|
+
// If we are transposing ourselves we can skip a few steps but have to cache some values
|
457
|
+
if (!dest || mat === dest) {
|
458
|
+
var a01 = mat[1], a02 = mat[2],
|
459
|
+
a12 = mat[5];
|
460
|
+
|
461
|
+
mat[1] = mat[3];
|
462
|
+
mat[2] = mat[6];
|
463
|
+
mat[3] = a01;
|
464
|
+
mat[5] = mat[7];
|
465
|
+
mat[6] = a02;
|
466
|
+
mat[7] = a12;
|
467
|
+
return mat;
|
468
|
+
}
|
469
|
+
|
470
|
+
dest[0] = mat[0];
|
471
|
+
dest[1] = mat[3];
|
472
|
+
dest[2] = mat[6];
|
473
|
+
dest[3] = mat[1];
|
474
|
+
dest[4] = mat[4];
|
475
|
+
dest[5] = mat[7];
|
476
|
+
dest[6] = mat[2];
|
477
|
+
dest[7] = mat[5];
|
478
|
+
dest[8] = mat[8];
|
479
|
+
return dest;
|
480
|
+
};
|
481
|
+
|
482
|
+
/*
|
483
|
+
* mat3.toMat4
|
484
|
+
* Copies the elements of a mat3 into the upper 3x3 elements of a mat4
|
485
|
+
*
|
486
|
+
* Params:
|
487
|
+
* mat - mat3 containing values to copy
|
488
|
+
* dest - Optional, mat4 receiving copied values
|
489
|
+
*
|
490
|
+
* Returns:
|
491
|
+
* dest if specified, a new mat4 otherwise
|
492
|
+
*/
|
493
|
+
mat3.toMat4 = function (mat, dest) {
|
494
|
+
if (!dest) { dest = mat4.create(); }
|
495
|
+
|
496
|
+
dest[15] = 1;
|
497
|
+
dest[14] = 0;
|
498
|
+
dest[13] = 0;
|
499
|
+
dest[12] = 0;
|
500
|
+
|
501
|
+
dest[11] = 0;
|
502
|
+
dest[10] = mat[8];
|
503
|
+
dest[9] = mat[7];
|
504
|
+
dest[8] = mat[6];
|
505
|
+
|
506
|
+
dest[7] = 0;
|
507
|
+
dest[6] = mat[5];
|
508
|
+
dest[5] = mat[4];
|
509
|
+
dest[4] = mat[3];
|
510
|
+
|
511
|
+
dest[3] = 0;
|
512
|
+
dest[2] = mat[2];
|
513
|
+
dest[1] = mat[1];
|
514
|
+
dest[0] = mat[0];
|
515
|
+
|
516
|
+
return dest;
|
517
|
+
};
|
518
|
+
|
519
|
+
/*
|
520
|
+
* mat3.str
|
521
|
+
* Returns a string representation of a mat3
|
522
|
+
*
|
523
|
+
* Params:
|
524
|
+
* mat - mat3 to represent as a string
|
525
|
+
*
|
526
|
+
* Returns:
|
527
|
+
* string representation of mat
|
528
|
+
*/
|
529
|
+
mat3.str = function (mat) {
|
530
|
+
return '[' + mat[0] + ', ' + mat[1] + ', ' + mat[2] +
|
531
|
+
', ' + mat[3] + ', ' + mat[4] + ', ' + mat[5] +
|
532
|
+
', ' + mat[6] + ', ' + mat[7] + ', ' + mat[8] + ']';
|
533
|
+
};
|
534
|
+
|
535
|
+
/*
|
536
|
+
* mat4 - 4x4 Matrix
|
537
|
+
*/
|
538
|
+
|
539
|
+
/*
|
540
|
+
* mat4.create
|
541
|
+
* Creates a new instance of a mat4 using the default array type
|
542
|
+
* Any javascript array containing at least 16 numeric elements can serve as a mat4
|
543
|
+
*
|
544
|
+
* Params:
|
545
|
+
* mat - Optional, mat4 containing values to initialize with
|
546
|
+
*
|
547
|
+
* Returns:
|
548
|
+
* New mat4
|
549
|
+
*/
|
550
|
+
mat4.create = function (mat) {
|
551
|
+
var dest = new MatrixArray(16);
|
552
|
+
|
553
|
+
if (mat) {
|
554
|
+
dest[0] = mat[0];
|
555
|
+
dest[1] = mat[1];
|
556
|
+
dest[2] = mat[2];
|
557
|
+
dest[3] = mat[3];
|
558
|
+
dest[4] = mat[4];
|
559
|
+
dest[5] = mat[5];
|
560
|
+
dest[6] = mat[6];
|
561
|
+
dest[7] = mat[7];
|
562
|
+
dest[8] = mat[8];
|
563
|
+
dest[9] = mat[9];
|
564
|
+
dest[10] = mat[10];
|
565
|
+
dest[11] = mat[11];
|
566
|
+
dest[12] = mat[12];
|
567
|
+
dest[13] = mat[13];
|
568
|
+
dest[14] = mat[14];
|
569
|
+
dest[15] = mat[15];
|
570
|
+
}
|
571
|
+
|
572
|
+
return dest;
|
573
|
+
};
|
574
|
+
|
575
|
+
/*
|
576
|
+
* mat4.set
|
577
|
+
* Copies the values of one mat4 to another
|
578
|
+
*
|
579
|
+
* Params:
|
580
|
+
* mat - mat4 containing values to copy
|
581
|
+
* dest - mat4 receiving copied values
|
582
|
+
*
|
583
|
+
* Returns:
|
584
|
+
* dest
|
585
|
+
*/
|
586
|
+
mat4.set = function (mat, dest) {
|
587
|
+
dest[0] = mat[0];
|
588
|
+
dest[1] = mat[1];
|
589
|
+
dest[2] = mat[2];
|
590
|
+
dest[3] = mat[3];
|
591
|
+
dest[4] = mat[4];
|
592
|
+
dest[5] = mat[5];
|
593
|
+
dest[6] = mat[6];
|
594
|
+
dest[7] = mat[7];
|
595
|
+
dest[8] = mat[8];
|
596
|
+
dest[9] = mat[9];
|
597
|
+
dest[10] = mat[10];
|
598
|
+
dest[11] = mat[11];
|
599
|
+
dest[12] = mat[12];
|
600
|
+
dest[13] = mat[13];
|
601
|
+
dest[14] = mat[14];
|
602
|
+
dest[15] = mat[15];
|
603
|
+
return dest;
|
604
|
+
};
|
605
|
+
|
606
|
+
/*
|
607
|
+
* mat4.identity
|
608
|
+
* Sets a mat4 to an identity matrix
|
609
|
+
*
|
610
|
+
* Params:
|
611
|
+
* dest - mat4 to set
|
612
|
+
*
|
613
|
+
* Returns:
|
614
|
+
* dest
|
615
|
+
*/
|
616
|
+
mat4.identity = function (dest) {
|
617
|
+
if (!dest) { dest = mat4.create(); }
|
618
|
+
dest[0] = 1;
|
619
|
+
dest[1] = 0;
|
620
|
+
dest[2] = 0;
|
621
|
+
dest[3] = 0;
|
622
|
+
dest[4] = 0;
|
623
|
+
dest[5] = 1;
|
624
|
+
dest[6] = 0;
|
625
|
+
dest[7] = 0;
|
626
|
+
dest[8] = 0;
|
627
|
+
dest[9] = 0;
|
628
|
+
dest[10] = 1;
|
629
|
+
dest[11] = 0;
|
630
|
+
dest[12] = 0;
|
631
|
+
dest[13] = 0;
|
632
|
+
dest[14] = 0;
|
633
|
+
dest[15] = 1;
|
634
|
+
return dest;
|
635
|
+
};
|
636
|
+
|
637
|
+
/*
|
638
|
+
* mat4.transpose
|
639
|
+
* Transposes a mat4 (flips the values over the diagonal)
|
640
|
+
*
|
641
|
+
* Params:
|
642
|
+
* mat - mat4 to transpose
|
643
|
+
* dest - Optional, mat4 receiving transposed values. If not specified result is written to mat
|
644
|
+
*
|
645
|
+
* Returns:
|
646
|
+
* dest is specified, mat otherwise
|
647
|
+
*/
|
648
|
+
mat4.transpose = function (mat, dest) {
|
649
|
+
// If we are transposing ourselves we can skip a few steps but have to cache some values
|
650
|
+
if (!dest || mat === dest) {
|
651
|
+
var a01 = mat[1], a02 = mat[2], a03 = mat[3],
|
652
|
+
a12 = mat[6], a13 = mat[7],
|
653
|
+
a23 = mat[11];
|
654
|
+
|
655
|
+
mat[1] = mat[4];
|
656
|
+
mat[2] = mat[8];
|
657
|
+
mat[3] = mat[12];
|
658
|
+
mat[4] = a01;
|
659
|
+
mat[6] = mat[9];
|
660
|
+
mat[7] = mat[13];
|
661
|
+
mat[8] = a02;
|
662
|
+
mat[9] = a12;
|
663
|
+
mat[11] = mat[14];
|
664
|
+
mat[12] = a03;
|
665
|
+
mat[13] = a13;
|
666
|
+
mat[14] = a23;
|
667
|
+
return mat;
|
668
|
+
}
|
669
|
+
|
670
|
+
dest[0] = mat[0];
|
671
|
+
dest[1] = mat[4];
|
672
|
+
dest[2] = mat[8];
|
673
|
+
dest[3] = mat[12];
|
674
|
+
dest[4] = mat[1];
|
675
|
+
dest[5] = mat[5];
|
676
|
+
dest[6] = mat[9];
|
677
|
+
dest[7] = mat[13];
|
678
|
+
dest[8] = mat[2];
|
679
|
+
dest[9] = mat[6];
|
680
|
+
dest[10] = mat[10];
|
681
|
+
dest[11] = mat[14];
|
682
|
+
dest[12] = mat[3];
|
683
|
+
dest[13] = mat[7];
|
684
|
+
dest[14] = mat[11];
|
685
|
+
dest[15] = mat[15];
|
686
|
+
return dest;
|
687
|
+
};
|
688
|
+
|
689
|
+
/*
|
690
|
+
* mat4.determinant
|
691
|
+
* Calculates the determinant of a mat4
|
692
|
+
*
|
693
|
+
* Params:
|
694
|
+
* mat - mat4 to calculate determinant of
|
695
|
+
*
|
696
|
+
* Returns:
|
697
|
+
* determinant of mat
|
698
|
+
*/
|
699
|
+
mat4.determinant = function (mat) {
|
700
|
+
// Cache the matrix values (makes for huge speed increases!)
|
701
|
+
var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3],
|
702
|
+
a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7],
|
703
|
+
a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11],
|
704
|
+
a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15];
|
705
|
+
|
706
|
+
return (a30 * a21 * a12 * a03 - a20 * a31 * a12 * a03 - a30 * a11 * a22 * a03 + a10 * a31 * a22 * a03 +
|
707
|
+
a20 * a11 * a32 * a03 - a10 * a21 * a32 * a03 - a30 * a21 * a02 * a13 + a20 * a31 * a02 * a13 +
|
708
|
+
a30 * a01 * a22 * a13 - a00 * a31 * a22 * a13 - a20 * a01 * a32 * a13 + a00 * a21 * a32 * a13 +
|
709
|
+
a30 * a11 * a02 * a23 - a10 * a31 * a02 * a23 - a30 * a01 * a12 * a23 + a00 * a31 * a12 * a23 +
|
710
|
+
a10 * a01 * a32 * a23 - a00 * a11 * a32 * a23 - a20 * a11 * a02 * a33 + a10 * a21 * a02 * a33 +
|
711
|
+
a20 * a01 * a12 * a33 - a00 * a21 * a12 * a33 - a10 * a01 * a22 * a33 + a00 * a11 * a22 * a33);
|
712
|
+
};
|
713
|
+
|
714
|
+
/*
|
715
|
+
* mat4.inverse
|
716
|
+
* Calculates the inverse matrix of a mat4
|
717
|
+
*
|
718
|
+
* Params:
|
719
|
+
* mat - mat4 to calculate inverse of
|
720
|
+
* dest - Optional, mat4 receiving inverse matrix. If not specified result is written to mat
|
721
|
+
*
|
722
|
+
* Returns:
|
723
|
+
* dest is specified, mat otherwise, null if matrix cannot be inverted
|
724
|
+
*/
|
725
|
+
mat4.inverse = function (mat, dest) {
|
726
|
+
if (!dest) { dest = mat; }
|
727
|
+
|
728
|
+
// Cache the matrix values (makes for huge speed increases!)
|
729
|
+
var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3],
|
730
|
+
a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7],
|
731
|
+
a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11],
|
732
|
+
a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15],
|
733
|
+
|
734
|
+
b00 = a00 * a11 - a01 * a10,
|
735
|
+
b01 = a00 * a12 - a02 * a10,
|
736
|
+
b02 = a00 * a13 - a03 * a10,
|
737
|
+
b03 = a01 * a12 - a02 * a11,
|
738
|
+
b04 = a01 * a13 - a03 * a11,
|
739
|
+
b05 = a02 * a13 - a03 * a12,
|
740
|
+
b06 = a20 * a31 - a21 * a30,
|
741
|
+
b07 = a20 * a32 - a22 * a30,
|
742
|
+
b08 = a20 * a33 - a23 * a30,
|
743
|
+
b09 = a21 * a32 - a22 * a31,
|
744
|
+
b10 = a21 * a33 - a23 * a31,
|
745
|
+
b11 = a22 * a33 - a23 * a32,
|
746
|
+
|
747
|
+
d = (b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06),
|
748
|
+
invDet;
|
749
|
+
|
750
|
+
// Calculate the determinant
|
751
|
+
if (!d) { return null; }
|
752
|
+
invDet = 1 / d;
|
753
|
+
|
754
|
+
dest[0] = (a11 * b11 - a12 * b10 + a13 * b09) * invDet;
|
755
|
+
dest[1] = (-a01 * b11 + a02 * b10 - a03 * b09) * invDet;
|
756
|
+
dest[2] = (a31 * b05 - a32 * b04 + a33 * b03) * invDet;
|
757
|
+
dest[3] = (-a21 * b05 + a22 * b04 - a23 * b03) * invDet;
|
758
|
+
dest[4] = (-a10 * b11 + a12 * b08 - a13 * b07) * invDet;
|
759
|
+
dest[5] = (a00 * b11 - a02 * b08 + a03 * b07) * invDet;
|
760
|
+
dest[6] = (-a30 * b05 + a32 * b02 - a33 * b01) * invDet;
|
761
|
+
dest[7] = (a20 * b05 - a22 * b02 + a23 * b01) * invDet;
|
762
|
+
dest[8] = (a10 * b10 - a11 * b08 + a13 * b06) * invDet;
|
763
|
+
dest[9] = (-a00 * b10 + a01 * b08 - a03 * b06) * invDet;
|
764
|
+
dest[10] = (a30 * b04 - a31 * b02 + a33 * b00) * invDet;
|
765
|
+
dest[11] = (-a20 * b04 + a21 * b02 - a23 * b00) * invDet;
|
766
|
+
dest[12] = (-a10 * b09 + a11 * b07 - a12 * b06) * invDet;
|
767
|
+
dest[13] = (a00 * b09 - a01 * b07 + a02 * b06) * invDet;
|
768
|
+
dest[14] = (-a30 * b03 + a31 * b01 - a32 * b00) * invDet;
|
769
|
+
dest[15] = (a20 * b03 - a21 * b01 + a22 * b00) * invDet;
|
770
|
+
|
771
|
+
return dest;
|
772
|
+
};
|
773
|
+
|
774
|
+
/*
|
775
|
+
* mat4.toRotationMat
|
776
|
+
* Copies the upper 3x3 elements of a mat4 into another mat4
|
777
|
+
*
|
778
|
+
* Params:
|
779
|
+
* mat - mat4 containing values to copy
|
780
|
+
* dest - Optional, mat4 receiving copied values
|
781
|
+
*
|
782
|
+
* Returns:
|
783
|
+
* dest is specified, a new mat4 otherwise
|
784
|
+
*/
|
785
|
+
mat4.toRotationMat = function (mat, dest) {
|
786
|
+
if (!dest) { dest = mat4.create(); }
|
787
|
+
|
788
|
+
dest[0] = mat[0];
|
789
|
+
dest[1] = mat[1];
|
790
|
+
dest[2] = mat[2];
|
791
|
+
dest[3] = mat[3];
|
792
|
+
dest[4] = mat[4];
|
793
|
+
dest[5] = mat[5];
|
794
|
+
dest[6] = mat[6];
|
795
|
+
dest[7] = mat[7];
|
796
|
+
dest[8] = mat[8];
|
797
|
+
dest[9] = mat[9];
|
798
|
+
dest[10] = mat[10];
|
799
|
+
dest[11] = mat[11];
|
800
|
+
dest[12] = 0;
|
801
|
+
dest[13] = 0;
|
802
|
+
dest[14] = 0;
|
803
|
+
dest[15] = 1;
|
804
|
+
|
805
|
+
return dest;
|
806
|
+
};
|
807
|
+
|
808
|
+
/*
|
809
|
+
* mat4.toMat3
|
810
|
+
* Copies the upper 3x3 elements of a mat4 into a mat3
|
811
|
+
*
|
812
|
+
* Params:
|
813
|
+
* mat - mat4 containing values to copy
|
814
|
+
* dest - Optional, mat3 receiving copied values
|
815
|
+
*
|
816
|
+
* Returns:
|
817
|
+
* dest is specified, a new mat3 otherwise
|
818
|
+
*/
|
819
|
+
mat4.toMat3 = function (mat, dest) {
|
820
|
+
if (!dest) { dest = mat3.create(); }
|
821
|
+
|
822
|
+
dest[0] = mat[0];
|
823
|
+
dest[1] = mat[1];
|
824
|
+
dest[2] = mat[2];
|
825
|
+
dest[3] = mat[4];
|
826
|
+
dest[4] = mat[5];
|
827
|
+
dest[5] = mat[6];
|
828
|
+
dest[6] = mat[8];
|
829
|
+
dest[7] = mat[9];
|
830
|
+
dest[8] = mat[10];
|
831
|
+
|
832
|
+
return dest;
|
833
|
+
};
|
834
|
+
|
835
|
+
/*
|
836
|
+
* mat4.toInverseMat3
|
837
|
+
* Calculates the inverse of the upper 3x3 elements of a mat4 and copies the result into a mat3
|
838
|
+
* The resulting matrix is useful for calculating transformed normals
|
839
|
+
*
|
840
|
+
* Params:
|
841
|
+
* mat - mat4 containing values to invert and copy
|
842
|
+
* dest - Optional, mat3 receiving values
|
843
|
+
*
|
844
|
+
* Returns:
|
845
|
+
* dest is specified, a new mat3 otherwise, null if the matrix cannot be inverted
|
846
|
+
*/
|
847
|
+
mat4.toInverseMat3 = function (mat, dest) {
|
848
|
+
// Cache the matrix values (makes for huge speed increases!)
|
849
|
+
var a00 = mat[0], a01 = mat[1], a02 = mat[2],
|
850
|
+
a10 = mat[4], a11 = mat[5], a12 = mat[6],
|
851
|
+
a20 = mat[8], a21 = mat[9], a22 = mat[10],
|
852
|
+
|
853
|
+
b01 = a22 * a11 - a12 * a21,
|
854
|
+
b11 = -a22 * a10 + a12 * a20,
|
855
|
+
b21 = a21 * a10 - a11 * a20,
|
856
|
+
|
857
|
+
d = a00 * b01 + a01 * b11 + a02 * b21,
|
858
|
+
id;
|
859
|
+
|
860
|
+
if (!d) { return null; }
|
861
|
+
id = 1 / d;
|
862
|
+
|
863
|
+
if (!dest) { dest = mat3.create(); }
|
864
|
+
|
865
|
+
dest[0] = b01 * id;
|
866
|
+
dest[1] = (-a22 * a01 + a02 * a21) * id;
|
867
|
+
dest[2] = (a12 * a01 - a02 * a11) * id;
|
868
|
+
dest[3] = b11 * id;
|
869
|
+
dest[4] = (a22 * a00 - a02 * a20) * id;
|
870
|
+
dest[5] = (-a12 * a00 + a02 * a10) * id;
|
871
|
+
dest[6] = b21 * id;
|
872
|
+
dest[7] = (-a21 * a00 + a01 * a20) * id;
|
873
|
+
dest[8] = (a11 * a00 - a01 * a10) * id;
|
874
|
+
|
875
|
+
return dest;
|
876
|
+
};
|
877
|
+
|
878
|
+
/*
|
879
|
+
* mat4.multiply
|
880
|
+
* Performs a matrix multiplication
|
881
|
+
*
|
882
|
+
* Params:
|
883
|
+
* mat - mat4, first operand
|
884
|
+
* mat2 - mat4, second operand
|
885
|
+
* dest - Optional, mat4 receiving operation result. If not specified result is written to mat
|
886
|
+
*
|
887
|
+
* Returns:
|
888
|
+
* dest if specified, mat otherwise
|
889
|
+
*/
|
890
|
+
mat4.multiply = function (mat, mat2, dest) {
|
891
|
+
if (!dest) { dest = mat; }
|
892
|
+
|
893
|
+
// Cache the matrix values (makes for huge speed increases!)
|
894
|
+
var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3],
|
895
|
+
a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7],
|
896
|
+
a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11],
|
897
|
+
a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15],
|
898
|
+
|
899
|
+
b00 = mat2[0], b01 = mat2[1], b02 = mat2[2], b03 = mat2[3],
|
900
|
+
b10 = mat2[4], b11 = mat2[5], b12 = mat2[6], b13 = mat2[7],
|
901
|
+
b20 = mat2[8], b21 = mat2[9], b22 = mat2[10], b23 = mat2[11],
|
902
|
+
b30 = mat2[12], b31 = mat2[13], b32 = mat2[14], b33 = mat2[15];
|
903
|
+
|
904
|
+
dest[0] = b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30;
|
905
|
+
dest[1] = b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31;
|
906
|
+
dest[2] = b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32;
|
907
|
+
dest[3] = b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33;
|
908
|
+
dest[4] = b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30;
|
909
|
+
dest[5] = b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31;
|
910
|
+
dest[6] = b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32;
|
911
|
+
dest[7] = b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33;
|
912
|
+
dest[8] = b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30;
|
913
|
+
dest[9] = b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31;
|
914
|
+
dest[10] = b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32;
|
915
|
+
dest[11] = b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33;
|
916
|
+
dest[12] = b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30;
|
917
|
+
dest[13] = b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31;
|
918
|
+
dest[14] = b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32;
|
919
|
+
dest[15] = b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33;
|
920
|
+
|
921
|
+
return dest;
|
922
|
+
};
|
923
|
+
|
924
|
+
/*
|
925
|
+
* mat4.multiplyVec3
|
926
|
+
* Transforms a vec3 with the given matrix
|
927
|
+
* 4th vector component is implicitly '1'
|
928
|
+
*
|
929
|
+
* Params:
|
930
|
+
* mat - mat4 to transform the vector with
|
931
|
+
* vec - vec3 to transform
|
932
|
+
* dest - Optional, vec3 receiving operation result. If not specified result is written to vec
|
933
|
+
*
|
934
|
+
* Returns:
|
935
|
+
* dest if specified, vec otherwise
|
936
|
+
*/
|
937
|
+
mat4.multiplyVec3 = function (mat, vec, dest) {
|
938
|
+
if (!dest) { dest = vec; }
|
939
|
+
|
940
|
+
var x = vec[0], y = vec[1], z = vec[2];
|
941
|
+
|
942
|
+
dest[0] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12];
|
943
|
+
dest[1] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13];
|
944
|
+
dest[2] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14];
|
945
|
+
|
946
|
+
return dest;
|
947
|
+
};
|
948
|
+
|
949
|
+
/*
|
950
|
+
* mat4.multiplyVec4
|
951
|
+
* Transforms a vec4 with the given matrix
|
952
|
+
*
|
953
|
+
* Params:
|
954
|
+
* mat - mat4 to transform the vector with
|
955
|
+
* vec - vec4 to transform
|
956
|
+
* dest - Optional, vec4 receiving operation result. If not specified result is written to vec
|
957
|
+
*
|
958
|
+
* Returns:
|
959
|
+
* dest if specified, vec otherwise
|
960
|
+
*/
|
961
|
+
mat4.multiplyVec4 = function (mat, vec, dest) {
|
962
|
+
if (!dest) { dest = vec; }
|
963
|
+
|
964
|
+
var x = vec[0], y = vec[1], z = vec[2], w = vec[3];
|
965
|
+
|
966
|
+
dest[0] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12] * w;
|
967
|
+
dest[1] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13] * w;
|
968
|
+
dest[2] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14] * w;
|
969
|
+
dest[3] = mat[3] * x + mat[7] * y + mat[11] * z + mat[15] * w;
|
970
|
+
|
971
|
+
return dest;
|
972
|
+
};
|
973
|
+
|
974
|
+
/*
|
975
|
+
* mat4.translate
|
976
|
+
* Translates a matrix by the given vector
|
977
|
+
*
|
978
|
+
* Params:
|
979
|
+
* mat - mat4 to translate
|
980
|
+
* vec - vec3 specifying the translation
|
981
|
+
* dest - Optional, mat4 receiving operation result. If not specified result is written to mat
|
982
|
+
*
|
983
|
+
* Returns:
|
984
|
+
* dest if specified, mat otherwise
|
985
|
+
*/
|
986
|
+
mat4.translate = function (mat, vec, dest) {
|
987
|
+
var x = vec[0], y = vec[1], z = vec[2],
|
988
|
+
a00, a01, a02, a03,
|
989
|
+
a10, a11, a12, a13,
|
990
|
+
a20, a21, a22, a23;
|
991
|
+
|
992
|
+
if (!dest || mat === dest) {
|
993
|
+
mat[12] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12];
|
994
|
+
mat[13] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13];
|
995
|
+
mat[14] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14];
|
996
|
+
mat[15] = mat[3] * x + mat[7] * y + mat[11] * z + mat[15];
|
997
|
+
return mat;
|
998
|
+
}
|
999
|
+
|
1000
|
+
a00 = mat[0]; a01 = mat[1]; a02 = mat[2]; a03 = mat[3];
|
1001
|
+
a10 = mat[4]; a11 = mat[5]; a12 = mat[6]; a13 = mat[7];
|
1002
|
+
a20 = mat[8]; a21 = mat[9]; a22 = mat[10]; a23 = mat[11];
|
1003
|
+
|
1004
|
+
dest[0] = a00; dest[1] = a01; dest[2] = a02; dest[3] = a03;
|
1005
|
+
dest[4] = a10; dest[5] = a11; dest[6] = a12; dest[7] = a13;
|
1006
|
+
dest[8] = a20; dest[9] = a21; dest[10] = a22; dest[11] = a23;
|
1007
|
+
|
1008
|
+
dest[12] = a00 * x + a10 * y + a20 * z + mat[12];
|
1009
|
+
dest[13] = a01 * x + a11 * y + a21 * z + mat[13];
|
1010
|
+
dest[14] = a02 * x + a12 * y + a22 * z + mat[14];
|
1011
|
+
dest[15] = a03 * x + a13 * y + a23 * z + mat[15];
|
1012
|
+
return dest;
|
1013
|
+
};
|
1014
|
+
|
1015
|
+
/*
|
1016
|
+
* mat4.scale
|
1017
|
+
* Scales a matrix by the given vector
|
1018
|
+
*
|
1019
|
+
* Params:
|
1020
|
+
* mat - mat4 to scale
|
1021
|
+
* vec - vec3 specifying the scale for each axis
|
1022
|
+
* dest - Optional, mat4 receiving operation result. If not specified result is written to mat
|
1023
|
+
*
|
1024
|
+
* Returns:
|
1025
|
+
* dest if specified, mat otherwise
|
1026
|
+
*/
|
1027
|
+
mat4.scale = function (mat, vec, dest) {
|
1028
|
+
var x = vec[0], y = vec[1], z = vec[2];
|
1029
|
+
|
1030
|
+
if (!dest || mat === dest) {
|
1031
|
+
mat[0] *= x;
|
1032
|
+
mat[1] *= x;
|
1033
|
+
mat[2] *= x;
|
1034
|
+
mat[3] *= x;
|
1035
|
+
mat[4] *= y;
|
1036
|
+
mat[5] *= y;
|
1037
|
+
mat[6] *= y;
|
1038
|
+
mat[7] *= y;
|
1039
|
+
mat[8] *= z;
|
1040
|
+
mat[9] *= z;
|
1041
|
+
mat[10] *= z;
|
1042
|
+
mat[11] *= z;
|
1043
|
+
return mat;
|
1044
|
+
}
|
1045
|
+
|
1046
|
+
dest[0] = mat[0] * x;
|
1047
|
+
dest[1] = mat[1] * x;
|
1048
|
+
dest[2] = mat[2] * x;
|
1049
|
+
dest[3] = mat[3] * x;
|
1050
|
+
dest[4] = mat[4] * y;
|
1051
|
+
dest[5] = mat[5] * y;
|
1052
|
+
dest[6] = mat[6] * y;
|
1053
|
+
dest[7] = mat[7] * y;
|
1054
|
+
dest[8] = mat[8] * z;
|
1055
|
+
dest[9] = mat[9] * z;
|
1056
|
+
dest[10] = mat[10] * z;
|
1057
|
+
dest[11] = mat[11] * z;
|
1058
|
+
dest[12] = mat[12];
|
1059
|
+
dest[13] = mat[13];
|
1060
|
+
dest[14] = mat[14];
|
1061
|
+
dest[15] = mat[15];
|
1062
|
+
return dest;
|
1063
|
+
};
|
1064
|
+
|
1065
|
+
/*
|
1066
|
+
* mat4.rotate
|
1067
|
+
* Rotates a matrix by the given angle around the specified axis
|
1068
|
+
* If rotating around a primary axis (X,Y,Z) one of the specialized rotation functions should be used instead for performance
|
1069
|
+
*
|
1070
|
+
* Params:
|
1071
|
+
* mat - mat4 to rotate
|
1072
|
+
* angle - angle (in radians) to rotate
|
1073
|
+
* axis - vec3 representing the axis to rotate around
|
1074
|
+
* dest - Optional, mat4 receiving operation result. If not specified result is written to mat
|
1075
|
+
*
|
1076
|
+
* Returns:
|
1077
|
+
* dest if specified, mat otherwise
|
1078
|
+
*/
|
1079
|
+
mat4.rotate = function (mat, angle, axis, dest) {
|
1080
|
+
var x = axis[0], y = axis[1], z = axis[2],
|
1081
|
+
len = Math.sqrt(x * x + y * y + z * z),
|
1082
|
+
s, c, t,
|
1083
|
+
a00, a01, a02, a03,
|
1084
|
+
a10, a11, a12, a13,
|
1085
|
+
a20, a21, a22, a23,
|
1086
|
+
b00, b01, b02,
|
1087
|
+
b10, b11, b12,
|
1088
|
+
b20, b21, b22;
|
1089
|
+
|
1090
|
+
if (!len) { return null; }
|
1091
|
+
if (len !== 1) {
|
1092
|
+
len = 1 / len;
|
1093
|
+
x *= len;
|
1094
|
+
y *= len;
|
1095
|
+
z *= len;
|
1096
|
+
}
|
1097
|
+
|
1098
|
+
s = Math.sin(angle);
|
1099
|
+
c = Math.cos(angle);
|
1100
|
+
t = 1 - c;
|
1101
|
+
|
1102
|
+
a00 = mat[0]; a01 = mat[1]; a02 = mat[2]; a03 = mat[3];
|
1103
|
+
a10 = mat[4]; a11 = mat[5]; a12 = mat[6]; a13 = mat[7];
|
1104
|
+
a20 = mat[8]; a21 = mat[9]; a22 = mat[10]; a23 = mat[11];
|
1105
|
+
|
1106
|
+
// Construct the elements of the rotation matrix
|
1107
|
+
b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s;
|
1108
|
+
b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s;
|
1109
|
+
b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c;
|
1110
|
+
|
1111
|
+
if (!dest) {
|
1112
|
+
dest = mat;
|
1113
|
+
} else if (mat !== dest) { // If the source and destination differ, copy the unchanged last row
|
1114
|
+
dest[12] = mat[12];
|
1115
|
+
dest[13] = mat[13];
|
1116
|
+
dest[14] = mat[14];
|
1117
|
+
dest[15] = mat[15];
|
1118
|
+
}
|
1119
|
+
|
1120
|
+
// Perform rotation-specific matrix multiplication
|
1121
|
+
dest[0] = a00 * b00 + a10 * b01 + a20 * b02;
|
1122
|
+
dest[1] = a01 * b00 + a11 * b01 + a21 * b02;
|
1123
|
+
dest[2] = a02 * b00 + a12 * b01 + a22 * b02;
|
1124
|
+
dest[3] = a03 * b00 + a13 * b01 + a23 * b02;
|
1125
|
+
|
1126
|
+
dest[4] = a00 * b10 + a10 * b11 + a20 * b12;
|
1127
|
+
dest[5] = a01 * b10 + a11 * b11 + a21 * b12;
|
1128
|
+
dest[6] = a02 * b10 + a12 * b11 + a22 * b12;
|
1129
|
+
dest[7] = a03 * b10 + a13 * b11 + a23 * b12;
|
1130
|
+
|
1131
|
+
dest[8] = a00 * b20 + a10 * b21 + a20 * b22;
|
1132
|
+
dest[9] = a01 * b20 + a11 * b21 + a21 * b22;
|
1133
|
+
dest[10] = a02 * b20 + a12 * b21 + a22 * b22;
|
1134
|
+
dest[11] = a03 * b20 + a13 * b21 + a23 * b22;
|
1135
|
+
return dest;
|
1136
|
+
};
|
1137
|
+
|
1138
|
+
/*
|
1139
|
+
* mat4.rotateX
|
1140
|
+
* Rotates a matrix by the given angle around the X axis
|
1141
|
+
*
|
1142
|
+
* Params:
|
1143
|
+
* mat - mat4 to rotate
|
1144
|
+
* angle - angle (in radians) to rotate
|
1145
|
+
* dest - Optional, mat4 receiving operation result. If not specified result is written to mat
|
1146
|
+
*
|
1147
|
+
* Returns:
|
1148
|
+
* dest if specified, mat otherwise
|
1149
|
+
*/
|
1150
|
+
mat4.rotateX = function (mat, angle, dest) {
|
1151
|
+
var s = Math.sin(angle),
|
1152
|
+
c = Math.cos(angle),
|
1153
|
+
a10 = mat[4],
|
1154
|
+
a11 = mat[5],
|
1155
|
+
a12 = mat[6],
|
1156
|
+
a13 = mat[7],
|
1157
|
+
a20 = mat[8],
|
1158
|
+
a21 = mat[9],
|
1159
|
+
a22 = mat[10],
|
1160
|
+
a23 = mat[11];
|
1161
|
+
|
1162
|
+
if (!dest) {
|
1163
|
+
dest = mat;
|
1164
|
+
} else if (mat !== dest) { // If the source and destination differ, copy the unchanged rows
|
1165
|
+
dest[0] = mat[0];
|
1166
|
+
dest[1] = mat[1];
|
1167
|
+
dest[2] = mat[2];
|
1168
|
+
dest[3] = mat[3];
|
1169
|
+
|
1170
|
+
dest[12] = mat[12];
|
1171
|
+
dest[13] = mat[13];
|
1172
|
+
dest[14] = mat[14];
|
1173
|
+
dest[15] = mat[15];
|
1174
|
+
}
|
1175
|
+
|
1176
|
+
// Perform axis-specific matrix multiplication
|
1177
|
+
dest[4] = a10 * c + a20 * s;
|
1178
|
+
dest[5] = a11 * c + a21 * s;
|
1179
|
+
dest[6] = a12 * c + a22 * s;
|
1180
|
+
dest[7] = a13 * c + a23 * s;
|
1181
|
+
|
1182
|
+
dest[8] = a10 * -s + a20 * c;
|
1183
|
+
dest[9] = a11 * -s + a21 * c;
|
1184
|
+
dest[10] = a12 * -s + a22 * c;
|
1185
|
+
dest[11] = a13 * -s + a23 * c;
|
1186
|
+
return dest;
|
1187
|
+
};
|
1188
|
+
|
1189
|
+
/*
|
1190
|
+
* mat4.rotateY
|
1191
|
+
* Rotates a matrix by the given angle around the Y axis
|
1192
|
+
*
|
1193
|
+
* Params:
|
1194
|
+
* mat - mat4 to rotate
|
1195
|
+
* angle - angle (in radians) to rotate
|
1196
|
+
* dest - Optional, mat4 receiving operation result. If not specified result is written to mat
|
1197
|
+
*
|
1198
|
+
* Returns:
|
1199
|
+
* dest if specified, mat otherwise
|
1200
|
+
*/
|
1201
|
+
mat4.rotateY = function (mat, angle, dest) {
|
1202
|
+
var s = Math.sin(angle),
|
1203
|
+
c = Math.cos(angle),
|
1204
|
+
a00 = mat[0],
|
1205
|
+
a01 = mat[1],
|
1206
|
+
a02 = mat[2],
|
1207
|
+
a03 = mat[3],
|
1208
|
+
a20 = mat[8],
|
1209
|
+
a21 = mat[9],
|
1210
|
+
a22 = mat[10],
|
1211
|
+
a23 = mat[11];
|
1212
|
+
|
1213
|
+
if (!dest) {
|
1214
|
+
dest = mat;
|
1215
|
+
} else if (mat !== dest) { // If the source and destination differ, copy the unchanged rows
|
1216
|
+
dest[4] = mat[4];
|
1217
|
+
dest[5] = mat[5];
|
1218
|
+
dest[6] = mat[6];
|
1219
|
+
dest[7] = mat[7];
|
1220
|
+
|
1221
|
+
dest[12] = mat[12];
|
1222
|
+
dest[13] = mat[13];
|
1223
|
+
dest[14] = mat[14];
|
1224
|
+
dest[15] = mat[15];
|
1225
|
+
}
|
1226
|
+
|
1227
|
+
// Perform axis-specific matrix multiplication
|
1228
|
+
dest[0] = a00 * c + a20 * -s;
|
1229
|
+
dest[1] = a01 * c + a21 * -s;
|
1230
|
+
dest[2] = a02 * c + a22 * -s;
|
1231
|
+
dest[3] = a03 * c + a23 * -s;
|
1232
|
+
|
1233
|
+
dest[8] = a00 * s + a20 * c;
|
1234
|
+
dest[9] = a01 * s + a21 * c;
|
1235
|
+
dest[10] = a02 * s + a22 * c;
|
1236
|
+
dest[11] = a03 * s + a23 * c;
|
1237
|
+
return dest;
|
1238
|
+
};
|
1239
|
+
|
1240
|
+
/*
|
1241
|
+
* mat4.rotateZ
|
1242
|
+
* Rotates a matrix by the given angle around the Z axis
|
1243
|
+
*
|
1244
|
+
* Params:
|
1245
|
+
* mat - mat4 to rotate
|
1246
|
+
* angle - angle (in radians) to rotate
|
1247
|
+
* dest - Optional, mat4 receiving operation result. If not specified result is written to mat
|
1248
|
+
*
|
1249
|
+
* Returns:
|
1250
|
+
* dest if specified, mat otherwise
|
1251
|
+
*/
|
1252
|
+
mat4.rotateZ = function (mat, angle, dest) {
|
1253
|
+
var s = Math.sin(angle),
|
1254
|
+
c = Math.cos(angle),
|
1255
|
+
a00 = mat[0],
|
1256
|
+
a01 = mat[1],
|
1257
|
+
a02 = mat[2],
|
1258
|
+
a03 = mat[3],
|
1259
|
+
a10 = mat[4],
|
1260
|
+
a11 = mat[5],
|
1261
|
+
a12 = mat[6],
|
1262
|
+
a13 = mat[7];
|
1263
|
+
|
1264
|
+
if (!dest) {
|
1265
|
+
dest = mat;
|
1266
|
+
} else if (mat !== dest) { // If the source and destination differ, copy the unchanged last row
|
1267
|
+
dest[8] = mat[8];
|
1268
|
+
dest[9] = mat[9];
|
1269
|
+
dest[10] = mat[10];
|
1270
|
+
dest[11] = mat[11];
|
1271
|
+
|
1272
|
+
dest[12] = mat[12];
|
1273
|
+
dest[13] = mat[13];
|
1274
|
+
dest[14] = mat[14];
|
1275
|
+
dest[15] = mat[15];
|
1276
|
+
}
|
1277
|
+
|
1278
|
+
// Perform axis-specific matrix multiplication
|
1279
|
+
dest[0] = a00 * c + a10 * s;
|
1280
|
+
dest[1] = a01 * c + a11 * s;
|
1281
|
+
dest[2] = a02 * c + a12 * s;
|
1282
|
+
dest[3] = a03 * c + a13 * s;
|
1283
|
+
|
1284
|
+
dest[4] = a00 * -s + a10 * c;
|
1285
|
+
dest[5] = a01 * -s + a11 * c;
|
1286
|
+
dest[6] = a02 * -s + a12 * c;
|
1287
|
+
dest[7] = a03 * -s + a13 * c;
|
1288
|
+
|
1289
|
+
return dest;
|
1290
|
+
};
|
1291
|
+
|
1292
|
+
/*
|
1293
|
+
* mat4.frustum
|
1294
|
+
* Generates a frustum matrix with the given bounds
|
1295
|
+
*
|
1296
|
+
* Params:
|
1297
|
+
* left, right - scalar, left and right bounds of the frustum
|
1298
|
+
* bottom, top - scalar, bottom and top bounds of the frustum
|
1299
|
+
* near, far - scalar, near and far bounds of the frustum
|
1300
|
+
* dest - Optional, mat4 frustum matrix will be written into
|
1301
|
+
*
|
1302
|
+
* Returns:
|
1303
|
+
* dest if specified, a new mat4 otherwise
|
1304
|
+
*/
|
1305
|
+
mat4.frustum = function (left, right, bottom, top, near, far, dest) {
|
1306
|
+
if (!dest) { dest = mat4.create(); }
|
1307
|
+
var rl = (right - left),
|
1308
|
+
tb = (top - bottom),
|
1309
|
+
fn = (far - near);
|
1310
|
+
dest[0] = (near * 2) / rl;
|
1311
|
+
dest[1] = 0;
|
1312
|
+
dest[2] = 0;
|
1313
|
+
dest[3] = 0;
|
1314
|
+
dest[4] = 0;
|
1315
|
+
dest[5] = (near * 2) / tb;
|
1316
|
+
dest[6] = 0;
|
1317
|
+
dest[7] = 0;
|
1318
|
+
dest[8] = (right + left) / rl;
|
1319
|
+
dest[9] = (top + bottom) / tb;
|
1320
|
+
dest[10] = -(far + near) / fn;
|
1321
|
+
dest[11] = -1;
|
1322
|
+
dest[12] = 0;
|
1323
|
+
dest[13] = 0;
|
1324
|
+
dest[14] = -(far * near * 2) / fn;
|
1325
|
+
dest[15] = 0;
|
1326
|
+
return dest;
|
1327
|
+
};
|
1328
|
+
|
1329
|
+
/*
|
1330
|
+
* mat4.perspective
|
1331
|
+
* Generates a perspective projection matrix with the given bounds
|
1332
|
+
*
|
1333
|
+
* Params:
|
1334
|
+
* fovy - scalar, vertical field of view
|
1335
|
+
* aspect - scalar, aspect ratio. typically viewport width/height
|
1336
|
+
* near, far - scalar, near and far bounds of the frustum
|
1337
|
+
* dest - Optional, mat4 frustum matrix will be written into
|
1338
|
+
*
|
1339
|
+
* Returns:
|
1340
|
+
* dest if specified, a new mat4 otherwise
|
1341
|
+
*/
|
1342
|
+
mat4.perspective = function (fovy, aspect, near, far, dest) {
|
1343
|
+
var top = near * Math.tan(fovy * Math.PI / 360.0),
|
1344
|
+
right = top * aspect;
|
1345
|
+
return mat4.frustum(-right, right, -top, top, near, far, dest);
|
1346
|
+
};
|
1347
|
+
|
1348
|
+
/*
|
1349
|
+
* mat4.ortho
|
1350
|
+
* Generates a orthogonal projection matrix with the given bounds
|
1351
|
+
*
|
1352
|
+
* Params:
|
1353
|
+
* left, right - scalar, left and right bounds of the frustum
|
1354
|
+
* bottom, top - scalar, bottom and top bounds of the frustum
|
1355
|
+
* near, far - scalar, near and far bounds of the frustum
|
1356
|
+
* dest - Optional, mat4 frustum matrix will be written into
|
1357
|
+
*
|
1358
|
+
* Returns:
|
1359
|
+
* dest if specified, a new mat4 otherwise
|
1360
|
+
*/
|
1361
|
+
mat4.ortho = function (left, right, bottom, top, near, far, dest) {
|
1362
|
+
if (!dest) { dest = mat4.create(); }
|
1363
|
+
var rl = (right - left),
|
1364
|
+
tb = (top - bottom),
|
1365
|
+
fn = (far - near);
|
1366
|
+
dest[0] = 2 / rl;
|
1367
|
+
dest[1] = 0;
|
1368
|
+
dest[2] = 0;
|
1369
|
+
dest[3] = 0;
|
1370
|
+
dest[4] = 0;
|
1371
|
+
dest[5] = 2 / tb;
|
1372
|
+
dest[6] = 0;
|
1373
|
+
dest[7] = 0;
|
1374
|
+
dest[8] = 0;
|
1375
|
+
dest[9] = 0;
|
1376
|
+
dest[10] = -2 / fn;
|
1377
|
+
dest[11] = 0;
|
1378
|
+
dest[12] = -(left + right) / rl;
|
1379
|
+
dest[13] = -(top + bottom) / tb;
|
1380
|
+
dest[14] = -(far + near) / fn;
|
1381
|
+
dest[15] = 1;
|
1382
|
+
return dest;
|
1383
|
+
};
|
1384
|
+
|
1385
|
+
/*
|
1386
|
+
* mat4.lookAt
|
1387
|
+
* Generates a look-at matrix with the given eye position, focal point, and up axis
|
1388
|
+
*
|
1389
|
+
* Params:
|
1390
|
+
* eye - vec3, position of the viewer
|
1391
|
+
* center - vec3, point the viewer is looking at
|
1392
|
+
* up - vec3 pointing "up"
|
1393
|
+
* dest - Optional, mat4 frustum matrix will be written into
|
1394
|
+
*
|
1395
|
+
* Returns:
|
1396
|
+
* dest if specified, a new mat4 otherwise
|
1397
|
+
*/
|
1398
|
+
mat4.lookAt = function (eye, center, up, dest) {
|
1399
|
+
if (!dest) { dest = mat4.create(); }
|
1400
|
+
|
1401
|
+
var x0, x1, x2, y0, y1, y2, z0, z1, z2, len,
|
1402
|
+
eyex = eye[0],
|
1403
|
+
eyey = eye[1],
|
1404
|
+
eyez = eye[2],
|
1405
|
+
upx = up[0],
|
1406
|
+
upy = up[1],
|
1407
|
+
upz = up[2],
|
1408
|
+
centerx = center[0],
|
1409
|
+
centery = center[1],
|
1410
|
+
centerz = center[2];
|
1411
|
+
|
1412
|
+
if (eyex === centerx && eyey === centery && eyez === centerz) {
|
1413
|
+
return mat4.identity(dest);
|
1414
|
+
}
|
1415
|
+
|
1416
|
+
//vec3.direction(eye, center, z);
|
1417
|
+
z0 = eyex - center[0];
|
1418
|
+
z1 = eyey - center[1];
|
1419
|
+
z2 = eyez - center[2];
|
1420
|
+
|
1421
|
+
// normalize (no check needed for 0 because of early return)
|
1422
|
+
len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2);
|
1423
|
+
z0 *= len;
|
1424
|
+
z1 *= len;
|
1425
|
+
z2 *= len;
|
1426
|
+
|
1427
|
+
//vec3.normalize(vec3.cross(up, z, x));
|
1428
|
+
x0 = upy * z2 - upz * z1;
|
1429
|
+
x1 = upz * z0 - upx * z2;
|
1430
|
+
x2 = upx * z1 - upy * z0;
|
1431
|
+
len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2);
|
1432
|
+
if (!len) {
|
1433
|
+
x0 = 0;
|
1434
|
+
x1 = 0;
|
1435
|
+
x2 = 0;
|
1436
|
+
} else {
|
1437
|
+
len = 1 / len;
|
1438
|
+
x0 *= len;
|
1439
|
+
x1 *= len;
|
1440
|
+
x2 *= len;
|
1441
|
+
}
|
1442
|
+
|
1443
|
+
//vec3.normalize(vec3.cross(z, x, y));
|
1444
|
+
y0 = z1 * x2 - z2 * x1;
|
1445
|
+
y1 = z2 * x0 - z0 * x2;
|
1446
|
+
y2 = z0 * x1 - z1 * x0;
|
1447
|
+
|
1448
|
+
len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2);
|
1449
|
+
if (!len) {
|
1450
|
+
y0 = 0;
|
1451
|
+
y1 = 0;
|
1452
|
+
y2 = 0;
|
1453
|
+
} else {
|
1454
|
+
len = 1 / len;
|
1455
|
+
y0 *= len;
|
1456
|
+
y1 *= len;
|
1457
|
+
y2 *= len;
|
1458
|
+
}
|
1459
|
+
|
1460
|
+
dest[0] = x0;
|
1461
|
+
dest[1] = y0;
|
1462
|
+
dest[2] = z0;
|
1463
|
+
dest[3] = 0;
|
1464
|
+
dest[4] = x1;
|
1465
|
+
dest[5] = y1;
|
1466
|
+
dest[6] = z1;
|
1467
|
+
dest[7] = 0;
|
1468
|
+
dest[8] = x2;
|
1469
|
+
dest[9] = y2;
|
1470
|
+
dest[10] = z2;
|
1471
|
+
dest[11] = 0;
|
1472
|
+
dest[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
|
1473
|
+
dest[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
|
1474
|
+
dest[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
|
1475
|
+
dest[15] = 1;
|
1476
|
+
|
1477
|
+
return dest;
|
1478
|
+
};
|
1479
|
+
|
1480
|
+
/*
|
1481
|
+
* mat4.fromRotationTranslation
|
1482
|
+
* Creates a matrix from a quaternion rotation and vector translation
|
1483
|
+
* This is equivalent to (but much faster than):
|
1484
|
+
*
|
1485
|
+
* mat4.identity(dest);
|
1486
|
+
* mat4.translate(dest, vec);
|
1487
|
+
* var quatMat = mat4.create();
|
1488
|
+
* quat4.toMat4(quat, quatMat);
|
1489
|
+
* mat4.multiply(dest, quatMat);
|
1490
|
+
*
|
1491
|
+
* Params:
|
1492
|
+
* quat - quat4 specifying the rotation by
|
1493
|
+
* vec - vec3 specifying the translation
|
1494
|
+
* dest - Optional, mat4 receiving operation result. If not specified result is written to a new mat4
|
1495
|
+
*
|
1496
|
+
* Returns:
|
1497
|
+
* dest if specified, a new mat4 otherwise
|
1498
|
+
*/
|
1499
|
+
mat4.fromRotationTranslation = function (quat, vec, dest) {
|
1500
|
+
if (!dest) { dest = mat4.create(); }
|
1501
|
+
|
1502
|
+
// Quaternion math
|
1503
|
+
var x = quat[0], y = quat[1], z = quat[2], w = quat[3],
|
1504
|
+
x2 = x + x,
|
1505
|
+
y2 = y + y,
|
1506
|
+
z2 = z + z,
|
1507
|
+
|
1508
|
+
xx = x * x2,
|
1509
|
+
xy = x * y2,
|
1510
|
+
xz = x * z2,
|
1511
|
+
yy = y * y2,
|
1512
|
+
yz = y * z2,
|
1513
|
+
zz = z * z2,
|
1514
|
+
wx = w * x2,
|
1515
|
+
wy = w * y2,
|
1516
|
+
wz = w * z2;
|
1517
|
+
|
1518
|
+
dest[0] = 1 - (yy + zz);
|
1519
|
+
dest[1] = xy + wz;
|
1520
|
+
dest[2] = xz - wy;
|
1521
|
+
dest[3] = 0;
|
1522
|
+
dest[4] = xy - wz;
|
1523
|
+
dest[5] = 1 - (xx + zz);
|
1524
|
+
dest[6] = yz + wx;
|
1525
|
+
dest[7] = 0;
|
1526
|
+
dest[8] = xz + wy;
|
1527
|
+
dest[9] = yz - wx;
|
1528
|
+
dest[10] = 1 - (xx + yy);
|
1529
|
+
dest[11] = 0;
|
1530
|
+
dest[12] = vec[0];
|
1531
|
+
dest[13] = vec[1];
|
1532
|
+
dest[14] = vec[2];
|
1533
|
+
dest[15] = 1;
|
1534
|
+
|
1535
|
+
return dest;
|
1536
|
+
};
|
1537
|
+
|
1538
|
+
/*
|
1539
|
+
* mat4.str
|
1540
|
+
* Returns a string representation of a mat4
|
1541
|
+
*
|
1542
|
+
* Params:
|
1543
|
+
* mat - mat4 to represent as a string
|
1544
|
+
*
|
1545
|
+
* Returns:
|
1546
|
+
* string representation of mat
|
1547
|
+
*/
|
1548
|
+
mat4.str = function (mat) {
|
1549
|
+
return '[' + mat[0] + ', ' + mat[1] + ', ' + mat[2] + ', ' + mat[3] +
|
1550
|
+
', ' + mat[4] + ', ' + mat[5] + ', ' + mat[6] + ', ' + mat[7] +
|
1551
|
+
', ' + mat[8] + ', ' + mat[9] + ', ' + mat[10] + ', ' + mat[11] +
|
1552
|
+
', ' + mat[12] + ', ' + mat[13] + ', ' + mat[14] + ', ' + mat[15] + ']';
|
1553
|
+
};
|
1554
|
+
|
1555
|
+
/*
|
1556
|
+
* quat4 - Quaternions
|
1557
|
+
*/
|
1558
|
+
|
1559
|
+
/*
|
1560
|
+
* quat4.create
|
1561
|
+
* Creates a new instance of a quat4 using the default array type
|
1562
|
+
* Any javascript array containing at least 4 numeric elements can serve as a quat4
|
1563
|
+
*
|
1564
|
+
* Params:
|
1565
|
+
* quat - Optional, quat4 containing values to initialize with
|
1566
|
+
*
|
1567
|
+
* Returns:
|
1568
|
+
* New quat4
|
1569
|
+
*/
|
1570
|
+
quat4.create = function (quat) {
|
1571
|
+
var dest = new MatrixArray(4);
|
1572
|
+
|
1573
|
+
if (quat) {
|
1574
|
+
dest[0] = quat[0];
|
1575
|
+
dest[1] = quat[1];
|
1576
|
+
dest[2] = quat[2];
|
1577
|
+
dest[3] = quat[3];
|
1578
|
+
}
|
1579
|
+
|
1580
|
+
return dest;
|
1581
|
+
};
|
1582
|
+
|
1583
|
+
/*
|
1584
|
+
* quat4.set
|
1585
|
+
* Copies the values of one quat4 to another
|
1586
|
+
*
|
1587
|
+
* Params:
|
1588
|
+
* quat - quat4 containing values to copy
|
1589
|
+
* dest - quat4 receiving copied values
|
1590
|
+
*
|
1591
|
+
* Returns:
|
1592
|
+
* dest
|
1593
|
+
*/
|
1594
|
+
quat4.set = function (quat, dest) {
|
1595
|
+
dest[0] = quat[0];
|
1596
|
+
dest[1] = quat[1];
|
1597
|
+
dest[2] = quat[2];
|
1598
|
+
dest[3] = quat[3];
|
1599
|
+
|
1600
|
+
return dest;
|
1601
|
+
};
|
1602
|
+
|
1603
|
+
/*
|
1604
|
+
* quat4.calculateW
|
1605
|
+
* Calculates the W component of a quat4 from the X, Y, and Z components.
|
1606
|
+
* Assumes that quaternion is 1 unit in length.
|
1607
|
+
* Any existing W component will be ignored.
|
1608
|
+
*
|
1609
|
+
* Params:
|
1610
|
+
* quat - quat4 to calculate W component of
|
1611
|
+
* dest - Optional, quat4 receiving calculated values. If not specified result is written to quat
|
1612
|
+
*
|
1613
|
+
* Returns:
|
1614
|
+
* dest if specified, quat otherwise
|
1615
|
+
*/
|
1616
|
+
quat4.calculateW = function (quat, dest) {
|
1617
|
+
var x = quat[0], y = quat[1], z = quat[2];
|
1618
|
+
|
1619
|
+
if (!dest || quat === dest) {
|
1620
|
+
quat[3] = -Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
|
1621
|
+
return quat;
|
1622
|
+
}
|
1623
|
+
dest[0] = x;
|
1624
|
+
dest[1] = y;
|
1625
|
+
dest[2] = z;
|
1626
|
+
dest[3] = -Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
|
1627
|
+
return dest;
|
1628
|
+
};
|
1629
|
+
|
1630
|
+
/*
|
1631
|
+
* quat4.inverse
|
1632
|
+
* Calculates the inverse of a quat4
|
1633
|
+
*
|
1634
|
+
* Params:
|
1635
|
+
* quat - quat4 to calculate inverse of
|
1636
|
+
* dest - Optional, quat4 receiving inverse values. If not specified result is written to quat
|
1637
|
+
*
|
1638
|
+
* Returns:
|
1639
|
+
* dest if specified, quat otherwise
|
1640
|
+
*/
|
1641
|
+
quat4.inverse = function (quat, dest) {
|
1642
|
+
if (!dest || quat === dest) {
|
1643
|
+
quat[0] *= -1;
|
1644
|
+
quat[1] *= -1;
|
1645
|
+
quat[2] *= -1;
|
1646
|
+
return quat;
|
1647
|
+
}
|
1648
|
+
dest[0] = -quat[0];
|
1649
|
+
dest[1] = -quat[1];
|
1650
|
+
dest[2] = -quat[2];
|
1651
|
+
dest[3] = quat[3];
|
1652
|
+
return dest;
|
1653
|
+
};
|
1654
|
+
|
1655
|
+
/*
|
1656
|
+
* quat4.length
|
1657
|
+
* Calculates the length of a quat4
|
1658
|
+
*
|
1659
|
+
* Params:
|
1660
|
+
* quat - quat4 to calculate length of
|
1661
|
+
*
|
1662
|
+
* Returns:
|
1663
|
+
* Length of quat
|
1664
|
+
*/
|
1665
|
+
quat4.length = function (quat) {
|
1666
|
+
var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
|
1667
|
+
return Math.sqrt(x * x + y * y + z * z + w * w);
|
1668
|
+
};
|
1669
|
+
|
1670
|
+
/*
|
1671
|
+
* quat4.normalize
|
1672
|
+
* Generates a unit quaternion of the same direction as the provided quat4
|
1673
|
+
* If quaternion length is 0, returns [0, 0, 0, 0]
|
1674
|
+
*
|
1675
|
+
* Params:
|
1676
|
+
* quat - quat4 to normalize
|
1677
|
+
* dest - Optional, quat4 receiving operation result. If not specified result is written to quat
|
1678
|
+
*
|
1679
|
+
* Returns:
|
1680
|
+
* dest if specified, quat otherwise
|
1681
|
+
*/
|
1682
|
+
quat4.normalize = function (quat, dest) {
|
1683
|
+
if (!dest) { dest = quat; }
|
1684
|
+
|
1685
|
+
var x = quat[0], y = quat[1], z = quat[2], w = quat[3],
|
1686
|
+
len = Math.sqrt(x * x + y * y + z * z + w * w);
|
1687
|
+
if (len === 0) {
|
1688
|
+
dest[0] = 0;
|
1689
|
+
dest[1] = 0;
|
1690
|
+
dest[2] = 0;
|
1691
|
+
dest[3] = 0;
|
1692
|
+
return dest;
|
1693
|
+
}
|
1694
|
+
len = 1 / len;
|
1695
|
+
dest[0] = x * len;
|
1696
|
+
dest[1] = y * len;
|
1697
|
+
dest[2] = z * len;
|
1698
|
+
dest[3] = w * len;
|
1699
|
+
|
1700
|
+
return dest;
|
1701
|
+
};
|
1702
|
+
|
1703
|
+
/*
|
1704
|
+
* quat4.multiply
|
1705
|
+
* Performs a quaternion multiplication
|
1706
|
+
*
|
1707
|
+
* Params:
|
1708
|
+
* quat - quat4, first operand
|
1709
|
+
* quat2 - quat4, second operand
|
1710
|
+
* dest - Optional, quat4 receiving operation result. If not specified result is written to quat
|
1711
|
+
*
|
1712
|
+
* Returns:
|
1713
|
+
* dest if specified, quat otherwise
|
1714
|
+
*/
|
1715
|
+
quat4.multiply = function (quat, quat2, dest) {
|
1716
|
+
if (!dest) { dest = quat; }
|
1717
|
+
|
1718
|
+
var qax = quat[0], qay = quat[1], qaz = quat[2], qaw = quat[3],
|
1719
|
+
qbx = quat2[0], qby = quat2[1], qbz = quat2[2], qbw = quat2[3];
|
1720
|
+
|
1721
|
+
dest[0] = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
|
1722
|
+
dest[1] = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
|
1723
|
+
dest[2] = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
|
1724
|
+
dest[3] = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
|
1725
|
+
|
1726
|
+
return dest;
|
1727
|
+
};
|
1728
|
+
|
1729
|
+
/*
|
1730
|
+
* quat4.multiplyVec3
|
1731
|
+
* Transforms a vec3 with the given quaternion
|
1732
|
+
*
|
1733
|
+
* Params:
|
1734
|
+
* quat - quat4 to transform the vector with
|
1735
|
+
* vec - vec3 to transform
|
1736
|
+
* dest - Optional, vec3 receiving operation result. If not specified result is written to vec
|
1737
|
+
*
|
1738
|
+
* Returns:
|
1739
|
+
* dest if specified, vec otherwise
|
1740
|
+
*/
|
1741
|
+
quat4.multiplyVec3 = function (quat, vec, dest) {
|
1742
|
+
if (!dest) { dest = vec; }
|
1743
|
+
|
1744
|
+
var x = vec[0], y = vec[1], z = vec[2],
|
1745
|
+
qx = quat[0], qy = quat[1], qz = quat[2], qw = quat[3],
|
1746
|
+
|
1747
|
+
// calculate quat * vec
|
1748
|
+
ix = qw * x + qy * z - qz * y,
|
1749
|
+
iy = qw * y + qz * x - qx * z,
|
1750
|
+
iz = qw * z + qx * y - qy * x,
|
1751
|
+
iw = -qx * x - qy * y - qz * z;
|
1752
|
+
|
1753
|
+
// calculate result * inverse quat
|
1754
|
+
dest[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
|
1755
|
+
dest[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
|
1756
|
+
dest[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
|
1757
|
+
|
1758
|
+
return dest;
|
1759
|
+
};
|
1760
|
+
|
1761
|
+
/*
|
1762
|
+
* quat4.toMat3
|
1763
|
+
* Calculates a 3x3 matrix from the given quat4
|
1764
|
+
*
|
1765
|
+
* Params:
|
1766
|
+
* quat - quat4 to create matrix from
|
1767
|
+
* dest - Optional, mat3 receiving operation result
|
1768
|
+
*
|
1769
|
+
* Returns:
|
1770
|
+
* dest if specified, a new mat3 otherwise
|
1771
|
+
*/
|
1772
|
+
quat4.toMat3 = function (quat, dest) {
|
1773
|
+
if (!dest) { dest = mat3.create(); }
|
1774
|
+
|
1775
|
+
var x = quat[0], y = quat[1], z = quat[2], w = quat[3],
|
1776
|
+
x2 = x + x,
|
1777
|
+
y2 = y + y,
|
1778
|
+
z2 = z + z,
|
1779
|
+
|
1780
|
+
xx = x * x2,
|
1781
|
+
xy = x * y2,
|
1782
|
+
xz = x * z2,
|
1783
|
+
yy = y * y2,
|
1784
|
+
yz = y * z2,
|
1785
|
+
zz = z * z2,
|
1786
|
+
wx = w * x2,
|
1787
|
+
wy = w * y2,
|
1788
|
+
wz = w * z2;
|
1789
|
+
|
1790
|
+
dest[0] = 1 - (yy + zz);
|
1791
|
+
dest[1] = xy + wz;
|
1792
|
+
dest[2] = xz - wy;
|
1793
|
+
|
1794
|
+
dest[3] = xy - wz;
|
1795
|
+
dest[4] = 1 - (xx + zz);
|
1796
|
+
dest[5] = yz + wx;
|
1797
|
+
|
1798
|
+
dest[6] = xz + wy;
|
1799
|
+
dest[7] = yz - wx;
|
1800
|
+
dest[8] = 1 - (xx + yy);
|
1801
|
+
|
1802
|
+
return dest;
|
1803
|
+
};
|
1804
|
+
|
1805
|
+
/*
|
1806
|
+
* quat4.toMat4
|
1807
|
+
* Calculates a 4x4 matrix from the given quat4
|
1808
|
+
*
|
1809
|
+
* Params:
|
1810
|
+
* quat - quat4 to create matrix from
|
1811
|
+
* dest - Optional, mat4 receiving operation result
|
1812
|
+
*
|
1813
|
+
* Returns:
|
1814
|
+
* dest if specified, a new mat4 otherwise
|
1815
|
+
*/
|
1816
|
+
quat4.toMat4 = function (quat, dest) {
|
1817
|
+
if (!dest) { dest = mat4.create(); }
|
1818
|
+
|
1819
|
+
var x = quat[0], y = quat[1], z = quat[2], w = quat[3],
|
1820
|
+
x2 = x + x,
|
1821
|
+
y2 = y + y,
|
1822
|
+
z2 = z + z,
|
1823
|
+
|
1824
|
+
xx = x * x2,
|
1825
|
+
xy = x * y2,
|
1826
|
+
xz = x * z2,
|
1827
|
+
yy = y * y2,
|
1828
|
+
yz = y * z2,
|
1829
|
+
zz = z * z2,
|
1830
|
+
wx = w * x2,
|
1831
|
+
wy = w * y2,
|
1832
|
+
wz = w * z2;
|
1833
|
+
|
1834
|
+
dest[0] = 1 - (yy + zz);
|
1835
|
+
dest[1] = xy + wz;
|
1836
|
+
dest[2] = xz - wy;
|
1837
|
+
dest[3] = 0;
|
1838
|
+
|
1839
|
+
dest[4] = xy - wz;
|
1840
|
+
dest[5] = 1 - (xx + zz);
|
1841
|
+
dest[6] = yz + wx;
|
1842
|
+
dest[7] = 0;
|
1843
|
+
|
1844
|
+
dest[8] = xz + wy;
|
1845
|
+
dest[9] = yz - wx;
|
1846
|
+
dest[10] = 1 - (xx + yy);
|
1847
|
+
dest[11] = 0;
|
1848
|
+
|
1849
|
+
dest[12] = 0;
|
1850
|
+
dest[13] = 0;
|
1851
|
+
dest[14] = 0;
|
1852
|
+
dest[15] = 1;
|
1853
|
+
|
1854
|
+
return dest;
|
1855
|
+
};
|
1856
|
+
|
1857
|
+
/*
|
1858
|
+
* quat4.slerp
|
1859
|
+
* Performs a spherical linear interpolation between two quat4
|
1860
|
+
*
|
1861
|
+
* Params:
|
1862
|
+
* quat - quat4, first quaternion
|
1863
|
+
* quat2 - quat4, second quaternion
|
1864
|
+
* slerp - interpolation amount between the two inputs
|
1865
|
+
* dest - Optional, quat4 receiving operation result. If not specified result is written to quat
|
1866
|
+
*
|
1867
|
+
* Returns:
|
1868
|
+
* dest if specified, quat otherwise
|
1869
|
+
*/
|
1870
|
+
quat4.slerp = function (quat, quat2, slerp, dest) {
|
1871
|
+
if (!dest) { dest = quat; }
|
1872
|
+
|
1873
|
+
var cosHalfTheta = quat[0] * quat2[0] + quat[1] * quat2[1] + quat[2] * quat2[2] + quat[3] * quat2[3],
|
1874
|
+
halfTheta,
|
1875
|
+
sinHalfTheta,
|
1876
|
+
ratioA,
|
1877
|
+
ratioB;
|
1878
|
+
|
1879
|
+
if (Math.abs(cosHalfTheta) >= 1.0) {
|
1880
|
+
if (dest !== quat) {
|
1881
|
+
dest[0] = quat[0];
|
1882
|
+
dest[1] = quat[1];
|
1883
|
+
dest[2] = quat[2];
|
1884
|
+
dest[3] = quat[3];
|
1885
|
+
}
|
1886
|
+
return dest;
|
1887
|
+
}
|
1888
|
+
|
1889
|
+
halfTheta = Math.acos(cosHalfTheta);
|
1890
|
+
sinHalfTheta = Math.sqrt(1.0 - cosHalfTheta * cosHalfTheta);
|
1891
|
+
|
1892
|
+
if (Math.abs(sinHalfTheta) < 0.001) {
|
1893
|
+
dest[0] = (quat[0] * 0.5 + quat2[0] * 0.5);
|
1894
|
+
dest[1] = (quat[1] * 0.5 + quat2[1] * 0.5);
|
1895
|
+
dest[2] = (quat[2] * 0.5 + quat2[2] * 0.5);
|
1896
|
+
dest[3] = (quat[3] * 0.5 + quat2[3] * 0.5);
|
1897
|
+
return dest;
|
1898
|
+
}
|
1899
|
+
|
1900
|
+
ratioA = Math.sin((1 - slerp) * halfTheta) / sinHalfTheta;
|
1901
|
+
ratioB = Math.sin(slerp * halfTheta) / sinHalfTheta;
|
1902
|
+
|
1903
|
+
dest[0] = (quat[0] * ratioA + quat2[0] * ratioB);
|
1904
|
+
dest[1] = (quat[1] * ratioA + quat2[1] * ratioB);
|
1905
|
+
dest[2] = (quat[2] * ratioA + quat2[2] * ratioB);
|
1906
|
+
dest[3] = (quat[3] * ratioA + quat2[3] * ratioB);
|
1907
|
+
|
1908
|
+
return dest;
|
1909
|
+
};
|
1910
|
+
|
1911
|
+
/*
|
1912
|
+
* quat4.str
|
1913
|
+
* Returns a string representation of a quaternion
|
1914
|
+
*
|
1915
|
+
* Params:
|
1916
|
+
* quat - quat4 to represent as a string
|
1917
|
+
*
|
1918
|
+
* Returns:
|
1919
|
+
* string representation of quat
|
1920
|
+
*/
|
1921
|
+
quat4.str = function (quat) {
|
1922
|
+
return '[' + quat[0] + ', ' + quat[1] + ', ' + quat[2] + ', ' + quat[3] + ']';
|
1923
|
+
};
|
1924
|
+
|