WebGL矩阵变换库

目录

矩阵变换库:

Matrix4对象所支持的方法和属性如表所示:

方法属性规范: 


虽然平移、旋转、缩放等变换操作都可以用一个4×4的矩阵表示,但是在写WebGL程序的时候,手动计算每个矩阵很耗费时间。为了简化编程,大多数WebGL开发者都使用矩阵操作函数库来隐藏矩阵计算的细节,简化与矩阵有关的操作。目前已经有一些开源的矩阵库。以下是一个比较好的矩阵变换库可供大家学习使用。有了矩阵函数库,进行如“平移,然后旋转”等各种复合的变换就很简单了。

Matrix4是该矩阵库提供的新类型 ,顾名思义,Matrix4对象(实例)表示一个4×4的矩阵。该对象内部使用类型化数组Floated2Array来存储矩阵的元素。

矩阵变换库:

/*** Constructor of Matrix4* If opt_src is specified, new matrix is initialized by opt_src.* Otherwise, new matrix is initialized by identity matrix.* @param opt_src source matrix(option)*/
var Matrix4 = function(opt_src) {var i, s, d;if (opt_src && typeof opt_src === 'object' && opt_src.hasOwnProperty('elements')) {s = opt_src.elements;d = new Float32Array(16);for (i = 0; i < 16; ++i) {d[i] = s[i];}this.elements = d;} else {this.elements = new Float32Array([1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]);}
};/*** Set the identity matrix.* @return this*/
Matrix4.prototype.setIdentity = function() {var e = this.elements;e[0] = 1;   e[4] = 0;   e[8]  = 0;   e[12] = 0;e[1] = 0;   e[5] = 1;   e[9]  = 0;   e[13] = 0;e[2] = 0;   e[6] = 0;   e[10] = 1;   e[14] = 0;e[3] = 0;   e[7] = 0;   e[11] = 0;   e[15] = 1;return this;
};/*** Copy matrix.* @param src source matrix* @return this*/
Matrix4.prototype.set = function(src) {var i, s, d;s = src.elements;d = this.elements;if (s === d) {return;}for (i = 0; i < 16; ++i) {d[i] = s[i];}return this;
};/*** Multiply the matrix from the right.* @param other The multiply matrix* @return this*/
Matrix4.prototype.concat = function(other) {var i, e, a, b, ai0, ai1, ai2, ai3;// Calculate e = a * be = this.elements;a = this.elements;b = other.elements;// If e equals b, copy b to temporary matrix.if (e === b) {b = new Float32Array(16);for (i = 0; i < 16; ++i) {b[i] = e[i];}}for (i = 0; i < 4; i++) {ai0=a[i];  ai1=a[i+4];  ai2=a[i+8];  ai3=a[i+12];e[i]    = ai0 * b[0]  + ai1 * b[1]  + ai2 * b[2]  + ai3 * b[3];e[i+4]  = ai0 * b[4]  + ai1 * b[5]  + ai2 * b[6]  + ai3 * b[7];e[i+8]  = ai0 * b[8]  + ai1 * b[9]  + ai2 * b[10] + ai3 * b[11];e[i+12] = ai0 * b[12] + ai1 * b[13] + ai2 * b[14] + ai3 * b[15];}return this;
};
Matrix4.prototype.multiply = Matrix4.prototype.concat;/*** Multiply the three-dimensional vector.* @param pos  The multiply vector* @return The result of multiplication(Float32Array)*/
Matrix4.prototype.multiplyVector3 = function(pos) {var e = this.elements;var p = pos.elements;var v = new Vector3();var result = v.elements;result[0] = p[0] * e[0] + p[1] * e[4] + p[2] * e[ 8] + e[12];result[1] = p[0] * e[1] + p[1] * e[5] + p[2] * e[ 9] + e[13];result[2] = p[0] * e[2] + p[1] * e[6] + p[2] * e[10] + e[14];return v;
};/*** Multiply the four-dimensional vector.* @param pos  The multiply vector* @return The result of multiplication(Float32Array)*/
Matrix4.prototype.multiplyVector4 = function(pos) {var e = this.elements;var p = pos.elements;var v = new Vector4();var result = v.elements;result[0] = p[0] * e[0] + p[1] * e[4] + p[2] * e[ 8] + p[3] * e[12];result[1] = p[0] * e[1] + p[1] * e[5] + p[2] * e[ 9] + p[3] * e[13];result[2] = p[0] * e[2] + p[1] * e[6] + p[2] * e[10] + p[3] * e[14];result[3] = p[0] * e[3] + p[1] * e[7] + p[2] * e[11] + p[3] * e[15];return v;
};/*** Transpose the matrix.* @return this*/
Matrix4.prototype.transpose = function() {var e, t;e = this.elements;t = e[ 1];  e[ 1] = e[ 4];  e[ 4] = t;t = e[ 2];  e[ 2] = e[ 8];  e[ 8] = t;t = e[ 3];  e[ 3] = e[12];  e[12] = t;t = e[ 6];  e[ 6] = e[ 9];  e[ 9] = t;t = e[ 7];  e[ 7] = e[13];  e[13] = t;t = e[11];  e[11] = e[14];  e[14] = t;return this;
};/*** Calculate the inverse matrix of specified matrix, and set to this.* @param other The source matrix* @return this*/
Matrix4.prototype.setInverseOf = function(other) {var i, s, d, inv, det;s = other.elements;d = this.elements;inv = new Float32Array(16);inv[0]  =   s[5]*s[10]*s[15] - s[5] *s[11]*s[14] - s[9] *s[6]*s[15]+ s[9]*s[7] *s[14] + s[13]*s[6] *s[11] - s[13]*s[7]*s[10];inv[4]  = - s[4]*s[10]*s[15] + s[4] *s[11]*s[14] + s[8] *s[6]*s[15]- s[8]*s[7] *s[14] - s[12]*s[6] *s[11] + s[12]*s[7]*s[10];inv[8]  =   s[4]*s[9] *s[15] - s[4] *s[11]*s[13] - s[8] *s[5]*s[15]+ s[8]*s[7] *s[13] + s[12]*s[5] *s[11] - s[12]*s[7]*s[9];inv[12] = - s[4]*s[9] *s[14] + s[4] *s[10]*s[13] + s[8] *s[5]*s[14]- s[8]*s[6] *s[13] - s[12]*s[5] *s[10] + s[12]*s[6]*s[9];inv[1]  = - s[1]*s[10]*s[15] + s[1] *s[11]*s[14] + s[9] *s[2]*s[15]- s[9]*s[3] *s[14] - s[13]*s[2] *s[11] + s[13]*s[3]*s[10];inv[5]  =   s[0]*s[10]*s[15] - s[0] *s[11]*s[14] - s[8] *s[2]*s[15]+ s[8]*s[3] *s[14] + s[12]*s[2] *s[11] - s[12]*s[3]*s[10];inv[9]  = - s[0]*s[9] *s[15] + s[0] *s[11]*s[13] + s[8] *s[1]*s[15]- s[8]*s[3] *s[13] - s[12]*s[1] *s[11] + s[12]*s[3]*s[9];inv[13] =   s[0]*s[9] *s[14] - s[0] *s[10]*s[13] - s[8] *s[1]*s[14]+ s[8]*s[2] *s[13] + s[12]*s[1] *s[10] - s[12]*s[2]*s[9];inv[2]  =   s[1]*s[6]*s[15] - s[1] *s[7]*s[14] - s[5] *s[2]*s[15]+ s[5]*s[3]*s[14] + s[13]*s[2]*s[7]  - s[13]*s[3]*s[6];inv[6]  = - s[0]*s[6]*s[15] + s[0] *s[7]*s[14] + s[4] *s[2]*s[15]- s[4]*s[3]*s[14] - s[12]*s[2]*s[7]  + s[12]*s[3]*s[6];inv[10] =   s[0]*s[5]*s[15] - s[0] *s[7]*s[13] - s[4] *s[1]*s[15]+ s[4]*s[3]*s[13] + s[12]*s[1]*s[7]  - s[12]*s[3]*s[5];inv[14] = - s[0]*s[5]*s[14] + s[0] *s[6]*s[13] + s[4] *s[1]*s[14]- s[4]*s[2]*s[13] - s[12]*s[1]*s[6]  + s[12]*s[2]*s[5];inv[3]  = - s[1]*s[6]*s[11] + s[1]*s[7]*s[10] + s[5]*s[2]*s[11]- s[5]*s[3]*s[10] - s[9]*s[2]*s[7]  + s[9]*s[3]*s[6];inv[7]  =   s[0]*s[6]*s[11] - s[0]*s[7]*s[10] - s[4]*s[2]*s[11]+ s[4]*s[3]*s[10] + s[8]*s[2]*s[7]  - s[8]*s[3]*s[6];inv[11] = - s[0]*s[5]*s[11] + s[0]*s[7]*s[9]  + s[4]*s[1]*s[11]- s[4]*s[3]*s[9]  - s[8]*s[1]*s[7]  + s[8]*s[3]*s[5];inv[15] =   s[0]*s[5]*s[10] - s[0]*s[6]*s[9]  - s[4]*s[1]*s[10]+ s[4]*s[2]*s[9]  + s[8]*s[1]*s[6]  - s[8]*s[2]*s[5];det = s[0]*inv[0] + s[1]*inv[4] + s[2]*inv[8] + s[3]*inv[12];if (det === 0) {return this;}det = 1 / det;for (i = 0; i < 16; i++) {d[i] = inv[i] * det;}return this;
};/*** Calculate the inverse matrix of this, and set to this.* @return this*/
Matrix4.prototype.invert = function() {return this.setInverseOf(this);
};/*** Set the orthographic projection matrix.* @param left The coordinate of the left of clipping plane.* @param right The coordinate of the right of clipping plane.* @param bottom The coordinate of the bottom of clipping plane.* @param top The coordinate of the top top clipping plane.* @param near The distances to the nearer depth clipping plane. This value is minus if the plane is to be behind the viewer.* @param far The distances to the farther depth clipping plane. This value is minus if the plane is to be behind the viewer.* @return this*/
Matrix4.prototype.setOrtho = function(left, right, bottom, top, near, far) {var e, rw, rh, rd;if (left === right || bottom === top || near === far) {throw 'null frustum';}rw = 1 / (right - left);rh = 1 / (top - bottom);rd = 1 / (far - near);e = this.elements;e[0]  = 2 * rw;e[1]  = 0;e[2]  = 0;e[3]  = 0;e[4]  = 0;e[5]  = 2 * rh;e[6]  = 0;e[7]  = 0;e[8]  = 0;e[9]  = 0;e[10] = -2 * rd;e[11] = 0;e[12] = -(right + left) * rw;e[13] = -(top + bottom) * rh;e[14] = -(far + near) * rd;e[15] = 1;return this;
};/*** Multiply the orthographic projection matrix from the right.* @param left The coordinate of the left of clipping plane.* @param right The coordinate of the right of clipping plane.* @param bottom The coordinate of the bottom of clipping plane.* @param top The coordinate of the top top clipping plane.* @param near The distances to the nearer depth clipping plane. This value is minus if the plane is to be behind the viewer.* @param far The distances to the farther depth clipping plane. This value is minus if the plane is to be behind the viewer.* @return this*/
Matrix4.prototype.ortho = function(left, right, bottom, top, near, far) {return this.concat(new Matrix4().setOrtho(left, right, bottom, top, near, far));
};/*** Set the perspective projection matrix.* @param left The coordinate of the left of clipping plane.* @param right The coordinate of the right of clipping plane.* @param bottom The coordinate of the bottom of clipping plane.* @param top The coordinate of the top top clipping plane.* @param near The distances to the nearer depth clipping plane. This value must be plus value.* @param far The distances to the farther depth clipping plane. This value must be plus value.* @return this*/
Matrix4.prototype.setFrustum = function(left, right, bottom, top, near, far) {var e, rw, rh, rd;if (left === right || top === bottom || near === far) {throw 'null frustum';}if (near <= 0) {throw 'near <= 0';}if (far <= 0) {throw 'far <= 0';}rw = 1 / (right - left);rh = 1 / (top - bottom);rd = 1 / (far - near);e = this.elements;e[ 0] = 2 * near * rw;e[ 1] = 0;e[ 2] = 0;e[ 3] = 0;e[ 4] = 0;e[ 5] = 2 * near * rh;e[ 6] = 0;e[ 7] = 0;e[ 8] = (right + left) * rw;e[ 9] = (top + bottom) * rh;e[10] = -(far + near) * rd;e[11] = -1;e[12] = 0;e[13] = 0;e[14] = -2 * near * far * rd;e[15] = 0;return this;
};/*** Multiply the perspective projection matrix from the right.* @param left The coordinate of the left of clipping plane.* @param right The coordinate of the right of clipping plane.* @param bottom The coordinate of the bottom of clipping plane.* @param top The coordinate of the top top clipping plane.* @param near The distances to the nearer depth clipping plane. This value must be plus value.* @param far The distances to the farther depth clipping plane. This value must be plus value.* @return this*/
Matrix4.prototype.frustum = function(left, right, bottom, top, near, far) {return this.concat(new Matrix4().setFrustum(left, right, bottom, top, near, far));
};/*** Set the perspective projection matrix by fovy and aspect.* @param fovy The angle between the upper and lower sides of the frustum.* @param aspect The aspect ratio of the frustum. (width/height)* @param near The distances to the nearer depth clipping plane. This value must be plus value.* @param far The distances to the farther depth clipping plane. This value must be plus value.* @return this*/
Matrix4.prototype.setPerspective = function(fovy, aspect, near, far) {var e, rd, s, ct;if (near === far || aspect === 0) {throw 'null frustum';}if (near <= 0) {throw 'near <= 0';}if (far <= 0) {throw 'far <= 0';}fovy = Math.PI * fovy / 180 / 2;s = Math.sin(fovy);if (s === 0) {throw 'null frustum';}rd = 1 / (far - near);ct = Math.cos(fovy) / s;e = this.elements;e[0]  = ct / aspect;e[1]  = 0;e[2]  = 0;e[3]  = 0;e[4]  = 0;e[5]  = ct;e[6]  = 0;e[7]  = 0;e[8]  = 0;e[9]  = 0;e[10] = -(far + near) * rd;e[11] = -1;e[12] = 0;e[13] = 0;e[14] = -2 * near * far * rd;e[15] = 0;return this;
};/*** Multiply the perspective projection matrix from the right.* @param fovy The angle between the upper and lower sides of the frustum.* @param aspect The aspect ratio of the frustum. (width/height)* @param near The distances to the nearer depth clipping plane. This value must be plus value.* @param far The distances to the farther depth clipping plane. This value must be plus value.* @return this*/
Matrix4.prototype.perspective = function(fovy, aspect, near, far) {return this.concat(new Matrix4().setPerspective(fovy, aspect, near, far));
};/*** Set the matrix for scaling.* @param x The scale factor along the X axis* @param y The scale factor along the Y axis* @param z The scale factor along the Z axis* @return this*/
Matrix4.prototype.setScale = function(x, y, z) {var e = this.elements;e[0] = x;  e[4] = 0;  e[8]  = 0;  e[12] = 0;e[1] = 0;  e[5] = y;  e[9]  = 0;  e[13] = 0;e[2] = 0;  e[6] = 0;  e[10] = z;  e[14] = 0;e[3] = 0;  e[7] = 0;  e[11] = 0;  e[15] = 1;return this;
};/*** Multiply the matrix for scaling from the right.* @param x The scale factor along the X axis* @param y The scale factor along the Y axis* @param z The scale factor along the Z axis* @return this*/
Matrix4.prototype.scale = function(x, y, z) {var e = this.elements;e[0] *= x;  e[4] *= y;  e[8]  *= z;e[1] *= x;  e[5] *= y;  e[9]  *= z;e[2] *= x;  e[6] *= y;  e[10] *= z;e[3] *= x;  e[7] *= y;  e[11] *= z;return this;
};/*** Set the matrix for translation.* @param x The X value of a translation.* @param y The Y value of a translation.* @param z The Z value of a translation.* @return this*/
Matrix4.prototype.setTranslate = function(x, y, z) {var e = this.elements;e[0] = 1;  e[4] = 0;  e[8]  = 0;  e[12] = x;e[1] = 0;  e[5] = 1;  e[9]  = 0;  e[13] = y;e[2] = 0;  e[6] = 0;  e[10] = 1;  e[14] = z;e[3] = 0;  e[7] = 0;  e[11] = 0;  e[15] = 1;return this;
};/*** Multiply the matrix for translation from the right.* @param x The X value of a translation.* @param y The Y value of a translation.* @param z The Z value of a translation.* @return this*/
Matrix4.prototype.translate = function(x, y, z) {var e = this.elements;e[12] += e[0] * x + e[4] * y + e[8]  * z;e[13] += e[1] * x + e[5] * y + e[9]  * z;e[14] += e[2] * x + e[6] * y + e[10] * z;e[15] += e[3] * x + e[7] * y + e[11] * z;return this;
};/*** Set the matrix for rotation.* The vector of rotation axis may not be normalized.* @param angle The angle of rotation (degrees)* @param x The X coordinate of vector of rotation axis.* @param y The Y coordinate of vector of rotation axis.* @param z The Z coordinate of vector of rotation axis.* @return this*/
Matrix4.prototype.setRotate = function(angle, x, y, z) {var e, s, c, len, rlen, nc, xy, yz, zx, xs, ys, zs;angle = Math.PI * angle / 180;e = this.elements;s = Math.sin(angle);c = Math.cos(angle);if (0 !== x && 0 === y && 0 === z) {// Rotation around X axisif (x < 0) {s = -s;}e[0] = 1;  e[4] = 0;  e[ 8] = 0;  e[12] = 0;e[1] = 0;  e[5] = c;  e[ 9] =-s;  e[13] = 0;e[2] = 0;  e[6] = s;  e[10] = c;  e[14] = 0;e[3] = 0;  e[7] = 0;  e[11] = 0;  e[15] = 1;} else if (0 === x && 0 !== y && 0 === z) {// Rotation around Y axisif (y < 0) {s = -s;}e[0] = c;  e[4] = 0;  e[ 8] = s;  e[12] = 0;e[1] = 0;  e[5] = 1;  e[ 9] = 0;  e[13] = 0;e[2] =-s;  e[6] = 0;  e[10] = c;  e[14] = 0;e[3] = 0;  e[7] = 0;  e[11] = 0;  e[15] = 1;} else if (0 === x && 0 === y && 0 !== z) {// Rotation around Z axisif (z < 0) {s = -s;}e[0] = c;  e[4] =-s;  e[ 8] = 0;  e[12] = 0;e[1] = s;  e[5] = c;  e[ 9] = 0;  e[13] = 0;e[2] = 0;  e[6] = 0;  e[10] = 1;  e[14] = 0;e[3] = 0;  e[7] = 0;  e[11] = 0;  e[15] = 1;} else {// Rotation around another axislen = Math.sqrt(x*x + y*y + z*z);if (len !== 1) {rlen = 1 / len;x *= rlen;y *= rlen;z *= rlen;}nc = 1 - c;xy = x * y;yz = y * z;zx = z * x;xs = x * s;ys = y * s;zs = z * s;e[ 0] = x*x*nc +  c;e[ 1] = xy *nc + zs;e[ 2] = zx *nc - ys;e[ 3] = 0;e[ 4] = xy *nc - zs;e[ 5] = y*y*nc +  c;e[ 6] = yz *nc + xs;e[ 7] = 0;e[ 8] = zx *nc + ys;e[ 9] = yz *nc - xs;e[10] = z*z*nc +  c;e[11] = 0;e[12] = 0;e[13] = 0;e[14] = 0;e[15] = 1;}return this;
};/*** Multiply the matrix for rotation from the right.* The vector of rotation axis may not be normalized.* @param angle The angle of rotation (degrees)* @param x The X coordinate of vector of rotation axis.* @param y The Y coordinate of vector of rotation axis.* @param z The Z coordinate of vector of rotation axis.* @return this*/
Matrix4.prototype.rotate = function(angle, x, y, z) {return this.concat(new Matrix4().setRotate(angle, x, y, z));
};/*** Set the viewing matrix.* @param eyeX, eyeY, eyeZ The position of the eye point.* @param centerX, centerY, centerZ The position of the reference point.* @param upX, upY, upZ The direction of the up vector.* @return this*/
Matrix4.prototype.setLookAt = function(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ) {var e, fx, fy, fz, rlf, sx, sy, sz, rls, ux, uy, uz;fx = centerX - eyeX;fy = centerY - eyeY;fz = centerZ - eyeZ;// Normalize f.rlf = 1 / Math.sqrt(fx*fx + fy*fy + fz*fz);fx *= rlf;fy *= rlf;fz *= rlf;// Calculate cross product of f and up.sx = fy * upZ - fz * upY;sy = fz * upX - fx * upZ;sz = fx * upY - fy * upX;// Normalize s.rls = 1 / Math.sqrt(sx*sx + sy*sy + sz*sz);sx *= rls;sy *= rls;sz *= rls;// Calculate cross product of s and f.ux = sy * fz - sz * fy;uy = sz * fx - sx * fz;uz = sx * fy - sy * fx;// Set to this.e = this.elements;e[0] = sx;e[1] = ux;e[2] = -fx;e[3] = 0;e[4] = sy;e[5] = uy;e[6] = -fy;e[7] = 0;e[8] = sz;e[9] = uz;e[10] = -fz;e[11] = 0;e[12] = 0;e[13] = 0;e[14] = 0;e[15] = 1;// Translate.return this.translate(-eyeX, -eyeY, -eyeZ);
};/*** Multiply the viewing matrix from the right.* @param eyeX, eyeY, eyeZ The position of the eye point.* @param centerX, centerY, centerZ The position of the reference point.* @param upX, upY, upZ The direction of the up vector.* @return this*/
Matrix4.prototype.lookAt = function(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ) {return this.concat(new Matrix4().setLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ));
};/*** Multiply the matrix for project vertex to plane from the right.* @param plane The array[A, B, C, D] of the equation of plane "Ax + By + Cz + D = 0".* @param light The array which stored coordinates of the light. if light[3]=0, treated as parallel light.* @return this*/
Matrix4.prototype.dropShadow = function(plane, light) {var mat = new Matrix4();var e = mat.elements;var dot = plane[0] * light[0] + plane[1] * light[1] + plane[2] * light[2] + plane[3] * light[3];e[ 0] = dot - light[0] * plane[0];e[ 1] =     - light[1] * plane[0];e[ 2] =     - light[2] * plane[0];e[ 3] =     - light[3] * plane[0];e[ 4] =     - light[0] * plane[1];e[ 5] = dot - light[1] * plane[1];e[ 6] =     - light[2] * plane[1];e[ 7] =     - light[3] * plane[1];e[ 8] =     - light[0] * plane[2];e[ 9] =     - light[1] * plane[2];e[10] = dot - light[2] * plane[2];e[11] =     - light[3] * plane[2];e[12] =     - light[0] * plane[3];e[13] =     - light[1] * plane[3];e[14] =     - light[2] * plane[3];e[15] = dot - light[3] * plane[3];return this.concat(mat);
}/*** Multiply the matrix for project vertex to plane from the right.(Projected by parallel light.)* @param normX, normY, normZ The normal vector of the plane.(Not necessary to be normalized.)* @param planeX, planeY, planeZ The coordinate of arbitrary points on a plane.* @param lightX, lightY, lightZ The vector of the direction of light.(Not necessary to be normalized.)* @return this*/
Matrix4.prototype.dropShadowDirectionally = function(normX, normY, normZ, planeX, planeY, planeZ, lightX, lightY, lightZ) {var a = planeX * normX + planeY * normY + planeZ * normZ;return this.dropShadow([normX, normY, normZ, -a], [lightX, lightY, lightZ, 0]);
};/*** Constructor of Vector3* If opt_src is specified, new vector is initialized by opt_src.* @param opt_src source vector(option)*/
var Vector3 = function(opt_src) {var v = new Float32Array(3);if (opt_src && typeof opt_src === 'object') {v[0] = opt_src[0]; v[1] = opt_src[1]; v[2] = opt_src[2];} this.elements = v;
}/*** Normalize.* @return this*/
Vector3.prototype.normalize = function() {var v = this.elements;var c = v[0], d = v[1], e = v[2], g = Math.sqrt(c*c+d*d+e*e);if(g){if(g == 1)return this;} else {v[0] = 0; v[1] = 0; v[2] = 0;return this;}g = 1/g;v[0] = c*g; v[1] = d*g; v[2] = e*g;return this;
};/*** Constructor of Vector4* If opt_src is specified, new vector is initialized by opt_src.* @param opt_src source vector(option)*/
var Vector4 = function(opt_src) {var v = new Float32Array(4);if (opt_src && typeof opt_src === 'object') {v[0] = opt_src[0]; v[1] = opt_src[1]; v[2] = opt_src[2]; v[3] = opt_src[3];} this.elements = v;
}

