Three. js source code annotation (8) Math/Matrix3.js
Wuji (http://blog.csdn.net/omni360)
This article follows the "signature-non-commercial use-consistency" creation public agreement
Reprinted please keep this sentence: Wuji-this blog focuses on Agile development and mobile and IOT device research: data visualization, GOLANG, Html5, WEBGL, THREE. JS. Otherwise, the post from this blog will not be reprinted or reprinted. Thank you for your cooperation.
// File: src/math/Matrix3.js/*** @ author alteredq/http://alteredqualia.com/* @ author WestLangley/http://github.com/WestLangley * @ author bhouston/http://exocortex.com * // Matrix3 object constructor. used to create a 3x3 matrix. the functional functions of the Matrix3 object are implemented using the function prototype object constructed by //. Actually, it is 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 with a length of 9. The parameters (11, 12, 13, 21, 22, 23, 31, 32, 33) passed to the array for initialization. /// NOTE: The parameters n11, n12, n13, n21, n22, n23, n31, n32, and n33 represent the values of elements in the 3x3 matrix, and n11 indicates the first row of the matrix, element value in the first column //////Matrix3///N11 1st rows, 1st column element value ///N12 1st rows, 2nd column element value ///N13 1st rows, 3rd column element value ///N21 2nd rows, 1st column element value ///N22 2nd rows, 2nd column element value ///N23 2nd rows, 3rd column element value ///N31 3rd rows, 1st column element value ///N32 3rd rows, 2nd column element value ///N33 3rd rows, 3rd column element value ///
Returns the new 3x3 matrix.
THREE. matrix3 = function (n11, n12, n13, n21, n22, n23, n31, n32, n33) {this. elements = new Float32Array (9); // create an array named 9 var te = this. elements; // copy the n11, n12, n13, n21, n22, n23, n31, n32, and n33 parameters to the elements in the array. If the parameters are not defined, replace, the 33 element is initialized to 1, and 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 functions provided by the Euler object. **************************************** /THREE. matrix3.prototype = {constructor: THREE. matrix3, // constructor/* // The set method is used to set the element values of Matrix3 (3x3 matrix. and returns the Euler (Euler's angle) of the new coordinate value ). /// TODO: The set method is modified to support n11, n12, n13, n21, n22, n23, n31, n32, and n33 polymorphism. *////Set///N11 1st rows, 1st column element value ///N12 1st rows, 2nd column element value ///N13 1st rows, 3rd column element value ///N21 2nd rows, 1st column element value ///N22 2nd rows, 2nd column element value ///N23 2nd rows, 3rd column element value ///N31 3rd rows, 1st column element value ///N32 3rd rows, 2nd column element value ///N33 3rd rows, 3rd column element value ///
Returns the new 3x3 matrix.
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; // return the new 3x3 matrix }, /* // The identity method is used to obtain the unit matrix of a 3x3 matrix. // NOTE: A matrix plays a special role in matrix multiplication, like 1 in the multiplication of numbers, we call this matrix A matrix in units. // It is a matrix, from the upper left corner to the lower right corner of the diagonal line (called the main diagonal line) all elements except 1 are 0. /// For the unit matrix, AE = EA = *////Identity///
Returns a matrix of 3x3.
Identity: function () {this. set (1, 0, 1, 0, 0, 1); return this; // returns a matrix of 3x3 }, /* // copy the element value of the 3x3 matrix. and return the new Matrix3 (3x3 matrix ). *////Copy///Euler (OLA )///
Returns the new Matrix3 (3x3 matrix)
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; // return the new Matrix3 (3x3 matrix )}, /* // The multiplyVector3 method is used to multiply the 3x3 matrix and a Vector3 (3D vector. and return the new 3x3 matrix. /// NOTE: The multiplyVector3 method has been deleted using vector. replace the applyMatrix3 (matrix) method, which is reserved for backward compatibility. /// NOTE: The multiplyVector3 method is often used to apply certain transformations. *////MultiplyVector3///3D vector ///
Returns the new 3x3 matrix.
MultiplyVector3: function (vector) {// The multiplyVector3 method has been deleted using vector. replace the console with the applyMatrix3 (matrix) method. warn ('Three. matrix3 :. multiplyVector3 () has been removed. use vector. applyMatrix3 (matrix) instead. '); return vector. applyMatrix3 (this); // call vector. applyMatrix3 () method, and pass the Matrix3 object itself to the past. After the application transformation, a new Matrix3 (3x3 matrix) is returned )}, /* // The multiplyVector3Array method is used to multiply array a and a Vector3 (3D vector. and return the new array object. /// NOTE: The multiplyVector3Array method has been deleted using matrix. replace the applyToVector3Array (array) method, which is reserved for backward compatibility. /// NOTE: The multiplyVector3Array method is often used to apply certain transformations. *////MultiplyVector3Array///Array object ///
And return the new array object.
MultiplyVector3Array: function (a) {// The multiplyVector3Array method has been deleted using matrix. replace the applyToVector3Array (array) method with the console. warn ('Three. matrix3 :. multiplyVector3Array () has been renamed. use matrix. applyToVector3Array (array) instead. '); return this. applyToVector3Array (a); // call matrix. applyToVector3Array () method, and pass the Matrix3 object itself to the past. After the application transformation, a new Matrix3 (3x3 matrix) is returned )}, /* // The applyToVector3Array method is used to multiply array a and a Vector3 (3D vector. and return the new array object. /// NOTE: The multiplyVector3Array method is often used to apply certain transformations. the offset and length parameters can be omitted. *////MultiplyVector3Array///Array object ///Offset, which can be omitted. If it is omitted as 0 .///Length, which can be omitted. If the omitted value is an array length ///
And return the new array object.
ApplyToVector3Array: function () {var v1 = new THREE. vector3 (); return function (array, offset, length) {if (offset = undefined) offset = 0; if (length = undefined) length = 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 pass the Matrix3 object itself to the past. After the application transformation, the new Matrix3 (3x3 matrix) array [j] = v1.x is returned; array [j + 1] = v1.y; array [j + 2] = v1.z;} return array; // return the new array object after the application transformation .};} (), // * // The multiplyScalar method is used to multiply the elements of Matrix3 (3x3 matrix) directly with the parameter s. and return the new Matrix3 (3x3 matrix ). /// NOTE: The parameter s passed here is a scalar. *////MultiplyScalar///The scalar that is used to multiply the value of the current Matrix3 (3x3 matrix) object. It is a numerical value ///
Returns the new Matrix3 (3x3 matrix)
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; // return the new Matrix3 (3x3 matrix)}, and the/* // determinant method is used to determine the determinant of Matrix3 (3x3 matrix). // NOTE: determine whether the inverse matrix of the matrix exists by solving the determining factor value (the value of the determining factor is not equal to 0, indicating that the matrix has an inverse matrix ). *////Determinant///
Returns the Third-Order Determinant of Matrix3 (3x3 matrix ).
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 the Third-Order Determinant of Matrix3 (3x3 matrix)}, and the/* // getInverse method is used to obtain the inverse matrix of Matrix3 (3x3 matrix. // NOTE: multiply the inverse matrix by the current matrix to obtain the unit matrix. *////MultiplyScalar///THREE. Matrix4 ///Exception flag ///
Returns the inverse matrix of Matrix3 (3x3 matrix.
GetInverse: function (matrix, throwOnInvertible) {// input: THREE. matrix4 // The input parameter Matrix is a 4x4 matrix // (based on http://code.google.com/p/webgl-mjs/) var me = Matrix. elements; var te = this. elements; te [0] = me [10] * me [5]-me [6] * me [9]; te [1] =-me [10] * me [1] + me [2] * me [9]; te [2] = me [6] * me [1]-me [2] * me [5]; te [3] =-me [10] * me [4] + me [6] * me [8]; te [4] = me [10] * 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]; // obtain the value of the matrix determinant // no inverses // no inverse matrix if (det = 0) {var msg = Matrix3.getInverse (): can't invert matrix, determinant is 0; // you are prompted that the matrix does not have an inverse matrix if (throwOnInvertible | false) {throw new Error (msg);} else {console. warn (msg);} this. identity (); // get a unit matrix return this; // return unit matrix} this. multiplyScalar (1.0/det); // returns the inverse matrix of Matrix3 (3x3 matrix) by dividing by the determinant .}, // The transpose method is used to obtain the transpose matrix of Matrix3 (3x3 matrix. /// NOTE: the transpose matrix nxm matrix of an mxn matrix, that is, the row and column exchanges of the matrix. /// example: /// // -- T // | 1 2 3 | 1 4 7 | // matrix A = | 4 5 6 | = | 2 5 8 | /// | 7 8 9 | 3 6 9 | ///--------*////Transpose///
Returns the transpose matrix of Matrix3 (3x3 matrix.
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 Matrix3 (3x3 matrix .}, /* // The flattenToArrayOffset method uses the offset parameter to specify the offset. The matrix is expanded to the array (parameter array) and a new array is returned. /// NOTE: The flattenToArrayOffset method can be used to convert a 3x3 matrix into a 4x4 matrix. /// -- // | 1 2 3 | // matrix A = | 4 5 6 | => flattenToArrayOffset (arrary, 3) => array (0, 0, 0, 0,, 9) // | 7 8 9 | ///----*////MultiplyScalar///Array object ///Offset ///
Returns an array containing matrix elements.
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 matrix elements}. // * // The getNormalMatrix method is used to obtain the Regular Matrix of Matrix4 (4x4 matrix. /// NOTE: the transpose matrix of the inverse matrix of the current matrix is the regular matrix of the current matrix. *////GetNormalMatrix///THREE. Matrix4 ///
The regular matrix of the Parameter m.
GetNormalMatrix: function (m) {// input: THREE. matrix4 // The input is a Matrix4 (4x4 matrix) object this. getInverse (m ). transpose (); // The transpose matrix of the inverse matrix of the current matrix is the regular matrix of the current matrix return this; // The regular matrix of the Parameter m }, /* // The transposeIntoArray method is used to store the transpose matrix of the current matrix to an array. then return the array. /// NOTE: The transposeIntoArray method is redundant. The matrix object itself is an array. *////TransposeIntoArray///THREE. Matrix4 ///
Transpose matrix of Parameter m.
Transposesponarray: 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: Why is this returned? Return this; // transpose matrix} of Parameter m,/* fromArray method // The fromArray method assigns an array storing the Matrix3 (3x3 matrix) element values to the current Matrix3 (3x3 matrix) object *////FromArray///Array of Matrix3 (3x3 matrix) element values ///
Returns the new Matrix3 (3x3 matrix)
FromArray: function (array) {this. elements. set (array); // call the set Method and assign the array value to the current Matrix3 (3x3 matrix) object return this; // return the new Matrix3 (3x3 matrix )}, /* toArray method // The toArray method assigns the element value of the current Matrix3 (3x3 matrix) to the array. returns an array object. *////ToArray///
Returns an array containing the Matrix3 (3x3 matrix) element values.
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 containing the Matrix3 (3x3 matrix) element value }, /* clone method // clone method to clone a Matrix3 (3x3 matrix) object. *////Clone///
Returns the cloned Matrix3 (3x3 matrix) object.
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 }};
Wuji (http://blog.csdn.net/omni360)
This article follows the "signature-non-commercial use-consistency" creation public agreement
Reprinted please keep this sentence: Wuji-this blog focuses on Agile development and mobile and IOT device research: data visualization, GOLANG, Html5, WEBGL, THREE. JS. Otherwise, the post from this blog will not be reprinted or reprinted. Thank you for your cooperation.
The following code is a comment on the Math/Matix3.js file in the THREE. JS source code file.
More updates in: https://github.com/omni360/three.js.sourcecode/blob/master/Three.js