Three.js Source Note (eight) math/matrix3.js

Source: Internet
Author: User
Tags function prototype scalar

Commercial Territory (http://blog.csdn.net/omni360/)

This article follows "Attribution-non-commercial use-consistent" authoring public agreement

Reprint Please keep this sentence: Business Domain-this blog focuses on Agile development and mobile and IoT device research: Data visualization, Golang, HTML5, WEBGL, three. JS, Otherwise, from this blog article refused to reprint or reprint, thank you for your cooperation.


The following code is a comment for the math/matrix3.js file in the three.js source file.

More updates in: https://github.com/omni360/three.js.sourcecode/blob/master/Three.js


file:src/math/matrix3.js/** * @author alteredq/http://alteredqualia.com/* @author westlangley/http://github.com/w Estlangley * @author The constructor of the Bhouston/http://exocortex.com *////matrix3 object. Used to create a 3x3 matrix. The function function of the Matrix3 object uses// Defines the constructed function prototype object to be implemented, which is actually an array.//////usage: var m = new Matrix3 (11, 12, 13, 21, 22, 23, 31, 32, 33)///Create a 3x3 matrix, which is actually an array of length 9, the parameter (1 1, 12, 13, 21, 22, 23, 31, 32, 33) are passed to the array for initialization.///note: Parameter N11, N12, N13, N21, N22, n23, N31, N32, N33 represents the value of the element in the 3x3 matrix, N11 represents the matrix The first row, the element value of the first column//////<summary>matrix3</summary>///<param name = "N11" type= "number" >n11 the 1th row, the 1th column of the element value </param>///<param name = "N12" type= "number" >n12 1th row, 2nd Column element value </param>///<param name = "N13" type= " Number ">n13 1th row, element value of column 3rd </param>///<param name =" N21 "type=" number ">n21 2nd row, 1th column element value </param>///&  Lt;param name = "N22" type= "number" >n22 2nd row, element value of 2nd column </param>///<param name = "N23" Type= "No." >n23 2nd line, section 3 column element value </param>///<param name = "N31" type= "Number ">n31 3rd row, element value of column 1th </param>///<param name =" N32 "type=" number ">n32 3rd row, 2nd Column element value </param>///& Lt;param name = "N33" type= "number" >n33 3rd row, element value of column 3rd </param>///<returns type= "Matrix3" > Return new 3x3 Matrix </ Returns>three.  Matrix3 = function (N11, N12, N13, N21, N22, n23, N31, N32, n33) {this.elements = new Float32array (9);//Create an array of length 9 var Te = this.elements;//parameters N11, N12, N13, N21, N22, n23, N31, N32, N33 are copied to the elements in the array, if the parameters are not defined, the 11,22,33 element of the matrix is initialized to 1, and the other elements are initialized to 0.te[ 0] = (n11!== undefined)? N11:1; te[3] = N12 | | 0; te[6] = N13 | | 0;te[1] = N21 | | 0; te[4] = (N22!== undefined)? N22:1; te[7] = n23 | | 0;te[2] = N31 | | 0; te[5] = N32 | | 0; te[8] = (n33!== undefined)? n33:1;};/ The following are the function functions provided by the Euler object. ****************************************/three. Matrix3.prototype = {Constructor:three. The matrix3,//constructor/*///set method is used to re-set the element value of the MATRIX3 (3x3 matrix). and returns the new coordinate value of Euler (Euler angle).//TODO: Modify the Set method, compatible with N11, N12, N13, N21,N22, N23, N31, N32, n33 parameter ellipsis supports polymorphism. *////<summary>set</summary>///<param name = "N11" type= "number" >n11 1 rows, element value for 1th column </param>///<param name = "N12" type= "number" >n12 1th row, column 2nd element value </param>///<param name = "N "type=" number ">n13 1th row, element value of column 3rd </param>///<param name =" N21 "type=" number ">n21 2nd row, 1th column element value </para M>///<param name = "N22" type= "number" >n22 2nd row, element value of 2nd column </param>///<param name = "N23" type= "#" >n23 2nd row, element value of column 3rd </param>///<param name = "N31" type= "number" >n31 3rd row, 1th column element value </param>///<para M name = "N32" type= "number" >n32 3rd row, element value of 2nd column </param>///<param name = "N33" type= "number" >n33 3rd row, 3rd column element Value </param>///<returns type= "Matrix3" > returns the new 3x3 matrix </returns>set:function (N11, N12, N13, N21, N22, N23, N31, N32, n33) {var te = this.elements;te[0] = N11; te[3] = N12; te[6] = n13;te[1] = N21; te[4] = N22; te[7] = n23;te[2] = n31; te[5] = N32; Te[8] = N33;return this;//returns a new 3x3 matrix},/*///identity method used to obtain the unit matrix of a 3x3 matrix//////Note: In matrix multiplication, there is a matrix that plays a special role, as in the multiplication of numbers 1, We call this matrix the unit matrix///It is a phalanx, from the upper left corner to the lower right corner of the diagonal (called the main diagonal) on the elements are all 1 is 0. For the unit matrix, there is Ae=ea=a*////<summary>identity</summary>///<returns type= "Matrix3 (3x3 matrix)" > Returns a unit matrix of the 3x3 matrix </returns>identity:function () {this.set (1, 0, 0,0, 1, 0,0, 0, 1); return this;//returns a unit matrix of the 3x3 matrix},/*/ The Copy method is used to copy the element values of the 3x3 matrix. and returns a new Matrix3 (3x3 matrix). *////<summary>copy</summary>///<param name = "Matrix3 ( 3x3 matrix) "Type=" Matrix3 (3x3 Matrix) ">euler (Euler angle) </param>///<returns type=" Matrix3 (3x3 matrix) "> Returns a new Matrix3 (  3x3 matrix) </returns>copy:function (m) {var me = M.elements;this.set (me[0], me[3], me[6],me[1], me[4], me[7 ],me[2], me[5], me[8]); return this;//returns a new Matrix3 (3x3 matrix)},/*///multiplyvector3 method for using a 3x3 matrix and a Vector3 (three-dimensional vector) Multiplies. and returns a new 3x3 matrix.///The Note:multiplyvector3 method has been removed using the Vector.applymatrix3 (matrix) method, which is reserved for backwards compatibility.//NOTE: The MultiplyVector3 method is often used to apply some kind of transformation. *////<summary>multipLyvector3</summary>///<param name = "Vector" type= "Vector3" > three-dimensional vector </param>///<returns type= " Matrix3 "> Returns a new 3x3 matrix </returns>multiplyvector3:function (vector) {// The MultiplyVector3 method has been removed using the Vector.applymatrix3 (matrix) method to replace the Console.warn (' three. Matrix3:. MultiplyVector3 () has been removed. Use Vector.applymatrix3 (matrix) instead. '); Return vector.applymatrix3 (this);//Call the Vector.applymatrix3 () method and pass the Matrix3 object itself past, after applying the transform, return the new Matrix3 (3x3 matrix)},/*/// The Multiplyvector3array method is used to multiply the array A and a Vector3 (three-dimensional vector). and returns a new Array object.//Note: The Multiplyvector3array method has been removed using the Matrix.applytovector3array (array) method, which is reserved for backwards compatibility.//NOTE: The Multiplyvector3array method is often used to apply some kind of transformation. *////<summary>multiplyvector3array</summary>///<param name = "a" Type= "Array" > Array object </param>///<returns type= "Array" > and returns the new Array object </returns>multiplyvector3array: The function (a) {//multiplyvector3array method has been removed using the Matrix.applytovector3array (array) method to replace the Console.warn (' three. Matrix3:. Multiplyvector3array () has been rEnamed. Use Matrix.applytovector3array (array) instead. '); Return This.applytovector3array (a);//Call the Matrix.applytovector3array () method and pass the Matrix3 object itself past, after applying the transform, return the new Matrix3 (3x3 matrix The},/*///applytovector3array method is used to multiply the array A and a Vector3 (three-dimensional vector). and returns a new Array object.//Note: The Multiplyvector3array method is often used to apply some kind of transformation. Parameter offset and parameter length can be omitted. *////<summary>multiplyvector3array</summary >///<param name = "Array" type= "array" > Array object </param>///<param name = "offset" type= "number" > Offset, can be omitted if omitted for 0.</param>///<param name = "Length" type= "number" > length, which can be omitted if the value is omitted for an array length </param>///< Returns type= "Array" > returns the new Array Object </returns>applytovector3array:function () {var v1 = new three. Vector3 (); return function (array, offset, length) {if (offset = = = undefined) offset = 0;if (length = = = undefined) le Ngth = Array.length;for (var i = 0, j = offset, il; i < length; i + = 3, J + = 3) {v1.x = array[J];v1.y = array[J + 1];v1.z = array[J + 2];v1.applymatrix3 (this);//Call Vector.applymatrix3 () method, and passes the Matrix3 object itself past, after applying the transform, returns the new Matrix3 (3x3 matrix) array[J] = v1.x;array[j + 1] = v1.y;array[J + 2] = v1.z;} Return array;//returns the new Array object after the transform was applied.};} (), the/*///multiplyscalar method is used to multiply the elements of the MATRIX3 (3x3 matrix) directly from the parameter S. and returns a new Matrix3 (3x3 matrix).//Note: The parameter s passed here is a scalar .*////< Summary>multiplyscalar</summary>///<param name = "S" type= "number" > scalar multiplied by the value of the current Matrix3 (3x3 matrix) object, numeric </param>///<returns type= "Matrix3" > Return new Matrix3 (3x3 matrix) </returns>multiplyscalar:function (s) {var Te = this.elements;te[0] *= s; Te[3] *= s; te[6] *= s;te[1] *= s; Te[4] *= s; te[7] *= s;te[2] *= s; te[5] *= s; te[8] *= S;return this;//returns the new Matrix3 (3x3 matrix)},/*///determinant method used to place the determinant of the Matrix3 (3x3 matrix)///NOTE: The existence of the inverse matrix of a matrix is determined by solving the determinant value (the value of the determinant is not equal to 0, which indicates that the matrix has a inverse matrix). *////<summary>determinant</summary>///<returns  Type= "Number" > returns the third-order determinant of Matrix3 (3x3 matrix) </returns>determinant:function () {var te = this.elements;var a = te[0], b = te[1], C = te[2],d = te[3], E = te[4], F = te[5],g = te[6], H = te[7], i = te[8];return A * e * i-a * f * h-b * d * i + b * f * g + c * d * h-c * e * g;//return Matrix3 (3x3 matrix The third-order determinant},/*///getinverse method is used to obtain the inverse matrix of the Matrix3 (3x3 matrix).///Note: The inverse matrix is multiplied by the current matrix to get the unit matrix. *////<summary>multiplyscalar </summary>///<param name = "Matrix" type= "Matrix4" >three. Matrix4</param>///<param name = "Throwoninvertible" type= "number" > Exception flag </param>///<returns type = "Matrix3" > returns the inverse matrix of Matrix3 (3x3 matrix). </returns>getinverse:function (Matrix, throwoninvertible) {//Input:three . matrix4//input parameter matrix is a 4x4 matrix//(based on http://code.google.com/p/webgl-mjs/) var me = Matrix.elements;var te = This.elem  ents;te[0] = me[] * me[5]-me[6] * me[9];te[1] =-me[] * me[1] + me[2] * me[9];te[2] = me[  6] * me[1]-me[2] * me[5];te[3] =-me[[] * me[4] + me[6] * me[8];te[4] = me[) * me[0]-me[ 2] * me[8];te[5] =-me[6] * me[0] + me[2] * me[4];te[6] = me[9] * me[4]-me[5 ] * me[8];te[7] =-me[9] * me[0] + me[1] * me[8];te[8] = me[5] * me[0]-me[1] * me[4];var det = me[0] * te[0] + me[1] * te[3] + me[2] * te[6];  Get the value of the matrix determinant of the parameter//no inverses//no inverse matrix if (det = = = 0) {var msg = "Matrix3.getinverse (): Can ' t invert matrix, determinant is 0 ";//Prompts the user that the matrix has no inverse matrix if (throwoninvertible | | false) {throw new Error (msg);} else {Console.warn (msg);} This.identity ();//Gets a unit matrix return this;//returns the unit matrix}this.multiplyscalar (1.0/det);//divided by the determinant to get the inverse matrix return this;//back to Matrix3 (     The inverse matrix of the 3x3 matrix. The},/*///transpose method is used to obtain the transpose matrix of the MATRIX3 (3x3 matrix).//Note: A matrix of MXN matrices is a matrix NXM matrix, which is the row and column exchange of matrices.///////------ --t///| 1 2 3 | | 1 4 7 |///matrix A =|  4 5 6 | = | 2 5 8 |///| 7 8 9 | | 3 6 9 |///--------*////<summary>transpose</summary>///<returns type= "Matrix3" > Return Matrix3 (3x3 matrix)  Transpose matrix. </returns>transpose:function () {var tmp, m = this.elements;tmp = m[1]; m[1] = m[3]; m[3] = Tmp;tmp = m[2]; m[2] = m[6]; m[6 ] = Tmp;tmp = m[5]; m[5] = m[7]; m[7] = Tmp;return this;//returns the transpose matrix of the MATRIX3 (3x3 matrix).},/*///flattentoarrayoffset method expands the matrix into an array (parameter array) by specifying an offset from the parameter offset. Returns a new array.///The Note:flattentoarrayoffset method can be used to transform a 3x3 matrix into a 4x4 matrix.//----///| 1 2 3 |///matrix A =|  4 5 6 | = Flattentoarrayoffset (arrary,3) = Array (0,0,0,0,1,2,3,0,0,0,0,4,5,6,0,0,0,0,7,8,9)///| 7 8 9 |///----*////<summary>multiplyscalar</summary>///<param name = "Array" type= "array" >array arrays Object </param>///<param name = "offset" type= "number" > Offset </param>///<returns type= "Matrix3" > Returns an array containing the matrix elements </returns>flattentoarrayoffset:function (array, offset) {var te = this.elements;array[offset] = te[0];array[offset + 1] = te[1];array[offset + 2] = te[2];array[offset + 3] = te[3];array[offset + 4] = Te [4];array[offset + 5] = te[5];array[offset + 6] = te[6];array[offset + 7] = te[7];array[offset + 8] = te[ 8];return array;//Returns an array containing the matrix elements},/*///getnormalmatThe Rix method is used to obtain the normal matrix of the MATRIX4 (4x4 matrix).//Note: The current matrix is the normal matrix of the inverse matrix. *////<summary>getnormalmatrix</summary >///<param name = "M" type= "Matrix4" >three. Matrix4</param>///<returns type= "Matrix3" > Normal matrix of parameter M. </returns>getnormalmatrix:function (m) {// Input:three. The matrix4//input is a Matrix4 (4x4 matrix) object This.getinverse (M). Transpose ();//The transpose matrix of the current matrix is the normal matrix of the current matrix return this;//parameter M, The/*///transposeintoarray method is used to store the transpose matrix of the current matrix into an array. The array is then returned.//The Note:transposeintoarray method is a bit redundant, and the Matrix object itself is an array. *//// <summary>transposeintoarray</summary>///<param name = "R" type= "Array" >three. Matrix4</param>///<returns type= "Matrix3" > Parameter m transpose matrix. </returns>transposeintoarray:function (r) {  var m = this.elements;r[0] = m[0];r[1] = m[3];r[2] = m[6];r[3] = m[1];r[4] = m[4];r[5] = m[7];r[6 ] = m[2];r[7] = m[5];r[8] = m[8];//todo: Return to the transpose matrix This?return this;//method},/*fromarray method to store the matrix An array of 3 (3x3 matrix) element values is assigned to the current Matrix3 (3x3 matrix) pairLike *////<summary>fromarray</summary>///<param name = "Array" type= "array" >matrix3 (3x3 matrix) An array of element values Array</param>///<returns type= "Matrix3" > Returns a new Matrix3 (3x3 matrix) </returns>fromarray:function ( Array) {This.elements.set (array);//Call the Set method, assign the array to the current Matrix3 (3x3 matrix) object return this;//return a new Matrix3 (3x3 matrix)},/*toarray method The ToArray method assigns an element value of the current Matrix3 (3x3 matrix) to an array of arrays. Returns an Array object. *////<summary>toarray</summary>///<returns Type= "Array" > returns an array containing Matrix3 (3x3 matrix) element values Array</returns>toarray:function () {var te = This.elements;return [te[ 0], te[1], te[2],te[3], te[4], te[5],te[6], te[7], te[8]];//Returns an array Matrix3 method that contains Array},/*clone (3x3 matrix) element values///CL The one method clones a Matrix3 (3x3 matrix) object. *////<summary>clone</summary>///<returns type= "Vector3" > Returns the cloned Matrix3 (3x3 matrix) object </returns>clone:function () {var te = This.elements;return new three. Matrix3 (te[0], te[3], te[6],te[1], te[4], te[7],te[2], te[5], te[8]);//Returns the cloned Matrix3 (3x3 Matrix) object}};


Commercial Territory (http://blog.csdn.net/omni360/)

This article follows "Attribution-non-commercial use-consistent" authoring public agreement

Reprint Please keep this sentence: Business Domain-this blog focuses on Agile development and mobile and IoT device research: Data visualization, Golang, HTML5, WEBGL, three. JS, Otherwise, from this blog article refused to reprint or reprint, thank you for your cooperation.


The following code is a comment for the math/matix3.js file in the three.js source file.

More updates in: https://github.com/omni360/three.js.sourcecode/blob/master/Three.js

Three.js Source Note (eight) math/matrix3.js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.