Matrix4对象所支持的方法和属性如表所示:

* 单位阵在矩阵乘法中的行为,就像数字1在乘法中的行为一样。将一个矩阵乘以单位阵,得到的结果和原矩阵完全相同。在单位阵中,对角线上的元素为1.0,其余的元素为0.0。

方法属性规范: 

从上表中可见,Matrix4对象有两种方法:一种方法的名称中含有前缀set,另一种则不含。包含set前缀的方法会根据参数计算出变换矩阵,然后将变换矩阵写入到自身中;而不含set前缀的方法,会先根据参数计算出变换矩阵,然后将自身与刚刚计算得到的变换矩阵相乘,然后把最终得到的结果再写入到Matrix4对象中。

如上表所示,Matrix4对象的方法十分强大且灵活。更重要的是,有了这些函数,进行变换就会变得轻而易举。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/58918.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

opencv 案例05-基于二值图像分析(简单缺陷检测)

缺陷检测&#xff0c;分为两个部分&#xff0c;一个部分是提取指定的轮廓&#xff0c;第二个部分通过对比实现划痕检测与缺角检测。本次主要搞定第一部分&#xff0c;学会观察图像与提取图像ROI对象轮廓外接矩形与轮廓。 下面是基于二值图像分析的大致流程 读取图像将图像转换…

