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 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.WebGL.values) return elm.Native.WebGL.values;
44 * Constant: MJS_VERSION
46 * mjs version number aa.bb.cc, encoded as an integer of the form:
49 const 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 const MJS_DO_ASSERT = true;
61 // Some hacks for running in both the shell and browser,
62 // and for supporting F32 and WebGLFloat arrays
63 try { WebGLFloatArray; } catch (x) { WebGLFloatArray = Float32Array; }
66 * Constant: MJS_FLOAT_ARRAY_TYPE
68 * The base float array type. By default, WebGLFloatArray.
70 * mjs can work with any array-like elements, but if an array
71 * creation is requested, it will create an array of the type
72 * MJS_FLOAT_ARRAY_TYPE. Also, the builtin constants such as (M4x4.I)
73 * will be of this type.
75 const MJS_FLOAT_ARRAY_TYPE = WebGLFloatArray;
76 //const MJS_FLOAT_ARRAY_TYPE = Float32Array;
77 //const MJS_FLOAT_ARRAY_TYPE = Float64Array;
78 //const MJS_FLOAT_ARRAY_TYPE = Array;
81 function MathUtils_assert(cond, msg) {
83 throw "Assertion failed: " + msg;
86 function MathUtils_assert() { }
92 * Methods for working with 3-element vectors. A vector is represented by a 3-element array.
93 * Any valid JavaScript array type may be used, but if new
94 * vectors are created they are created using the configured MJS_FLOAT_ARRAY_TYPE.
99 V3._temp1 = new MJS_FLOAT_ARRAY_TYPE(3);
100 V3._temp2 = new MJS_FLOAT_ARRAY_TYPE(3);
101 V3._temp3 = new MJS_FLOAT_ARRAY_TYPE(3);
103 if (MJS_FLOAT_ARRAY_TYPE == Array) {
104 V3.x = [1.0, 0.0, 0.0];
105 V3.y = [0.0, 1.0, 0.0];
106 V3.z = [0.0, 0.0, 1.0];
108 V3.$ = function V3_$(x, y, z) {
112 V3.clone = function V3_clone(a) {
113 //MathUtils_assert(a.length == 3, "a.length == 3");
114 return [a[0], a[1], a[2]];
117 V3.x = new MJS_FLOAT_ARRAY_TYPE([1.0, 0.0, 0.0]);
118 V3.y = new MJS_FLOAT_ARRAY_TYPE([0.0, 1.0, 0.0]);
119 V3.z = new MJS_FLOAT_ARRAY_TYPE([0.0, 0.0, 1.0]);
124 * Creates a new 3-element vector with the given values.
128 * x,y,z - the 3 elements of the new vector.
132 * A new vector containing with the given argument values.
135 V3.$ = function V3_$(x, y, z) {
136 return new MJS_FLOAT_ARRAY_TYPE([x, y, z]);
142 * Clone the given 3-element vector.
146 * a - the 3-element vector to clone
150 * A new vector with the same values as the passed-in one.
153 V3.clone = function V3_clone(a) {
154 //MathUtils_assert(a.length == 3, "a.length == 3");
155 return new MJS_FLOAT_ARRAY_TYPE(a);
169 * a - the first vector operand
170 * b - the second vector operand
171 * r - optional vector to store the result in
175 * If r is specified, returns r after performing the operation.
176 * Otherwise, returns a new 3-element vector with the result.
178 V3.add = function V3_add(a, b, r) {
179 //MathUtils_assert(a.length == 3, "a.length == 3");
180 //MathUtils_assert(b.length == 3, "b.length == 3");
181 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
184 r = new MJS_FLOAT_ARRAY_TYPE(3);
199 * a - the first vector operand
200 * b - the second vector operand
201 * r - optional vector to store the result in
205 * If r is specified, returns r after performing the operation.
206 * Otherwise, returns a new 3-element vector with the result.
208 V3.sub = function V3_sub(a, b, r) {
209 //MathUtils_assert(a.length == 3, "a.length == 3");
210 //MathUtils_assert(b.length == 3, "b.length == 3");
211 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
214 r = new MJS_FLOAT_ARRAY_TYPE(3);
229 * a - the vector operand
230 * r - optional vector to store the result in
234 * If r is specified, returns r after performing the operation.
235 * Otherwise, returns a new 3-element vector with the result.
237 V3.neg = function V3_neg(a, r) {
238 //MathUtils_assert(a.length == 3, "a.length == 3");
239 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
242 r = new MJS_FLOAT_ARRAY_TYPE(3);
250 * Function: V3.direction
253 * r = (a - b) / |a - b|. The result is the normalized
254 * direction from a to b.
258 * a - the first vector operand
259 * b - the second vector operand
260 * r - optional vector to store the result in
264 * If r is specified, returns r after performing the operation.
265 * Otherwise, returns a new 3-element vector with the result.
267 V3.direction = function V3_direction(a, b, r) {
268 //MathUtils_assert(a.length == 3, "a.length == 3");
269 //MathUtils_assert(b.length == 3, "b.length == 3");
270 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
273 r = new MJS_FLOAT_ARRAY_TYPE(3);
274 return V3.normalize(V3.sub(a, b, r), r);
278 * Function: V3.length
284 * a - the vector operand
288 * The length of the given vector.
290 V3.length = function V3_length(a) {
291 //MathUtils_assert(a.length == 3, "a.length == 3");
293 return Math.sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
297 * Function: V3.lengthSquard
299 * Perform r = |a|*|a|.
303 * a - the vector operand
307 * The square of the length of the given vector.
309 V3.lengthSquared = function V3_lengthSquared(a) {
310 //MathUtils_assert(a.length == 3, "a.length == 3");
312 return a[0]*a[0] + a[1]*a[1] + a[2]*a[2];
316 * Function: V3.normalize
318 * Perform r = a / |a|.
322 * a - the vector operand
323 * r - optional vector to store the result in
327 * If r is specified, returns r after performing the operation.
328 * Otherwise, returns a new 3-element vector with the result.
330 V3.normalize = function V3_normalize(a, r) {
331 //MathUtils_assert(a.length == 3, "a.length == 3");
332 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
335 r = new MJS_FLOAT_ARRAY_TYPE(3);
336 var im = 1.0 / V3.length(a);
350 * a - the vector operand
352 * r - optional vector to store the result in
356 * If r is specified, returns r after performing the operation.
357 * Otherwise, returns a new 3-element vector with the result.
359 V3.scale = function V3_scale(a, k, r) {
360 //MathUtils_assert(a.length == 3, "a.length == 3");
361 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
364 r = new MJS_FLOAT_ARRAY_TYPE(3);
379 * a - the first vector operand
380 * b - the second vector operand
384 * The dot product of a and b.
386 V3.dot = function V3_dot(a, b) {
387 //MathUtils_assert(a.length == 3, "a.length == 3");
388 //MathUtils_assert(b.length == 3, "b.length == 3");
402 * a - the first vector operand
403 * b - the second vector operand
404 * r - optional vector to store the result in
408 * If r is specified, returns r after performing the operation.
409 * Otherwise, returns a new 3-element vector with the result.
411 V3.cross = function V3_cross(a, b, r) {
412 //MathUtils_assert(a.length == 3, "a.length == 3");
413 //MathUtils_assert(b.length == 3, "b.length == 3");
414 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
417 r = new MJS_FLOAT_ARRAY_TYPE(3);
418 r[0] = a[1]*b[2] - a[2]*b[1];
419 r[1] = a[2]*b[0] - a[0]*b[2];
420 r[2] = a[0]*b[1] - a[1]*b[0];
425 * Function: V3.mul4x4
432 * m - the 4x4 matrix operand
433 * v - the 3-element 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.
440 * The 4-element result vector is divided by the w component
441 * and returned as a 3-element vector.
443 V3.mul4x4 = function V3_mul4x4(m, v, r) {
444 //MathUtils_assert(m.length == 16, "m.length == 16");
445 //MathUtils_assert(v.length == 3, "v.length == 3");
446 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
451 r = new MJS_FLOAT_ARRAY_TYPE(3);
455 w = V3.dot(v, tmp) + m[15];
459 r[0] = (V3.dot(v, tmp) + m[12])/w;
463 r[1] = (V3.dot(v, tmp) + m[13])/w;
467 r[2] = (V3.dot(v, tmp) + m[14])/w;
474 * Methods for working with 4x4 matrices. A matrix is represented by a 16-element array
475 * in column-major order. Any valid JavaScript array type may be used, but if new
476 * matrices are created they are created using the configured MJS_FLOAT_ARRAY_TYPE.
481 M4x4._temp1 = new MJS_FLOAT_ARRAY_TYPE(16);
482 M4x4._temp2 = new MJS_FLOAT_ARRAY_TYPE(16);
484 if (MJS_FLOAT_ARRAY_TYPE == Array) {
485 M4x4.I = [1.0, 0.0, 0.0, 0.0,
490 M4x4.$ = function M4x4_$(m00, m01, m02, m03,
495 return [m00, m01, m02, m03,
501 M4x4.clone = function M4x4_clone(m) {
502 //MathUtils_assert(m.length == 16, "m.length == 16");
503 return new [m[0], m[1], m[2], m[3],
504 m[4], m[5], m[6], m[7],
505 m[8], m[9], m[10], m[11],
506 m[12], m[13], m[14], m[15]];
509 M4x4.I = new MJS_FLOAT_ARRAY_TYPE([1.0, 0.0, 0.0, 0.0,
512 0.0, 0.0, 0.0, 1.0]);
517 * Creates a new 4x4 matrix with the given values.
521 * m00..m15 - the 16 elements of the new matrix.
525 * A new matrix filled with the given argument values.
527 M4x4.$ = function M4x4_$(m00, m01, m02, m03,
532 return new MJS_FLOAT_ARRAY_TYPE([m00, m01, m02, m03,
535 m12, m13, m14, m15]);
539 * Function: M4x4.clone
541 * Clone the given 4x4 matrix.
545 * m - the 4x4 matrix to clone
549 * A new matrix with the same values as the passed-in one.
551 M4x4.clone = function M4x4_clone(m) {
552 //MathUtils_assert(m.length == 16, "m.length == 16");
553 return new MJS_FLOAT_ARRAY_TYPE(m);
557 M4x4.identity = M4x4.I;
560 * Function: M4x4.topLeft3x3
562 * Return the top left 3x3 matrix from the given 4x4 matrix m.
567 * r - optional 3x3 matrix to store the result in
571 * If r is specified, returns r after performing the operation.
572 * Otherwise, returns a new 3x3 matrix with the result.
574 M4x4.topLeft3x3 = function M4x4_topLeft3x3(m, r) {
575 //MathUtils_assert(m.length == 16, "m.length == 16");
576 //MathUtils_assert(r == undefined || r.length == 9, "r == undefined || r.length == 9");
579 r = new MJS_FLOAT_ARRAY_TYPE(9);
580 r[0] = m[0]; r[1] = m[1]; r[2] = m[2];
581 r[3] = m[4]; r[4] = m[5]; r[5] = m[6];
582 r[6] = m[8]; r[7] = m[9]; r[8] = m[10];
587 * Function: M4x4.inverseOrthonormal
589 * Computes the inverse of the given matrix m, assuming that
590 * the matrix is orthonormal.
595 * r - optional 4x4 matrix to store the result in
599 * If r is specified, returns r after performing the operation.
600 * Otherwise, returns a new 4x4 matrix with the result.
602 M4x4.inverseOrthonormal = function M4x4_inverseOrthonormal(m, r) {
603 //MathUtils_assert(m.length == 16, "m.length == 16");
604 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
605 //MathUtils_assert(m != r, "m != r");
608 r = new MJS_FLOAT_ARRAY_TYPE(16);
609 M4x4.transpose(m, r);
610 var t = [m[12], m[13], m[14]];
611 r[3] = r[7] = r[11] = 0;
612 r[12] = -V3.dot([r[0], r[4], r[8]], t);
613 r[13] = -V3.dot([r[1], r[5], r[9]], t);
614 r[14] = -V3.dot([r[2], r[6], r[10]], t);
619 * Function: M4x4.inverseTo3x3
621 * Computes the inverse of the given matrix m, but calculates
622 * only the top left 3x3 values of the result.
627 * r - optional 3x3 matrix to store the result in
631 * If r is specified, returns r after performing the operation.
632 * Otherwise, returns a new 3x3 matrix with the result.
634 M4x4.inverseTo3x3 = function M4x4_inverseTo3x3(m, r) {
635 //MathUtils_assert(m.length == 16, "m.length == 16");
636 //MathUtils_assert(r == undefined || r.length == 9, "r == undefined || r.length == 9");
639 r = new MJS_FLOAT_ARRAY_TYPE(9);
641 var a11 = m[10]*m[5]-m[6]*m[9],
642 a21 = -m[10]*m[1]+m[2]*m[9],
643 a31 = m[6]*m[1]-m[2]*m[5],
644 a12 = -m[10]*m[4]+m[6]*m[8],
645 a22 = m[10]*m[0]-m[2]*m[8],
646 a32 = -m[6]*m[0]+m[2]*m[4],
647 a13 = m[9]*m[4]-m[5]*m[8],
648 a23 = -m[9]*m[0]+m[1]*m[8],
649 a33 = m[5]*m[0]-m[1]*m[4];
650 var det = m[0]*(a11) + m[1]*(a12) + m[2]*(a13);
651 if (det == 0) // no inverse
652 throw "matrix not invertible";
653 var idet = 1.0 / det;
669 * Function: M4x4.makeFrustum
671 * Creates a matrix for a projection frustum with the given parameters.
675 * left - the left coordinate of the frustum
676 * right- the right coordinate of the frustum
677 * bottom - the bottom coordinate of the frustum
678 * top - the top coordinate of the frustum
679 * znear - the near z distance of the frustum
680 * zfar - the far z distance of the frustum
681 * r - optional 4x4 matrix to store the result in
685 * If r is specified, returns r after creating the projection matrix.
686 * Otherwise, returns a new 4x4 matrix.
688 M4x4.makeFrustum = function M4x4_makeFrustum(left, right, bottom, top, znear, zfar, r) {
689 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
692 r = new MJS_FLOAT_ARRAY_TYPE(16);
694 var X = 2*znear/(right-left);
695 var Y = 2*znear/(top-bottom);
696 var A = (right+left)/(right-left);
697 var B = (top+bottom)/(top-bottom);
698 var C = -(zfar+znear)/(zfar-znear);
699 var D = -2*zfar*znear/(zfar-znear);
701 r[0] = 2*znear/(right-left);
706 r[5] = 2*znear/(top-bottom);
709 r[8] = (right+left)/(right-left);
710 r[9] = (top+bottom)/(top-bottom);
711 r[10] = -(zfar+znear)/(zfar-znear);
715 r[14] = -2*zfar*znear/(zfar-znear);
722 * Function: M4x4.makePerspective
724 * Creates a matrix for a perspective projection with the given parameters.
728 * fovy - field of view in the y axis, in degrees
729 * aspect - aspect ratio
730 * znear - the near z distance of the projection
731 * zfar - the far z distance of the projection
732 * r - optional 4x4 matrix to store the result in
736 * If r is specified, returns r after creating the projection matrix.
737 * Otherwise, returns a new 4x4 matrix.
739 M4x4.makePerspective = function M4x4_makePerspective (fovy, aspect, znear, zfar, r) {
740 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
742 var ymax = znear * Math.tan(fovy * Math.PI / 360.0);
744 var xmin = ymin * aspect;
745 var xmax = ymax * aspect;
747 return M4x4.makeFrustum(xmin, xmax, ymin, ymax, znear, zfar, r);
751 * Function: M4x4.makeOrtho
753 * Creates a matrix for an orthogonal frustum projection with the given parameters.
757 * left - the left coordinate of the frustum
758 * right- the right coordinate of the frustum
759 * bottom - the bottom coordinate of the frustum
760 * top - the top coordinate of the frustum
761 * znear - the near z distance of the frustum
762 * zfar - the far z distance of the frustum
763 * r - optional 4x4 matrix to store the result in
767 * If r is specified, returns r after creating the projection matrix.
768 * Otherwise, returns a new 4x4 matrix.
770 M4x4.makeOrtho = function M4x4_makeOrtho (left, right, bottom, top, znear, zfar, r) {
771 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
774 r = new MJS_FLOAT_ARRAY_TYPE(16);
776 var tX = -(right+left)/(right-left);
777 var tY = -(top+bottom)/(top-bottom);
778 var tZ = -(zfar+znear)/(zfar-znear);
779 var X = 2 / (right-left);
780 var Y = 2 / (top-bottom);
781 var Z = -2 / (zfar-znear);
783 r[0] = 2 / (right-left);
788 r[5] = 2 / (top-bottom);
793 r[10] = -2 / (zfar-znear);
795 r[12] = -(right+left)/(right-left);
796 r[13] = -(top+bottom)/(top-bottom);
797 r[14] = -(zfar+znear)/(zfar-znear);
804 * Function: M4x4.makeOrtho
806 * Creates a matrix for a 2D orthogonal frustum projection with the given parameters.
807 * znear and zfar are assumed to be -1 and 1, respectively.
811 * left - the left coordinate of the frustum
812 * right- the right coordinate of the frustum
813 * bottom - the bottom coordinate of the frustum
814 * top - the top coordinate of the frustum
815 * r - optional 4x4 matrix to store the result in
819 * If r is specified, returns r after creating the projection matrix.
820 * Otherwise, returns a new 4x4 matrix.
822 M4x4.makeOrtho2D = function M4x4_makeOrtho2D (left, right, bottom, top, r) {
823 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
825 return M4x4.makeOrtho(left, right, bottom, top, -1, 1, r);
831 * Performs r = a * b.
835 * a - the first matrix operand
836 * b - the second matrix operand
837 * r - optional 4x4 matrix to store the result in
841 * If r is specified, returns r after performing the operation.
842 * Otherwise, returns a new 4x4 matrix with the result.
844 M4x4.mul = function M4x4_mul(a, b, r) {
845 //MathUtils_assert(a.length == 16, "a.length == 16");
846 //MathUtils_assert(b.length == 16, "b.length == 16");
847 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
848 //MathUtils_assert(a != r && b != r, "a != r && b != r");
851 r = new MJS_FLOAT_ARRAY_TYPE(16);
887 r[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
888 r[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
889 r[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
890 r[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
891 r[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
892 r[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
893 r[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
894 r[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
895 r[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
896 r[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
897 r[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
898 r[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
899 r[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
900 r[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
901 r[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
902 r[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
908 * Function: M4x4.mulOffset
910 * Performs r' = a * b, where r' is the 16 elements of r starting at element o.
914 * a - the first matrix operand
915 * b - the second matrix operand
916 * r - array to store the result in
917 * o - offset into r at which to start storing results
923 M4x4.mulOffset = function M4x4_mulOffset(a, b, r, o) {
924 //MathUtils_assert(a.length == 16, "a.length == 16");
925 //MathUtils_assert(b.length == 16, "b.length == 16");
960 r[o+0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
961 r[o+1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
962 r[o+2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
963 r[o+3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
964 r[o+4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
965 r[o+5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
966 r[o+6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
967 r[o+7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
968 r[o+8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
969 r[o+9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
970 r[o+10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
971 r[o+11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
972 r[o+12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
973 r[o+13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
974 r[o+14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
975 r[o+15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
981 * Function: M4x4.mulAffine
983 * Performs r = a * b, assuming a and b are affine (elements 3,7,11,15 = 0,0,0,1)
987 * a - the first matrix operand
988 * b - the second matrix operand
989 * r - optional 4x4 matrix to store the result in
993 * If r is specified, returns r after performing the operation.
994 * Otherwise, returns a new 4x4 matrix with the result.
996 M4x4.mulAffine = function M4x4_mulAffine(a, b, r) {
997 //MathUtils_assert(a.length == 16, "a.length == 16");
998 //MathUtils_assert(b.length == 16, "b.length == 16");
999 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1000 //MathUtils_assert(a != r && b != r, "a != r && b != r");
1003 r = new MJS_FLOAT_ARRAY_TYPE(16);
1030 r[0] = a11 * b11 + a12 * b21 + a13 * b31;
1031 r[1] = a21 * b11 + a22 * b21 + a23 * b31;
1032 r[2] = a31 * b11 + a32 * b21 + a33 * b31;
1034 r[4] = a11 * b12 + a12 * b22 + a13 * b32;
1035 r[5] = a21 * b12 + a22 * b22 + a23 * b32;
1036 r[6] = a31 * b12 + a32 * b22 + a33 * b32;
1038 r[8] = a11 * b13 + a12 * b23 + a13 * b33;
1039 r[9] = a21 * b13 + a22 * b23 + a23 * b33;
1040 r[10] = a31 * b13 + a32 * b23 + a33 * b33;
1042 r[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14;
1043 r[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24;
1044 r[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34;
1051 * Function: M4x4.mulAffineOffset
1053 * Performs r' = a * b, assuming a and b are affine (elements 3,7,11,15 = 0,0,0,1), where r' is the 16 elements of r starting at element o
1057 * a - the first matrix operand
1058 * b - the second matrix operand
1059 * r - array to store the result in
1060 * o - offset into r at which to start storing results
1066 M4x4.mulAffine = function M4x4_mulAffine(a, b, r, o) {
1067 //MathUtils_assert(a.length == 16, "a.length == 16");
1068 //MathUtils_assert(b.length == 16, "b.length == 16");
1071 r = new MJS_FLOAT_ARRAY_TYPE(16);
1098 r[o+0] = a11 * b11 + a12 * b21 + a13 * b31;
1099 r[o+1] = a21 * b11 + a22 * b21 + a23 * b31;
1100 r[o+2] = a31 * b11 + a32 * b21 + a33 * b31;
1102 r[o+4] = a11 * b12 + a12 * b22 + a13 * b32;
1103 r[o+5] = a21 * b12 + a22 * b22 + a23 * b32;
1104 r[o+6] = a31 * b12 + a32 * b22 + a33 * b32;
1106 r[o+8] = a11 * b13 + a12 * b23 + a13 * b33;
1107 r[o+9] = a21 * b13 + a22 * b23 + a23 * b33;
1108 r[o+10] = a31 * b13 + a32 * b23 + a33 * b33;
1110 r[o+12] = a11 * b14 + a12 * b24 + a13 * b34 + a14;
1111 r[o+13] = a21 * b14 + a22 * b24 + a23 * b34 + a24;
1112 r[o+14] = a31 * b14 + a32 * b24 + a33 * b34 + a34;
1119 * Function: M4x4.makeRotate
1121 * Creates a transformation matrix for rotation by angle radians about the 3-element vector axis.
1125 * angle - the angle of rotation, in radians
1126 * axis - the axis around which the rotation is performed, a 3-element vector
1127 * r - optional 4x4 matrix to store the result in
1131 * If r is specified, returns r after creating the matrix.
1132 * Otherwise, returns a new 4x4 matrix with the result.
1134 M4x4.makeRotate = function M4x4_makeRotate(angle, axis, r) {
1135 //MathUtils_assert(angle.length == 3, "angle.length == 3");
1136 //MathUtils_assert(axis.length == 3, "axis.length == 3");
1137 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1140 r = new MJS_FLOAT_ARRAY_TYPE(16);
1142 axis = V3.normalize(axis, V3._temp1);
1143 var x = axis[0], y = axis[1], z = axis[2];
1144 var c = Math.cos(angle);
1146 var s = Math.sin(angle);
1169 * Function: M4x4.rotate
1171 * Concatenates a rotation of angle radians about the axis to the give matrix m.
1175 * angle - the angle of rotation, in radians
1176 * axis - the axis around which the rotation is performed, a 3-element vector
1177 * m - the matrix to concatenate the rotation to
1178 * r - optional 4x4 matrix to store the result in
1182 * If r is specified, returns r after performing the operation.
1183 * Otherwise, returns a new 4x4 matrix with the result.
1185 M4x4.rotate = function M4x4_rotate(angle, axis, m, r) {
1186 //MathUtils_assert(angle.length == 3, "angle.length == 3");
1187 //MathUtils_assert(axis.length == 3, "axis.length == 3");
1188 //MathUtils_assert(m.length == 16, "m.length == 16");
1189 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1192 r = new MJS_FLOAT_ARRAY_TYPE(16);
1193 var a0=axis [0], a1=axis [1], a2=axis [2];
1194 var l = Math.sqrt(a0*a0 + a1*a1 + a2*a2);
1195 var x = a0, y = a1, z = a2;
1202 var c = Math.cos(angle);
1204 var s = Math.sin(angle);
1208 var xyc1 = x * y * c1;
1209 var xzc1 = x * z * c1;
1210 var yzc1 = y * z * c1;
1225 var t11 = x * x * c1 + c;
1226 var t21 = xyc1 + zs;
1227 var t31 = xzc1 - ys;
1228 var t12 = xyc1 - zs;
1229 var t22 = y * y * c1 + c;
1230 var t32 = yzc1 + xs;
1231 var t13 = xzc1 + ys;
1232 var t23 = yzc1 - xs;
1233 var t33 = z * z * c1 + c;
1235 r[0] = m11 * t11 + m12 * t21 + m13 * t31;
1236 r[1] = m21 * t11 + m22 * t21 + m23 * t31;
1237 r[2] = m31 * t11 + m32 * t21 + m33 * t31;
1238 r[3] = m41 * t11 + m42 * t21 + m43 * t31;
1239 r[4] = m11 * t12 + m12 * t22 + m13 * t32;
1240 r[5] = m21 * t12 + m22 * t22 + m23 * t32;
1241 r[6] = m31 * t12 + m32 * t22 + m33 * t32;
1242 r[7] = m41 * t12 + m42 * t22 + m43 * t32;
1243 r[8] = m11 * t13 + m12 * t23 + m13 * t33;
1244 r[9] = m21 * t13 + m22 * t23 + m23 * t33;
1245 r[10] = m31 * t13 + m32 * t23 + m33 * t33;
1246 r[11] = m41 * t13 + m42 * t23 + m43 * t33;
1257 * Function: M4x4.makeScale3
1259 * Creates a transformation matrix for scaling by 3 scalar values, one for
1260 * each of the x, y, and z directions.
1264 * x - the scale for the x axis
1265 * y - the scale for the y axis
1266 * z - the scale for the z axis
1267 * r - optional 4x4 matrix to store the result in
1271 * If r is specified, returns r after creating the matrix.
1272 * Otherwise, returns a new 4x4 matrix with the result.
1274 M4x4.makeScale3 = function M4x4_makeScale3(x, y, z, r) {
1275 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1278 r = new MJS_FLOAT_ARRAY_TYPE(16);
1301 * Function: M4x4.makeScale1
1303 * Creates a transformation matrix for a uniform scale by a single scalar value.
1307 * k - the scale factor
1308 * r - optional 4x4 matrix to store the result in
1312 * If r is specified, returns r after creating the matrix.
1313 * Otherwise, returns a new 4x4 matrix with the result.
1315 M4x4.makeScale1 = function M4x4_makeScale1(k, r) {
1316 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1318 return M4x4.makeScale3(k, k, k, r);
1322 * Function: M4x4.makeScale
1324 * Creates a transformation matrix for scaling each of the x, y, and z axes by the amount
1325 * given in the corresponding element of the 3-element vector.
1329 * v - the 3-element vector containing the scale factors
1330 * r - optional 4x4 matrix to store the result in
1334 * If r is specified, returns r after creating the matrix.
1335 * Otherwise, returns a new 4x4 matrix with the result.
1337 M4x4.makeScale = function M4x4_makeScale(v, r) {
1338 //MathUtils_assert(v.length == 3, "v.length == 3");
1339 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1341 return M4x4.makeScale3(v[0], v[1], v[2], r);
1345 * Function: M4x4.scale3
1347 M4x4.scale3 = function M4x4_scale3(x, y, z, m, r) {
1348 //MathUtils_assert(m.length == 16, "m.length == 16");
1349 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1368 r = new MJS_FLOAT_ARRAY_TYPE(16);
1391 * Function: M4x4.scale1
1393 M4x4.scale1 = function M4x4_scale1(k, m, r) {
1394 //MathUtils_assert(m.length == 16, "m.length == 16");
1395 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1414 r = new MJS_FLOAT_ARRAY_TYPE(16);
1437 * Function: M4x4.scale1
1439 M4x4.scale = function M4x4_scale(v, m, r) {
1440 //MathUtils_assert(v.length == 3, "v.length == 3");
1441 //MathUtils_assert(m.length == 16, "m.length == 16");
1442 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1443 var x = v[0], y = v[1], z = v[2];
1462 r = new MJS_FLOAT_ARRAY_TYPE(16);
1486 * Function: M4x4.makeTranslate3
1488 M4x4.makeTranslate3 = function M4x4_makeTranslate3(x, y, z, r) {
1489 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1492 r = new MJS_FLOAT_ARRAY_TYPE(16);
1515 * Function: M4x4.makeTranslate1
1517 M4x4.makeTranslate1 = function M4x4_makeTranslate1 (k, r) {
1518 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1520 return M4x4.makeTranslate3(k, k, k, r);
1524 * Function: M4x4.makeTranslate
1526 M4x4.makeTranslate = function M4x4_makeTranslate (v, r) {
1527 //MathUtils_assert(v.length == 3, "v.length == 3");
1528 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1530 return M4x4.makeTranslate3(v[0], v[1], v[2], r);
1534 * Function: M4x4.translate3Self
1536 M4x4.translate3Self = function M4x4_translate3Self (x, y, z, m) {
1537 //MathUtils_assert(m.length == 16, "m.length == 16");
1538 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1539 m[12] += m[0] * x + m[4] * y + m[8] * z;
1540 m[13] += m[1] * x + m[5] * y + m[9] * z;
1541 m[14] += m[2] * x + m[6] * y + m[10] * z;
1542 m[15] += m[3] * x + m[7] * y + m[11] * z;
1547 * Function: M4x4.translate3
1549 M4x4.translate3 = function M4x4_translate3 (x, y, z, m, r) {
1550 //MathUtils_assert(m.length == 16, "m.length == 16");
1551 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1554 m[12] += m[0] * x + m[4] * y + m[8] * z;
1555 m[13] += m[1] * x + m[5] * y + m[9] * z;
1556 m[14] += m[2] * x + m[6] * y + m[10] * z;
1557 m[15] += m[3] * x + m[7] * y + m[11] * z;
1562 r = new MJS_FLOAT_ARRAY_TYPE(16);
1590 r[12] = m11 * x + m12 * y + m13 * z + m[12];
1591 r[13] = m21 * x + m22 * y + m23 * z + m[13];
1592 r[14] = m31 * x + m32 * y + m33 * z + m[14];
1593 r[15] = m41 * x + m42 * y + m43 * z + m[15];
1599 * Function: M4x4.translate1
1601 M4x4.translate1 = function M4x4_translate1 (k, m, r) {
1602 //MathUtils_assert(m.length == 16, "m.length == 16");
1603 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1605 return M4x4.translate3(k, k, k, m, r);
1608 * Function: M4x4.translateSelf
1610 M4x4.translateSelf = function M4x4_translateSelf (v, m) {
1611 //MathUtils_assert(v.length == 3, "v.length == 3");
1612 //MathUtils_assert(m.length == 16, "m.length == 16");
1613 var x=v[0], y=v[1], z=v[2];
1614 m[12] += m[0] * x + m[4] * y + m[8] * z;
1615 m[13] += m[1] * x + m[5] * y + m[9] * z;
1616 m[14] += m[2] * x + m[6] * y + m[10] * z;
1617 m[15] += m[3] * x + m[7] * y + m[11] * z;
1621 * Function: M4x4.translate
1623 M4x4.translate = function M4x4_translate (v, m, r) {
1624 //MathUtils_assert(v.length == 3, "v.length == 3");
1625 //MathUtils_assert(m.length == 16, "m.length == 16");
1626 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1627 var x=v[0], y=v[1], z=v[2];
1629 m[12] += m[0] * x + m[4] * y + m[8] * z;
1630 m[13] += m[1] * x + m[5] * y + m[9] * z;
1631 m[14] += m[2] * x + m[6] * y + m[10] * z;
1632 m[15] += m[3] * x + m[7] * y + m[11] * z;
1637 r = new MJS_FLOAT_ARRAY_TYPE(16);
1664 r[12] = m11 * x + m12 * y + m13 * z + m[12];
1665 r[13] = m21 * x + m22 * y + m23 * z + m[13];
1666 r[14] = m31 * x + m32 * y + m33 * z + m[14];
1667 r[15] = m41 * x + m42 * y + m43 * z + m[15];
1673 * Function: M4x4.makeLookAt
1675 M4x4.makeLookAt = function M4x4_makeLookAt (eye, center, up, r) {
1676 //MathUtils_assert(eye.length == 3, "eye.length == 3");
1677 //MathUtils_assert(center.length == 3, "center.length == 3");
1678 //MathUtils_assert(up.length == 3, "up.length == 3");
1679 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1681 var z = V3.direction(eye, center, V3._temp1);
1682 var x = V3.normalize(V3.cross(up, z, V3._temp2), V3._temp2);
1683 var y = V3.normalize(V3.cross(z, x, V3._temp3), V3._temp3);
1685 var tm1 = M4x4._temp1;
1686 var tm2 = M4x4._temp2;
1705 tm2[0] = 1; tm2[1] = 0; tm2[2] = 0; tm2[3] = 0;
1706 tm2[4] = 0; tm2[5] = 1; tm2[6] = 0; tm2[7] = 0;
1707 tm2[8] = 0; tm2[9] = 0; tm2[10] = 1; tm2[11] = 0;
1708 tm2[12] = -eye[0]; tm2[13] = -eye[1]; tm2[14] = -eye[2]; tm2[15] = 1;
1711 r = new MJS_FLOAT_ARRAY_TYPE(16);
1712 return M4x4.mul(tm1, tm2, r);
1716 * Function: M4x4.transposeSelf
1718 M4x4.transposeSelf = function M4x4_transposeSelf (m) {
1719 //MathUtils_assert(m.length == 16, "m.length == 16");
1720 var tmp = m[1]; m[1] = m[4]; m[4] = tmp;
1721 tmp = m[2]; m[2] = m[8]; m[8] = tmp;
1722 tmp = m[3]; m[3] = m[12]; m[12] = tmp;
1723 tmp = m[6]; m[6] = m[9]; m[9] = tmp;
1724 tmp = m[7]; m[7] = m[13]; m[13] = tmp;
1725 tmp = m[11]; m[11] = m[14]; m[14] = tmp;
1729 * Function: M4x4.transpose
1731 M4x4.transpose = function M4x4_transpose (m, r) {
1732 //MathUtils_assert(m.length == 16, "m.length == 16");
1733 //MathUtils_assert(r == undefined || r.length == 16, "r == undefined || r.length == 16");
1737 tmp = m[1]; m[1] = m[4]; m[4] = tmp;
1738 tmp = m[2]; m[2] = m[8]; m[8] = tmp;
1739 tmp = m[3]; m[3] = m[12]; m[12] = tmp;
1740 tmp = m[6]; m[6] = m[9]; m[9] = tmp;
1741 tmp = m[7]; m[7] = m[13]; m[13] = tmp;
1742 tmp = m[11]; m[11] = m[14]; m[14] = tmp;
1747 r = new MJS_FLOAT_ARRAY_TYPE(16);
1749 r[0] = m[0]; r[1] = m[4]; r[2] = m[8]; r[3] = m[12];
1750 r[4] = m[1]; r[5] = m[5]; r[6] = m[9]; r[7] = m[13];
1751 r[8] = m[2]; r[9] = m[6]; r[10] = m[10]; r[11] = m[14];
1752 r[12] = m[3]; r[13] = m[7]; r[14] = m[11]; r[15] = m[15];
1759 * Function: M4x4.transformPoint
1761 M4x4.transformPoint = function M4x4_transformPoint (m, v, r) {
1762 //MathUtils_assert(m.length == 16, "m.length == 16");
1763 //MathUtils_assert(v.length == 3, "v.length == 3");
1764 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
1766 r = new MJS_FLOAT_ARRAY_TYPE(3);
1768 var v0 = v[0], v1 = v[1], v2 = v[2];
1770 r[0] = m[0] * v0 + m[4] * v1 + m[8] * v2 + m[12];
1771 r[1] = m[1] * v0 + m[5] * v1 + m[9] * v2 + m[13];
1772 r[2] = m[2] * v0 + m[6] * v1 + m[10] * v2 + m[14];
1773 var w = m[3] * v0 + m[7] * v1 + m[11] * v2 + m[15];
1785 * Function: M4x4.transformLine
1787 M4x4.transformLine = function M4x4_transformLine(m, v, r) {
1788 //MathUtils_assert(m.length == 16, "m.length == 16");
1789 //MathUtils_assert(v.length == 3, "v.length == 3");
1790 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
1792 r = new MJS_FLOAT_ARRAY_TYPE(3);
1794 var v0 = v[0], v1 = v[1], v2 = v[2];
1795 r[0] = m[0] * v0 + m[4] * v1 + m[8] * v2;
1796 r[1] = m[1] * v0 + m[5] * v1 + m[9] * v2;
1797 r[2] = m[2] * v0 + m[6] * v1 + m[10] * v2;
1798 var w = m[3] * v0 + m[7] * v1 + m[11] * v2;
1811 * Function: M4x4.transformPointAffine
1813 M4x4.transformPointAffine = function M4x4_transformPointAffine (m, v, r) {
1814 //MathUtils_assert(m.length == 16, "m.length == 16");
1815 //MathUtils_assert(v.length == 3, "v.length == 3");
1816 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
1818 r = new MJS_FLOAT_ARRAY_TYPE(3);
1820 var v0 = v[0], v1 = v[1], v2 = v[2];
1822 r[0] = m[0] * v0 + m[4] * v1 + m[8] * v2 + m[12];
1823 r[1] = m[1] * v0 + m[5] * v1 + m[9] * v2 + m[13];
1824 r[2] = m[2] * v0 + m[6] * v1 + m[10] * v2 + m[14];
1830 * Function: M4x4.transformLineAffine
1832 M4x4.transformLineAffine = function M4x4_transformLineAffine(m, v, r) {
1833 //MathUtils_assert(m.length == 16, "m.length == 16");
1834 //MathUtils_assert(v.length == 3, "v.length == 3");
1835 //MathUtils_assert(r == undefined || r.length == 3, "r == undefined || r.length == 3");
1837 r = new MJS_FLOAT_ARRAY_TYPE(3);
1839 var v0 = v[0], v1 = v[1], v2 = v[2];
1840 r[0] = m[0] * v0 + m[4] * v1 + m[8] * v2;
1841 r[1] = m[1] * v0 + m[5] * v1 + m[9] * v2;
1842 r[2] = m[2] * v0 + m[6] * v1 + m[10] * v2;
1853 v3direction: A2(V3.direction),
1854 v3length: V3.length,
1855 v3lengthSquared: V3.lengthSquared,
1856 v3normalize: V3.normalize,
1857 v3scale: A2(V3.scale),
1859 v3cross: A2(V3.cross),
1860 v3mul4x4: A2(V3.mul4x4),
1862 m4x4clone: M4x4.clone,
1863 m4x4topLeft3x3: M4x4.topLeft3x3,
1864 m4x4inverseOrthonormal: M4x4.inverseOrthonormal,
1865 m4x4inverseTo3x3: M4x4.inverseTo3x3,
1866 m4x4makeFrustum: A6(M4x4.makeFrustum),
1867 m4x4makePerspective: A4(M4x4.makePerspective),
1868 m4x4makeOrtho: A6(M4x4.makeOrtho),
1869 m4x4makeOrtho2D: A4(M4x4.makeOrtho2D),
1870 m4x4mul: A2(M4x4.mul),
1871 m4x4Affine: A2(M4x4.mulAffine),
1872 m4x4makeRotate: A2(M4x4.makeRotate),
1873 m4x4rotate: A3(M4x4.rotate),
1874 m4x4makeScale3: A3(M4x4.makeScale3),
1875 m4x4makeScale1: M4x4.makeScale1,
1876 m4x4makeScale: M4x4.makeScale,
1877 m4x4scale3: A4(M4x4.scale3),
1878 m4x4scale1: A2(M4x4.scale1),
1879 m4x4scale: A2(M4x4.scale),
1880 m4x4makeTranslate3: A3(M4x4.makeTranslate3),
1881 m4x4makeTranslate1: M4x4.makeTranslate1,
1882 m4x4makeTranslate: M4x4.makeTranslate,
1883 m4x4translate3: A4(M4x4.translate3),
1884 m4x4translate1: A2(M4x4.translate1),
1885 m4x4translate: A2(M4x4.translate),
1886 m4x4makeLookAt: A3(M4x4.makeLookAt),
1887 m4x4transpose: M4x4.transpose,
1888 m4x4transformPoint: A2(M4x4.transformPoint),
1890 m4x4_transformLine(m, v, r) {
1891 m4x4_transformPointAffine (m, v, r) {
1892 m4x4_transformLineAffine(m, v, r) {
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