📄 matrix3d.as
字号:
m114 = m1.tx; m214 = m2.tx; m124 = m1.ty; m224 = m2.ty; m134 = m1.tz; m234 = m2.tz; sxx = m111 * m211 + m112 * m221 + m113 * m231; sxy = m111 * m212 + m112 * m222 + m113 * m232; sxz = m111 * m213 + m112 * m223 + m113 * m233; tx = m111 * m214 + m112 * m224 + m113 * m234 + m114; syx = m121 * m211 + m122 * m221 + m123 * m231; syy = m121 * m212 + m122 * m222 + m123 * m232; syz = m121 * m213 + m122 * m223 + m123 * m233; ty = m121 * m214 + m122 * m224 + m123 * m234 + m124; szx = m131 * m211 + m132 * m221 + m133 * m231; szy = m131 * m212 + m132 * m222 + m133 * m232; szz = m131 * m213 + m132 * m223 + m133 * m233; tz = m131 * m214 + m132 * m224 + m133 * m234 + m134; } /** * Scales the 3d matrix by the given amount in each dimension * * @param x The scale value along the x axis. * @param y The scale value along the y axis. * @param z The scale value along the z axis. */ public function scale(x:Number, y:Number, z:Number):void { sxx *= x; syx *= x; szx *= x; sxy *= y; syy *= y; szy *= y; sxz *= z; syz *= z; szz *= z; } /** * Fill the 3d matrix with the 3x3 rotation matrix section of the given 3d matrix. * * @param m THe 3d matrix to copy from. */ public function copy3x3(m:Matrix3D):Matrix3D { sxx = m.sxx; sxy = m.sxy; sxz = m.sxz; syx = m.syx; syy = m.syy; syz = m.syz; szx = m.szx; szy = m.szy; szz = m.szz; return this; } /** * Fill the 3d matrix with all matrix values of the given 3d matrix. * * @param m THe 3d matrix to copy from. */ public function clone(m:Matrix3D):Matrix3D { sxx = m.sxx; sxy = m.sxy; sxz = m.sxz; tx = m.tx; syx = m.syx; syy = m.syy; syz = m.syz; ty = m.ty; szx = m.szx; szy = m.szy; szz = m.szz; tz = m.tz; return m; } /** * Returns the euler angle represented by the 3x3 matrix rotation. * * @return A 3d number representing the 3 euler angles. */ public function matrix2euler():Number3D { var angle:Number3D = new Number3D(); var d :Number = -Math.asin(Math.max(-1, Math.min(1, sxz))); // Calculate Y-axis angle var c :Number = Math.cos(d); angle.y = d * toDEGREES; var trX:Number, trY:Number; if (Math.abs(c) > 0.005) // Gimball lock? { trX = szz / c; // No, so get X-axis angle trY = -syz / c; angle.x = Math.atan2(trY, trX) * toDEGREES; trX = sxx / c; // Get Z-axis angle trY = -sxy / c; angle.z = Math.atan2(trY, trX) * toDEGREES; } else // Gimball lock has occurred { angle.x = 0; // Set X-axis angle to zero trX = syy; // And calculate Z-axis angle trY = syx; angle.z = Math.atan2(trY, trX) * toDEGREES; } return angle; } /** * Fills the 3d matrix object with values representing the given rotation around a vector. * * @param u The x value of the rotation vector. * @param v The y value of the rotation vector. * @param w The z value of the rotation vector. * @param angle The angle in radians of the rotation. */ public function rotationMatrix(u:Number, v:Number, w:Number, angle:Number):void { nCos = Math.cos(angle); nSin = Math.sin(angle); scos = 1 - nCos; suv = u * v * scos; svw = v * w * scos; suw = u * w * scos; sw = nSin * w; sv = nSin * v; su = nSin * u; sxx = nCos + u * u * scos; // nCos + u*u*(1-nCos) sxy = -sw + suv; // -nSin * w sxz = sv + suw; // -nSin * v syx = sw + suv; // nSin*w + u*v*(1-nCos) syy = nCos + v * v * scos; syz = -su + svw; szx = -sv + suw; // -nSin*v + u*w*(1-nCos) szy = su + svw; szz = nCos + w * w * scos; } /** * Fills the 3d matrix object with values representing the given translation. * * @param u The translation along the x axis. * @param v The translation along the y axis. * @param w The translation along the z axis.. */ public function translationMatrix(u:Number, v:Number, w:Number):void { sxx = syy = szz = 1; sxy = sxz = syz = syz = szx = szy = 0; tx = u; ty = v; tz = w; } /** * Fills the 3d matrix object with values representing the given scaling. * * @param u The scale along the x axis. * @param v The scale along the y axis. * @param w The scale along the z axis.. */ public function scaleMatrix(u:Number, v:Number, w:Number):void { tx = sxy = sxz = 0; syz = ty = syz = 0; szx = szy = tz = 0; sxx = u; syy = v; szz = w; } /** * Fills the 3d matrix object with the result from the inverse calulation of the given 3d matrix. * * @param m The 3d matrix object used for the inverse calulation. */ public function inverse(m:Matrix3D):void { d = m.det; if (Math.abs(d) < 0.001) { // Determinant zero, there's no inverse return; } d = 1 / d; m111 = m.sxx; m121 = m.syx; m131 = m.szx; m112 = m.sxy; m122 = m.syy; m132 = m.szy; m113 = m.sxz; m123 = m.syz; m133 = m.szz; m114 = m.tx; m124 = m.ty; m134 = m.tz; sxx = d * (m122 * m133 - m132 * m123), sxy = -d* (m112 * m133 - m132 * m113), sxz = d * (m112 * m123 - m122 * m113), tx = -d* (m112 * (m123*m134 - m133*m124) - m122 * (m113*m134 - m133*m114) + m132 * (m113*m124 - m123*m114)), syx = -d* (m121 * m133 - m131 * m123), syy = d * (m111 * m133 - m131 * m113), syz = -d* (m111 * m123 - m121 * m113), ty = d * (m111 * (m123*m134 - m133*m124) - m121 * (m113*m134 - m133*m114) + m131 * (m113*m124 - m123*m114)), szx = d * (m121 * m132 - m131 * m122), szy = -d* (m111 * m132 - m131 * m112), szz = d * (m111 * m122 - m121 * m112), tz = -d* (m111 * (m122*m134 - m132*m124) - m121 * (m112*m134 - m132*m114) + m131 * (m112*m124 - m122*m114)); } /** * Fills the 3d matrix object with values representing the transformation made by the given quaternion. * * @param quarternion The quarterion object to convert. */ public function quaternion2matrix(quarternion:Quaternion):void { x = quarternion.x; y = quarternion.y; z = quarternion.z; w = quarternion.w; xx = x * x; xy = x * y; xz = x * z; xw = x * w; yy = y * y; yz = y * z; yw = y * w; zz = z * z; zw = z * w; sxx = 1 - 2 * (yy + zz); sxy = 2 * (xy - zw); sxz = 2 * (xz + yw); syx = 2 * (xy + zw); syy = 1 - 2 * (xx + zz); syz = 2 * (yz - xw); szx = 2 * (xz - yw); szy = 2 * (yz + xw); szz = 1 - 2 * (xx + yy); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -