Matrix4.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. */
  8. THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  9. this.n11 = n11 || 1; this.n12 = n12 || 0; this.n13 = n13 || 0; this.n14 = n14 || 0;
  10. this.n21 = n21 || 0; this.n22 = n22 || 1; this.n23 = n23 || 0; this.n24 = n24 || 0;
  11. this.n31 = n31 || 0; this.n32 = n32 || 0; this.n33 = n33 || 1; this.n34 = n34 || 0;
  12. this.n41 = n41 || 0; this.n42 = n42 || 0; this.n43 = n43 || 0; this.n44 = n44 || 1;
  13. this.flat = new Array( 16 );
  14. this.m33 = new THREE.Matrix3();
  15. };
  16. THREE.Matrix4.prototype = {
  17. identity: function () {
  18. this.n11 = 1; this.n12 = 0; this.n13 = 0; this.n14 = 0;
  19. this.n21 = 0; this.n22 = 1; this.n23 = 0; this.n24 = 0;
  20. this.n31 = 0; this.n32 = 0; this.n33 = 1; this.n34 = 0;
  21. this.n41 = 0; this.n42 = 0; this.n43 = 0; this.n44 = 1;
  22. return this;
  23. },
  24. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  25. this.n11 = n11; this.n12 = n12; this.n13 = n13; this.n14 = n14;
  26. this.n21 = n21; this.n22 = n22; this.n23 = n23; this.n24 = n24;
  27. this.n31 = n31; this.n32 = n32; this.n33 = n33; this.n34 = n34;
  28. this.n41 = n41; this.n42 = n42; this.n43 = n43; this.n44 = n44;
  29. return this;
  30. },
  31. copy: function ( m ) {
  32. this.n11 = m.n11; this.n12 = m.n12; this.n13 = m.n13; this.n14 = m.n14;
  33. this.n21 = m.n21; this.n22 = m.n22; this.n23 = m.n23; this.n24 = m.n24;
  34. this.n31 = m.n31; this.n32 = m.n32; this.n33 = m.n33; this.n34 = m.n34;
  35. this.n41 = m.n41; this.n42 = m.n42; this.n43 = m.n43; this.n44 = m.n44;
  36. return this;
  37. },
  38. lookAt: function ( eye, center, up ) {
  39. var x = THREE.Matrix4.__tmpVec1, y = THREE.Matrix4.__tmpVec2, z = THREE.Matrix4.__tmpVec3;
  40. z.sub( eye, center ).normalize();
  41. x.cross( up, z ).normalize();
  42. y.cross( z, x ).normalize();
  43. this.n11 = x.x; this.n12 = x.y; this.n13 = x.z; this.n14 = - x.dot( eye );
  44. this.n21 = y.x; this.n22 = y.y; this.n23 = y.z; this.n24 = - y.dot( eye );
  45. this.n31 = z.x; this.n32 = z.y; this.n33 = z.z; this.n34 = - z.dot( eye );
  46. this.n41 = 0; this.n42 = 0; this.n43 = 0; this.n44 = 1;
  47. return this;
  48. },
  49. multiplyVector3: function ( v ) {
  50. var vx = v.x, vy = v.y, vz = v.z,
  51. d = 1 / ( this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 );
  52. v.x = ( this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 ) * d;
  53. v.y = ( this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 ) * d;
  54. v.z = ( this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 ) * d;
  55. return v;
  56. },
  57. multiplyVector4: function ( v ) {
  58. var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
  59. v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 * vw;
  60. v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 * vw;
  61. v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 * vw;
  62. v.w = this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 * vw;
  63. return v;
  64. },
  65. crossVector: function ( a ) {
  66. var v = new THREE.Vector4();
  67. v.x = this.n11 * a.x + this.n12 * a.y + this.n13 * a.z + this.n14 * a.w;
  68. v.y = this.n21 * a.x + this.n22 * a.y + this.n23 * a.z + this.n24 * a.w;
  69. v.z = this.n31 * a.x + this.n32 * a.y + this.n33 * a.z + this.n34 * a.w;
  70. v.w = ( a.w ) ? this.n41 * a.x + this.n42 * a.y + this.n43 * a.z + this.n44 * a.w : 1;
  71. return v;
  72. },
  73. multiply: function ( a, b ) {
  74. var a11 = a.n11, a12 = a.n12, a13 = a.n13, a14 = a.n14,
  75. a21 = a.n21, a22 = a.n22, a23 = a.n23, a24 = a.n24,
  76. a31 = a.n31, a32 = a.n32, a33 = a.n33, a34 = a.n34,
  77. a41 = a.n41, a42 = a.n42, a43 = a.n43, a44 = a.n44,
  78. b11 = b.n11, b12 = b.n12, b13 = b.n13, b14 = b.n14,
  79. b21 = b.n21, b22 = b.n22, b23 = b.n23, b24 = b.n24,
  80. b31 = b.n31, b32 = b.n32, b33 = b.n33, b34 = b.n34,
  81. b41 = b.n41, b42 = b.n42, b43 = b.n43, b44 = b.n44;
  82. this.n11 = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  83. this.n12 = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  84. this.n13 = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  85. this.n14 = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  86. this.n21 = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  87. this.n22 = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  88. this.n23 = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  89. this.n24 = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  90. this.n31 = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  91. this.n32 = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  92. this.n33 = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  93. this.n34 = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  94. this.n41 = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  95. this.n42 = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  96. this.n43 = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  97. this.n44 = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  98. return this;
  99. },
  100. multiplySelf: function ( m ) {
  101. var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14,
  102. n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24,
  103. n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34,
  104. n41 = this.n41, n42 = this.n42, n43 = this.n43, n44 = this.n44,
  105. mn11 = m.n11, mn21 = m.n21, mn31 = m.n31, mn41 = m.n41,
  106. mn12 = m.n12, mn22 = m.n22, mn32 = m.n32, mn42 = m.n42,
  107. mn13 = m.n13, mn23 = m.n23, mn33 = m.n33, mn43 = m.n43,
  108. mn14 = m.n14, mn24 = m.n24, mn34 = m.n34, mn44 = m.n44;
  109. this.n11 = n11 * mn11 + n12 * mn21 + n13 * mn31 + n14 * mn41;
  110. this.n12 = n11 * mn12 + n12 * mn22 + n13 * mn32 + n14 * mn42;
  111. this.n13 = n11 * mn13 + n12 * mn23 + n13 * mn33 + n14 * mn43;
  112. this.n14 = n11 * mn14 + n12 * mn24 + n13 * mn34 + n14 * mn44;
  113. this.n21 = n21 * mn11 + n22 * mn21 + n23 * mn31 + n24 * mn41;
  114. this.n22 = n21 * mn12 + n22 * mn22 + n23 * mn32 + n24 * mn42;
  115. this.n23 = n21 * mn13 + n22 * mn23 + n23 * mn33 + n24 * mn43;
  116. this.n24 = n21 * mn14 + n22 * mn24 + n23 * mn34 + n24 * mn44;
  117. this.n31 = n31 * mn11 + n32 * mn21 + n33 * mn31 + n34 * mn41;
  118. this.n32 = n31 * mn12 + n32 * mn22 + n33 * mn32 + n34 * mn42;
  119. this.n33 = n31 * mn13 + n32 * mn23 + n33 * mn33 + n34 * mn43;
  120. this.n34 = n31 * mn14 + n32 * mn24 + n33 * mn34 + n34 * mn44;
  121. this.n41 = n41 * mn11 + n42 * mn21 + n43 * mn31 + n44 * mn41;
  122. this.n42 = n41 * mn12 + n42 * mn22 + n43 * mn32 + n44 * mn42;
  123. this.n43 = n41 * mn13 + n42 * mn23 + n43 * mn33 + n44 * mn43;
  124. this.n44 = n41 * mn14 + n42 * mn24 + n43 * mn34 + n44 * mn44;
  125. return this;
  126. },
  127. multiplyScalar: function ( s ) {
  128. this.n11 *= s; this.n12 *= s; this.n13 *= s; this.n14 *= s;
  129. this.n21 *= s; this.n22 *= s; this.n23 *= s; this.n24 *= s;
  130. this.n31 *= s; this.n32 *= s; this.n33 *= s; this.n34 *= s;
  131. this.n41 *= s; this.n42 *= s; this.n43 *= s; this.n44 *= s;
  132. return this;
  133. },
  134. determinant: function () {
  135. var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14,
  136. n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24,
  137. n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34,
  138. n41 = this.n41, n42 = this.n42, n43 = this.n43, n44 = this.n44;
  139. //TODO: make this more efficient
  140. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  141. return (
  142. n14 * n23 * n32 * n41-
  143. n13 * n24 * n32 * n41-
  144. n14 * n22 * n33 * n41+
  145. n12 * n24 * n33 * n41+
  146. n13 * n22 * n34 * n41-
  147. n12 * n23 * n34 * n41-
  148. n14 * n23 * n31 * n42+
  149. n13 * n24 * n31 * n42+
  150. n14 * n21 * n33 * n42-
  151. n11 * n24 * n33 * n42-
  152. n13 * n21 * n34 * n42+
  153. n11 * n23 * n34 * n42+
  154. n14 * n22 * n31 * n43-
  155. n12 * n24 * n31 * n43-
  156. n14 * n21 * n32 * n43+
  157. n11 * n24 * n32 * n43+
  158. n12 * n21 * n34 * n43-
  159. n11 * n22 * n34 * n43-
  160. n13 * n22 * n31 * n44+
  161. n12 * n23 * n31 * n44+
  162. n13 * n21 * n32 * n44-
  163. n11 * n23 * n32 * n44-
  164. n12 * n21 * n33 * n44+
  165. n11 * n22 * n33 * n44 );
  166. },
  167. transpose: function () {
  168. function swap( obj, p1, p2 ) {
  169. var aux = obj[ p1 ];
  170. obj[ p1 ] = obj[ p2 ];
  171. obj[ p2 ] = aux;
  172. }
  173. swap( this, 'n21', 'n12' );
  174. swap( this, 'n31', 'n13' );
  175. swap( this, 'n32', 'n23' );
  176. swap( this, 'n41', 'n14' );
  177. swap( this, 'n42', 'n24' );
  178. swap( this, 'n43', 'n34' );
  179. return this;
  180. },
  181. clone: function () {
  182. var m = new THREE.Matrix4();
  183. m.n11 = this.n11; m.n12 = this.n12; m.n13 = this.n13; m.n14 = this.n14;
  184. m.n21 = this.n21; m.n22 = this.n22; m.n23 = this.n23; m.n24 = this.n24;
  185. m.n31 = this.n31; m.n32 = this.n32; m.n33 = this.n33; m.n34 = this.n34;
  186. m.n41 = this.n41; m.n42 = this.n42; m.n43 = this.n43; m.n44 = this.n44;
  187. return m;
  188. },
  189. flatten: function() {
  190. this.flat[ 0 ] = this.n11;
  191. this.flat[ 1 ] = this.n21;
  192. this.flat[ 2 ] = this.n31;
  193. this.flat[ 3 ] = this.n41;
  194. this.flat[ 4 ] = this.n12;
  195. this.flat[ 5 ] = this.n22;
  196. this.flat[ 6 ] = this.n32;
  197. this.flat[ 7 ] = this.n42;
  198. this.flat[ 8 ] = this.n13;
  199. this.flat[ 9 ] = this.n23;
  200. this.flat[ 10 ] = this.n33;
  201. this.flat[ 11 ] = this.n43;
  202. this.flat[ 12 ] = this.n14;
  203. this.flat[ 13 ] = this.n24;
  204. this.flat[ 14 ] = this.n34;
  205. this.flat[ 15 ] = this.n44;
  206. return this.flat;
  207. },
  208. toString: function() {
  209. return "| " + this.n11 + " " + this.n12 + " " + this.n13 + " " + this.n14 + " |\n" +
  210. "| " + this.n21 + " " + this.n22 + " " + this.n23 + " " + this.n24 + " |\n" +
  211. "| " + this.n31 + " " + this.n32 + " " + this.n33 + " " + this.n34 + " |\n" +
  212. "| " + this.n41 + " " + this.n42 + " " + this.n43 + " " + this.n44 + " |";
  213. }
  214. };
  215. THREE.Matrix4.translationMatrix = function ( x, y, z ) {
  216. var m = new THREE.Matrix4();
  217. m.n14 = x;
  218. m.n24 = y;
  219. m.n34 = z;
  220. return m;
  221. };
  222. THREE.Matrix4.scaleMatrix = function ( x, y, z ) {
  223. var m = new THREE.Matrix4();
  224. m.n11 = x;
  225. m.n22 = y;
  226. m.n33 = z;
  227. return m;
  228. };
  229. THREE.Matrix4.rotationXMatrix = function ( theta ) {
  230. var rot = new THREE.Matrix4();
  231. rot.n22 = rot.n33 = Math.cos( theta );
  232. rot.n32 = Math.sin( theta );
  233. rot.n23 = - rot.n32;
  234. return rot;
  235. };
  236. THREE.Matrix4.rotationYMatrix = function ( theta ) {
  237. var rot = new THREE.Matrix4();
  238. rot.n11 = rot.n33 = Math.cos( theta );
  239. rot.n13 = Math.sin( theta );
  240. rot.n31 = - rot.n13;
  241. return rot;
  242. };
  243. THREE.Matrix4.rotationZMatrix = function ( theta ) {
  244. var rot = new THREE.Matrix4();
  245. rot.n11 = rot.n22 = Math.cos( theta );
  246. rot.n21 = Math.sin( theta );
  247. rot.n12 = - rot.n21;
  248. return rot;
  249. };
  250. THREE.Matrix4.rotationAxisAngleMatrix = function ( axis, angle ) {
  251. //Based on http://www.gamedev.net/reference/articles/article1199.asp
  252. var rot = new THREE.Matrix4(),
  253. c = Math.cos( angle ),
  254. s = Math.sin( angle ),
  255. t = 1 - c,
  256. x = axis.x, y = axis.y, z = axis.z,
  257. tx = t * x, ty = t * y;
  258. rot.n11 = tx * x + c;
  259. rot.n12 = tx * y - s * z;
  260. rot.n13 = tx * z + s * y;
  261. rot.n21 = tx * y + s * z;
  262. rot.n22 = ty * y + c;
  263. rot.n23 = ty * z - s * x;
  264. rot.n31 = tx * z - s * y;
  265. rot.n32 = ty * z + s * x;
  266. rot.n33 = t * z * z + c;
  267. return rot;
  268. };
  269. THREE.Matrix4.makeInvert = function ( m1 ) {
  270. var n11 = m1.n11, n12 = m1.n12, n13 = m1.n13, n14 = m1.n14,
  271. n21 = m1.n21, n22 = m1.n22, n23 = m1.n23, n24 = m1.n24,
  272. n31 = m1.n31, n32 = m1.n32, n33 = m1.n33, n34 = m1.n34,
  273. n41 = m1.n41, n42 = m1.n42, n43 = m1.n43, n44 = m1.n44;
  274. //TODO: make this more efficient
  275. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  276. var m2 = new THREE.Matrix4();
  277. m2.n11 = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  278. m2.n12 = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  279. m2.n13 = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  280. m2.n14 = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  281. m2.n21 = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  282. m2.n22 = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  283. m2.n23 = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  284. m2.n24 = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  285. m2.n31 = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  286. m2.n32 = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  287. m2.n33 = n13*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  288. m2.n34 = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  289. m2.n41 = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  290. m2.n42 = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  291. m2.n43 = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  292. m2.n44 = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  293. m2.multiplyScalar( 1 / m1.determinant() );
  294. return m2;
  295. };
  296. THREE.Matrix4.makeInvert3x3 = function ( m1 ) {
  297. // input: THREE.Matrix4, output: THREE.Matrix3
  298. // ( based on http://code.google.com/p/webgl-mjs/ )
  299. var m = m1.flatten(),
  300. m2 = m1.m33,
  301. a11 = m[ 10 ] * m[ 5 ] - m[ 6 ] * m[ 9 ],
  302. a21 = - m[ 10 ] * m[ 1 ] + m[ 2 ] * m[ 9 ],
  303. a31 = m[ 6 ] * m[ 1 ] - m[ 2 ] * m[ 5 ],
  304. a12 = - m[ 10 ] * m[ 4 ] + m[ 6 ] * m[ 8 ],
  305. a22 = m[ 10 ] * m[ 0 ] - m[ 2 ] * m[ 8 ],
  306. a32 = - m[ 6 ] * m[ 0 ] + m[ 2 ] * m[ 4 ],
  307. a13 = m[ 9 ] * m[ 4 ] - m[ 5 ] * m[ 8 ],
  308. a23 = - m[ 9 ] * m[ 0 ] + m[ 1 ] * m[ 8 ],
  309. a33 = m[ 5 ] * m[ 0 ] - m[ 1 ] * m[ 4 ],
  310. det = m[ 0 ] * ( a11 ) + m[ 1 ] * ( a12 ) + m[ 2 ] * ( a13 ),
  311. idet;
  312. // no inverse
  313. if (det == 0) throw "matrix not invertible";
  314. idet = 1.0 / det;
  315. m2.m[ 0 ] = idet * a11; m2.m[ 1 ] = idet * a21; m2.m[ 2 ] = idet * a31;
  316. m2.m[ 3 ] = idet * a12; m2.m[ 4 ] = idet * a22; m2.m[ 5 ] = idet * a32;
  317. m2.m[ 6 ] = idet * a13; m2.m[ 7 ] = idet * a23; m2.m[ 8 ] = idet * a33;
  318. return m2;
  319. }
  320. THREE.Matrix4.makeFrustum = function( left, right, bottom, top, near, far ) {
  321. var m, x, y, a, b, c, d;
  322. m = new THREE.Matrix4();
  323. x = 2 * near / ( right - left );
  324. y = 2 * near / ( top - bottom );
  325. a = ( right + left ) / ( right - left );
  326. b = ( top + bottom ) / ( top - bottom );
  327. c = - ( far + near ) / ( far - near );
  328. d = - 2 * far * near / ( far - near );
  329. m.n11 = x; m.n12 = 0; m.n13 = a; m.n14 = 0;
  330. m.n21 = 0; m.n22 = y; m.n23 = b; m.n24 = 0;
  331. m.n31 = 0; m.n32 = 0; m.n33 = c; m.n34 = d;
  332. m.n41 = 0; m.n42 = 0; m.n43 = - 1; m.n44 = 0;
  333. return m;
  334. };
  335. THREE.Matrix4.makePerspective = function( fov, aspect, near, far ) {
  336. var ymax, ymin, xmin, xmax;
  337. ymax = near * Math.tan( fov * Math.PI / 360 );
  338. ymin = - ymax;
  339. xmin = ymin * aspect;
  340. xmax = ymax * aspect;
  341. return THREE.Matrix4.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  342. };
  343. THREE.Matrix4.makeOrtho = function( left, right, top, bottom, near, far ) {
  344. var m, x, y, z, w, h, p;
  345. m = new THREE.Matrix4();
  346. w = right - left;
  347. h = top - bottom;
  348. p = far - near;
  349. x = ( right + left ) / w;
  350. y = ( top + bottom ) / h;
  351. z = ( far + near ) / p;
  352. m.n11 = 2 / w; m.n12 = 0; m.n13 = 0; m.n14 = -x;
  353. m.n21 = 0; m.n22 = 2 / h; m.n23 = 0; m.n24 = -y;
  354. m.n31 = 0; m.n32 = 0; m.n33 = -2 / p; m.n34 = -z;
  355. m.n41 = 0; m.n42 = 0; m.n43 = 0; m.n44 = 1;
  356. return m;
  357. };
  358. THREE.Matrix4.__tmpVec1 = new THREE.Vector3();
  359. THREE.Matrix4.__tmpVec2 = new THREE.Vector3();
  360. THREE.Matrix4.__tmpVec3 = new THREE.Vector3();
粤ICP备19079148号