Spring Cloud + Spring Boot 项目搭建结构层次示例讲解

Spring Cloud Spring Boot 项目搭建结构层次示例讲解 Spring Cloud 项目搭建结构层次示例Spring Cloud示例&#xff1a; Spring Boot 项目搭建结构层次讲解Spring Boot 项目通常按照一种常见的架构模式组织&#xff0c;可以分为以下几个主要层次&#xff1a;当构建一个 Spring…

【算法系列篇】位运算

文章目录 前言什么是位运算算法1.判断字符是否唯一1.1 题目要求1.2 做题思路1.3 Java代码实现 2. 丢失的数字2.1 题目要求2.2 做题思路2.3 Java代码实现 3. 两数之和3.1 题目要求3.2 做题思路3.3 Java代码实现 4. 只出现一次的数字4.1 题目要求4.2 做题思路4.3 Java代码实现 5.…

Ansible项目实战管理/了解项目环境/项目管理

一&#xff0c;项目环境 1.项目基础 项目过程 调研阶段 设计阶段 开发阶段 测试阶段 运营阶段 2.项目环境 个人开发环境 公司开发环境 项目测试环境 项目预发布环境 灰度环境&#xff1a;本身是生产环境&#xff0c;安装项目规划&#xff0c;最终所有的生产环境都发…

