Matrix4.js 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * @author supereggbert / http://www.paulbrunt.co.uk/
  4. * @author philogb / http://blog.thejit.org/
  5. * @author jordi_ros / http://plattsoft.com
  6. * @author D1plo1d / http://github.com/D1plo1d
  7. * @author alteredq / http://alteredqualia.com/
  8. * @author mikael emtinger / http://gomo.se/
  9. * @author timknip / http://www.floorplanner.com/
  10. */
  11. THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  12. this.elements = new Float32Array( 16 );
  13. this.set(
  14. ( n11 !== undefined ) ? n11 : 1, n12 || 0, n13 || 0, n14 || 0,
  15. n21 || 0, ( n22 !== undefined ) ? n22 : 1, n23 || 0, n24 || 0,
  16. n31 || 0, n32 || 0, ( n33 !== undefined ) ? n33 : 1, n34 || 0,
  17. n41 || 0, n42 || 0, n43 || 0, ( n44 !== undefined ) ? n44 : 1
  18. );
  19. };
  20. THREE.Matrix4.prototype = {
  21. constructor: THREE.Matrix4,
  22. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  23. var te = this.elements;
  24. te[0] = n11; te[4] = n12; te[8] = n13; te[12] = n14;
  25. te[1] = n21; te[5] = n22; te[9] = n23; te[13] = n24;
  26. te[2] = n31; te[6] = n32; te[10] = n33; te[14] = n34;
  27. te[3] = n41; te[7] = n42; te[11] = n43; te[15] = n44;
  28. return this;
  29. },
  30. identity: function () {
  31. this.set(
  32. 1, 0, 0, 0,
  33. 0, 1, 0, 0,
  34. 0, 0, 1, 0,
  35. 0, 0, 0, 1
  36. );
  37. return this;
  38. },
  39. copy: function ( m ) {
  40. var me = m.elements;
  41. this.set(
  42. me[0], me[4], me[8], me[12],
  43. me[1], me[5], me[9], me[13],
  44. me[2], me[6], me[10], me[14],
  45. me[3], me[7], me[11], me[15]
  46. );
  47. return this;
  48. },
  49. lookAt: function ( eye, target, up ) {
  50. var te = this.elements;
  51. var x = THREE.Matrix4.__v1;
  52. var y = THREE.Matrix4.__v2;
  53. var z = THREE.Matrix4.__v3;
  54. z.sub( eye, target ).normalize();
  55. if ( z.length() === 0 ) {
  56. z.z = 1;
  57. }
  58. x.cross( up, z ).normalize();
  59. if ( x.length() === 0 ) {
  60. z.x += 0.0001;
  61. x.cross( up, z ).normalize();
  62. }
  63. y.cross( z, x );
  64. te[0] = x.x; te[4] = y.x; te[8] = z.x;
  65. te[1] = x.y; te[5] = y.y; te[9] = z.y;
  66. te[2] = x.z; te[6] = y.z; te[10] = z.z;
  67. return this;
  68. },
  69. multiply: function ( a, b ) {
  70. var ae = a.elements;
  71. var be = b.elements;
  72. var te = this.elements;
  73. var a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
  74. var a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
  75. var a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
  76. var a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
  77. var b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12];
  78. var b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13];
  79. var b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14];
  80. var b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15];
  81. te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  82. te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  83. te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  84. te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  85. te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  86. te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  87. te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  88. te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  89. te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  90. te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  91. te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  92. te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  93. te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  94. te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  95. te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  96. te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  97. return this;
  98. },
  99. multiplySelf: function ( m ) {
  100. return this.multiply( this, m );
  101. },
  102. multiplyToArray: function ( a, b, r ) {
  103. var te = this.elements;
  104. this.multiply( a, b );
  105. r[ 0 ] = te[0]; r[ 1 ] = te[1]; r[ 2 ] = te[2]; r[ 3 ] = te[3];
  106. r[ 4 ] = te[4]; r[ 5 ] = te[5]; r[ 6 ] = te[6]; r[ 7 ] = te[7];
  107. r[ 8 ] = te[8]; r[ 9 ] = te[9]; r[ 10 ] = te[10]; r[ 11 ] = te[11];
  108. r[ 12 ] = te[12]; r[ 13 ] = te[13]; r[ 14 ] = te[14]; r[ 15 ] = te[15];
  109. return this;
  110. },
  111. multiplyScalar: function ( s ) {
  112. var te = this.elements;
  113. te[0] *= s; te[4] *= s; te[8] *= s; te[12] *= s;
  114. te[1] *= s; te[5] *= s; te[9] *= s; te[13] *= s;
  115. te[2] *= s; te[6] *= s; te[10] *= s; te[14] *= s;
  116. te[3] *= s; te[7] *= s; te[11] *= s; te[15] *= s;
  117. return this;
  118. },
  119. multiplyVector3: function ( v ) {
  120. var te = this.elements;
  121. var vx = v.x, vy = v.y, vz = v.z;
  122. var d = 1 / ( te[3] * vx + te[7] * vy + te[11] * vz + te[15] );
  123. v.x = ( te[0] * vx + te[4] * vy + te[8] * vz + te[12] ) * d;
  124. v.y = ( te[1] * vx + te[5] * vy + te[9] * vz + te[13] ) * d;
  125. v.z = ( te[2] * vx + te[6] * vy + te[10] * vz + te[14] ) * d;
  126. return v;
  127. },
  128. multiplyVector4: function ( v ) {
  129. var te = this.elements;
  130. var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
  131. v.x = te[0] * vx + te[4] * vy + te[8] * vz + te[12] * vw;
  132. v.y = te[1] * vx + te[5] * vy + te[9] * vz + te[13] * vw;
  133. v.z = te[2] * vx + te[6] * vy + te[10] * vz + te[14] * vw;
  134. v.w = te[3] * vx + te[7] * vy + te[11] * vz + te[15] * vw;
  135. return v;
  136. },
  137. rotateAxis: function ( v ) {
  138. var te = this.elements;
  139. var vx = v.x, vy = v.y, vz = v.z;
  140. v.x = vx * te[0] + vy * te[4] + vz * te[8];
  141. v.y = vx * te[1] + vy * te[5] + vz * te[9];
  142. v.z = vx * te[2] + vy * te[6] + vz * te[10];
  143. v.normalize();
  144. return v;
  145. },
  146. crossVector: function ( a ) {
  147. var te = this.elements;
  148. var v = new THREE.Vector4();
  149. v.x = te[0] * a.x + te[4] * a.y + te[8] * a.z + te[12] * a.w;
  150. v.y = te[1] * a.x + te[5] * a.y + te[9] * a.z + te[13] * a.w;
  151. v.z = te[2] * a.x + te[6] * a.y + te[10] * a.z + te[14] * a.w;
  152. v.w = ( a.w ) ? te[3] * a.x + te[7] * a.y + te[11] * a.z + te[15] * a.w : 1;
  153. return v;
  154. },
  155. determinant: function () {
  156. var te = this.elements;
  157. var n11 = te[0], n12 = te[4], n13 = te[8], n14 = te[12];
  158. var n21 = te[1], n22 = te[5], n23 = te[9], n24 = te[13];
  159. var n31 = te[2], n32 = te[6], n33 = te[10], n34 = te[14];
  160. var n41 = te[3], n42 = te[7], n43 = te[11], n44 = te[15];
  161. //TODO: make this more efficient
  162. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  163. return (
  164. n14 * n23 * n32 * n41-
  165. n13 * n24 * n32 * n41-
  166. n14 * n22 * n33 * n41+
  167. n12 * n24 * n33 * n41+
  168. n13 * n22 * n34 * n41-
  169. n12 * n23 * n34 * n41-
  170. n14 * n23 * n31 * n42+
  171. n13 * n24 * n31 * n42+
  172. n14 * n21 * n33 * n42-
  173. n11 * n24 * n33 * n42-
  174. n13 * n21 * n34 * n42+
  175. n11 * n23 * n34 * n42+
  176. n14 * n22 * n31 * n43-
  177. n12 * n24 * n31 * n43-
  178. n14 * n21 * n32 * n43+
  179. n11 * n24 * n32 * n43+
  180. n12 * n21 * n34 * n43-
  181. n11 * n22 * n34 * n43-
  182. n13 * n22 * n31 * n44+
  183. n12 * n23 * n31 * n44+
  184. n13 * n21 * n32 * n44-
  185. n11 * n23 * n32 * n44-
  186. n12 * n21 * n33 * n44+
  187. n11 * n22 * n33 * n44
  188. );
  189. },
  190. transpose: function () {
  191. var te = this.elements;
  192. var tmp;
  193. tmp = te[1]; te[1] = te[4]; te[4] = tmp;
  194. tmp = te[2]; te[2] = te[8]; te[8] = tmp;
  195. tmp = te[6]; te[6] = te[9]; te[9] = tmp;
  196. tmp = te[3]; te[3] = te[12]; te[12] = tmp;
  197. tmp = te[7]; te[7] = te[13]; te[13] = tmp;
  198. tmp = te[11]; te[11] = te[14]; te[14] = tmp;
  199. return this;
  200. },
  201. flattenToArray: function ( flat ) {
  202. var te = this.elements;
  203. flat[ 0 ] = te[0]; flat[ 1 ] = te[1]; flat[ 2 ] = te[2]; flat[ 3 ] = te[3];
  204. flat[ 4 ] = te[4]; flat[ 5 ] = te[5]; flat[ 6 ] = te[6]; flat[ 7 ] = te[7];
  205. flat[ 8 ] = te[8]; flat[ 9 ] = te[9]; flat[ 10 ] = te[10]; flat[ 11 ] = te[11];
  206. flat[ 12 ] = te[12]; flat[ 13 ] = te[13]; flat[ 14 ] = te[14]; flat[ 15 ] = te[15];
  207. return flat;
  208. },
  209. flattenToArrayOffset: function( flat, offset ) {
  210. var te = this.elements;
  211. flat[ offset ] = te[0];
  212. flat[ offset + 1 ] = te[1];
  213. flat[ offset + 2 ] = te[2];
  214. flat[ offset + 3 ] = te[3];
  215. flat[ offset + 4 ] = te[4];
  216. flat[ offset + 5 ] = te[5];
  217. flat[ offset + 6 ] = te[6];
  218. flat[ offset + 7 ] = te[7];
  219. flat[ offset + 8 ] = te[8];
  220. flat[ offset + 9 ] = te[9];
  221. flat[ offset + 10 ] = te[10];
  222. flat[ offset + 11 ] = te[11];
  223. flat[ offset + 12 ] = te[12];
  224. flat[ offset + 13 ] = te[13];
  225. flat[ offset + 14 ] = te[14];
  226. flat[ offset + 15 ] = te[15];
  227. return flat;
  228. },
  229. getPosition: function () {
  230. var te = this.elements;
  231. return THREE.Matrix4.__v1.set( te[12], te[13], te[14] );
  232. },
  233. setPosition: function ( v ) {
  234. var te = this.elements;
  235. te[12] = v.x;
  236. te[13] = v.y;
  237. te[14] = v.z;
  238. return this;
  239. },
  240. getColumnX: function () {
  241. var te = this.elements;
  242. return THREE.Matrix4.__v1.set( te[0], te[1], te[2] );
  243. },
  244. getColumnY: function () {
  245. var te = this.elements;
  246. return THREE.Matrix4.__v1.set( te[4], te[5], te[6] );
  247. },
  248. getColumnZ: function() {
  249. var te = this.elements;
  250. return THREE.Matrix4.__v1.set( te[8], te[9], te[10] );
  251. },
  252. getInverse: function ( m ) {
  253. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  254. var te = this.elements;
  255. var me = m.elements;
  256. var n11 = me[0], n12 = me[4], n13 = me[8], n14 = me[12];
  257. var n21 = me[1], n22 = me[5], n23 = me[9], n24 = me[13];
  258. var n31 = me[2], n32 = me[6], n33 = me[10], n34 = me[14];
  259. var n41 = me[3], n42 = me[7], n43 = me[11], n44 = me[15];
  260. te[0] = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  261. te[4] = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  262. te[8] = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  263. te[12] = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  264. te[1] = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  265. te[5] = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  266. te[9] = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  267. te[13] = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  268. te[2] = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  269. te[6] = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  270. te[10] = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  271. te[14] = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  272. te[3] = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  273. te[7] = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  274. te[11] = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  275. te[15] = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  276. this.multiplyScalar( 1 / m.determinant() );
  277. return this;
  278. },
  279. setRotationFromEuler: function( v, order ) {
  280. var te = this.elements;
  281. var x = v.x, y = v.y, z = v.z;
  282. var a = Math.cos( x ), b = Math.sin( x );
  283. var c = Math.cos( y ), d = Math.sin( y );
  284. var e = Math.cos( z ), f = Math.sin( z );
  285. switch ( order ) {
  286. case 'YXZ':
  287. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  288. te[0] = ce + df * b;
  289. te[4] = de * b - cf;
  290. te[8] = a * d;
  291. te[1] = a * f;
  292. te[5] = a * e;
  293. te[9] = - b;
  294. te[2] = cf * b - de;
  295. te[6] = df + ce * b;
  296. te[10] = a * c;
  297. break;
  298. case 'ZXY':
  299. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  300. te[0] = ce - df * b;
  301. te[4] = - a * f;
  302. te[8] = de + cf * b;
  303. te[1] = cf + de * b;
  304. te[5] = a * e;
  305. te[9] = df - ce * b;
  306. te[2] = - a * d;
  307. te[6] = b;
  308. te[10] = a * c;
  309. break;
  310. case 'ZYX':
  311. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  312. te[0] = c * e;
  313. te[4] = be * d - af;
  314. te[8] = ae * d + bf;
  315. te[1] = c * f;
  316. te[5] = bf * d + ae;
  317. te[9] = af * d - be;
  318. te[2] = - d;
  319. te[6] = b * c;
  320. te[10] = a * c;
  321. break;
  322. case 'YZX':
  323. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  324. te[0] = c * e;
  325. te[4] = bd - ac * f;
  326. te[8] = bc * f + ad;
  327. te[1] = f;
  328. te[5] = a * e;
  329. te[9] = - b * e;
  330. te[2] = - d * e;
  331. te[6] = ad * f + bc;
  332. te[10] = ac - bd * f;
  333. break;
  334. case 'XZY':
  335. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  336. te[0] = c * e;
  337. te[4] = - f;
  338. te[8] = d * e;
  339. te[1] = ac * f + bd;
  340. te[5] = a * e;
  341. te[9] = ad * f - bc;
  342. te[2] = bc * f - ad;
  343. te[6] = b * e;
  344. te[10] = bd * f + ac;
  345. break;
  346. default: // 'XYZ'
  347. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  348. te[0] = c * e;
  349. te[4] = - c * f;
  350. te[8] = d;
  351. te[1] = af + be * d;
  352. te[5] = ae - bf * d;
  353. te[9] = - b * c;
  354. te[2] = bf - ae * d;
  355. te[6] = be + af * d;
  356. te[10] = a * c;
  357. break;
  358. }
  359. return this;
  360. },
  361. setRotationFromQuaternion: function( q ) {
  362. var te = this.elements;
  363. var x = q.x, y = q.y, z = q.z, w = q.w;
  364. var x2 = x + x, y2 = y + y, z2 = z + z;
  365. var xx = x * x2, xy = x * y2, xz = x * z2;
  366. var yy = y * y2, yz = y * z2, zz = z * z2;
  367. var wx = w * x2, wy = w * y2, wz = w * z2;
  368. te[0] = 1 - ( yy + zz );
  369. te[4] = xy - wz;
  370. te[8] = xz + wy;
  371. te[1] = xy + wz;
  372. te[5] = 1 - ( xx + zz );
  373. te[9] = yz - wx;
  374. te[2] = xz - wy;
  375. te[6] = yz + wx;
  376. te[10] = 1 - ( xx + yy );
  377. return this;
  378. },
  379. compose: function ( translation, rotation, scale ) {
  380. var te = this.elements;
  381. var mRotation = THREE.Matrix4.__m1;
  382. var mScale = THREE.Matrix4.__m2;
  383. mRotation.identity();
  384. mRotation.setRotationFromQuaternion( rotation );
  385. mScale.makeScale( scale.x, scale.y, scale.z );
  386. this.multiply( mRotation, mScale );
  387. te[12] = translation.x;
  388. te[13] = translation.y;
  389. te[14] = translation.z;
  390. return this;
  391. },
  392. decompose: function ( translation, rotation, scale ) {
  393. var te = this.elements;
  394. // grab the axis vectors
  395. var x = THREE.Matrix4.__v1;
  396. var y = THREE.Matrix4.__v2;
  397. var z = THREE.Matrix4.__v3;
  398. x.set( te[0], te[1], te[2] );
  399. y.set( te[4], te[5], te[6] );
  400. z.set( te[8], te[9], te[10] );
  401. translation = ( translation instanceof THREE.Vector3 ) ? translation : new THREE.Vector3();
  402. rotation = ( rotation instanceof THREE.Quaternion ) ? rotation : new THREE.Quaternion();
  403. scale = ( scale instanceof THREE.Vector3 ) ? scale : new THREE.Vector3();
  404. scale.x = x.length();
  405. scale.y = y.length();
  406. scale.z = z.length();
  407. translation.x = te[12];
  408. translation.y = te[13];
  409. translation.z = te[14];
  410. // scale the rotation part
  411. var matrix = THREE.Matrix4.__m1;
  412. matrix.copy( this );
  413. matrix.elements[0] /= scale.x;
  414. matrix.elements[1] /= scale.x;
  415. matrix.elements[2] /= scale.x;
  416. matrix.elements[4] /= scale.y;
  417. matrix.elements[5] /= scale.y;
  418. matrix.elements[6] /= scale.y;
  419. matrix.elements[8] /= scale.z;
  420. matrix.elements[9] /= scale.z;
  421. matrix.elements[10] /= scale.z;
  422. rotation.setFromRotationMatrix( matrix );
  423. return [ translation, rotation, scale ];
  424. },
  425. extractPosition: function ( m ) {
  426. var te = this.elements;
  427. var me = m.elements;
  428. te[12] = me[12];
  429. te[13] = me[13];
  430. te[14] = me[14];
  431. return this;
  432. },
  433. extractRotation: function ( m ) {
  434. var te = this.elements;
  435. var me = m.elements;
  436. var vector = THREE.Matrix4.__v1;
  437. var scaleX = 1 / vector.set( me[0], me[1], me[2] ).length();
  438. var scaleY = 1 / vector.set( me[4], me[5], me[6] ).length();
  439. var scaleZ = 1 / vector.set( me[8], me[9], me[10] ).length();
  440. te[0] = me[0] * scaleX;
  441. te[1] = me[1] * scaleX;
  442. te[2] = me[2] * scaleX;
  443. te[4] = me[4] * scaleY;
  444. te[5] = me[5] * scaleY;
  445. te[6] = me[6] * scaleY;
  446. te[8] = me[8] * scaleZ;
  447. te[9] = me[9] * scaleZ;
  448. te[10] = me[10] * scaleZ;
  449. return this;
  450. },
  451. //
  452. translate: function ( v ) {
  453. var te = this.elements;
  454. var x = v.x, y = v.y, z = v.z;
  455. te[12] = te[0] * x + te[4] * y + te[8] * z + te[12];
  456. te[13] = te[1] * x + te[5] * y + te[9] * z + te[13];
  457. te[14] = te[2] * x + te[6] * y + te[10] * z + te[14];
  458. te[15] = te[3] * x + te[7] * y + te[11] * z + te[15];
  459. return this;
  460. },
  461. rotateX: function ( angle ) {
  462. var te = this.elements;
  463. var m12 = te[4];
  464. var m22 = te[5];
  465. var m32 = te[6];
  466. var m42 = te[7];
  467. var m13 = te[8];
  468. var m23 = te[9];
  469. var m33 = te[10];
  470. var m43 = te[11];
  471. var c = Math.cos( angle );
  472. var s = Math.sin( angle );
  473. te[4] = c * m12 + s * m13;
  474. te[5] = c * m22 + s * m23;
  475. te[6] = c * m32 + s * m33;
  476. te[7] = c * m42 + s * m43;
  477. te[8] = c * m13 - s * m12;
  478. te[9] = c * m23 - s * m22;
  479. te[10] = c * m33 - s * m32;
  480. te[11] = c * m43 - s * m42;
  481. return this;
  482. },
  483. rotateY: function ( angle ) {
  484. var te = this.elements;
  485. var m11 = te[0];
  486. var m21 = te[1];
  487. var m31 = te[2];
  488. var m41 = te[3];
  489. var m13 = te[8];
  490. var m23 = te[9];
  491. var m33 = te[10];
  492. var m43 = te[11];
  493. var c = Math.cos( angle );
  494. var s = Math.sin( angle );
  495. te[0] = c * m11 - s * m13;
  496. te[1] = c * m21 - s * m23;
  497. te[2] = c * m31 - s * m33;
  498. te[3] = c * m41 - s * m43;
  499. te[8] = c * m13 + s * m11;
  500. te[9] = c * m23 + s * m21;
  501. te[10] = c * m33 + s * m31;
  502. te[11] = c * m43 + s * m41;
  503. return this;
  504. },
  505. rotateZ: function ( angle ) {
  506. var te = this.elements;
  507. var m11 = te[0];
  508. var m21 = te[1];
  509. var m31 = te[2];
  510. var m41 = te[3];
  511. var m12 = te[4];
  512. var m22 = te[5];
  513. var m32 = te[6];
  514. var m42 = te[7];
  515. var c = Math.cos( angle );
  516. var s = Math.sin( angle );
  517. te[0] = c * m11 + s * m12;
  518. te[1] = c * m21 + s * m22;
  519. te[2] = c * m31 + s * m32;
  520. te[3] = c * m41 + s * m42;
  521. te[4] = c * m12 - s * m11;
  522. te[5] = c * m22 - s * m21;
  523. te[6] = c * m32 - s * m31;
  524. te[7] = c * m42 - s * m41;
  525. return this;
  526. },
  527. rotateByAxis: function ( axis, angle ) {
  528. var te = this.elements;
  529. // optimize by checking axis
  530. if ( axis.x === 1 && axis.y === 0 && axis.z === 0 ) {
  531. return this.rotateX( angle );
  532. } else if ( axis.x === 0 && axis.y === 1 && axis.z === 0 ) {
  533. return this.rotateY( angle );
  534. } else if ( axis.x === 0 && axis.y === 0 && axis.z === 1 ) {
  535. return this.rotateZ( angle );
  536. }
  537. var x = axis.x, y = axis.y, z = axis.z;
  538. var n = Math.sqrt(x * x + y * y + z * z);
  539. x /= n;
  540. y /= n;
  541. z /= n;
  542. var xx = x * x, yy = y * y, zz = z * z;
  543. var c = Math.cos( angle );
  544. var s = Math.sin( angle );
  545. var oneMinusCosine = 1 - c;
  546. var xy = x * y * oneMinusCosine;
  547. var xz = x * z * oneMinusCosine;
  548. var yz = y * z * oneMinusCosine;
  549. var xs = x * s;
  550. var ys = y * s;
  551. var zs = z * s;
  552. var r11 = xx + (1 - xx) * c;
  553. var r21 = xy + zs;
  554. var r31 = xz - ys;
  555. var r12 = xy - zs;
  556. var r22 = yy + (1 - yy) * c;
  557. var r32 = yz + xs;
  558. var r13 = xz + ys;
  559. var r23 = yz - xs;
  560. var r33 = zz + (1 - zz) * c;
  561. var m11 = te[0], m21 = te[1], m31 = te[2], m41 = te[3];
  562. var m12 = te[4], m22 = te[5], m32 = te[6], m42 = te[7];
  563. var m13 = te[8], m23 = te[9], m33 = te[10], m43 = te[11];
  564. var m14 = te[12], m24 = te[13], m34 = te[14], m44 = te[15];
  565. te[0] = r11 * m11 + r21 * m12 + r31 * m13;
  566. te[1] = r11 * m21 + r21 * m22 + r31 * m23;
  567. te[2] = r11 * m31 + r21 * m32 + r31 * m33;
  568. te[3] = r11 * m41 + r21 * m42 + r31 * m43;
  569. te[4] = r12 * m11 + r22 * m12 + r32 * m13;
  570. te[5] = r12 * m21 + r22 * m22 + r32 * m23;
  571. te[6] = r12 * m31 + r22 * m32 + r32 * m33;
  572. te[7] = r12 * m41 + r22 * m42 + r32 * m43;
  573. te[8] = r13 * m11 + r23 * m12 + r33 * m13;
  574. te[9] = r13 * m21 + r23 * m22 + r33 * m23;
  575. te[10] = r13 * m31 + r23 * m32 + r33 * m33;
  576. te[11] = r13 * m41 + r23 * m42 + r33 * m43;
  577. return this;
  578. },
  579. scale: function ( v ) {
  580. var te = this.elements;
  581. var x = v.x, y = v.y, z = v.z;
  582. te[0] *= x; te[4] *= y; te[8] *= z;
  583. te[1] *= x; te[5] *= y; te[9] *= z;
  584. te[2] *= x; te[6] *= y; te[10] *= z;
  585. te[3] *= x; te[7] *= y; te[11] *= z;
  586. return this;
  587. },
  588. getMaxScaleOnAxis: function () {
  589. var te = this.elements;
  590. var scaleXSq = te[0] * te[0] + te[1] * te[1] + te[2] * te[2];
  591. var scaleYSq = te[4] * te[4] + te[5] * te[5] + te[6] * te[6];
  592. var scaleZSq = te[8] * te[8] + te[9] * te[9] + te[10] * te[10];
  593. return Math.sqrt( Math.max( scaleXSq, Math.max( scaleYSq, scaleZSq ) ) );
  594. },
  595. //
  596. makeTranslation: function ( x, y, z ) {
  597. this.set(
  598. 1, 0, 0, x,
  599. 0, 1, 0, y,
  600. 0, 0, 1, z,
  601. 0, 0, 0, 1
  602. );
  603. return this;
  604. },
  605. makeRotationX: function ( theta ) {
  606. var c = Math.cos( theta ), s = Math.sin( theta );
  607. this.set(
  608. 1, 0, 0, 0,
  609. 0, c, -s, 0,
  610. 0, s, c, 0,
  611. 0, 0, 0, 1
  612. );
  613. return this;
  614. },
  615. makeRotationY: function ( theta ) {
  616. var c = Math.cos( theta ), s = Math.sin( theta );
  617. this.set(
  618. c, 0, s, 0,
  619. 0, 1, 0, 0,
  620. -s, 0, c, 0,
  621. 0, 0, 0, 1
  622. );
  623. return this;
  624. },
  625. makeRotationZ: function ( theta ) {
  626. var c = Math.cos( theta ), s = Math.sin( theta );
  627. this.set(
  628. c, -s, 0, 0,
  629. s, c, 0, 0,
  630. 0, 0, 1, 0,
  631. 0, 0, 0, 1
  632. );
  633. return this;
  634. },
  635. makeRotationAxis: function ( axis, angle ) {
  636. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  637. var c = Math.cos( angle );
  638. var s = Math.sin( angle );
  639. var t = 1 - c;
  640. var x = axis.x, y = axis.y, z = axis.z;
  641. var tx = t * x, ty = t * y;
  642. this.set(
  643. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  644. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  645. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  646. 0, 0, 0, 1
  647. );
  648. return this;
  649. },
  650. makeScale: function ( x, y, z ) {
  651. this.set(
  652. x, 0, 0, 0,
  653. 0, y, 0, 0,
  654. 0, 0, z, 0,
  655. 0, 0, 0, 1
  656. );
  657. return this;
  658. },
  659. makeFrustum: function ( left, right, bottom, top, near, far ) {
  660. var te = this.elements;
  661. var x = 2 * near / ( right - left );
  662. var y = 2 * near / ( top - bottom );
  663. var a = ( right + left ) / ( right - left );
  664. var b = ( top + bottom ) / ( top - bottom );
  665. var c = - ( far + near ) / ( far - near );
  666. var d = - 2 * far * near / ( far - near );
  667. te[0] = x; te[4] = 0; te[8] = a; te[12] = 0;
  668. te[1] = 0; te[5] = y; te[9] = b; te[13] = 0;
  669. te[2] = 0; te[6] = 0; te[10] = c; te[14] = d;
  670. te[3] = 0; te[7] = 0; te[11] = - 1; te[15] = 0;
  671. return this;
  672. },
  673. makePerspective: function ( fov, aspect, near, far ) {
  674. var ymax = near * Math.tan( fov * Math.PI / 360 );
  675. var ymin = - ymax;
  676. var xmin = ymin * aspect;
  677. var xmax = ymax * aspect;
  678. return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  679. },
  680. makeOrthographic: function ( left, right, top, bottom, near, far ) {
  681. var te = this.elements;
  682. var w = right - left;
  683. var h = top - bottom;
  684. var p = far - near;
  685. var x = ( right + left ) / w;
  686. var y = ( top + bottom ) / h;
  687. var z = ( far + near ) / p;
  688. te[0] = 2 / w; te[4] = 0; te[8] = 0; te[12] = -x;
  689. te[1] = 0; te[5] = 2 / h; te[9] = 0; te[13] = -y;
  690. te[2] = 0; te[6] = 0; te[10] = -2 / p; te[14] = -z;
  691. te[3] = 0; te[7] = 0; te[11] = 0; te[15] = 1;
  692. return this;
  693. },
  694. clone: function () {
  695. var te = this.elements;
  696. return new THREE.Matrix4(
  697. te[0], te[4], te[8], te[12],
  698. te[1], te[5], te[9], te[13],
  699. te[2], te[6], te[10], te[14],
  700. te[3], te[7], te[11], te[15]
  701. );
  702. }
  703. };
  704. THREE.Matrix4.__v1 = new THREE.Vector3();
  705. THREE.Matrix4.__v2 = new THREE.Vector3();
  706. THREE.Matrix4.__v3 = new THREE.Vector3();
  707. THREE.Matrix4.__m1 = new THREE.Matrix4();
  708. THREE.Matrix4.__m2 = new THREE.Matrix4();
粤ICP备19079148号