3 * Copyright (c) 2010 Mozilla Corporation
4 * Copyright (c) 2010 Vladimir Vukicevic
5 * Copyright (c) 2013 John Mayer
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use,
11 * copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
32 * Vector and Matrix math utilities for JavaScript, optimized for WebGL.
33 * Edited to work with the Elm Programming Language
37Elm.Native.MJS.make = function(elm) {
39 elm.Native = elm.Native || {};
40 elm.Native.MJS = elm.Native.MJS || {};
41 if (elm.Native.MJS.values) return elm.Native.MJS.values;
44 * Constant: MJS_VERSION
46 * mjs version number aa.bb.cc, encoded as an integer of the form:
49 var MJS_VERSION = 0x000000;
52 * Constant: MJS_DO_ASSERT
54 * Enables or disables runtime assertions.
56 * For potentially more performance, the assert methods can be
57 * commented out in each place where they are called.
59 var MJS_DO_ASSERT = false;
62 * Constant: MJS_FLOAT_ARRAY_TYPE
64 * The base float array type. By default, WebGLFloatArray.
66 * mjs can work with any array-like elements, but if an array
67 * creation is requested, it will create an array of the type
68 * MJS_FLOAT_ARRAY_TYPE. Also, the builtin constants such as (M4x4.I)
69 * will be of this type.
71 //MJS_FLOAT_ARRAY_TYPE = WebGLFloatArray;
72 var MJS_FLOAT_ARRAY_TYPE = Float32Array;
73 //MJS_FLOAT_ARRAY_TYPE = Float64Array;
74 //MJS_FLOAT_ARRAY_TYPE = Array;
77 function MathUtils_assert(cond, msg) {
79 throw "Assertion failed: " + msg;
83 function MathUtils_assert() { }
89 * Methods for working with 3-element vectors. A vector is represented by a 3-element array.
90 * Any valid JavaScript array type may be used, but if new
91 * vectors are created they are created using the configured MJS_FLOAT_ARRAY_TYPE.
96 V3._temp1 = new MJS_FLOAT_ARRAY_TYPE(3);
97 V3._temp2 = new MJS_FLOAT_ARRAY_TYPE(3);
98 V3._temp3 = new MJS_FLOAT_ARRAY_TYPE(3);
100 if (MJS_FLOAT_ARRAY_TYPE == Array) {
101 V3.x = [1.0, 0.0, 0.0];
102 V3.y = [0.0, 1.0, 0.0];
103 V3.z = [0.0, 0.0, 1.0];
105 V3.$ = function V3_$(x, y, z) {
109 V3.x = new MJS_FLOAT_ARRAY_TYPE([1.0, 0.0, 0.0]);
110 V3.y = new MJS_FLOAT_ARRAY_TYPE([0.0, 1.0, 0.0]);
111 V3.z = new MJS_FLOAT_ARRAY_TYPE([0.0, 0.0, 1.0]);
116 * Creates a new 3-element vector with the given values.
120 * x,y,z - the 3 elements of the new vector.
124 * A new vector containing with the given argument values.
127 V3.$ = function V3_$(x, y, z) {
128 return new MJS_FLOAT_ARRAY_TYPE([x, y, z]);
135 V3.getX = function V3_getX(a) {
138 V3.getY = function V3_getY(a) {
141 V3.getZ = function V3_getZ(a) {
145 V3.toTuple3 = function V3_toTuple3(a) {
153 V3.fromTuple3 = function V3_fromTuple3(t) {
154 return new MJS_FLOAT_ARRAY_TYPE([t._0, t._1, t._2]);
157 V3.toRecord3 = function V3_toRecord3(a) {
165 V3.fromRecord3 = function V3_fromRecord3(r) {
166 return new MJS_FLOAT_ARRAY_TYPE([r.x, r.y, r.z]);
176 * a - the first vector operand
177 * b - the second vector operand
178 * r - optional vector to store the result in
182 * If r is specified, returns r after performing the operation.
183 * Otherwise, returns a new 3-element vector with the result.
185 V3.add = function V3_add(a, b, r) {
186 //MathUtils_assert(a.length == 3, "a.length == 3");
187 //MathUtils_assert(b.length == 3, "b.length == 3");
188 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
191 r = new MJS_FLOAT_ARRAY_TYPE(3);
206 * a - the first vector operand
207 * b - the second vector operand
208 * r - optional vector to store the result in
212 * If r is specified, returns r after performing the operation.
213 * Otherwise, returns a new 3-element vector with the result.
215 V3.sub = function V3_sub(a, b, r) {
216 //MathUtils_assert(a.length == 3, "a.length == 3");
217 //MathUtils_assert(b.length == 3, "b.length == 3");
218 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
221 r = new MJS_FLOAT_ARRAY_TYPE(3);
236 * a - the vector operand
237 * r - optional vector to store the result in
241 * If r is specified, returns r after performing the operation.
242 * Otherwise, returns a new 3-element vector with the result.
244 V3.neg = function V3_neg(a, r) {
245 //MathUtils_assert(a.length == 3, "a.length == 3");
246 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
249 r = new MJS_FLOAT_ARRAY_TYPE(3);
257 * Function: V3.direction
260 * r = (a - b) / |a - b|. The result is the normalized
261 * direction from a to b.
265 * a - the first vector operand
266 * b - the second vector operand
267 * r - optional vector to store the result in
271 * If r is specified, returns r after performing the operation.
272 * Otherwise, returns a new 3-element vector with the result.
274 V3.direction = function V3_direction(a, b, r) {
275 //MathUtils_assert(a.length == 3, "a.length == 3");
276 //MathUtils_assert(b.length == 3, "b.length == 3");
277 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
280 r = new MJS_FLOAT_ARRAY_TYPE(3);
281 return V3.normalize(V3.sub(a, b, r), r);
285 * Function: V3.length
291 * a - the vector operand
295 * The length of the given vector.
297 V3.length = function V3_length(a) {
298 //MathUtils_assert(a.length == 3, "a.length == 3");
300 return Math.sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
304 * Function: V3.lengthSquard
306 * Perform r = |a|*|a|.
310 * a - the vector operand
314 * The square of the length of the given vector.
316 V3.lengthSquared = function V3_lengthSquared(a) {
317 //MathUtils_assert(a.length == 3, "a.length == 3");
319 return a[0]*a[0] + a[1]*a[1] + a[2]*a[2];
323 * Function: V3.normalize
325 * Perform r = a / |a|.
329 * a - the vector operand
330 * r - optional vector to store the result in
334 * If r is specified, returns r after performing the operation.
335 * Otherwise, returns a new 3-element vector with the result.
337 V3.normalize = function V3_normalize(a, r) {
338 //MathUtils_assert(a.length == 3, "a.length == 3");
339 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
342 r = new MJS_FLOAT_ARRAY_TYPE(3);
343 var im = 1.0 / V3.length(a);
357 * a - the vector operand
359 * r - optional vector to store the result in
363 * If r is specified, returns r after performing the operation.
364 * Otherwise, returns a new 3-element vector with the result.
366 V3.scale = function V3_scale(a, k, r) {
367 //MathUtils_assert(a.length == 3, "a.length == 3");
368 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
371 r = new MJS_FLOAT_ARRAY_TYPE(3);
386 * a - the first vector operand
387 * b - the second vector operand
391 * The dot product of a and b.
393 V3.dot = function V3_dot(a, b) {
394 //MathUtils_assert(a.length == 3, "a.length == 3");
395 //MathUtils_assert(b.length == 3, "b.length == 3");
409 * a - the first vector operand
410 * b - the second vector operand
411 * r - optional vector to store the result in
415 * If r is specified, returns r after performing the operation.
416 * Otherwise, returns a new 3-element vector with the result.
418 V3.cross = function V3_cross(a, b, r) {
419 //MathUtils_assert(a.length == 3, "a.length == 3");
420 //MathUtils_assert(b.length == 3, "b.length == 3");
421 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
424 r = new MJS_FLOAT_ARRAY_TYPE(3);
425 r[0] = a[1]*b[2] - a[2]*b[1];
426 r[1] = a[2]*b[0] - a[0]*b[2];
427 r[2] = a[0]*b[1] - a[1]*b[0];
432 * Function: V3.mul4x4
439 * m - the 4x4 matrix operand
440 * v - the 3-element vector operand
441 * r - optional vector to store the result in
445 * If r is specified, returns r after performing the operation.
446 * Otherwise, returns a new 3-element vector with the result.
447 * The 4-element result vector is divided by the w component
448 * and returned as a 3-element vector.
450 V3.mul4x4 = function V3_mul4x4(m, v, r) {
451 //MathUtils_assert(m.length == 16, "m.length == 16");
452 //MathUtils_assert(v.length == 3, "v.length == 3");
453 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
458 r = new MJS_FLOAT_ARRAY_TYPE(3);
462 w = V3.dot(v, tmp) + m[15];
466 r[0] = (V3.dot(v, tmp) + m[12])/w;
470 r[1] = (V3.dot(v, tmp) + m[13])/w;
474 r[2] = (V3.dot(v, tmp) + m[14])/w;
481 * Methods for working with 4x4 matrices. A matrix is represented by a 16-element array
482 * in column-major order. Any valid JavaScript array type may be used, but if new
483 * matrices are created they are created using the configured MJS_FLOAT_ARRAY_TYPE.
488 M4x4._temp1 = new MJS_FLOAT_ARRAY_TYPE(16);
489 M4x4._temp2 = new MJS_FLOAT_ARRAY_TYPE(16);
491 if (MJS_FLOAT_ARRAY_TYPE == Array) {
492 M4x4.I = [1.0, 0.0, 0.0, 0.0,
497 M4x4.$ = function M4x4_$(m00, m01, m02, m03,
502 return [m00, m01, m02, m03,
508 M4x4.I = new MJS_FLOAT_ARRAY_TYPE([1.0, 0.0, 0.0, 0.0,
511 0.0, 0.0, 0.0, 1.0]);
516 * Creates a new 4x4 matrix with the given values.
520 * m00..m15 - the 16 elements of the new matrix.
524 * A new matrix filled with the given argument values.
526 M4x4.$ = function M4x4_$(m00, m01, m02, m03,
531 return new MJS_FLOAT_ARRAY_TYPE([m00, m01, m02, m03,
534 m12, m13, m14, m15]);
538 M4x4.identity = M4x4.I;
541 * Function: M4x4.topLeft3x3
543 * Return the top left 3x3 matrix from the given 4x4 matrix m.
548 * r - optional 3x3 matrix to store the result in
552 * If r is specified, returns r after performing the operation.
553 * Otherwise, returns a new 3x3 matrix with the result.
555 M4x4.topLeft3x3 = function M4x4_topLeft3x3(m, r) {
556 //MathUtils_assert(m.length == 16, "m.length == 16");
557 //MathUtils_assert(r == undefined || r.length == 9, "r == undefined || r.length == 9");
560 r = new MJS_FLOAT_ARRAY_TYPE(9);
561 r[0] = m[0]; r[1] = m[1]; r[2] = m[2];
562 r[3] = m[4]; r[4] = m[5]; r[5] = m[6];
563 r[6] = m[8]; r[7] = m[9]; r[8] = m[10];
568 * Function: M4x4.inverseOrthonormal
570 * Computes the inverse of the given matrix m, assuming that
571 * the matrix is orthonormal.
576 * r - optional 4x4 matrix to store the result in
580 * If r is specified, returns r after performing the operation.
581 * Otherwise, returns a new 4x4 matrix with the result.
583 M4x4.inverseOrthonormal = function M4x4_inverseOrthonormal(m, r) {
584 //MathUtils_assert(m.length == 16, "m.length == 16");
585 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
586 //MathUtils_assert(m != r, "m != r");
589 r = new MJS_FLOAT_ARRAY_TYPE(16);
590 M4x4.transpose(m, r);
591 var t = [m[12], m[13], m[14]];
592 r[3] = r[7] = r[11] = 0;
593 r[12] = -V3.dot([r[0], r[4], r[8]], t);
594 r[13] = -V3.dot([r[1], r[5], r[9]], t);
595 r[14] = -V3.dot([r[2], r[6], r[10]], t);
600 * Function: M4x4.inverseTo3x3
602 * Computes the inverse of the given matrix m, but calculates
603 * only the top left 3x3 values of the result.
608 * r - optional 3x3 matrix to store the result in
612 * If r is specified, returns r after performing the operation.
613 * Otherwise, returns a new 3x3 matrix with the result.
615 M4x4.inverseTo3x3 = function M4x4_inverseTo3x3(m, r) {
616 //MathUtils_assert(m.length == 16, "m.length == 16");
617 //MathUtils_assert(r == undefined || r.length == 9, "r == undefined || r.length == 9");
620 r = new MJS_FLOAT_ARRAY_TYPE(9);
622 var a11 = m[10]*m[5]-m[6]*m[9],
623 a21 = -m[10]*m[1]+m[2]*m[9],
624 a31 = m[6]*m[1]-m[2]*m[5],
625 a12 = -m[10]*m[4]+m[6]*m[8],
626 a22 = m[10]*m[0]-m[2]*m[8],
627 a32 = -m[6]*m[0]+m[2]*m[4],
628 a13 = m[9]*m[4]-m[5]*m[8],
629 a23 = -m[9]*m[0]+m[1]*m[8],
630 a33 = m[5]*m[0]-m[1]*m[4];
631 var det = m[0]*(a11) + m[1]*(a12) + m[2]*(a13);
632 if (det == 0) // no inverse
633 throw "matrix not invertible";
634 var idet = 1.0 / det;
650 * Function: M4x4.makeFrustum
652 * Creates a matrix for a projection frustum with the given parameters.
656 * left - the left coordinate of the frustum
657 * right- the right coordinate of the frustum
658 * bottom - the bottom coordinate of the frustum
659 * top - the top coordinate of the frustum
660 * znear - the near z distance of the frustum
661 * zfar - the far z distance of the frustum
662 * r - optional 4x4 matrix to store the result in
666 * If r is specified, returns r after creating the projection matrix.
667 * Otherwise, returns a new 4x4 matrix.
669 M4x4.makeFrustum = function M4x4_makeFrustum(left, right, bottom, top, znear, zfar, r) {
670 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
673 r = new MJS_FLOAT_ARRAY_TYPE(16);
675 var X = 2*znear/(right-left);
676 var Y = 2*znear/(top-bottom);
677 var A = (right+left)/(right-left);
678 var B = (top+bottom)/(top-bottom);
679 var C = -(zfar+znear)/(zfar-znear);
680 var D = -2*zfar*znear/(zfar-znear);
682 r[0] = 2*znear/(right-left);
687 r[5] = 2*znear/(top-bottom);
690 r[8] = (right+left)/(right-left);
691 r[9] = (top+bottom)/(top-bottom);
692 r[10] = -(zfar+znear)/(zfar-znear);
696 r[14] = -2*zfar*znear/(zfar-znear);
703 * Function: M4x4.makePerspective
705 * Creates a matrix for a perspective projection with the given parameters.
709 * fovy - field of view in the y axis, in degrees
710 * aspect - aspect ratio
711 * znear - the near z distance of the projection
712 * zfar - the far z distance of the projection
713 * r - optional 4x4 matrix to store the result in
717 * If r is specified, returns r after creating the projection matrix.
718 * Otherwise, returns a new 4x4 matrix.
720 M4x4.makePerspective = function M4x4_makePerspective (fovy, aspect, znear, zfar, r) {
721 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
723 var ymax = znear * Math.tan(fovy * Math.PI / 360.0);
725 var xmin = ymin * aspect;
726 var xmax = ymax * aspect;
728 return M4x4.makeFrustum(xmin, xmax, ymin, ymax, znear, zfar, r);
732 * Function: M4x4.makeOrtho
734 * Creates a matrix for an orthogonal frustum projection with the given parameters.
738 * left - the left coordinate of the frustum
739 * right- the right coordinate of the frustum
740 * bottom - the bottom coordinate of the frustum
741 * top - the top coordinate of the frustum
742 * znear - the near z distance of the frustum
743 * zfar - the far z distance of the frustum
744 * r - optional 4x4 matrix to store the result in
748 * If r is specified, returns r after creating the projection matrix.
749 * Otherwise, returns a new 4x4 matrix.
751 M4x4.makeOrtho = function M4x4_makeOrtho (left, right, bottom, top, znear, zfar, r) {
752 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
755 r = new MJS_FLOAT_ARRAY_TYPE(16);
757 var tX = -(right+left)/(right-left);
758 var tY = -(top+bottom)/(top-bottom);
759 var tZ = -(zfar+znear)/(zfar-znear);
760 var X = 2 / (right-left);
761 var Y = 2 / (top-bottom);
762 var Z = -2 / (zfar-znear);
764 r[0] = 2 / (right-left);
769 r[5] = 2 / (top-bottom);
774 r[10] = -2 / (zfar-znear);
776 r[12] = -(right+left)/(right-left);
777 r[13] = -(top+bottom)/(top-bottom);
778 r[14] = -(zfar+znear)/(zfar-znear);
785 * Function: M4x4.makeOrtho2D
787 * Creates a matrix for a 2D orthogonal frustum projection with the given parameters.
788 * znear and zfar are assumed to be -1 and 1, respectively.
792 * left - the left coordinate of the frustum
793 * right- the right coordinate of the frustum
794 * bottom - the bottom coordinate of the frustum
795 * top - the top coordinate of the frustum
796 * r - optional 4x4 matrix to store the result in
800 * If r is specified, returns r after creating the projection matrix.
801 * Otherwise, returns a new 4x4 matrix.
803 M4x4.makeOrtho2D = function M4x4_makeOrtho2D (left, right, bottom, top, r) {
804 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
806 return M4x4.makeOrtho(left, right, bottom, top, -1, 1, r);
812 * Performs r = a * b.
816 * a - the first matrix operand
817 * b - the second matrix operand
818 * r - optional 4x4 matrix to store the result in
822 * If r is specified, returns r after performing the operation.
823 * Otherwise, returns a new 4x4 matrix with the result.
825 M4x4.mul = function M4x4_mul(a, b, r) {
826 //MathUtils_assert(a.length == 16, "a.length == 16");
827 //MathUtils_assert(b.length == 16, "b.length == 16");
828 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
829 //MathUtils_assert(a != r && b != r, "a != r && b != r");
832 r = new MJS_FLOAT_ARRAY_TYPE(16);
868 r[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
869 r[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
870 r[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
871 r[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
872 r[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
873 r[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
874 r[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
875 r[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
876 r[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
877 r[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
878 r[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
879 r[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
880 r[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
881 r[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
882 r[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
883 r[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
889 * Function: M4x4.mulAffine
891 * Performs r = a * b, assuming a and b are affine (elements 3,7,11,15 = 0,0,0,1)
895 * a - the first matrix operand
896 * b - the second matrix operand
897 * r - optional 4x4 matrix to store the result in
901 * If r is specified, returns r after performing the operation.
902 * Otherwise, returns a new 4x4 matrix with the result.
904 M4x4.mulAffine = function M4x4_mulAffine(a, b, r) {
905 //MathUtils_assert(a.length == 16, "a.length == 16");
906 //MathUtils_assert(b.length == 16, "b.length == 16");
907 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
908 //MathUtils_assert(a != r && b != r, "a != r && b != r");
911 r = new MJS_FLOAT_ARRAY_TYPE(16);
938 r[0] = a11 * b11 + a12 * b21 + a13 * b31;
939 r[1] = a21 * b11 + a22 * b21 + a23 * b31;
940 r[2] = a31 * b11 + a32 * b21 + a33 * b31;
942 r[4] = a11 * b12 + a12 * b22 + a13 * b32;
943 r[5] = a21 * b12 + a22 * b22 + a23 * b32;
944 r[6] = a31 * b12 + a32 * b22 + a33 * b32;
946 r[8] = a11 * b13 + a12 * b23 + a13 * b33;
947 r[9] = a21 * b13 + a22 * b23 + a23 * b33;
948 r[10] = a31 * b13 + a32 * b23 + a33 * b33;
950 r[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14;
951 r[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24;
952 r[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34;
959 * Function: M4x4.makeRotate
961 * Creates a transformation matrix for rotation by angle radians about the 3-element vector axis.
965 * angle - the angle of rotation, in radians
966 * axis - the axis around which the rotation is performed, a 3-element vector
967 * r - optional 4x4 matrix to store the result in
971 * If r is specified, returns r after creating the matrix.
972 * Otherwise, returns a new 4x4 matrix with the result.
974 M4x4.makeRotate = function M4x4_makeRotate(angle, axis, r) {
975 //MathUtils_assert(angle.length == 3, "angle.length == 3");
976 //MathUtils_assert(axis.length == 3, "axis.length == 3");
977 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
980 r = new MJS_FLOAT_ARRAY_TYPE(16);
982 axis = V3.normalize(axis, V3._temp1);
983 var x = axis[0], y = axis[1], z = axis[2];
984 var c = Math.cos(angle);
986 var s = Math.sin(angle);
1009 * Function: M4x4.rotate
1011 * Concatenates a rotation of angle radians about the axis to the give matrix m.
1015 * angle - the angle of rotation, in radians
1016 * axis - the axis around which the rotation is performed, a 3-element vector
1017 * m - the matrix to concatenate the rotation to
1018 * r - optional 4x4 matrix to store the result in
1022 * If r is specified, returns r after performing the operation.
1023 * Otherwise, returns a new 4x4 matrix with the result.
1025 M4x4.rotate = function M4x4_rotate(angle, axis, m, r) {
1026 //MathUtils_assert(angle.length == 3, "angle.length == 3");
1027 //MathUtils_assert(axis.length == 3, "axis.length == 3");
1028 //MathUtils_assert(m.length == 16, "m.length == 16");
1029 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1032 r = new MJS_FLOAT_ARRAY_TYPE(16);
1033 var a0=axis [0], a1=axis [1], a2=axis [2];
1034 var l = Math.sqrt(a0*a0 + a1*a1 + a2*a2);
1035 var x = a0, y = a1, z = a2;
1042 var c = Math.cos(angle);
1044 var s = Math.sin(angle);
1048 var xyc1 = x * y * c1;
1049 var xzc1 = x * z * c1;
1050 var yzc1 = y * z * c1;
1065 var t11 = x * x * c1 + c;
1066 var t21 = xyc1 + zs;
1067 var t31 = xzc1 - ys;
1068 var t12 = xyc1 - zs;
1069 var t22 = y * y * c1 + c;
1070 var t32 = yzc1 + xs;
1071 var t13 = xzc1 + ys;
1072 var t23 = yzc1 - xs;
1073 var t33 = z * z * c1 + c;
1075 r[0] = m11 * t11 + m12 * t21 + m13 * t31;
1076 r[1] = m21 * t11 + m22 * t21 + m23 * t31;
1077 r[2] = m31 * t11 + m32 * t21 + m33 * t31;
1078 r[3] = m41 * t11 + m42 * t21 + m43 * t31;
1079 r[4] = m11 * t12 + m12 * t22 + m13 * t32;
1080 r[5] = m21 * t12 + m22 * t22 + m23 * t32;
1081 r[6] = m31 * t12 + m32 * t22 + m33 * t32;
1082 r[7] = m41 * t12 + m42 * t22 + m43 * t32;
1083 r[8] = m11 * t13 + m12 * t23 + m13 * t33;
1084 r[9] = m21 * t13 + m22 * t23 + m23 * t33;
1085 r[10] = m31 * t13 + m32 * t23 + m33 * t33;
1086 r[11] = m41 * t13 + m42 * t23 + m43 * t33;
1097 * Function: M4x4.makeScale3
1099 * Creates a transformation matrix for scaling by 3 scalar values, one for
1100 * each of the x, y, and z directions.
1104 * x - the scale for the x axis
1105 * y - the scale for the y axis
1106 * z - the scale for the z axis
1107 * r - optional 4x4 matrix to store the result in
1111 * If r is specified, returns r after creating the matrix.
1112 * Otherwise, returns a new 4x4 matrix with the result.
1114 M4x4.makeScale3 = function M4x4_makeScale3(x, y, z, r) {
1115 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1118 r = new MJS_FLOAT_ARRAY_TYPE(16);
1141 * Function: M4x4.makeScale1
1143 * Creates a transformation matrix for a uniform scale by a single scalar value.
1147 * k - the scale factor
1148 * r - optional 4x4 matrix to store the result in
1152 * If r is specified, returns r after creating the matrix.
1153 * Otherwise, returns a new 4x4 matrix with the result.
1155 M4x4.makeScale1 = function M4x4_makeScale1(k, r) {
1156 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1158 return M4x4.makeScale3(k, k, k, r);
1162 * Function: M4x4.makeScale
1164 * Creates a transformation matrix for scaling each of the x, y, and z axes by the amount
1165 * given in the corresponding element of the 3-element vector.
1169 * v - the 3-element vector containing the scale factors
1170 * r - optional 4x4 matrix to store the result in
1174 * If r is specified, returns r after creating the matrix.
1175 * Otherwise, returns a new 4x4 matrix with the result.
1177 M4x4.makeScale = function M4x4_makeScale(v, r) {
1178 //MathUtils_assert(v.length == 3, "v.length == 3");
1179 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1181 return M4x4.makeScale3(v[0], v[1], v[2], r);
1185 * Function: M4x4.scale3
1187 M4x4.scale3 = function M4x4_scale3(x, y, z, m, r) {
1188 //MathUtils_assert(m.length == 16, "m.length == 16");
1189 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1208 r = new MJS_FLOAT_ARRAY_TYPE(16);
1231 * Function: M4x4.scale1
1233 M4x4.scale1 = function M4x4_scale1(k, m, r) {
1234 //MathUtils_assert(m.length == 16, "m.length == 16");
1235 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1254 r = new MJS_FLOAT_ARRAY_TYPE(16);
1277 * Function: M4x4.scale1
1279 M4x4.scale = function M4x4_scale(v, m, r) {
1280 //MathUtils_assert(v.length == 3, "v.length == 3");
1281 //MathUtils_assert(m.length == 16, "m.length == 16");
1282 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1283 var x = v[0], y = v[1], z = v[2];
1302 r = new MJS_FLOAT_ARRAY_TYPE(16);
1326 * Function: M4x4.makeTranslate3
1328 M4x4.makeTranslate3 = function M4x4_makeTranslate3(x, y, z, r) {
1329 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1332 r = new MJS_FLOAT_ARRAY_TYPE(16);
1355 * Function: M4x4.makeTranslate1
1357 M4x4.makeTranslate1 = function M4x4_makeTranslate1 (k, r) {
1358 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1360 return M4x4.makeTranslate3(k, k, k, r);
1364 * Function: M4x4.makeTranslate
1366 M4x4.makeTranslate = function M4x4_makeTranslate (v, r) {
1367 //MathUtils_assert(v.length == 3, "v.length == 3");
1368 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1370 return M4x4.makeTranslate3(v[0], v[1], v[2], r);
1374 * Function: M4x4.translate3Self
1376 M4x4.translate3Self = function M4x4_translate3Self (x, y, z, m) {
1377 //MathUtils_assert(m.length == 16, "m.length == 16");
1378 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1379 m[12] += m[0] * x + m[4] * y + m[8] * z;
1380 m[13] += m[1] * x + m[5] * y + m[9] * z;
1381 m[14] += m[2] * x + m[6] * y + m[10] * z;
1382 m[15] += m[3] * x + m[7] * y + m[11] * z;
1387 * Function: M4x4.translate3
1389 M4x4.translate3 = function M4x4_translate3 (x, y, z, m, r) {
1390 //MathUtils_assert(m.length == 16, "m.length == 16");
1391 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1394 m[12] += m[0] * x + m[4] * y + m[8] * z;
1395 m[13] += m[1] * x + m[5] * y + m[9] * z;
1396 m[14] += m[2] * x + m[6] * y + m[10] * z;
1397 m[15] += m[3] * x + m[7] * y + m[11] * z;
1402 r = new MJS_FLOAT_ARRAY_TYPE(16);
1430 r[12] = m11 * x + m12 * y + m13 * z + m[12];
1431 r[13] = m21 * x + m22 * y + m23 * z + m[13];
1432 r[14] = m31 * x + m32 * y + m33 * z + m[14];
1433 r[15] = m41 * x + m42 * y + m43 * z + m[15];
1439 * Function: M4x4.translate1
1441 M4x4.translate1 = function M4x4_translate1 (k, m, r) {
1442 //MathUtils_assert(m.length == 16, "m.length == 16");
1443 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1445 return M4x4.translate3(k, k, k, m, r);
1448 * Function: M4x4.translateSelf
1450 M4x4.translateSelf = function M4x4_translateSelf (v, m) {
1451 //MathUtils_assert(v.length == 3, "v.length == 3");
1452 //MathUtils_assert(m.length == 16, "m.length == 16");
1453 var x=v[0], y=v[1], z=v[2];
1454 m[12] += m[0] * x + m[4] * y + m[8] * z;
1455 m[13] += m[1] * x + m[5] * y + m[9] * z;
1456 m[14] += m[2] * x + m[6] * y + m[10] * z;
1457 m[15] += m[3] * x + m[7] * y + m[11] * z;
1461 * Function: M4x4.translate
1463 M4x4.translate = function M4x4_translate (v, m, r) {
1464 //MathUtils_assert(v.length == 3, "v.length == 3");
1465 //MathUtils_assert(m.length == 16, "m.length == 16");
1466 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1467 var x=v[0], y=v[1], z=v[2];
1469 m[12] += m[0] * x + m[4] * y + m[8] * z;
1470 m[13] += m[1] * x + m[5] * y + m[9] * z;
1471 m[14] += m[2] * x + m[6] * y + m[10] * z;
1472 m[15] += m[3] * x + m[7] * y + m[11] * z;
1477 r = new MJS_FLOAT_ARRAY_TYPE(16);
1504 r[12] = m11 * x + m12 * y + m13 * z + m[12];
1505 r[13] = m21 * x + m22 * y + m23 * z + m[13];
1506 r[14] = m31 * x + m32 * y + m33 * z + m[14];
1507 r[15] = m41 * x + m42 * y + m43 * z + m[15];
1513 * Function: M4x4.makeLookAt
1515 M4x4.makeLookAt = function M4x4_makeLookAt (eye, center, up, r) {
1516 //MathUtils_assert(eye.length == 3, "eye.length == 3");
1517 //MathUtils_assert(center.length == 3, "center.length == 3");
1518 //MathUtils_assert(up.length == 3, "up.length == 3");
1519 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1521 var z = V3.direction(eye, center, V3._temp1);
1522 var x = V3.normalize(V3.cross(up, z, V3._temp2), V3._temp2);
1523 var y = V3.normalize(V3.cross(z, x, V3._temp3), V3._temp3);
1525 var tm1 = M4x4._temp1;
1526 var tm2 = M4x4._temp2;
1545 tm2[0] = 1; tm2[1] = 0; tm2[2] = 0; tm2[3] = 0;
1546 tm2[4] = 0; tm2[5] = 1; tm2[6] = 0; tm2[7] = 0;
1547 tm2[8] = 0; tm2[9] = 0; tm2[10] = 1; tm2[11] = 0;
1548 tm2[12] = -eye[0]; tm2[13] = -eye[1]; tm2[14] = -eye[2]; tm2[15] = 1;
1551 r = new MJS_FLOAT_ARRAY_TYPE(16);
1552 return M4x4.mul(tm1, tm2, r);
1556 * Function: M4x4.transposeSelf
1558 M4x4.transposeSelf = function M4x4_transposeSelf (m) {
1559 //MathUtils_assert(m.length == 16, "m.length == 16");
1560 var tmp = m[1]; m[1] = m[4]; m[4] = tmp;
1561 tmp = m[2]; m[2] = m[8]; m[8] = tmp;
1562 tmp = m[3]; m[3] = m[12]; m[12] = tmp;
1563 tmp = m[6]; m[6] = m[9]; m[9] = tmp;
1564 tmp = m[7]; m[7] = m[13]; m[13] = tmp;
1565 tmp = m[11]; m[11] = m[14]; m[14] = tmp;
1569 * Function: M4x4.transpose
1571 M4x4.transpose = function M4x4_transpose (m, r) {
1572 //MathUtils_assert(m.length == 16, "m.length == 16");
1573 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1577 tmp = m[1]; m[1] = m[4]; m[4] = tmp;
1578 tmp = m[2]; m[2] = m[8]; m[8] = tmp;
1579 tmp = m[3]; m[3] = m[12]; m[12] = tmp;
1580 tmp = m[6]; m[6] = m[9]; m[9] = tmp;
1581 tmp = m[7]; m[7] = m[13]; m[13] = tmp;
1582 tmp = m[11]; m[11] = m[14]; m[14] = tmp;
1587 r = new MJS_FLOAT_ARRAY_TYPE(16);
1589 r[0] = m[0]; r[1] = m[4]; r[2] = m[8]; r[3] = m[12];
1590 r[4] = m[1]; r[5] = m[5]; r[6] = m[9]; r[7] = m[13];
1591 r[8] = m[2]; r[9] = m[6]; r[10] = m[10]; r[11] = m[14];
1592 r[12] = m[3]; r[13] = m[7]; r[14] = m[11]; r[15] = m[15];
1599 * Function: M4x4.transformPoint
1601 M4x4.transformPoint = function M4x4_transformPoint (m, v, r) {
1602 //MathUtils_assert(m.length == 16, "m.length == 16");
1603 //MathUtils_assert(v.length == 3, "v.length == 3");
1604 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
1606 r = new MJS_FLOAT_ARRAY_TYPE(3);
1608 var v0 = v[0], v1 = v[1], v2 = v[2];
1610 r[0] = m[0] * v0 + m[4] * v1 + m[8] * v2 + m[12];
1611 r[1] = m[1] * v0 + m[5] * v1 + m[9] * v2 + m[13];
1612 r[2] = m[2] * v0 + m[6] * v1 + m[10] * v2 + m[14];
1613 var w = m[3] * v0 + m[7] * v1 + m[11] * v2 + m[15];
1625 * Function: M4x4.transformLine
1627 M4x4.transformLine = function M4x4_transformLine(m, v, r) {
1628 //MathUtils_assert(m.length == 16, "m.length == 16");
1629 //MathUtils_assert(v.length == 3, "v.length == 3");
1630 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
1632 r = new MJS_FLOAT_ARRAY_TYPE(3);
1634 var v0 = v[0], v1 = v[1], v2 = v[2];
1635 r[0] = m[0] * v0 + m[4] * v1 + m[8] * v2;
1636 r[1] = m[1] * v0 + m[5] * v1 + m[9] * v2;
1637 r[2] = m[2] * v0 + m[6] * v1 + m[10] * v2;
1638 var w = m[3] * v0 + m[7] * v1 + m[11] * v2;
1651 * Function: M4x4.transformPointAffine
1653 M4x4.transformPointAffine = function M4x4_transformPointAffine (m, v, r) {
1654 //MathUtils_assert(m.length == 16, "m.length == 16");
1655 //MathUtils_assert(v.length == 3, "v.length == 3");
1656 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
1658 r = new MJS_FLOAT_ARRAY_TYPE(3);
1660 var v0 = v[0], v1 = v[1], v2 = v[2];
1662 r[0] = m[0] * v0 + m[4] * v1 + m[8] * v2 + m[12];
1663 r[1] = m[1] * v0 + m[5] * v1 + m[9] * v2 + m[13];
1664 r[2] = m[2] * v0 + m[6] * v1 + m[10] * v2 + m[14];
1670 * Function: M4x4.transformLineAffine
1672 M4x4.transformLineAffine = function M4x4_transformLineAffine(m, v, r) {
1673 //MathUtils_assert(m.length == 16, "m.length == 16");
1674 //MathUtils_assert(v.length == 3, "v.length == 3");
1675 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
1677 r = new MJS_FLOAT_ARRAY_TYPE(3);
1679 var v0 = v[0], v1 = v[1], v2 = v[2];
1680 r[0] = m[0] * v0 + m[4] * v1 + m[8] * v2;
1681 r[1] = m[1] * v0 + m[5] * v1 + m[9] * v2;
1682 r[2] = m[2] * v0 + m[6] * v1 + m[10] * v2;
1687 M4x4.makeBasis = function M4x4_makeBasis(vx,vy,vz) {
1689 var r = new MJS_FLOAT_ARRAY_TYPE(16);
1718 toTuple3: V3.toTuple3,
1719 toRecord3: V3.toRecord3,
1720 fromTuple3: V3.fromTuple3,
1721 fromRecord3: V3.fromRecord3,
1725 v3direction: F2(V3.direction),
1726 v3length: V3.length,
1727 v3lengthSquared: V3.lengthSquared,
1728 v3normalize: V3.normalize,
1729 v3scale: F2(V3.scale),
1731 v3cross: F2(V3.cross),
1732 v3mul4x4: F2(V3.mul4x4),
1734 m4x4identity: M4x4.identity,
1735 m4x4topLeft3x3: M4x4.topLeft3x3,
1736 m4x4inverseOrthonormal: M4x4.inverseOrthonormal,
1737 m4x4inverseTo3x3: M4x4.inverseTo3x3,
1738 m4x4makeFrustum: F6(M4x4.makeFrustum),
1739 m4x4makePerspective: F4(M4x4.makePerspective),
1740 m4x4makeOrtho: F6(M4x4.makeOrtho),
1741 m4x4makeOrtho2D: F4(M4x4.makeOrtho2D),
1742 m4x4mul: F2(M4x4.mul),
1743 m4x4Affine: F2(M4x4.mulAffine),
1744 m4x4makeRotate: F2(M4x4.makeRotate),
1745 m4x4rotate: F3(M4x4.rotate),
1746 m4x4makeScale3: F3(M4x4.makeScale3),
1747 m4x4makeScale1: M4x4.makeScale1,
1748 m4x4makeScale: M4x4.makeScale,
1749 m4x4scale3: F4(M4x4.scale3),
1750 m4x4scale: F2(M4x4.scale),
1751 m4x4makeTranslate3: F3(M4x4.makeTranslate3),
1752 m4x4makeTranslate: M4x4.makeTranslate,
1753 m4x4translate3: F4(M4x4.translate3),
1754 m4x4translate: F2(M4x4.translate),
1755 m4x4makeLookAt: F3(M4x4.makeLookAt),
1756 m4x4transpose: M4x4.transpose,
1757 m4x4transformPoint: F2(M4x4.transformPoint),
1758 m4x4makeBasis: F3(M4x4.makeBasis),
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list