Python框架【模板继承 、继承模板实战、类视图 、类视图的好处 、类视图使用场景、基于调度方法的类视图】(四)

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱敲代码的小王&#xff0c;CSDN博客博主,Python小白 &#x1f4d5;系列专栏&#xff1a;python入门到实战、Python爬虫开发、Python办公自动化、Python数据分析、Python前后端开发 &#x1f4e7;如果文章知识点有错误…

CUDA小白 - NPP(2) -图像处理-算数和逻辑操作(2)

cuda小白 原始API链接 NPP GPU架构近些年也有不少的变化&#xff0c;具体的可以参考别的博主的介绍&#xff0c;都比较详细。还有一些cuda中的专有名词的含义&#xff0c;可以参考《详解CUDA的Context、Stream、Warp、SM、SP、Kernel、Block、Grid》 常见的NppStatus&#xf…

解压jar包并导入库环境

背景 因为各种历史原因&#xff0c;当初的maven依赖环境已下载不了了&#xff0c;所以需要从生产环境的jar包里&#xff0c;获取库环境来本地运行。 但是网上很多方法都是用mvn install命令&#xff0c;一个个jar包导入的&#xff0c;不符合我的需求&#xff08;需要导入280多…

R语言APRIORI关联规则、K-MEANS均值聚类分析中药专利复方治疗用药规律网络可视化...

