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) {
144 V3.setX = function V3_setX(x, a) {
145 return new MJS_FLOAT_ARRAY_TYPE(x, a[1], a[2]);
147 V3.setY = function V3_setY(y, a) {
148 return new MJS_FLOAT_ARRAY_TYPE(a[0], y, a[2]);
150 V3.setZ = function V3_setZ(z, a) {
151 return new MJS_FLOAT_ARRAY_TYPE(a[0], a[1], z);
154 V3.toTuple3 = function V3_toTuple3(a) {
162 V3.fromTuple3 = function V3_fromTuple3(t) {
163 return new MJS_FLOAT_ARRAY_TYPE([t._0, t._1, t._2]);
166 V3.toRecord3 = function V3_toRecord3(a) {
174 V3.fromRecord3 = function V3_fromRecord3(r) {
175 return new MJS_FLOAT_ARRAY_TYPE([r.x, r.y, r.z]);
185 * a - the first vector operand
186 * b - the second vector operand
187 * r - optional vector to store the result in
191 * If r is specified, returns r after performing the operation.
192 * Otherwise, returns a new 3-element vector with the result.
194 V3.add = function V3_add(a, b, r) {
195 //MathUtils_assert(a.length == 3, "a.length == 3");
196 //MathUtils_assert(b.length == 3, "b.length == 3");
197 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
200 r = new MJS_FLOAT_ARRAY_TYPE(3);
215 * a - the first vector operand
216 * b - the second vector operand
217 * r - optional vector to store the result in
221 * If r is specified, returns r after performing the operation.
222 * Otherwise, returns a new 3-element vector with the result.
224 V3.sub = function V3_sub(a, b, r) {
225 //MathUtils_assert(a.length == 3, "a.length == 3");
226 //MathUtils_assert(b.length == 3, "b.length == 3");
227 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
230 r = new MJS_FLOAT_ARRAY_TYPE(3);
245 * a - the vector operand
246 * r - optional vector to store the result in
250 * If r is specified, returns r after performing the operation.
251 * Otherwise, returns a new 3-element vector with the result.
253 V3.neg = function V3_neg(a, r) {
254 //MathUtils_assert(a.length == 3, "a.length == 3");
255 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
258 r = new MJS_FLOAT_ARRAY_TYPE(3);
266 * Function: V3.direction
269 * r = (a - b) / |a - b|. The result is the normalized
270 * direction from a to b.
274 * a - the first vector operand
275 * b - the second vector operand
276 * r - optional vector to store the result in
280 * If r is specified, returns r after performing the operation.
281 * Otherwise, returns a new 3-element vector with the result.
283 V3.direction = function V3_direction(a, b, r) {
284 //MathUtils_assert(a.length == 3, "a.length == 3");
285 //MathUtils_assert(b.length == 3, "b.length == 3");
286 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
289 r = new MJS_FLOAT_ARRAY_TYPE(3);
290 return V3.normalize(V3.sub(a, b, r), r);
294 * Function: V3.length
300 * a - the vector operand
304 * The length of the given vector.
306 V3.length = function V3_length(a) {
307 //MathUtils_assert(a.length == 3, "a.length == 3");
309 return Math.sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
313 * Function: V3.lengthSquard
315 * Perform r = |a|*|a|.
319 * a - the vector operand
323 * The square of the length of the given vector.
325 V3.lengthSquared = function V3_lengthSquared(a) {
326 //MathUtils_assert(a.length == 3, "a.length == 3");
328 return a[0]*a[0] + a[1]*a[1] + a[2]*a[2];
331 V3.distance = function V3_distance(a, b) {
332 var dx = a[0] - b[0];
333 var dy = a[1] - b[1];
334 var dz = a[2] - b[2];
335 return Math.sqrt(dx * dx + dy * dy + dz * dz);
338 V3.distanceSquared = function V3_distanceSquared(a) {
339 var dx = a[0] - b[0];
340 var dy = a[1] - b[1];
341 var dz = a[2] - b[2];
342 return dx * dx + dy * dy + dz * dz;
346 * Function: V3.normalize
348 * Perform r = a / |a|.
352 * a - the vector operand
353 * r - optional vector to store the result in
357 * If r is specified, returns r after performing the operation.
358 * Otherwise, returns a new 3-element vector with the result.
360 V3.normalize = function V3_normalize(a, r) {
361 //MathUtils_assert(a.length == 3, "a.length == 3");
362 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
365 r = new MJS_FLOAT_ARRAY_TYPE(3);
366 var im = 1.0 / V3.length(a);
380 * a - the vector operand
382 * r - optional vector to store the result in
386 * If r is specified, returns r after performing the operation.
387 * Otherwise, returns a new 3-element vector with the result.
389 V3.scale = function V3_scale(a, k, r) {
390 //MathUtils_assert(a.length == 3, "a.length == 3");
391 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
394 r = new MJS_FLOAT_ARRAY_TYPE(3);
409 * a - the first vector operand
410 * b - the second vector operand
414 * The dot product of a and b.
416 V3.dot = function V3_dot(a, b) {
417 //MathUtils_assert(a.length == 3, "a.length == 3");
418 //MathUtils_assert(b.length == 3, "b.length == 3");
432 * a - the first vector operand
433 * b - the second vector operand
434 * r - optional vector to store the result in
438 * If r is specified, returns r after performing the operation.
439 * Otherwise, returns a new 3-element vector with the result.
441 V3.cross = function V3_cross(a, b, r) {
442 //MathUtils_assert(a.length == 3, "a.length == 3");
443 //MathUtils_assert(b.length == 3, "b.length == 3");
444 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
447 r = new MJS_FLOAT_ARRAY_TYPE(3);
448 r[0] = a[1]*b[2] - a[2]*b[1];
449 r[1] = a[2]*b[0] - a[0]*b[2];
450 r[2] = a[0]*b[1] - a[1]*b[0];
455 * Function: V3.mul4x4
462 * m - the 4x4 matrix operand
463 * v - the 3-element vector operand
464 * r - optional vector to store the result in
468 * If r is specified, returns r after performing the operation.
469 * Otherwise, returns a new 3-element vector with the result.
470 * The 4-element result vector is divided by the w component
471 * and returned as a 3-element vector.
473 V3.mul4x4 = function V3_mul4x4(m, v, r) {
474 //MathUtils_assert(m.length == 16, "m.length == 16");
475 //MathUtils_assert(v.length == 3, "v.length == 3");
476 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
481 r = new MJS_FLOAT_ARRAY_TYPE(3);
485 w = V3.dot(v, tmp) + m[15];
489 r[0] = (V3.dot(v, tmp) + m[12])/w;
493 r[1] = (V3.dot(v, tmp) + m[13])/w;
497 r[2] = (V3.dot(v, tmp) + m[14])/w;
504 * Methods for working with 4x4 matrices. A matrix is represented by a 16-element array
505 * in column-major order. Any valid JavaScript array type may be used, but if new
506 * matrices are created they are created using the configured MJS_FLOAT_ARRAY_TYPE.
511 M4x4._temp1 = new MJS_FLOAT_ARRAY_TYPE(16);
512 M4x4._temp2 = new MJS_FLOAT_ARRAY_TYPE(16);
514 if (MJS_FLOAT_ARRAY_TYPE == Array) {
515 M4x4.I = [1.0, 0.0, 0.0, 0.0,
520 M4x4.$ = function M4x4_$(m00, m01, m02, m03,
525 return [m00, m01, m02, m03,
531 M4x4.I = new MJS_FLOAT_ARRAY_TYPE([1.0, 0.0, 0.0, 0.0,
534 0.0, 0.0, 0.0, 1.0]);
539 * Creates a new 4x4 matrix with the given values.
543 * m00..m15 - the 16 elements of the new matrix.
547 * A new matrix filled with the given argument values.
549 M4x4.$ = function M4x4_$(m00, m01, m02, m03,
554 return new MJS_FLOAT_ARRAY_TYPE([m00, m01, m02, m03,
557 m12, m13, m14, m15]);
561 M4x4.identity = M4x4.I;
564 * Function: M4x4.topLeft3x3
566 * Return the top left 3x3 matrix from the given 4x4 matrix m.
571 * r - optional 3x3 matrix to store the result in
575 * If r is specified, returns r after performing the operation.
576 * Otherwise, returns a new 3x3 matrix with the result.
578 M4x4.topLeft3x3 = function M4x4_topLeft3x3(m, r) {
579 //MathUtils_assert(m.length == 16, "m.length == 16");
580 //MathUtils_assert(r == undefined || r.length == 9, "r == undefined || r.length == 9");
583 r = new MJS_FLOAT_ARRAY_TYPE(9);
584 r[0] = m[0]; r[1] = m[1]; r[2] = m[2];
585 r[3] = m[4]; r[4] = m[5]; r[5] = m[6];
586 r[6] = m[8]; r[7] = m[9]; r[8] = m[10];
591 * Function: M4x4.inverseOrthonormal
593 * Computes the inverse of the given matrix m, assuming that
594 * the matrix is orthonormal.
599 * r - optional 4x4 matrix to store the result in
603 * If r is specified, returns r after performing the operation.
604 * Otherwise, returns a new 4x4 matrix with the result.
606 M4x4.inverseOrthonormal = function M4x4_inverseOrthonormal(m, r) {
607 //MathUtils_assert(m.length == 16, "m.length == 16");
608 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
609 //MathUtils_assert(m != r, "m != r");
612 r = new MJS_FLOAT_ARRAY_TYPE(16);
613 M4x4.transpose(m, r);
614 var t = [m[12], m[13], m[14]];
615 r[3] = r[7] = r[11] = 0;
616 r[12] = -V3.dot([r[0], r[4], r[8]], t);
617 r[13] = -V3.dot([r[1], r[5], r[9]], t);
618 r[14] = -V3.dot([r[2], r[6], r[10]], t);
623 * Function: M4x4.inverseTo3x3
625 * Computes the inverse of the given matrix m, but calculates
626 * only the top left 3x3 values of the result.
631 * r - optional 3x3 matrix to store the result in
635 * If r is specified, returns r after performing the operation.
636 * Otherwise, returns a new 3x3 matrix with the result.
638 M4x4.inverseTo3x3 = function M4x4_inverseTo3x3(m, r) {
639 //MathUtils_assert(m.length == 16, "m.length == 16");
640 //MathUtils_assert(r == undefined || r.length == 9, "r == undefined || r.length == 9");
643 r = new MJS_FLOAT_ARRAY_TYPE(9);
645 var a11 = m[10]*m[5]-m[6]*m[9],
646 a21 = -m[10]*m[1]+m[2]*m[9],
647 a31 = m[6]*m[1]-m[2]*m[5],
648 a12 = -m[10]*m[4]+m[6]*m[8],
649 a22 = m[10]*m[0]-m[2]*m[8],
650 a32 = -m[6]*m[0]+m[2]*m[4],
651 a13 = m[9]*m[4]-m[5]*m[8],
652 a23 = -m[9]*m[0]+m[1]*m[8],
653 a33 = m[5]*m[0]-m[1]*m[4];
654 var det = m[0]*(a11) + m[1]*(a12) + m[2]*(a13);
655 if (det == 0) // no inverse
656 throw "matrix not invertible";
657 var idet = 1.0 / det;
673 * Function: M4x4.makeFrustum
675 * Creates a matrix for a projection frustum with the given parameters.
679 * left - the left coordinate of the frustum
680 * right- the right coordinate of the frustum
681 * bottom - the bottom coordinate of the frustum
682 * top - the top coordinate of the frustum
683 * znear - the near z distance of the frustum
684 * zfar - the far z distance of the frustum
685 * r - optional 4x4 matrix to store the result in
689 * If r is specified, returns r after creating the projection matrix.
690 * Otherwise, returns a new 4x4 matrix.
692 M4x4.makeFrustum = function M4x4_makeFrustum(left, right, bottom, top, znear, zfar, r) {
693 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
696 r = new MJS_FLOAT_ARRAY_TYPE(16);
698 var X = 2*znear/(right-left);
699 var Y = 2*znear/(top-bottom);
700 var A = (right+left)/(right-left);
701 var B = (top+bottom)/(top-bottom);
702 var C = -(zfar+znear)/(zfar-znear);
703 var D = -2*zfar*znear/(zfar-znear);
705 r[0] = 2*znear/(right-left);
710 r[5] = 2*znear/(top-bottom);
713 r[8] = (right+left)/(right-left);
714 r[9] = (top+bottom)/(top-bottom);
715 r[10] = -(zfar+znear)/(zfar-znear);
719 r[14] = -2*zfar*znear/(zfar-znear);
726 * Function: M4x4.makePerspective
728 * Creates a matrix for a perspective projection with the given parameters.
732 * fovy - field of view in the y axis, in degrees
733 * aspect - aspect ratio
734 * znear - the near z distance of the projection
735 * zfar - the far z distance of the projection
736 * r - optional 4x4 matrix to store the result in
740 * If r is specified, returns r after creating the projection matrix.
741 * Otherwise, returns a new 4x4 matrix.
743 M4x4.makePerspective = function M4x4_makePerspective (fovy, aspect, znear, zfar, r) {
744 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
746 var ymax = znear * Math.tan(fovy * Math.PI / 360.0);
748 var xmin = ymin * aspect;
749 var xmax = ymax * aspect;
751 return M4x4.makeFrustum(xmin, xmax, ymin, ymax, znear, zfar, r);
755 * Function: M4x4.makeOrtho
757 * Creates a matrix for an orthogonal frustum projection with the given parameters.
761 * left - the left coordinate of the frustum
762 * right- the right coordinate of the frustum
763 * bottom - the bottom coordinate of the frustum
764 * top - the top coordinate of the frustum
765 * znear - the near z distance of the frustum
766 * zfar - the far z distance of the frustum
767 * r - optional 4x4 matrix to store the result in
771 * If r is specified, returns r after creating the projection matrix.
772 * Otherwise, returns a new 4x4 matrix.
774 M4x4.makeOrtho = function M4x4_makeOrtho (left, right, bottom, top, znear, zfar, r) {
775 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
778 r = new MJS_FLOAT_ARRAY_TYPE(16);
780 var tX = -(right+left)/(right-left);
781 var tY = -(top+bottom)/(top-bottom);
782 var tZ = -(zfar+znear)/(zfar-znear);
783 var X = 2 / (right-left);
784 var Y = 2 / (top-bottom);
785 var Z = -2 / (zfar-znear);
787 r[0] = 2 / (right-left);
792 r[5] = 2 / (top-bottom);
797 r[10] = -2 / (zfar-znear);
799 r[12] = -(right+left)/(right-left);
800 r[13] = -(top+bottom)/(top-bottom);
801 r[14] = -(zfar+znear)/(zfar-znear);
808 * Function: M4x4.makeOrtho2D
810 * Creates a matrix for a 2D orthogonal frustum projection with the given parameters.
811 * znear and zfar are assumed to be -1 and 1, respectively.
815 * left - the left coordinate of the frustum
816 * right- the right coordinate of the frustum
817 * bottom - the bottom coordinate of the frustum
818 * top - the top coordinate of the frustum
819 * r - optional 4x4 matrix to store the result in
823 * If r is specified, returns r after creating the projection matrix.
824 * Otherwise, returns a new 4x4 matrix.
826 M4x4.makeOrtho2D = function M4x4_makeOrtho2D (left, right, bottom, top, r) {
827 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
829 return M4x4.makeOrtho(left, right, bottom, top, -1, 1, r);
835 * Performs r = a * b.
839 * a - the first matrix operand
840 * b - the second matrix operand
841 * r - optional 4x4 matrix to store the result in
845 * If r is specified, returns r after performing the operation.
846 * Otherwise, returns a new 4x4 matrix with the result.
848 M4x4.mul = function M4x4_mul(a, b, r) {
849 //MathUtils_assert(a.length == 16, "a.length == 16");
850 //MathUtils_assert(b.length == 16, "b.length == 16");
851 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
852 //MathUtils_assert(a != r && b != r, "a != r && b != r");
855 r = new MJS_FLOAT_ARRAY_TYPE(16);
891 r[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
892 r[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
893 r[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
894 r[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
895 r[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
896 r[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
897 r[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
898 r[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
899 r[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
900 r[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
901 r[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
902 r[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
903 r[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
904 r[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
905 r[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
906 r[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
912 * Function: M4x4.mulAffine
914 * Performs r = a * b, assuming a and b are affine (elements 3,7,11,15 = 0,0,0,1)
918 * a - the first matrix operand
919 * b - the second matrix operand
920 * r - optional 4x4 matrix to store the result in
924 * If r is specified, returns r after performing the operation.
925 * Otherwise, returns a new 4x4 matrix with the result.
927 M4x4.mulAffine = function M4x4_mulAffine(a, b, r) {
928 //MathUtils_assert(a.length == 16, "a.length == 16");
929 //MathUtils_assert(b.length == 16, "b.length == 16");
930 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
931 //MathUtils_assert(a != r && b != r, "a != r && b != r");
934 r = new MJS_FLOAT_ARRAY_TYPE(16);
961 r[0] = a11 * b11 + a12 * b21 + a13 * b31;
962 r[1] = a21 * b11 + a22 * b21 + a23 * b31;
963 r[2] = a31 * b11 + a32 * b21 + a33 * b31;
965 r[4] = a11 * b12 + a12 * b22 + a13 * b32;
966 r[5] = a21 * b12 + a22 * b22 + a23 * b32;
967 r[6] = a31 * b12 + a32 * b22 + a33 * b32;
969 r[8] = a11 * b13 + a12 * b23 + a13 * b33;
970 r[9] = a21 * b13 + a22 * b23 + a23 * b33;
971 r[10] = a31 * b13 + a32 * b23 + a33 * b33;
973 r[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14;
974 r[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24;
975 r[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34;
982 * Function: M4x4.makeRotate
984 * Creates a transformation matrix for rotation by angle radians about the 3-element vector axis.
988 * angle - the angle of rotation, in radians
989 * axis - the axis around which the rotation is performed, a 3-element vector
990 * r - optional 4x4 matrix to store the result in
994 * If r is specified, returns r after creating the matrix.
995 * Otherwise, returns a new 4x4 matrix with the result.
997 M4x4.makeRotate = function M4x4_makeRotate(angle, axis, r) {
998 //MathUtils_assert(angle.length == 3, "angle.length == 3");
999 //MathUtils_assert(axis.length == 3, "axis.length == 3");
1000 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1003 r = new MJS_FLOAT_ARRAY_TYPE(16);
1005 axis = V3.normalize(axis, V3._temp1);
1006 var x = axis[0], y = axis[1], z = axis[2];
1007 var c = Math.cos(angle);
1009 var s = Math.sin(angle);
1032 * Function: M4x4.rotate
1034 * Concatenates a rotation of angle radians about the axis to the give matrix m.
1038 * angle - the angle of rotation, in radians
1039 * axis - the axis around which the rotation is performed, a 3-element vector
1040 * m - the matrix to concatenate the rotation to
1041 * r - optional 4x4 matrix to store the result in
1045 * If r is specified, returns r after performing the operation.
1046 * Otherwise, returns a new 4x4 matrix with the result.
1048 M4x4.rotate = function M4x4_rotate(angle, axis, m, r) {
1049 //MathUtils_assert(angle.length == 3, "angle.length == 3");
1050 //MathUtils_assert(axis.length == 3, "axis.length == 3");
1051 //MathUtils_assert(m.length == 16, "m.length == 16");
1052 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1055 r = new MJS_FLOAT_ARRAY_TYPE(16);
1056 var a0=axis [0], a1=axis [1], a2=axis [2];
1057 var l = Math.sqrt(a0*a0 + a1*a1 + a2*a2);
1058 var x = a0, y = a1, z = a2;
1065 var c = Math.cos(angle);
1067 var s = Math.sin(angle);
1071 var xyc1 = x * y * c1;
1072 var xzc1 = x * z * c1;
1073 var yzc1 = y * z * c1;
1088 var t11 = x * x * c1 + c;
1089 var t21 = xyc1 + zs;
1090 var t31 = xzc1 - ys;
1091 var t12 = xyc1 - zs;
1092 var t22 = y * y * c1 + c;
1093 var t32 = yzc1 + xs;
1094 var t13 = xzc1 + ys;
1095 var t23 = yzc1 - xs;
1096 var t33 = z * z * c1 + c;
1098 r[0] = m11 * t11 + m12 * t21 + m13 * t31;
1099 r[1] = m21 * t11 + m22 * t21 + m23 * t31;
1100 r[2] = m31 * t11 + m32 * t21 + m33 * t31;
1101 r[3] = m41 * t11 + m42 * t21 + m43 * t31;
1102 r[4] = m11 * t12 + m12 * t22 + m13 * t32;
1103 r[5] = m21 * t12 + m22 * t22 + m23 * t32;
1104 r[6] = m31 * t12 + m32 * t22 + m33 * t32;
1105 r[7] = m41 * t12 + m42 * t22 + m43 * t32;
1106 r[8] = m11 * t13 + m12 * t23 + m13 * t33;
1107 r[9] = m21 * t13 + m22 * t23 + m23 * t33;
1108 r[10] = m31 * t13 + m32 * t23 + m33 * t33;
1109 r[11] = m41 * t13 + m42 * t23 + m43 * t33;
1120 * Function: M4x4.makeScale3
1122 * Creates a transformation matrix for scaling by 3 scalar values, one for
1123 * each of the x, y, and z directions.
1127 * x - the scale for the x axis
1128 * y - the scale for the y axis
1129 * z - the scale for the z axis
1130 * r - optional 4x4 matrix to store the result in
1134 * If r is specified, returns r after creating the matrix.
1135 * Otherwise, returns a new 4x4 matrix with the result.
1137 M4x4.makeScale3 = function M4x4_makeScale3(x, y, z, r) {
1138 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1141 r = new MJS_FLOAT_ARRAY_TYPE(16);
1164 * Function: M4x4.makeScale1
1166 * Creates a transformation matrix for a uniform scale by a single scalar value.
1170 * k - the scale factor
1171 * r - optional 4x4 matrix to store the result in
1175 * If r is specified, returns r after creating the matrix.
1176 * Otherwise, returns a new 4x4 matrix with the result.
1178 M4x4.makeScale1 = function M4x4_makeScale1(k, r) {
1179 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1181 return M4x4.makeScale3(k, k, k, r);
1185 * Function: M4x4.makeScale
1187 * Creates a transformation matrix for scaling each of the x, y, and z axes by the amount
1188 * given in the corresponding element of the 3-element vector.
1192 * v - the 3-element vector containing the scale factors
1193 * r - optional 4x4 matrix to store the result in
1197 * If r is specified, returns r after creating the matrix.
1198 * Otherwise, returns a new 4x4 matrix with the result.
1200 M4x4.makeScale = function M4x4_makeScale(v, r) {
1201 //MathUtils_assert(v.length == 3, "v.length == 3");
1202 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1204 return M4x4.makeScale3(v[0], v[1], v[2], r);
1208 * Function: M4x4.scale3
1210 M4x4.scale3 = function M4x4_scale3(x, y, z, m, r) {
1211 //MathUtils_assert(m.length == 16, "m.length == 16");
1212 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1231 r = new MJS_FLOAT_ARRAY_TYPE(16);
1254 * Function: M4x4.scale1
1256 M4x4.scale1 = function M4x4_scale1(k, m, r) {
1257 //MathUtils_assert(m.length == 16, "m.length == 16");
1258 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1277 r = new MJS_FLOAT_ARRAY_TYPE(16);
1300 * Function: M4x4.scale1
1302 M4x4.scale = function M4x4_scale(v, m, r) {
1303 //MathUtils_assert(v.length == 3, "v.length == 3");
1304 //MathUtils_assert(m.length == 16, "m.length == 16");
1305 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1306 var x = v[0], y = v[1], z = v[2];
1325 r = new MJS_FLOAT_ARRAY_TYPE(16);
1349 * Function: M4x4.makeTranslate3
1351 M4x4.makeTranslate3 = function M4x4_makeTranslate3(x, y, z, r) {
1352 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1355 r = new MJS_FLOAT_ARRAY_TYPE(16);
1378 * Function: M4x4.makeTranslate1
1380 M4x4.makeTranslate1 = function M4x4_makeTranslate1 (k, r) {
1381 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1383 return M4x4.makeTranslate3(k, k, k, r);
1387 * Function: M4x4.makeTranslate
1389 M4x4.makeTranslate = function M4x4_makeTranslate (v, r) {
1390 //MathUtils_assert(v.length == 3, "v.length == 3");
1391 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1393 return M4x4.makeTranslate3(v[0], v[1], v[2], r);
1397 * Function: M4x4.translate3Self
1399 M4x4.translate3Self = function M4x4_translate3Self (x, y, z, m) {
1400 //MathUtils_assert(m.length == 16, "m.length == 16");
1401 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1402 m[12] += m[0] * x + m[4] * y + m[8] * z;
1403 m[13] += m[1] * x + m[5] * y + m[9] * z;
1404 m[14] += m[2] * x + m[6] * y + m[10] * z;
1405 m[15] += m[3] * x + m[7] * y + m[11] * z;
1410 * Function: M4x4.translate3
1412 M4x4.translate3 = function M4x4_translate3 (x, y, z, m, r) {
1413 //MathUtils_assert(m.length == 16, "m.length == 16");
1414 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1417 m[12] += m[0] * x + m[4] * y + m[8] * z;
1418 m[13] += m[1] * x + m[5] * y + m[9] * z;
1419 m[14] += m[2] * x + m[6] * y + m[10] * z;
1420 m[15] += m[3] * x + m[7] * y + m[11] * z;
1425 r = new MJS_FLOAT_ARRAY_TYPE(16);
1453 r[12] = m11 * x + m12 * y + m13 * z + m[12];
1454 r[13] = m21 * x + m22 * y + m23 * z + m[13];
1455 r[14] = m31 * x + m32 * y + m33 * z + m[14];
1456 r[15] = m41 * x + m42 * y + m43 * z + m[15];
1462 * Function: M4x4.translate1
1464 M4x4.translate1 = function M4x4_translate1 (k, m, r) {
1465 //MathUtils_assert(m.length == 16, "m.length == 16");
1466 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1468 return M4x4.translate3(k, k, k, m, r);
1471 * Function: M4x4.translateSelf
1473 M4x4.translateSelf = function M4x4_translateSelf (v, m) {
1474 //MathUtils_assert(v.length == 3, "v.length == 3");
1475 //MathUtils_assert(m.length == 16, "m.length == 16");
1476 var x=v[0], y=v[1], z=v[2];
1477 m[12] += m[0] * x + m[4] * y + m[8] * z;
1478 m[13] += m[1] * x + m[5] * y + m[9] * z;
1479 m[14] += m[2] * x + m[6] * y + m[10] * z;
1480 m[15] += m[3] * x + m[7] * y + m[11] * z;
1484 * Function: M4x4.translate
1486 M4x4.translate = function M4x4_translate (v, m, r) {
1487 //MathUtils_assert(v.length == 3, "v.length == 3");
1488 //MathUtils_assert(m.length == 16, "m.length == 16");
1489 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1490 var x=v[0], y=v[1], z=v[2];
1492 m[12] += m[0] * x + m[4] * y + m[8] * z;
1493 m[13] += m[1] * x + m[5] * y + m[9] * z;
1494 m[14] += m[2] * x + m[6] * y + m[10] * z;
1495 m[15] += m[3] * x + m[7] * y + m[11] * z;
1500 r = new MJS_FLOAT_ARRAY_TYPE(16);
1527 r[12] = m11 * x + m12 * y + m13 * z + m[12];
1528 r[13] = m21 * x + m22 * y + m23 * z + m[13];
1529 r[14] = m31 * x + m32 * y + m33 * z + m[14];
1530 r[15] = m41 * x + m42 * y + m43 * z + m[15];
1536 * Function: M4x4.makeLookAt
1538 M4x4.makeLookAt = function M4x4_makeLookAt (eye, center, up, r) {
1539 //MathUtils_assert(eye.length == 3, "eye.length == 3");
1540 //MathUtils_assert(center.length == 3, "center.length == 3");
1541 //MathUtils_assert(up.length == 3, "up.length == 3");
1542 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1544 var z = V3.direction(eye, center, V3._temp1);
1545 var x = V3.normalize(V3.cross(up, z, V3._temp2), V3._temp2);
1546 var y = V3.normalize(V3.cross(z, x, V3._temp3), V3._temp3);
1548 var tm1 = M4x4._temp1;
1549 var tm2 = M4x4._temp2;
1568 tm2[0] = 1; tm2[1] = 0; tm2[2] = 0; tm2[3] = 0;
1569 tm2[4] = 0; tm2[5] = 1; tm2[6] = 0; tm2[7] = 0;
1570 tm2[8] = 0; tm2[9] = 0; tm2[10] = 1; tm2[11] = 0;
1571 tm2[12] = -eye[0]; tm2[13] = -eye[1]; tm2[14] = -eye[2]; tm2[15] = 1;
1574 r = new MJS_FLOAT_ARRAY_TYPE(16);
1575 return M4x4.mul(tm1, tm2, r);
1579 * Function: M4x4.transposeSelf
1581 M4x4.transposeSelf = function M4x4_transposeSelf (m) {
1582 //MathUtils_assert(m.length == 16, "m.length == 16");
1583 var tmp = m[1]; m[1] = m[4]; m[4] = tmp;
1584 tmp = m[2]; m[2] = m[8]; m[8] = tmp;
1585 tmp = m[3]; m[3] = m[12]; m[12] = tmp;
1586 tmp = m[6]; m[6] = m[9]; m[9] = tmp;
1587 tmp = m[7]; m[7] = m[13]; m[13] = tmp;
1588 tmp = m[11]; m[11] = m[14]; m[14] = tmp;
1592 * Function: M4x4.transpose
1594 M4x4.transpose = function M4x4_transpose (m, r) {
1595 //MathUtils_assert(m.length == 16, "m.length == 16");
1596 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1600 tmp = m[1]; m[1] = m[4]; m[4] = tmp;
1601 tmp = m[2]; m[2] = m[8]; m[8] = tmp;
1602 tmp = m[3]; m[3] = m[12]; m[12] = tmp;
1603 tmp = m[6]; m[6] = m[9]; m[9] = tmp;
1604 tmp = m[7]; m[7] = m[13]; m[13] = tmp;
1605 tmp = m[11]; m[11] = m[14]; m[14] = tmp;
1610 r = new MJS_FLOAT_ARRAY_TYPE(16);
1612 r[0] = m[0]; r[1] = m[4]; r[2] = m[8]; r[3] = m[12];
1613 r[4] = m[1]; r[5] = m[5]; r[6] = m[9]; r[7] = m[13];
1614 r[8] = m[2]; r[9] = m[6]; r[10] = m[10]; r[11] = m[14];
1615 r[12] = m[3]; r[13] = m[7]; r[14] = m[11]; r[15] = m[15];
1622 * Function: M4x4.transformPoint
1624 M4x4.transformPoint = function M4x4_transformPoint (m, v, r) {
1625 //MathUtils_assert(m.length == 16, "m.length == 16");
1626 //MathUtils_assert(v.length == 3, "v.length == 3");
1627 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
1629 r = new MJS_FLOAT_ARRAY_TYPE(3);
1631 var v0 = v[0], v1 = v[1], v2 = v[2];
1633 r[0] = m[0] * v0 + m[4] * v1 + m[8] * v2 + m[12];
1634 r[1] = m[1] * v0 + m[5] * v1 + m[9] * v2 + m[13];
1635 r[2] = m[2] * v0 + m[6] * v1 + m[10] * v2 + m[14];
1636 var w = m[3] * v0 + m[7] * v1 + m[11] * v2 + m[15];
1648 * Function: M4x4.transformLine
1650 M4x4.transformLine = function M4x4_transformLine(m, v, r) {
1651 //MathUtils_assert(m.length == 16, "m.length == 16");
1652 //MathUtils_assert(v.length == 3, "v.length == 3");
1653 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
1655 r = new MJS_FLOAT_ARRAY_TYPE(3);
1657 var v0 = v[0], v1 = v[1], v2 = v[2];
1658 r[0] = m[0] * v0 + m[4] * v1 + m[8] * v2;
1659 r[1] = m[1] * v0 + m[5] * v1 + m[9] * v2;
1660 r[2] = m[2] * v0 + m[6] * v1 + m[10] * v2;
1661 var w = m[3] * v0 + m[7] * v1 + m[11] * v2;
1674 * Function: M4x4.transformPointAffine
1676 M4x4.transformPointAffine = function M4x4_transformPointAffine (m, v, r) {
1677 //MathUtils_assert(m.length == 16, "m.length == 16");
1678 //MathUtils_assert(v.length == 3, "v.length == 3");
1679 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
1681 r = new MJS_FLOAT_ARRAY_TYPE(3);
1683 var v0 = v[0], v1 = v[1], v2 = v[2];
1685 r[0] = m[0] * v0 + m[4] * v1 + m[8] * v2 + m[12];
1686 r[1] = m[1] * v0 + m[5] * v1 + m[9] * v2 + m[13];
1687 r[2] = m[2] * v0 + m[6] * v1 + m[10] * v2 + m[14];
1693 * Function: M4x4.transformLineAffine
1695 M4x4.transformLineAffine = function M4x4_transformLineAffine(m, v, r) {
1696 //MathUtils_assert(m.length == 16, "m.length == 16");
1697 //MathUtils_assert(v.length == 3, "v.length == 3");
1698 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
1700 r = new MJS_FLOAT_ARRAY_TYPE(3);
1702 var v0 = v[0], v1 = v[1], v2 = v[2];
1703 r[0] = m[0] * v0 + m[4] * v1 + m[8] * v2;
1704 r[1] = m[1] * v0 + m[5] * v1 + m[9] * v2;
1705 r[2] = m[2] * v0 + m[6] * v1 + m[10] * v2;
1710 M4x4.makeBasis = function M4x4_makeBasis(vx,vy,vz) {
1712 var r = new MJS_FLOAT_ARRAY_TYPE(16);
1741 v3setX: F2(V3.setX),
1742 v3setY: F2(V3.setY),
1743 v3setZ: F2(V3.setZ),
1744 toTuple3: V3.toTuple3,
1745 toRecord3: V3.toRecord3,
1746 fromTuple3: V3.fromTuple3,
1747 fromRecord3: V3.fromRecord3,
1751 v3direction: F2(V3.direction),
1752 v3length: V3.length,
1753 v3lengthSquared: V3.lengthSquared,
1754 v3distance: F2(V3.distance),
1755 v3distanceSquared: F2(V3.distanceSquared),
1756 v3normalize: V3.normalize,
1757 v3scale: F2(V3.scale),
1759 v3cross: F2(V3.cross),
1760 v3mul4x4: F2(V3.mul4x4),
1762 m4x4identity: M4x4.identity,
1763 m4x4topLeft3x3: M4x4.topLeft3x3,
1764 m4x4inverseOrthonormal: M4x4.inverseOrthonormal,
1765 m4x4inverseTo3x3: M4x4.inverseTo3x3,
1766 m4x4makeFrustum: F6(M4x4.makeFrustum),
1767 m4x4makePerspective: F4(M4x4.makePerspective),
1768 m4x4makeOrtho: F6(M4x4.makeOrtho),
1769 m4x4makeOrtho2D: F4(M4x4.makeOrtho2D),
1770 m4x4mul: F2(M4x4.mul),
1771 m4x4Affine: F2(M4x4.mulAffine),
1772 m4x4makeRotate: F2(M4x4.makeRotate),
1773 m4x4rotate: F3(M4x4.rotate),
1774 m4x4makeScale3: F3(M4x4.makeScale3),
1775 m4x4makeScale1: M4x4.makeScale1,
1776 m4x4makeScale: M4x4.makeScale,
1777 m4x4scale3: F4(M4x4.scale3),
1778 m4x4scale: F2(M4x4.scale),
1779 m4x4makeTranslate3: F3(M4x4.makeTranslate3),
1780 m4x4makeTranslate: M4x4.makeTranslate,
1781 m4x4translate3: F4(M4x4.translate3),
1782 m4x4translate: F2(M4x4.translate),
1783 m4x4makeLookAt: F3(M4x4.makeLookAt),
1784 m4x4transpose: M4x4.transpose,
1785 m4x4transformPoint: F2(M4x4.transformPoint),
1786 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