全文链接&#xff1a;http://tecdat.cn/?p30605 应用关联规则、聚类方法等数据挖掘技术分析治疗的中药专利复方组方配伍规律&#xff08;点击文末“阅读原文”获取完整代码数据&#xff09;。 方法检索治疗中药专利复方&#xff0c;排除外用中药及中西药物合用的复方。最近我们…

CSS3D+动画

CSS3D 1.css3D 给父元素设置 perspective:景深:近大远小的效果900-1200px这个范围内 transform-style:是否设置3D环境 flat 2D环境 默认值 perserve-3D环境 3D功能函数 1.位移: translateZ()translate3D(x,y,z) <!DOCTYPE html> <html lang"en"><h…

linux下载安装jdk

1.java安装unzip,zip yum install -y unzip zip 2.安装vim yum -y install vim* Linux-jdk1.8下载地址点击下载 jdk1.8-linux 提取码&#xff1a;h40h 1、将安装包上传至Linux服务器 2、创建安装文件夹 创建install mkdir -p /export/install3、解压到install文件夹中 tar…

13.搬砖

目录 题目 Description Input Output 思路&#xff08;归并排序&#xff09; 具体步骤如下 C整体代码&#xff08;含详细注释&#xff09; 归并排序总结 核心步骤 代码模板 题目 Description 小张在暑假时间来到工地搬砖挣钱。包工头交给他一项艰巨的任务&#xff0…

Royal TSX 6 Mac多协议远程软件

Royal TSX是一款功能强大的远程桌面管理软件&#xff0c;适用于Mac操作系统。它允许用户通过一个集成的界面来管理和访问多个远程计算机和服务器。 Royal TSX支持多种远程协议&#xff0c;包括RDP、VNC、SSH、Telnet和FTP等&#xff0c;可以方便地连接到Windows、Linux、Mac和其…

非煤矿山风险监测预警算法 yolov8

非煤矿山风险监测预警算法通过yolov8网络模型深度学习算法框架&#xff0c;非煤矿山风险监测预警算法在煤矿关键地点安装摄像机等设备利用智能化视频识别技术&#xff0c;能够实时分析人员出入井口的情况&#xff0c;人数变化并检测作业状态。YOLO的结构非常简单&#xff0c;就…

ios开发 swift5 苹果系统自带的图标 SF Symbols

文章目录 1.官网app的下载和使用2.使用代码 1.官网app的下载和使用 苹果官网网址&#xff1a;SF Symbols 通过上面的网址可以下载dmg, 安装到自己的mac上 貌似下面这样不能展示出动画&#xff0c;还是要使用动画的代码 .bounce.up.byLayer2.使用代码 UIKit UIImage(system…

解决报错Java: 非法字符: ‘\ufeff‘

方法一&#xff1a;直接remove BOM&#xff0c;再重新启动程序。 方法二&#xff1a;用notpad打开&#xff0c;点击编码为utf-8格式&#xff0c;保存。

GrapeCity Documents V6.0 Update 2发布,新增支持SpreadJS的.sjs文件格式

近日&#xff0c;GrapeCity Documents 正式迎来其V6.2 的发布更新&#xff0c;能够支持 SpreadJS 中 .sjs 类型的文件。这一重大更新将为用户带来更多地惊喜。 .sjs文件有两个关键优势&#xff1a;空间更小且导入导出速度更快。通过采用 .sjs格式&#xff0c;GcExcel实现了更高…

PO设计模式是selenium自动化测试中最佳的设计模式之一

Page Object Model&#xff1a;PO设计模式是selenium自动化测试中最佳的设计模式之一&#xff0c;主要体现在对界面交互细节的封装&#xff0c;也就是在实际测试中只关注业务流程就OK了传统的设计中&#xff0c;在新增测试用例之后&#xff0c;代码会有以下几个问题&#xff1a…

maven部署

一、下载Maven 地址&#xff1a;Maven – Download Apache Maven 二、解压缩&#xff0c;设置环境变量 tar -xvf apache-maven-3.8.8-bin.tar.gz export MAVEN_HOME/opt/apache-maven-3.8.8 export PATH$MAVEN_HOME/bin:$PATH echo $MAVEN_HOME echo $PATH mvn -v

Stable Diffusion Web UI的原理与使用

Stable Diffusion是一套基于Diffusion扩散模型生成技术的图片生成方案&#xff0c;随着技术的不断发展以及工业界对这套工程细节的不断优化&#xff0c;使其终于能在个人电脑上运行&#xff0c;本文将从github下载开始讲一讲如何使用Stable Diffusion Web UI进行AI图像的生成。…

水稻叶病害数据集(目标检测,yolo使用)

1.数据集文件夹 train文件夹&#xff08;44229张&#xff09;&#xff0c;test文件夹&#xff08;4741张&#xff09;&#xff0c;valid文件夹&#xff08;6000张&#xff09; 2.train文件夹展示 labels展示 标签txt展示 data.yaml文件展示 对数据集感兴趣的可以关注最后一行…